Using the reduction clause

General OpenMP discussion

Using the reduction clause

Postby octavio » Fri May 11, 2012 1:07 pm

Hi
I want to use the reduction clause to parallelize something like this:

Code: Select all

.
.
.
#pragma parallel for ... reduction(+: sum_1,  sum_2, ..., sum_100)
for(i=0; i<N; i++)
{
      if(proposition_1) sum_1++;
      if(proposition_2) sum_2++;
                      ...
      if(proposition_100) sum_100++;
}
.
.
.


and I know the variables sum_i have to be scalars.

Does I have to write down each "if(proposition_i) sum_i ++ "" or is there a better way to do this?
octavio
 
Posts: 4
Joined: Thu Feb 02, 2012 8:02 am

Re: Using the reduction clause

Postby MarkB » Mon May 14, 2012 11:56 am

Are you using scalars just so you can use the reduction clause? If so, it would be neater to use an array and a critical section:
Code: Select all

    .
    .
    .
    #pragma omp parallel private(t_sum) shared (sum)
   {
    #pragma omp for
    for(i=0; i<N; i++)
    {
      for (j=0; j<100;j++){
          if(proposition(j)) t_sum[j]++;
       }
    }
    #pragma omp critical
    {
       for (j=0; j<100;j++){
          sum[j] +=  t_sum[j];
       }
     }
    }
    .
    .
    .
MarkB
 
Posts: 450
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 12 guests