Matjaž
Matjaž

Reputation: 2115

OpenMP parallel for not working as expected

I have simple program:

int index;
unsigned int count = 0;

#pragma omp parallel for
for (index = 0; index < 10000; index++)
{
    count++;
}

Problem is that I think count should be equal to 10000. But instead it is usually somewhere around 5000. In about 10% it is equal to 10000.

What is going on here?

Upvotes: 0

Views: 566

Answers (1)

petrmikheev
petrmikheev

Reputation: 342

As suggested in comments, you need reduction directive:

int index;
unsigned int count = 0;

#pragma omp parallel for reduction(+:count)
for (index = 0; index < 10000; index++)
{
    count++;
}

Upvotes: 5

Related Questions