I have a code:
#include <stdio.h>
#include <omp.h>
static void func()
{
char t[30];
sprintf(t,"%d %d\n",omp_get_num_threads(),omp_get_thread_num());
write(1,t,strlen(t));
write(1,"checkpoint 1\n",13);
#pragma omp barrier
write(1,"checkpoint 2\n",13);
#pragma omp barrier
write(1,"checkpoint 3\n",13);
#pragma omp barrier
write(1,"checkpoint 4\n",13);
}
int main()
{
int i;
#pragma omp parallel for
for(i=0;i<2;i++)
{
func();
}
}
and the output:
8 1
8 0
checkpoint 1
checkpoint 1
checkpoint 2
checkpoint 2
[here my program blocks].
if I change 2 in for to 8, it works. but I want to have 2 in my for loop.
how to make my code working without using omp_set_num_threads(2) before the loop? (also it works when I put this)
Barriers are used to synchronize all threads in the team. All arriving threads will be blocked until all threads have reached the barrier. Then and only then may they all continue.
As a consequence of this, in order for your program to terminate, all threads have to reach the same number of barriers during their life. If one thread has more barriers than the others, that thread can never pass its extra barrier because it will wait for the other threads - which will never get there because they don't have that extra barrier.
You have a barrier in the function
func
, which is executed once per iteration of your parallel loop. Since it's up to OpenMP to assign those iterations to threads, not all of them may receive the same number of iterations (in your case they do receive the same number for a thread count of 2 - one iteration for every thread, that's why your program terminates then). That unequal number of barriers blocks your program.Make sure to place barriers only in places where you know that all threads will execute it equally often.