Openmp and reduction on std::vector?

前端 未结 1 1602
抹茶落季
抹茶落季 2020-12-06 13:56

I want to make this code parallel:

std::vector res(n,0);
std::vector vals(m);
std::vector indexes(m);
// fill indexes          


        
相关标签:
1条回答
  • 2020-12-06 14:27

    It is fairly straight forward to do a user declared reduction for C++ vectors of a specific type:

    #include <algorithm>
    #include <vector>
    
    #pragma omp declare reduction(vec_float_plus : std::vector<float> : \
                                  std::transform(omp_out.begin(), omp_out.end(), omp_in.begin(), omp_out.begin(), std::plus<float>())) \
                        initializer(omp_priv = decltype(omp_orig)(omp_orig.size()))
    
    std::vector<float> res(n,0);
    #pragma omp parallel for reduction(vec_float_plus : res)
    for(size_t i=0; i<m; i++){
        res[...] += ...;
    }
    

    1a) Not knowing m at compile time is not a requirement.

    1b) You cannot use the array section reduction on std::vectors, because they are not arrays (and std::vector::data is not an identifier). If it were possible, you'd have to use n, as this is the number of elements in the array section.

    2) As long as you are only reading indexes and vals, there is no issue.

    Edit: The original initializer caluse was simpler: initializer(omp_priv = omp_orig). However, if the original copy is then not full of zeroes, the result will be wrong. Therefore, I suggest the more complicated initializer which always creates zero-element vectors.

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