I am testing OpenMP min reduction. If I write my code like the following, 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 by replacing "if" statement with "std::min", then the result is 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 OpenMP min reduction interfering with std::min?