Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested loop OpenMP Parallellizing, private or public index?

Imagine you have a nested loop for in a parallel region, something like:

#pragma omp parallel
{
     for (int i = 0, ...) {
          for (int j = 0, ...) { }}}

or

#pragma omp parallel
{
     for (int i = 0, ...) {
          for (int j = i, ...) { }}}

If we use #pragma omp for, automatically the i index becomes private. But... do we need to set the j index to private or public? What is the effect?

#pragma omp parallel
{
     #pragma omp for shared(j)
     for (int i = 0, ...) {
           for (int j = 0, ...) { }}}

or

#pragma omp parallel
{
     #pragma omp for private(j)
     for (int i = 0, ...) {
          for (int j = 0, ...) { }}}

Thanks in advance!

like image 338
alienflow Avatar asked May 05 '26 21:05

alienflow


1 Answers

Everything declarded inside the parallel region is automatically private. That is (presumably) the behavior you want: Each iteration of i should loop through all j, the j loops are all independent (thus private, not public). However, you are actually lacking the important parallel part: If you don't write

#pragma omp parallel for

but only

#pragma omp for

then you won't get anything happening in parallel (unless you first created a parallel region with #pragma omp parallel in an enclosing scope)!

like image 81
Max Langhof Avatar answered May 08 '26 13:05

Max Langhof