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

前端 未结 5 646
一整个雨季
一整个雨季 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:15

    Only in Fortran in OpenMP 3.0, and probably only with certain compilers.

    See the last example (Example 3) on:

    http://wikis.sun.com/display/openmp/Fortran+Allocatable+Arrays

    0 讨论(0)
  • 2020-12-15 06:27

    Now the latest openMP 4.5 spec has supports of reduction of C/C++ arrays. http://openmp.org/wp/2015/11/openmp-45-specs-released/

    And latest GCC 6.1 also has supported this feature. http://openmp.org/wp/2016/05/gcc-61-released-supports-openmp-45/

    But I didn't give it a try yet. Wish others can test this feature.

    0 讨论(0)
  • 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 <vector>
    #include <iostream>
    
    int main(){
      std::vector<int> vec;
    
      #pragma omp declare reduction (merge : std::vector<int> : 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<<x<<"\n";
    
      return 0;
    }
    

    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.

    0 讨论(0)
  • 2020-12-15 06:35

    OpenMP cannot perform reductions on array or structure type variables (see restrictions).

    You also might want to read up on private and shared clauses. private declares a variable to be private to each thread, where as shared declares a variable to be shared among all threads. I also found the answer to this question very useful with regards to OpenMP and arrays.

    0 讨论(0)
  • 2020-12-15 06:40

    Array reduction is now possible with OpenMP 4.5 for C and C++. Here's an example:

    #include <iostream>
    
    int main()
    {
    
      int myArray[6] = {};
    
      #pragma omp parallel for reduction(+:myArray[:6])
      for (int i=0; i<50; ++i)
      {
        double a = 2.0; // Or something non-trivial justifying the parallelism...
        for (int n = 0; n<6; ++n)
        {
          myArray[n] += a;
        }
      }
      // Print the array elements to see them summed   
      for (int n = 0; n<6; ++n)
      {
        std::cout << myArray[n] << " " << std::endl;
      } 
    }
    

    Outputs:

    100
    100
    100
    100
    100
    100
    

    I compiled this with GCC 6.2. You can see which common compiler versions support the OpenMP 4.5 features here: https://www.openmp.org/resources/openmp-compilers-tools/

    Note from the comments above that while this is convenient syntax, it may invoke a lot of overheads from creating copies of each array section for each thread.

    0 讨论(0)
提交回复
热议问题