Correct way to reimplement OpenMPs min/max reduction with flush -
some days ago came mind, piece of code implement min/max reduction in openmp, used quite often, might not correct:
in cases, when openmp min-max reduction clause not available (old openmp version) or needed index maximum value used code this:
#pragma omp parallel private(mymax,mymax_idx) shared(globalmax,globalmax_idx) { #pragma omp (...) { } if (mymax >= globalmax) { #pragma omp critical { if ((mymax > globalmax)||(mymax == globalmax && globalmax_idx < mymax_idx) { globalmax = mymax; globalmax_idx = mymax_idx; } } } }
now came mind, code might produce wrong results because shared variable not mean threads access same portion of memory, might use private copy might not date other threads. need use #pragma omp flush
synchronize variable.
[...] #pragma omp flush(globalmax) if (mymax > globalmax) { #pragma omp critical { if (mymax > globalmax) globalmax = mymax; } } [...]
in m. süß et al, common mistakes in openmp , how avoid them implementation described
this reimplementation of reduction using max operator.
but wonder if piece of code correct because don't see writing thread flushing version of globalmax
memory.
also in case of searching index need flush globalmax_idx
variable. right?
this question kind of related
- openmp c++ algorithms min, max, median, average, accepted answer question not use
flush
@ all, i'm unsure if robust. - explict flush direcitve openmp: when necessary , when helpful links this tutorial says critical region flush on enter , exit (i have not yet found information in different place though...), make naive implementation , answer question above correct.
so if code "common mistakes in openmp" assuming critical region flush worth explicitly flush globalmax
-variable before if
?
what code should use?
Comments
Post a Comment