简体   繁体   中英

openmp on for loop

I have the following code:

for(i=0; i<num1; i++)
{
 j=i%num2;
 a[j]=do_compute(j);
}

Let's assume num2 << num1. If I wanted to use omp for, is there any need to use j as private? ie as follows:

#pragma omp parallel private(i,j)
{
 #pragma omp for
 for(i=0; i<num1; i++)
 {
  j=i%num2;
  a[j]=do_compute(j);
 }
}

or would this suffice:

#pragma omp parallel
{
 #pragma omp for
 for(i=0; i<num1; i++)
 {
  j=i%num2;
  a[j]=do_compute(j);
 }
}

Thanks.

If j is only used in the scope of the loop, I'd do the following:

[...]
unsigned int i;
#pragma omp parallel for
for(i = 0; i < num1; ++i)
{
    unsigned int j = i % num2;
    a[j] = do_compute(j);
}
[...]

not trying to be picky here, but given that do_compute is deterministic and has no side-effects, you'd be better off with just using

[...]
unsigned int i;
#pragma omp parallel for
for(i = 0; i < num2; ++i)
    a[i] = do_compute(i);
[...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM