문제

This is testing part of the code:

    float a = 0;
    float b = 0;
    int c = 0;
    int d = 0;
    #pragma omp parallel for schedule (dynamic, 1)  reduction(+ : a, b, c, d)
    for(i=0; i<100; i++) {
        a +=1;
        b +=1;
        c +=1;
        d +=1;
    }

    printf("a: %d, b: %d, c: %d, d: %d\n", a, b, c, d);

For some reasons my results are always:

a: 100, b: 100, c: 0, d: 202
a: 100, b: 100, c: 0, d: 202
a: 100, b: 100, c: 0, d: 202
a: 100, b: 100, c: 0, d: 202
a: 100, b: 100, c: 0, d: 202
a: 100, b: 100, c: 0, d: 202
a: 100, b: 100, c: 0, d: 202

Why aren't a, b, c, d all equal to 100?

도움이 되었습니까?

해결책

You are using %d formats to print floating point numbers. That causes undefined behaviour. Use:

   printf("a: %f, b: %f, c: %d, d: %d\n", a, b, c, d);

And you'll see you get the right answers.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top