0

I wrote a finite element program which assembles small matrices into a huge sparse matrix. It is multithreaded (using std::thread). Depending on the number of threads, each thread is in charge of computing some amount of small matrices and writing the result into the sparse matrix. Thread 1 writes to indexes (0,0) up to (i-1, i-1) for example, thread 2 writes to indexes from (i, i) to (2*i -1, 2*i -1) and so on, where i is precalculated depending on the number of threads used.

I would like to do the same with std::for_each and std::execution::par. However, I read I cannot choose the number of threads used. Is there any way I could tell std::for_each where to write the results, that is, the precise location (i, j) of the sparse matrix?

mfnx
  • 2,894
  • 1
  • 12
  • 28
  • 1
    Why don't you use [openmp](https://stackoverflow.com/questions/60753685/c-ppl-h-for-parallel-for-in-linux/60754014#60754014) instead of self made thread pooling ? – Victor Gubin Nov 02 '20 at 13:24
  • 1
    @VictorGubin Well, I did not, no idea why. I'm asking about parallel for_each though. – mfnx Nov 02 '20 at 13:27
  • [std::for_each](https://en.cppreference.com/w/cpp/algorithm/for_each) is single threaded, is in it. Off cause you can [capture](https://en.cppreference.com/w/cpp/language/lambda) your `std::size_t i=0; j=0; [i,j](...) {++i,++j ...}` cords by lambda you going to put into std::for_each. Unlike std::for_each, `#pragma omp parallel for simd for(...)` give's you parallel execution. – Victor Gubin Nov 02 '20 at 13:36
  • @VictorGubin As I understand it, std::for_each(std::execution::par, ...) would execute with multiple threads where the number of threads is determined based on information provided by the OS. Also, I can't calculate i if I don't know the total number of threads which will be used. – mfnx Nov 02 '20 at 14:10

0 Answers0