Min reduction Min And min

I am testing OpenMP min shortening. If I write my code as follows, it will return the correct result: res = 3.

#include <omp.h>
#include <iostream>
#include <algorithm>

int main(){

    omp_set_num_threads(5);

    float res=10;

#pragma omp parallel for default(shared) reduction(min:res)
    for(int i1 = 0; i1 <= 10; i1++)
        for(int i0 = 0; i0 <= 10; i0++)
            if(res > 3.0+i1+20*i0)
                res = 3.0+i1+20*i0;

    std::cout << "res = " << res << std::endl;

    return 0;
}

But if I write in an alternative way, replacing the "if" statement with "std :: min", then the result will be wrong: res = 10.

#include <omp.h>
#include <iostream>
#include <algorithm>

int main(){

    omp_set_num_threads(5);

    float res=10;

#pragma omp parallel for default(shared) reduction(min:res)
    for(int i1 = 0; i1 <= 10; i1++)
        for(int i0 = 0; i0 <= 10; i0++)
            res = std::min(res,static_cast<float>(3.0+i1+20*i0));

    std::cout << "res = " << res << std::endl;

    return 0;
}

Is the minimum OpenMP reduction reduced when using std :: min?

+4
source share
3 answers

The problem is that you have a data race.

OpenMP: res, . , res .

std::min, res . res shared - default(shared), res , .

. std::min, - .

0

, : , .

reduction, , , min (.. ), , . , , min. , .

, , : icpc (ICC) 16.0.0 OpenMP 201307. ++, ?

0

I would swap loops before making a conclusion. In a similar context, I found separate variables to reduce the inner and outer loop. -std :: min works well with icpc but not g ++ or msvc

-1
source

Source: https://habr.com/ru/post/1654023/


All Articles