2

Learning openMP

    // array b

    #pragma omp parallel for // reduction(&&: b[i])?
    for (i=2; i<=N; i++)
    {
      // create local array for each thread
      int *localb;
      localb = (int*) malloc(N*sizeof(int));
      memcpy(localb, b, N*sizeof(localb));

        #pragma omp for private(j)
        for (j=i+1; j<=N; j++)
        {
            if (j%i == 0)
                localb[j] = 0;
        }

Is it possible to reduce each element in global array b using reduction(&&: b[i]) so that b[i] = localb[j] && b[i]? All data is either 0 or 1; 0 if j is divisible by i, and 1 otherwise.

1
  • Array reduction is in current standard but not supported by all implementations. Anyway it tends to exhibit limited scaling Commented Apr 16, 2017 at 18:24

1 Answer 1

2

Yes, reduction on arrays is directly supported by the standard since OpenMP 4.5, just via reduction(&&: b).

If you implementation does not yet support that, refer to the older answers about that.

Sign up to request clarification or add additional context in comments.

3 Comments

And to make sure, can the reduction take place inside the inner loop, or must I create another inner loop exclusively for reducing each element of b
No, reduction always takes place at the end of a parallel region. Note that you do not need to declare a localb, just use b. The rest is done by OpenMP transparently.
Wouldn't there be a race condition since b is a shared resource? And to clarify again (sorry), I need to add b[i] = b[i] && b[i] after the inner loop for the reduction to take place?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.