openmp parallel for loop with two or more reductions

39,945

Solution 1

You can do reduction by specifying more than one variable separated by a comma, i.e. a list:

#pragma omp parallel for default(shared) reduction(+:sum,result) ...

Private thread variables will be created for sum and result that will be combined using + and assigned to the original global variables at the end of the thread block.

Also, variable y should be marked private.

See https://computing.llnl.gov/tutorials/openMP/#REDUCTION

Solution 2

You can simply add another reduction clause:

#include <iostream>
#include <cmath>

int main(){
    double sum_i = 0, max_i = -1;
    #pragma omp parallel for reduction(+:sum_i) reduction(max:max_i)
    for (int i=0; i<5000; i++){
        sum_i += i;
        if (i > max_i)
            max_i = i;
    }
    std::cout << "Sum = " << sum_i << std::endl;
    std::cout << "Max = " << max_i << std::endl;
    return 0;
}

From OpenMP 4.5 Complete Specifications (Nov 2015)

Any number of reduction clauses can be specified on the directive, but a list item can appear only once in the reduction clauses for that directive.

The same works on Visual C++ that uses oMP v2.0: reduction VC++

Share:
39,945

Related videos on Youtube

pyCthon
Author by

pyCthon

Updated on July 09, 2022

Comments

  • pyCthon
    pyCthon almost 2 years

    Hi just wondering if this is the right way to go going about having a regular for loop but with two reductions , is this the right approach below? Would this work with more then two reductions as well. Is there a better way to do this? also is there any chance to integrate this with an MPI_ALLREDUCE command?

    heres the psuedo code
    
          #pragma omp parallel for \
          default(shared) private(i) \
          //todo first  reduction(+:sum)
          //todo second reduction(+:result)
    
          for loop i < n; i ++; {
            y = fun(x,z,i)
            sum += fun2(y,x)
            result += fun3(y,z)
          }
    
  • worenga
    worenga over 8 years
    What if there are different operations to be performed e.g. + and max ?
  • Azmisov
    Azmisov about 8 years
    @mightyuhu See my answer
  • Royi
    Royi over 6 years
    It doesn't work in GCC 7.1. Any idea how to apply 2 different reductions?
  • Marv
    Marv almost 6 years
    @Royi maybe it's because of the comma after the first reduction term? I don't recall commas being used to distinguish omp parameters.