OpenMP - Easy Loop But Still Endless?

I have a very strange problem using OpenMP in my C ++ code:

void update(double *source, double *target, int n)
{
    target[0] = source[0];
    target[n-1] = source[n-1];
    #pragma omp parallel for 
    for(int i = 1; i < n-1; ++i) 
        target[i] = (1.0/3.0) * (source[i-1] + source[i] + source[i+1]);
}

Both sources and targets are double arrays with n elements. The code works fine when used without OpenMP. But as soon as I use pragma, the code seems to get stuck in this loop. The fact is that I absolutely DO NOT IDEA why. Hope someone can help me.

+5
source share
1 answer

How big is n?

OpenMP parallel for . GOMP ( OpenMP, gcc), (dynamic,1) . , ( i-1 i+1) , , . . , :

#pragma omp parallel for schedule(dynamic,1024)

1024 . , ( , , "" ). , , , L1 L2 .

, for , .

#pragma omp parallel for schedule(static)

, .

, OpenMP . , GOMP_CPU_AFFINITY.

Edit:

, gcc 4.2.1, , . , GOMP schedule(static).

#include <stdio.h>
#include <omp.h>

int main(int argc, char** argv)
{
    int i;
    #pragma omp parallel for
    for (i=0; i<15; i++) {
        int id = omp_get_thread_num();
        printf("%d assigned to thread %d\n", i, id);
    }
}

:

$ ./test_sched | sort -n
0 assigned to thread 0
1 assigned to thread 0
2 assigned to thread 0
3 assigned to thread 0
4 assigned to thread 0
5 assigned to thread 0
6 assigned to thread 0
7 assigned to thread 0
8 assigned to thread 1
9 assigned to thread 1
10 assigned to thread 1
11 assigned to thread 1
12 assigned to thread 1
13 assigned to thread 1
14 assigned to thread 1
+2

All Articles