# Sample solution

#### Content

The wanted RAII class can be formulated quite simply:

struct Guard {
Guard(std::mutex& mutex) : mutex(mutex) {
mutex.lock();
}
~Guard() {
mutex.unlock();
}
std::mutex& mutex;
};


This allows to simplify the gen method:

double gen() {
Guard guard(mutex);
return uniform(mt);
}


It shouldn't be surprising that a similar class is present in the standard C++ library. It is named std::lock_guard and gets the mutex class as template parameter:

double gen() {
std::lock_guard<std::mutex> guard(mutex);
return uniform(mt);
}

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

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

struct Guard {
Guard(std::mutex& mutex) : mutex(mutex) {
mutex.lock();
}
~Guard() {
mutex.unlock();
}
std::mutex& mutex;
};

struct MyRandomGenerator {
MyRandomGenerator() : mt(std::random_device()()), uniform(-100, 100) {
}
double gen() {
Guard guard(mutex);
return uniform(mt);
}
private:
std::mutex mutex;
std::mt19937 mt;
std::uniform_real_distribution<double> uniform;
};

template <
template<typename> class MatrixA, typename T,
typename RNG,
Require< Ge<MatrixA<T>> > = true
>
void randomInit(MatrixA<T>& A, RNG& rng) {
for (auto [i, j, Aij]: A) {
Aij = rng.gen();
(void) i; (void) j; // suppress gcc warning
}
}

int main() {
MyRandomGenerator rng;
GeMatrix<double> A(51, 7);

for (std::size_t index = 0; index < nof_threads; ++index) {
auto firstRow = slices.offset(index);
auto numRows = slices.size(index);
A_ = A.view(firstRow, 0, numRows, A.numCols()),
&rng
]() mutable {
randomInit(A_, rng);
});
}
for (std::size_t index = 0; index < nof_threads; ++index) {
}
print(A, " %7.2f");
}

