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 = 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 < 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/ws19/session16 -std=c++17 -o random_init10 random_init10.cpp theon$ ./random_init10 -24.04 47.38 -72.74 57.80 -85.11 -84.66 98.82 50.75 98.13 99.91 54.93 -56.83 -79.75 -52.36 25.02 47.73 45.08 48.16 52.64 -42.83 69.48 -88.75 -90.23 19.15 98.06 -89.66 29.77 77.73 -19.76 -86.85 -49.53 -32.96 -43.49 -66.10 -52.73 -90.09 49.00 -13.48 0.30 -69.23 59.17 4.43 78.37 -70.87 43.04 -91.43 -23.96 -86.89 63.51 -13.55 -43.34 71.24 53.28 -49.78 32.39 -10.94 -16.25 48.17 -74.11 -97.07 -61.55 2.01 75.02 39.83 73.68 74.73 -24.32 11.13 72.69 66.54 41.07 52.83 54.48 56.76 68.39 -43.98 -50.92 11.04 91.38 62.01 -31.55 -78.97 25.52 -32.83 -88.98 63.50 3.91 -69.68 -36.28 -87.11 2.23 23.37 -85.88 45.75 30.24 69.69 -11.56 -18.61 4.81 -90.91 -57.79 -49.91 -25.23 -55.06 -58.27 85.62 -13.22 -11.87 23.95 88.97 -14.11 42.03 8.31 38.79 -11.11 -72.67 -34.38 75.80 -47.06 4.74 44.38 -6.59 -97.58 83.14 -67.69 -48.69 74.01 71.83 69.94 42.08 -25.53 96.11 -66.21 46.82 -1.92 -59.84 -73.77 -26.10 -93.19 77.56 -6.66 81.38 84.79 -70.83 94.99 -15.21 33.24 19.80 -66.63 -76.10 74.76 9.04 -72.65 79.05 -36.66 -47.38 -85.07 11.40 -11.14 11.58 71.48 -39.51 -54.63 13.68 -94.26 -91.28 -57.70 68.74 97.17 -89.17 45.83 74.04 -60.32 36.41 23.87 -45.53 69.43 -78.12 40.24 81.75 -10.40 -2.58 -32.13 -81.44 -79.59 -17.63 -62.43 -2.91 48.18 -51.19 -1.61 -14.36 70.72 43.77 -75.66 73.14 -0.72 35.04 -37.56 38.15 -1.35 -36.79 90.86 26.65 -21.82 7.97 -54.53 73.79 97.93 49.37 -29.67 -2.14 -93.65 30.58 70.17 -49.93 -89.98 23.66 -17.49 46.96 -77.50 51.63 75.91 36.47 -20.85 38.88 40.74 26.81 52.96 81.86 17.36 -79.03 96.10 -9.25 11.07 13.17 -70.36 20.09 -21.45 86.99 1.01 86.29 -40.88 -49.07 -4.42 -35.84 -10.12 95.30 27.11 -76.42 -39.60 16.16 -50.76 83.59 -18.35 -44.52 85.63 -43.83 94.04 48.16 4.53 65.20 -62.40 -89.26 -69.41 89.07 -16.23 10.72 -28.68 -89.97 27.00 -69.47 15.22 82.99 -53.07 95.37 -2.83 93.43 -13.17 4.34 88.75 -7.70 -66.66 -49.16 -31.04 20.17 -2.11 -36.67 95.90 57.88 -35.56 -50.57 7.67 -0.50 7.01 -7.51 -91.70 89.44 47.14 -37.10 23.32 -7.59 35.70 75.63 76.00 58.68 11.25 -19.92 -52.14 7.31 76.97 -11.73 -56.35 -68.93 84.65 85.79 -82.21 -52.35 0.68 -52.36 2.71 93.22 35.40 48.44 -65.11 -81.75 64.63 -34.23 -84.81 29.73 -83.68 -83.73 -44.26 -40.55 50.69 10.72 76.49 -77.60 -24.57 -68.34 83.74 5.95 42.00 -71.81 -37.66 62.33 96.07 -89.10 -30.31 -38.62 8.30 82.51 88.57 17.80 -48.39 -35.60 -38.13 theon$
heim$ g++-8.3 -O3 -g -I/home/numerik/pub/hpc/ws19/session16 -std=c++17 -o random_init10 random_init10.cpp -lpthread heim$ ./random_init10 4.72 57.30 96.96 -68.86 3.07 27.10 54.52 -83.94 -68.92 -84.34 33.78 95.92 -70.08 -8.33 -28.38 2.09 86.27 26.04 97.17 -61.02 -0.84 42.03 -61.46 8.55 73.30 -78.91 61.57 22.86 21.44 -11.24 63.64 74.63 -77.68 21.76 80.40 41.75 -46.72 96.38 -46.14 13.38 18.64 -39.68 73.34 38.29 -69.49 23.96 -44.04 30.38 -87.94 -61.94 -96.36 99.64 70.16 7.85 -25.52 -4.65 29.71 12.30 78.46 -84.71 7.34 84.84 -60.34 86.62 86.73 -7.83 21.70 66.33 -2.09 59.43 57.82 -71.43 76.76 -17.99 -94.93 -14.80 -19.97 61.35 -84.48 -39.64 4.13 -59.88 -24.23 8.83 -19.98 -78.60 -58.02 -62.76 66.63 -9.55 -14.62 27.18 -31.97 19.33 -2.44 -38.41 -29.35 42.72 -71.74 16.68 9.03 -30.27 -85.40 -31.68 80.56 0.45 2.98 -92.25 76.17 54.15 28.09 0.43 24.84 -40.08 87.24 -65.99 -73.40 96.86 29.94 66.03 23.93 1.28 -6.86 -85.82 68.40 15.95 19.47 2.50 19.60 -47.61 25.69 -73.90 -43.38 26.55 -99.37 -61.43 -40.79 7.19 21.13 9.97 54.39 0.56 -73.96 92.63 -62.93 38.97 65.61 73.36 -36.86 92.77 -94.17 90.27 -30.22 -97.31 8.25 -70.43 -94.90 1.76 51.70 87.89 -13.84 -5.82 30.59 -94.16 0.32 -36.15 -40.71 52.23 -33.24 73.36 89.48 -62.76 -12.39 -22.19 63.26 54.40 -4.14 65.34 79.24 -21.08 60.87 -59.87 -41.50 -41.25 26.65 -13.87 -93.20 58.61 -5.03 -80.24 48.05 -91.67 -31.24 22.06 -56.25 75.28 -14.53 78.12 -61.64 -6.54 -84.77 64.01 -58.10 73.56 21.25 -3.31 82.24 -56.50 -50.07 -75.70 36.62 -32.86 -94.04 35.74 -44.22 88.00 -40.91 53.72 -71.00 56.29 64.95 6.12 33.66 -0.73 -0.81 -49.67 -8.17 -86.78 44.75 83.53 13.70 -14.97 -7.31 69.92 -89.04 44.40 -33.22 -36.07 -39.70 20.89 68.29 -55.87 -94.40 -70.91 -27.49 47.30 -11.06 -50.86 97.56 18.57 72.01 -39.25 -2.88 78.87 -60.59 30.90 75.37 -70.12 48.83 45.79 71.89 -84.22 -32.19 77.85 43.12 -39.83 25.39 -65.53 33.10 -14.33 -27.43 57.70 58.81 34.18 61.07 69.86 98.61 73.47 63.11 19.74 11.88 -98.00 40.26 -44.49 -50.49 8.81 -33.73 -70.25 -65.32 37.79 -48.34 -82.64 80.27 -50.12 18.61 33.58 -4.53 20.69 -97.04 -29.17 77.25 75.72 -19.27 18.20 12.46 -42.22 76.57 48.77 57.31 -11.45 -66.20 3.01 -64.23 4.91 -86.60 -97.97 45.34 17.44 -77.39 66.20 79.05 -91.65 -46.14 31.21 -96.73 54.34 -45.10 -61.17 -56.74 -62.95 53.05 32.05 4.11 -82.42 91.16 94.29 53.81 -40.03 -3.06 64.44 -89.70 -29.41 -68.78 -65.57 -68.21 9.73 33.02 48.03 28.81 -9.22 24.51 -16.68 -50.61 72.96 96.36 -82.79 -62.97 heim$