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

so if code "common mistakes in openmp" assuming critical region flush worth explicitly flush globalmax-variable before if?

what code should use?


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -