Additional reduction example?

The public comment period closed January 31, 2008. This forum is now locked (read only).
Locked
henlof

Additional reduction example?

Post by henlof »

Hi,

I've encountered a situation where you have a reduction directive inside a parallel region and omit the barrier after the initialization of the reduction variable. In this case there will be a race for the reduction value even in the case of no nowait clause. Here's an example:

Code: Select all

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

int main(void)
{
  int a;
  int i,myid;

#pragma omp parallel shared(a) private(i,myid)
  {
    myid = omp_get_thread_num();

    if(myid==0)
      sleep(1); // Or load imbalance

    a = 0;
// A correct program should have a barrier here
#pragma omp for reduction(+:a)
    for(i=0;i<10;i++)
      a+=i;
#pragma omp single
    printf("Sum is %d\n",a);
  }
}
Maybe this behaviour should be clarified in example A.35?

lfm
Posts: 135
Joined: Sun Oct 21, 2007 4:58 pm
Location: OpenMP ARB

Re: Additional reduction example?

Post by lfm »

Thanks, I put this on the list for consideration.

anv
Posts: 32
Joined: Wed Dec 12, 2007 9:36 am

Re: Additional reduction example?

Post by anv »

I think the example provided is quite similar to the following example

Code: Select all

#pragma omp parallel shared( a )
{
    a = 0;
    a += 1;
}
This is just a matter of any access to shared variable inside the parallel region, regardless of is this a reduction, or simple assignment, or read access. It is obvious that it is the user's responsibility to have such accesses synchronized, and I believe this is clearly covered by the memory model section in the specification (p 14, lines 8 - 10). Example A.35 does not have this problem, as the initialization of shared variables made in serial code.

Locked