Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top