tiger
tiger

Reputation: 45

OpenMP for loop when using a different index var to index the array?

I'm trying parallelize a for loop but I am not sure how because this loop is using a variable k that is not i (i is incremented by two because I am dealing with two parts of array a at a time) for the index of arrays b and c.

for (int i = 0; i < asize; i +=2)
{
    b[k] = a[i];
    c[k] = a[i + 1];

    k++;
}

#pragma omp parallel for causes a data race with k and produces wrong results

And #pragma omp parallel for private(k) cause each thread to increment k on its own I.E. multiple threads writing to b[0] and c[0] at the same time also producing wrong results.

Upvotes: 0

Views: 800

Answers (1)

Brice
Brice

Reputation: 1580

Something like that?

for (int k = 0; 2*k < asize; k++) // i = 2*k
{
    b[k] = a[2*k];
    c[k] = a[2*k + 1];
}

Upvotes: 2

Related Questions