theon$g++ -O3 -g -I/home/numerik/pub/hpc/ws19/session16 -std=c++17 -o random_init9 random_init9.cpp theon$ ./random_init9
91.61   49.35  -90.07   -1.73   53.38   51.23  -55.87
-0.09   59.57  -81.86    7.13   36.51   58.61  -67.74
-11.29   80.32    0.60   12.73  -50.94  -88.11  -49.64
-74.40  -85.65  -87.28   99.93  -41.68   45.43   49.74
1.28  -51.36  -11.55   64.68   31.23  -74.61    7.18
-46.01   42.31   93.78   34.98   43.76   22.83   68.01
15.64   53.39   97.54   54.98   54.81   71.58   23.38
61.55   28.99  -72.58  -53.40  -89.27   15.82   41.90
-52.93  -70.69   78.32   75.77  -88.28   91.46  -96.27
-88.61  -42.34  -35.55  -95.89   89.33   49.25  -67.93
-82.86  -28.76  -27.17  -53.83   15.62   99.74  -46.57
8.27   18.07  -28.92    3.59  -31.47   14.57  -98.96
86.02   98.89   39.52   -6.06  -50.20  -58.07  -13.97
-17.76   71.63  -19.30  -68.95   82.82  -86.29   63.66
-17.76   58.16  -80.17  -13.42   64.68  -51.59   23.38
-52.21  -46.75  -49.71   78.91  -44.79  -80.76   46.36
-92.83   61.13  -58.99  -33.27   65.28   87.08   68.47
-24.19   61.61  -27.15   -3.60   35.18  -79.71  -89.43
62.61  -89.97  -69.77  -99.14   62.69  -48.52  -61.21
31.61   94.32  -64.62  -28.07  -20.34   61.83   50.32
-52.11   14.40   88.35   28.56  -95.18   13.58  -68.65
-69.76  -62.19   82.55  -92.81   61.73  -20.46  -50.85
68.77  -96.25  -88.00  -64.44  -36.79  -50.65   15.53
56.47   54.23   50.77  -82.12   18.20   45.96  -90.23
-9.72   82.45  -44.85  -24.11  -25.19  -46.66    1.41
-35.86   21.75   -6.24   16.88   79.84   93.77   84.87
95.53  -46.68  -67.82   32.06   42.29   12.83  -49.82
64.28  -71.80  -72.42    3.34   91.94   20.69   18.15
92.27   71.63  -26.76   25.33   32.26   53.55  -84.39
-16.34   86.35   -0.82  -57.90   65.03   27.03   -5.67
76.17  -83.67  -11.47  -50.14  -78.98   78.58   34.71
-71.27  -96.03  -81.47   94.22  -83.45  -60.27   20.21
91.05  -97.63  -97.52   34.12  -88.73   19.30   63.53
22.81  -79.23   75.31  -90.29   63.16  -80.96  -49.36
56.78  -37.00   87.17  -81.72  -33.55   94.02  -73.09
-28.15  -57.52   94.93   59.49   88.01   47.83  -10.56
-28.06  -25.82  -71.54  -98.54   92.45  -48.29  -45.50
87.59  -49.58  -70.30   12.04   -1.07  -53.97    2.61
-0.56  -11.38  -79.19  -17.73    9.21  -63.06   -5.57
61.35   23.97   30.37  -55.31   59.32   50.91   69.87
37.96  -38.15  -45.62  -43.64   92.27  -55.39  -92.96
-28.69   -5.35   27.90  -35.15  -77.23   80.53  -97.98
46.34  -54.31  -86.51   -4.11   93.43    9.56   26.70
95.83   96.01   22.20  -51.69  -10.89  -40.07   66.97
-97.26   34.83   -5.84  -94.82    8.61   99.26  -45.07
-76.22    8.70  -81.21   91.94   75.42   -4.36  -21.83
40.46   60.53  -29.94  -18.61  -73.06   -8.07  -57.41
95.79   60.00  -35.98   41.66  -63.55  -20.69  -84.39
49.94   -4.99   -2.60  -52.29  -86.19   97.84  -54.57
-50.76  -62.99  -55.42   -2.46   -6.69  -40.35   -1.72
95.16   36.89   53.87  -93.62  -98.90   66.45   71.40
theon$ heim$ g++-8.3 -O3 -g -I/home/numerik/pub/hpc/ws19/session16 -std=c++17 -o random_init9 random_init9.cpp -lpthread
heim$./random_init9 -18.34 -82.55 -33.01 -91.82 66.29 -63.08 -36.06 60.73 13.11 -2.10 -34.09 -4.36 -74.55 -80.30 -33.32 -2.48 11.90 -33.01 -7.33 -24.57 99.71 46.63 -33.19 -65.95 79.95 -29.89 81.73 62.37 76.24 27.28 78.01 80.30 87.92 44.97 34.63 -76.45 57.52 65.14 -74.11 -2.50 52.54 84.06 -38.95 -39.02 42.31 -56.76 -81.40 97.41 0.84 46.12 6.22 94.45 -67.77 -27.10 82.78 80.81 98.37 -3.58 -84.42 -29.65 -57.63 97.68 18.13 -76.42 35.43 63.01 10.97 -7.49 2.80 74.59 -52.50 -97.72 -51.81 -84.56 -90.63 -19.22 -3.17 -0.51 -96.08 -26.47 43.56 55.52 68.07 -95.70 -73.15 -39.50 31.92 58.23 35.16 -90.31 -3.89 -92.56 55.30 -8.32 76.54 6.01 -47.66 -66.55 -39.42 -35.45 -95.60 27.47 -88.16 -62.28 15.94 51.23 -97.71 -35.82 -59.68 41.87 -77.63 -77.37 64.45 -26.81 17.83 -73.59 -33.78 49.28 79.87 6.09 95.95 32.82 -73.90 48.78 -24.46 93.57 62.83 13.84 7.75 -20.36 -66.36 -96.73 -4.96 35.73 -16.40 -8.27 -99.96 71.48 43.61 94.94 -26.80 -49.36 -22.05 86.70 -58.31 -96.69 46.33 -66.13 -29.12 -4.41 -48.35 -31.12 29.71 -56.23 98.38 -83.27 -98.40 92.54 -11.90 27.21 -43.16 40.39 4.28 -39.68 52.15 -6.61 -84.01 46.12 52.59 77.23 -36.01 41.28 -79.12 -31.73 -84.06 -13.71 36.79 -8.52 -66.18 -61.66 -17.74 93.29 50.76 75.52 54.44 62.30 -68.51 55.08 69.97 -49.00 -36.69 5.47 -53.55 -78.76 -70.43 9.67 92.41 4.08 -64.79 42.40 -39.92 82.12 -43.63 -61.94 -32.22 -41.77 66.29 87.96 -29.31 -87.35 -96.97 -85.34 8.35 -87.20 -95.23 -75.19 -47.67 16.87 61.29 11.63 47.23 -46.51 -77.34 -53.60 -12.68 3.36 63.32 -84.97 -2.75 9.93 -93.76 -93.18 -52.16 67.02 -89.03 49.21 -94.10 -26.17 -94.30 41.42 -20.97 -94.48 17.21 -11.52 -1.29 -97.55 -78.15 -62.99 57.94 59.94 49.03 67.11 -84.20 -66.69 46.46 40.61 -89.71 19.00 -63.19 -37.32 -99.95 47.60 -39.21 92.25 90.55 -6.31 15.00 -41.40 -6.42 -8.81 88.93 65.85 93.81 -11.14 25.88 42.76 -12.74 87.93 -40.70 42.24 49.68 -97.71 -59.77 -88.69 19.16 -96.66 51.56 0.91 -82.11 -82.97 45.34 -79.66 -5.38 7.66 -56.58 -86.66 2.56 -58.20 74.97 61.61 -58.98 -55.46 5.07 -94.96 -10.94 98.06 66.10 81.53 52.00 58.65 -28.76 -6.70 18.81 -7.81 65.45 92.34 24.54 50.66 -44.54 56.26 73.81 -83.69 28.96 -57.15 34.74 8.03 14.05 -60.61 64.28 49.39 -2.37 -72.26 -83.43 71.89 -24.07 75.06 -80.89 86.36 -83.71 3.24 -45.31 10.38 44.90 -61.75 16.39 -38.73 68.59 63.80 54.45 -53.09 21.63 52.67 -19.17 4.96 -31.57 -65.25 87.11 heim$