I am having a problem with some OpenMP code unexpectedly freezing, here is the shortest example I could find that causes the problem (it's very difficult to pinpoint exactly which lines are necessary because the freeze is very intermittent):
- Code: Select all
#include <omp.h>
#include <stdlib.h>
#include <stdio.h>
void func()
{
long* x = malloc(100 * sizeof(long));
for (int i = 0; i < 100; i++)
x[i] = i;
long* y = malloc(100 * sizeof(long));
for (int i = 0; i < 100; i++)
y[i] = i;
#pragma omp parallel num_threads(2)
{
#pragma omp for
for (int j = 0; j < 10; j++)
{
}
}
free(y);
free(x);
}
int main()
{
for (int trial = 0; ; trial++)
{
if (trial % 10000 == 0)
printf("done %d\n", trial);
func();
}
}
I am compiling with
gcc -o bug -fopenmp -std=c99 bug.c
When I run this on my system (Mac OS 10.6.8, intel core 2 duo, apple GCC 4.2.1), most of the time it runs for a while (perhaps a million iterations, maybe 15-30 seconds), and then freezes. A debugging session with gdb suggests that it is getting stuck on waiting for some synchronisation object at the #pragma omp parallel directive, i.e. it never gets to the #pragma omp for, but I'm not 100% sure of this, because when I insert printf's in various places the behaviour sometimes goes away. The freeze happens more reliably when the system is under moderate to high CPU load (e.g. constantly tabbing through windows or refreshing a web browser). Using top, I see that under normal conditions it consumes about 125% CPU, but after the freeze it drops to about 80% (even when nothing else is running).
I am an OpenMP newbie, so I would like to know whether my code is correct, or have I made a programming error. Other possibilities include compiler or OS bugs. I cannot reproduce this behaviour on a few other systems. It is very suspicious that it happens more often when the system is under high load.
Thanks for your help
david
