Sample solution

Content

#include <cstdlib>
#include <list>
#include <mutex>
#include <random>
#include <thread>
#include <vector>
#include <hpc/aux/slices.hpp>
#include <hpc/matvec/gematrix.hpp>
#include <hpc/matvec/iterators.hpp>
#include <hpc/matvec/print.hpp>

using namespace hpc;
using namespace hpc::matvec;
using namespace hpc::aux;

template<typename T>
struct RandomEnginePool {
      using EngineType = T;
      T get() {
         /* check if we have a free engine in the unused list */
         {
            std::lock_guard<std::mutex> lock(mutex);
            if (unused.size() > 0) {
               T rg = unused.front();
               unused.pop_front();
               return rg;
            }
         }
         /* prepare new random generator */
         return T(r());
      }
      void free(T engine) {
         std::lock_guard<std::mutex> lock(mutex);
         unused.push_back(engine);
      }
   private:
      std::mutex mutex;
      std::random_device r;
      std::list<T> unused;
};

template <
   template<typename> class MatrixA, typename T,
   typename POOL,
   Require< Ge<MatrixA<T>> > = true
>
void randomInit(MatrixA<T>& A, POOL& pool) {
   std::uniform_real_distribution<double> uniform(-100, 100);
   auto engine = pool.get();

   for (auto [i, j, Aij]: A) {
      Aij = uniform(engine);
      (void) i; (void) j; // suppress gcc warning
   }
   pool.free(engine);
}

int main() {
   RandomEnginePool<std::mt19937> pool;
   GeMatrix<double> A(51, 7);
   std::size_t nof_threads = std::thread::hardware_concurrency();

   std::vector<std::thread> threads(nof_threads);
   UniformSlices<std::size_t> slices(nof_threads, A.numRows());
   for (std::size_t index = 0; index < nof_threads; ++index) {
      auto firstRow = slices.offset(index);
      auto numRows = slices.size(index);
      auto A_ = A.view(firstRow, 0, numRows, A.numCols());
      threads[index] = std::thread([A_=std::move(A_),&pool]() mutable {
	 randomInit(A_, pool);
      });
   }
   for (std::size_t index = 0; index < nof_threads; ++index) {
      threads[index].join();
   }
   print(A, " %7.2f");
}
theon$ g++ -O3 -g -I/home/numerik/pub/hpc/ws18/session16 -std=c++17 -o random_init10 random_init10.cpp
theon$ ./random_init10
  -58.35  -16.17   17.01  -49.96   23.56  -87.38   60.88
    1.14   48.69   -3.28  -60.21   44.54  -78.96   65.31
   79.14  -48.02  -74.32   49.19   63.65   31.27   -9.32
   65.41   56.50   16.24   77.42  -19.50  -72.15   82.01
   30.69  -65.41   60.58  -32.92   76.50   91.92   37.81
  -64.43   -3.15   83.57   61.71  -46.53  -91.32    8.31
  -15.70  -40.37   13.17  -20.06   15.69  -81.17   88.91
   32.70  -38.05   17.12   89.15  -25.97   89.44  -52.52
  -98.96   58.21   12.61   49.61   95.45  -45.19   88.13
  -92.76  -73.22   94.34   15.06   -3.80   16.86  -27.15
  -14.06   92.04  -44.35  -75.88  -98.81  -29.77   93.79
  -71.91  -48.16   12.70   49.81   99.37   29.38  -94.75
   43.57   14.71  -46.24  -11.00    1.46  -81.03  -41.60
   35.69  -94.94   19.97   42.38  -42.06  -75.68   77.20
   86.43  -62.91   39.04   75.40    5.78  -72.51   19.83
   86.17  -85.43  -71.27  -31.24  -97.26  -70.75   40.17
   96.97   68.47  -70.01  -85.47   -2.09  -73.34   99.36
  -96.28   69.38  -20.61  -84.25  -15.71   11.17   13.36
   93.08  -55.49   74.72  -47.59  -94.81  -88.67  -51.69
  -40.48  -61.23   52.07  -86.97   92.67   36.02   79.24
   -9.36  -62.15  -77.73  -24.14   73.78   97.30  -51.27
   13.57   75.67   97.57   41.22  -42.69  -31.90    1.91
   72.73  -54.24   54.07   90.88  -29.94   60.95   75.68
   -2.97   21.04  -97.11    5.12  -80.41   20.72   27.40
  -66.05   81.38  -90.88  -81.74  -83.59   13.71  -40.72
  -97.45   30.08  -98.76  -59.49   40.33   81.47  -56.41
    7.49   46.72   91.74  -93.25   32.80  -28.51   77.50
   78.64  -21.17  -53.62  -73.65  -64.79  -71.43   53.62
  -31.32   20.67   52.28  -17.79   47.70   86.55  -20.70
   78.05    5.54   25.90   78.64   58.50   28.30   43.83
  -17.49  -41.57  -65.91  -17.73   69.82  -88.38   14.43
   83.74  -75.09   94.21   48.03   70.03   88.33  -58.68
   59.34  -53.42   54.97  -11.03   86.55   69.56   44.79
  -34.56   74.59   16.00  -33.24   32.34   41.52  -74.55
    1.85   74.99  -73.87  -84.51   21.66  -52.43   70.61
   22.56  -86.97   96.11  -31.67  -23.78   79.48  -53.62
  -66.05   24.58   99.85   21.75   62.95  -12.11  -50.42
   65.01   96.81    5.53   39.23  -12.42   72.84   51.68
  -72.99   51.04   42.18   65.86  -96.15   59.84  -91.45
  -80.46  -17.19   22.52  -70.33   67.66   34.73   69.32
   81.95   58.26  -69.22   23.24  -47.98    9.06  -39.17
   95.89   56.76  -78.77   58.64  -18.10  -76.30   -7.15
  -19.59    8.46   45.56  -55.75  -92.36  -86.41   26.51
   60.79  -63.85   14.15  -54.74  -30.33  -24.46   -3.61
  -92.79    5.23   -2.36  -90.23  -10.11   37.09    4.75
   84.86  -64.48   33.24   86.99  -78.86   90.53   15.38
  -99.87   73.62    2.42   70.43  -19.31   26.61  -59.24
   63.27   86.94   63.06   -9.91    3.41   11.20  -69.78
  -64.11  -90.44  -96.92  -29.95   32.67  -34.77  -21.68
   11.81   74.77    7.88  -80.77  -32.01  -44.88   81.25
   78.82  -11.49   72.38    1.34   87.72  -49.38  -61.69
theon$