Glorius
Glorius

Reputation: 11

C - parallelize recurrence omp

I have a problem: I have to parallelize this piece of code with OMP.

There is a problem of data dependencies and I don't know how to solve it. Any suggestions?

for (n = 2; n < N+1; n++) {
  dz = *(dynamic_d + n-1)*z;
  *(dynamic_A + n) = *(dynamic_A + n-1) + dz * (*(dynamic_A + n-2));
  *(dynamic_B + n) = *(dynamic_B + n-1) + dz * (*(dynamic_B + n-2));
}

Upvotes: 0

Views: 124

Answers (1)

Zulan
Zulan

Reputation: 22670

You cannot parallelize the loop iterations due to the depdency, but you can split the computation of dynamic_A vs dynamic_B using sections:

#pragma omp parallel sections
{
    #pragma omp section
    {
        // NOTE: Declare n and dz locally so that it is private!
        for (int n = 2; n < N+1; n++) {
            my_type dz = dynamic_d[n-1] * z;
            dynamic_A[n] = dynamic_A[n-1] + dz * dynamic_A[n-2];
        }
    }
    #pragma omp section
    {
        for (int n = 2; n < N+1; n++) {
            my_type dz = dynamic_d[n-1] * z;
            dynamic_B[n] = dynamic_B[n-1] + dz * dynamic_B[n-2];
        }
    }
}

Please use array indexing instead of the unholy pointer arithmetic referencing abnormity.

Upvotes: 1

Related Questions