#include #include #include #include #include #include #include #include #include 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 uniform; }; template typename std::enable_if::value, void>::type randomInit(MA& A, RNG& rng) { using ElementType = typename MA::ElementType; using Index = typename MA::Index; hpc::matvec::apply(A, [&](ElementType& val, Index i, Index j) -> void { val = rng.gen(); }); } int main() { using namespace hpc::matvec; using namespace hpc::aux; MyRandomGenerator rng; GeMatrix A(51, 7); unsigned int nof_threads = std::thread::hardware_concurrency(); std::vector threads(nof_threads); Slices::Index> slices(nof_threads, A.numRows); for (int index = 0; index < nof_threads; ++index) { auto firstRow = slices.offset(index); auto numRows = slices.size(index); auto A_ = A(firstRow, 0, numRows, A.numCols); threads[index] = std::thread([=,&rng]() mutable { randomInit(A_, rng); }); } for (int index = 0; index < nof_threads; ++index) { threads[index].join(); } print(A, "A"); }