Barriers

General OpenMP discussion

Barriers

How can i put 2 omp barriers on this ?

I get a fatal error in the lines where i need barriers

void *function()
{
int id = 0, level = 2, i = 0;

id++;
printf("ID = %d\n", id);

while(level <= N)
{
for (i=level+(id-1)*level; i<=N; i=i+level*P)
{
V[i] = V[i] + V[i-level/2];
}

//Barrier
#pragma omp barrier

level = level*2;
}

level = level/2;

if(level == N)
{
level = level/2;
}

while(level > 1)
{
for(i=level+level/2+(id-1)*level; i<=N; i=i+level*P)
{
V[i] = V[i] + V[i-level/2];
}

//Barrier
//#pragma omp barrier

level = level/2;
}
}

and main part

#pragma omp parallel
{
#pragma omp for
for (i=0;i<P;i++)
{
function();
}
}
AliG

Posts: 1
Joined: Wed Nov 13, 2013 1:00 pm

Re: Barriers

Is P equal to the number of threads, by any chance?

If so, then you can simply write the main as

Code: Select all
`#pragma omp parallel{function();}`

The reason that you get an error is that barriers are not allowed be nested inside a worksharing for construct. This is because in general, if the number of parallel loop iterations is not divisible exactly by the number of threads, some threads will encounter the barrier more times than others, which would result in deadlock.
MarkB

Posts: 557
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh