Additional reduction example?

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

Additional reduction example?

Postby henlof » Tue Jan 15, 2008 5:01 am


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();

      sleep(1); // Or load imbalance

    a = 0;
// A correct program should have a barrier here
#pragma omp for reduction(+:a)
#pragma omp single
    printf("Sum is %d\n",a);

Maybe this behaviour should be clarified in example A.35?

Re: Additional reduction example?

Postby lfm » Fri Jan 18, 2008 8:06 pm

Thanks, I put this on the list for consideration.
Posts: 135
Joined: Sun Oct 21, 2007 4:58 pm
Location: OpenMP ARB

Re: Additional reduction example?

Postby anv » Thu Jan 31, 2008 2:04 am

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.
Posts: 32
Joined: Wed Dec 12, 2007 9:36 am

Return to Draft 3.0 Public Comment

Who is online

Users browsing this forum: No registered users and 0 guests