Search code examples
c++multithreadingmaxopenmpreduction

c++ openmp for loop max reduction value during interim of loop execution


When computing a max reduction inside a parallel for loop what is the value of the max reduction variableat intermediate times during the loop's execution? Is it the max only for a particular thread or is it the max of all threads?

The reason I ask is that I want to use the current max value inside the loop to perform a calculation and I want it to be the current maximum of all the threads not just the thread that is executing the loop.

For example:

#include <stdio.h>
#include <omp.h>

int main(int argc, char *argv[]) {

    double randomarray[10];
    //initialize the random array

    double outputarray[10];
    double currentmax = 0;

    #pragma omp parallel for reduction(max:currentmax)
    for( i=0;i<10; i++) {

        if(randomarray[i] > currentmax)
        {
            currentmax = randomarray[i];   
        }

        output[i]=randomarray[i]/currentmax;
        // is this current max for the currently 
        // executing thread or all threads?
    }

}

Solution

  • Is it the max only for a particular thread or is it the max of all threads?

    It's a "per thread" private value within OpenMP parallel region.

    The following code snippet might implement what you want to do, but it doesn't feel so meaningful.

    #pragma omp parallel for
    for( i=0;i<10; i++) {
      double local_max;
    
      #pragma omp critical
      {
        if(randomarray[i] > currentmax)
        {
          currentmax = randomarray[i];   
        }
        local_max = currentmax;
      }
    
      output[i]=randomarray[i]/local_max;
    }