Parallelizing a for loop using openmp & replacing push_back

前端 未结 3 1434
深忆病人
深忆病人 2020-12-19 11:18

I\'d like to parallelize the following piece of code but am new to openmp and creating parallel code.

std::vector good_matches;
for (int i = 0;         


        
3条回答
  •  猫巷女王i
    2020-12-19 11:55

    One possibility may be to use private vectors for each thread and combine them in the end:

    #include
    
    #include
    #include
    #include
    #include
    
    using namespace std;
    
    int main()
    {
      vector global_vector;  
      vector< vector > buffers;
    
      #pragma omp parallel
      {
        auto nthreads = omp_get_num_threads();
        auto id = omp_get_thread_num();
        //
        // Correctly set the number of buffers
        //
      #pragma omp single
        {
          buffers.resize( nthreads );
        }
        //
        // Each thread works on its chunk
        // If order is important maintain schedule static
        //
      #pragma omp for schedule(static)
        for(size_t ii = 0; ii < 100; ++ii) {      
          if( ii % 2 != 0 ) { // Any other condition will do
              buffers[id].push_back(ii);
          }
        }
        //
        // Combine buffers together
        //
        #pragma omp single
        {
          for( auto & buffer : buffers) {
            move(buffer.begin(),buffer.end(),back_inserter(global_vector));
          }
        }
      }
      //
      // Print the result
      //
      for( auto & x : global_vector) {
        cout << x << endl;
      }    
      return 0;
    }
    

    The actual speed-up depends only on the amount of work done inside each loop.

提交回复
热议问题