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.

Guille
  • 31
  • 1
  • 3
  • Array reduction is in current standard but not supported by all implementations. Anyway it tends to exhibit limited scaling – tim18 Apr 16 '17 at 18:24

1 Answers1

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.

Community
  • 1
  • 1
Zulan
  • 21,896
  • 6
  • 49
  • 109
  • 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 – Guille Apr 16 '17 at 23:42
  • 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. – Zulan Apr 16 '17 at 23:44
  • 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? – Guille Apr 16 '17 at 23:54