Is it possible to do a reduction on an array with openmp?

前端 未结 5 670
一整个雨季
一整个雨季 2020-12-15 05:57

Does OpenMP natively support reduction of a variable that represents an array?

This would work something like the following...

float* a = (float*) c         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 06:31

    OpenMP can perform this operation as of OpenMP 4.5 and GCC 6.3 (and possibly lower) supports it. An example program looks as follows:

    #include 
    #include 
    
    int main(){
      std::vector vec;
    
      #pragma omp declare reduction (merge : std::vector : omp_out.insert(omp_out.end(), omp_in.begin(), omp_in.end()))
    
      #pragma omp parallel for default(none) schedule(static) reduction(merge: vec)
      for(int i=0;i<100;i++)
        vec.push_back(i);
    
      for(const auto x: vec)
        std::cout<

    Note that omp_out and omp_in are special variables and that the type of the declare reduction must match the vector you are planning to reduce on.

提交回复
热议问题