Chun-Ye Lu
Chun-Ye Lu

Reputation: 385

OpenMP - expect data race situation but did not actually occur

I write the following code and I expect that the data race would occur because of several thread may modify a at the same time and get a wrong answer.

// test.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int a = 0;

    #pragma omp paralle for
    for (int i = 0; i < 10000000; i++) {
        a = a + 1;
    }

    printf("a = %d\n", a);    // correct answer = 10000000

    return 0;
}

But the output seems correct:

$ gcc test.c -std=c99 -fopenmp
$ ./a.out
a = 10000000

I have executed it for several times and it always gave me correct answer.
Why there is no data race occur? Is it just a coincide?
(I know that I should use reduction but I just wonder why it still work well without reduction.)

Upvotes: 0

Views: 43

Answers (1)

Daniel Kleinstein
Daniel Kleinstein

Reputation: 5502

You've written #pragma omp paralle for when you meant #pragma omp parallel for (notice parallel instead of paralle). If you fix this then you'll see your data race.

Upvotes: 2

Related Questions