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?
reduction(min:
should work since OpenMP 3.1. Another interesting question is if it works withfmin
. I think I saw an example of this in the manual.fmin
andstd::min
are not the same thing despite what some people think. – Transmittermin
should be implemented like thisout=in<out?in:out;
Maybe on your systemstd:min()
doesn't abide to this... – Centroidfmaxf
so I think it works forfmin
andfmax
as well. I thoughstd::min
expanded to a ternary operator so I am not sure why it would fail. You would have to look at the macro. – Transmitterstd::min
can be implemented in many different ways en.cppreference.com/w/cpp/algorithm/min. What you can do is create a custom Reduction operator with OpenMP4.0 which will work withstd::min
. – Transmitterstd::min
? What compiler are you using? What are your compiler options? – Transmitter