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!
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)!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With