Barriers

General OpenMP discussion

Barriers

Postby AliG » Wed Nov 13, 2013 1:07 pm

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

Postby MarkB » Thu Nov 14, 2013 2:10 am

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: 422
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 8 guests