#include <cstdlib> #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 < template<typename> class MatrixA, typename T, Require< Ge<MatrixA<T>> > = true > void randomInit(MatrixA<T>& A) { std::random_device random; std::mt19937 mt{random()}; std::uniform_real_distribution<T> uniform(-100, 100); for (auto [i, j, Aij]: A) { Aij = uniform(mt); (void) i; (void) j; // suppress gcc warning } } int main() { 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()) ]() mutable { randomInit(A_); }); } for (std::size_t index = 0; index < nof_threads; ++index) { threads[index].join(); } print(A, " %7.2f"); } |