Weird results with openmp reduction

2019-09-15 05:48发布

In have this class:

template <typename C, typename R, typename D>
class myClass{
    public:
    R Query(const C &query);
    ...
    private:
    struct Compare{
        D val;
        size_t index;
    };
    #pragma omp declare reduction(minimum : Compare : omp_out = omp_in.val < omp_out.val ? omp_in : omp_out)
    std::vector<C> values;
    ...
}


template <typename C, typename R, typename D>
R Cache<C,R,D>::Query(const C &query){
    Compare min;
    R result;
    if(!values.empty()){
            #pragma omp parallel for reduction(minimum:min)
            for(size_t i=1; i<values.size(); i++){
                D d = distance->compute(query, values[i]);
                std::cout<<" distance("<<query<<" "<<values[i].code<<")= "<<d;
                if(d < min.val){
                    std::cout<<" NEW MIN!";
                    min.val = d;
                    min.index = i;
                }
                std::cout<<std::endl;
            }
        std::cout<<"min.val="<<min.val<<std::endl;
   }
...

D d = distance->compute(query, values[i]); value is correct (and the following print too).

However, for some weird reason, everytime that the parallel for is computed, min.val is 0 (in few words the last cout prints always min.val=0).

Obviously this is not the whole code and I made it as simple as possible.

2条回答
The star\"
2楼-- · 2019-09-15 06:30

The initializer is not needed at all If you initialize the struct inside the parallel block

 #pragma omp declare reduction( \
    minimum : Compare : omp_out = omp_in.val < omp_out.val ? omp_in : omp_out\
    ) 

#pragma omp parallel reduction(minimum:min)
{
    D d = distance->compute(query, values[0]);
    min = Compare{d,0}
    #pragma omp for
    for(size_t i=1; i<values.size(); i++){
        d = distance->compute(query, values[i]);
        if(d < min.val)
            min = Compare{d,i};
    }
}
查看更多
太酷不给撩
3楼-- · 2019-09-15 06:50

You are missing the initalizer for user declared reduction:

#pragma omp declare reduction(minimum : Compare : omp_out = omp_in.val < omp_out.val ? omp_in : omp_out) \
                initializer (omp_priv=Compare{std::numeric_limits<D>::max(), -1})

Alternatively, you can add a member initialization:

template <typename D>
class Foo {
struct Compare{
    D val {std::numeric_limits<D>::max()};
    int index {-1};
};

This is more general and will make sure the program works correctly even without -fopenmp.

查看更多
登录 后发表回答