Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openmp parallel for loop with two or more reductions

Tags:

c++

c

openmp

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)
      }
like image 465
pyCthon Avatar asked Feb 22 '12 02:02

pyCthon


Video Answer


1 Answers

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

like image 93
devil Avatar answered Oct 14 '22 10:10

devil