Sample solution

Content

#include <cstdlib>
#include <deque>
#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::aux;
using namespace hpc::matvec;

template<typename T>
struct RandomEnginePool {
      using EngineType = T;
      T get() {
         /* check if we have a free engine in the unused deque */
         {
            std::lock_guard<std::mutex> lock(mutex);
            if (unused.size() > 0) {
               T rg = std::move(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::deque<T> unused;
};

template<typename T>
struct RandomEngineGuard {
   using EngineType = T;
   RandomEngineGuard(RandomEnginePool<T>& pool) :
      pool(pool), engine(pool.get()) {
   }
   ~RandomEngineGuard() {
      pool.free(std::move(engine));
   }
   T& get() {
      return engine;
   }
   RandomEnginePool<T>& pool;
   T engine;
};

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

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

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);
      threads[index] = std::thread([
	       A_ = A.view(firstRow, 0, numRows, A.numCols()),
	       &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/ws19/session16 -std=c++17 -o random_init11 random_init11.cpp
theon$ ./random_init11
   24.72  -96.36  -60.71   27.51   -8.03  -61.18   51.26
   86.70   58.84   85.86  -37.25  -12.04   26.44   83.25
  -55.81   18.59  -75.22   60.77  -48.42  -35.16  -43.99
    5.69   32.29   15.54  -78.45  -77.67   17.37  -77.68
   -2.80   -1.44   77.71  -46.07   86.29    8.04   67.98
   69.03   53.32   37.54  -14.09  -80.23   80.07    0.97
   27.28    6.60   47.34   63.93   42.91   79.90   17.20
  -60.81   63.05   48.73   -8.43   83.90   72.66  -33.22
  -79.27   82.24   28.84   27.28  -25.42   86.72   29.98
  -24.72   -5.34  -97.52   55.72  -96.34   85.46   60.89
  -22.23   24.37  -45.77  -92.15  -80.43  -16.90    7.00
   84.64  -89.36  -80.61   -3.91   19.80   52.24   78.59
  -44.66  -20.82   60.31  -47.08   93.50  -90.00  -66.87
  -14.13  -95.04  -79.03   64.60   32.02  -45.54   73.99
   46.18    4.79  -63.01   49.16  -93.49   94.28   41.89
  -81.07   91.44  -82.87  -38.07   84.01  -73.72   76.24
  -20.11  -66.01  -66.60   15.00  -77.52  -59.70   76.76
  -24.95  -51.74   80.05  -47.79   -5.57  -13.38   86.09
   57.50  -71.30  -58.74   61.43  -45.45   66.59   67.39
   13.54  -51.76   87.82  -52.24   34.65   93.38   -1.05
  -80.54   61.30  -17.42   33.55   69.46   25.17   47.75
  -27.33   65.19   86.12  -77.01   98.63   60.80  -40.70
  -63.72   79.23  -34.82  -96.79   73.56   81.40   64.26
  -36.64   15.06   70.34   74.57   79.75   65.27   56.97
  -19.48   -3.26   63.75  -20.88   59.39  -91.88   81.00
  -86.00  -65.14  -97.74  -75.43  -61.54   46.12   -7.40
  -77.36   79.88  -76.85  -37.46   76.62   74.07  -30.68
   80.11  -12.87   50.93   83.83   55.84  -98.19   28.36
   48.69   38.19  -45.77   84.49   16.19  -52.34  -49.91
   61.42   71.95  -93.79  -90.84   88.75  -39.88   -2.64
  -84.37   72.09   72.42   82.09   18.34   14.81  -53.95
   91.54   16.12   78.46  -88.19   74.39   29.99   42.60
   83.68  -53.04   98.76  -12.74   69.06   78.28   49.35
   58.70  -94.57   85.37  -95.26  -34.30  -46.11   70.31
   63.05   34.78   17.72   -0.27  -11.04  -59.71   94.49
   41.81   81.28  -50.77  -97.84   80.17  -90.30   84.97
  -69.83  -22.08  -26.21   71.01  -81.85   25.17   60.72
  -51.88  -54.39   92.52  -36.55   60.89  -31.89  -51.16
  -99.34   17.51  -97.91   -5.94   32.81   -0.09   -2.00
   78.86  -68.25  -28.32   61.71  -30.79   90.65    4.39
   38.30  -96.66  -40.87  -86.07  -69.63   62.70    6.93
   67.18  -76.68  -47.52   88.06  -45.88  -90.55   69.62
   -6.78  -88.20  -85.35   33.83   67.71  -51.22  -28.87
   -3.93   71.48   -7.92  -50.73  -14.97  -44.85   79.28
   71.39  -55.07   70.64   69.49   42.91  -97.79    3.35
  -58.60  -22.03   92.15   89.03   -2.19  -33.06  -53.43
   22.65  -23.97  -58.04   45.22   39.76  -74.18   78.75
   87.55  -28.46   86.08   64.13  -80.81   99.55  -24.83
   81.73  -22.57  -36.54   51.42  -56.98    6.21  -39.47
  -40.43  -26.19  -98.30   54.20   66.61  -60.75   99.74
   68.98  -82.51  -19.53  -64.31   -4.34  -24.09  -18.29
theon$ 
heim$ g++-8.3 -O3 -g -I/home/numerik/pub/hpc/ws19/session16 -std=c++17 -o random_init11 random_init11.cpp -lpthread
heim$ ./random_init11
  -89.02   -1.33   54.73  -71.54  -55.63  -21.48  -37.62
   63.52  -65.21  -74.44  -63.28  -64.12  -32.33   54.65
   16.05  -66.14  -10.54  -97.37   56.77  -72.91  -63.67
   21.93  -95.72  -36.12  -42.29  -65.08   49.66  -33.17
   66.83  -52.93   94.30  -52.20   47.60  -32.81  -80.74
  -26.93   75.80   12.85  -93.26  -66.67  -92.38  -52.84
   66.02  -82.73   52.33   11.79   36.71    5.19   47.14
   72.90  -42.50  -34.90    6.44  -38.10  -88.30   64.82
  -62.79   67.63  -30.11    5.67  -19.88  -77.53  -62.19
   56.92  -48.32  -27.63  -17.71  -37.76  -75.41  -67.24
  -81.00   99.99   50.87  -79.70   99.34  -86.71  -17.30
   13.14   83.65   29.07   83.76  -15.92   26.72  -85.89
   64.86  -26.95   92.94   90.26   26.65  -75.70  -69.19
   60.12   60.81   53.26   49.09  -56.38   30.56  -77.79
  -15.59  -95.68   15.36   15.37  -61.25   34.05   48.77
   40.31   -0.26   24.19  -89.38  -75.74   14.55  -11.88
    0.72   87.72  -34.52   42.82  -43.20   34.51  -54.99
  -51.90  -93.66  -26.97   81.99  -75.68  -21.17   33.65
  -22.51   52.85  -63.79   17.41   68.41    7.07    5.56
  -36.74  -96.62    2.46  -94.86   -8.09   30.93  -57.44
   46.06  -84.41  -80.70  -89.97    4.96  -62.57   21.42
   64.28   51.50   15.88   19.72  -87.52  -11.54  -81.42
  -63.70   92.84  -49.30  -11.01    6.78  -92.75   16.36
   31.16  -60.45   43.26   87.66  -82.37   48.96  -86.50
  -67.68  -48.61   84.73   56.90  -71.47  -59.67  -85.79
   45.86   40.38   61.52   66.14  -25.06   14.87  -59.93
   13.45  -17.82  -52.39  -49.62    7.79  -16.14   34.96
   26.56   -7.01   68.36  -32.51  -66.17  -19.16  -21.57
  -64.87  -43.57   52.88   38.47  -53.18  -55.96   24.50
  -80.50  -43.39  -19.74   45.22   -7.11  -73.66  -77.75
   40.33  -85.78  -51.81  -69.05   59.88  -55.78  -20.46
  -87.51   14.13  -36.88   22.98   21.65   52.56   -8.46
   59.43   85.48  -38.98   18.48   93.93   27.29    4.69
  -99.31  -25.38  -66.58  -42.83   56.90   15.59  -21.53
  -30.42  -90.28  -53.32   -0.10  -88.89  -37.49  -35.92
  -24.50   49.98   56.39    1.44  -46.84  -69.73  -34.74
   79.76  -68.29   -9.70  -68.85  -62.34   62.83  -93.13
   26.99    4.13   51.08  -47.51  -33.48   97.78  -27.85
  -39.08   12.81   26.26  -61.87  -79.03   86.16  -19.23
   92.75  -42.95   -7.85  -86.27  -75.65  -78.25  -93.91
  -61.92   64.51  -33.52   82.06   -5.33   60.11  -15.59
  -80.47   81.60   11.31  -97.62   27.59  -92.03   23.09
   76.90   53.26  -78.64   50.72   99.82   48.93   -2.06
   27.75  -15.57    0.11  -20.02  -90.54   27.87   -7.66
  -32.84   32.86  -71.53   45.69   63.65   12.52  -53.56
   12.39   17.60   70.84  -28.09  -18.69  -20.03  -19.20
   98.44   58.35    7.46  -67.54  -81.45  -24.01   52.63
   95.30   -6.98   61.11  -63.81   72.82  -20.19    9.71
   58.46  -77.59   31.86   15.91  -45.66   76.06   89.08
  -36.55  -76.45  -80.93   56.03  -15.09   -9.61   38.05
    8.95  -43.79  -44.78  -87.34   -2.19   10.50   94.24
heim$