# Array assignment in OpenACC using dummy indices

**URL:** <https://forums.developer.nvidia.com/t/array-assignment-in-openacc-using-dummy-indices/134596>\
**Category:** Legacy PGI Compilers (archived)\
**Created:** [August 20, 2015, 8:44am UTC](https://forums.developer.nvidia.com/t/array-assignment-in-openacc-using-dummy-indices/134596 "2015-08-20T08:44:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nrlugg](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@nrlugg](https://forums.developer.nvidia.com/u/nrlugg)\
**Post date:** [August 20, 2015, 8:44am UTC](https://forums.developer.nvidia.com/t/array-assignment-in-openacc-using-dummy-indices/134596/1 "2015-08-20T08:44:10Z")

</div>

Hi All,

I am trying to implement OpenACC in Fortran loops where arrays are assigned using dummy variables that are not the loop indices. For example:

```nohighlight
subroutine test_sub
implicit none
integer :: ix,iy,jx,jy,n
real, allocatable :: X(:,:)

n = 1000
allocate(X(n,n))

!$acc kernels loop copy(X)
do ix = -n/2,n/2
    !$acc loop private(jx,jy)
    do iy = -n/2,n/2
        jx = modulo(ix,n)+1
        jy = modulo(iy,n)+1

        X(jy,jx) = jx **2 + jy** 2
    end do
end do

end subroutine

```

However, the -Minfo=accel diagnostic output tells me that this cannot be parallelized (without privatization of X, which is impossible because it is too large):

Generating copy(x(:,:))  
Parallelization would require privatization of array x(:,:)  
Accelerator scalar kernel generated  
Parallelization would require privatization of array x(:,:)

Is there any way to do this?

---

<div class="post-metadata">

**Author:** ![MatColgrove](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/matcolgrove/32/14047_2.png) [@MatColgrove](https://forums.developer.nvidia.com/u/MatColgrove)\
**Post date:** [August 20, 2015, 4:41pm UTC](https://forums.developer.nvidia.com/t/array-assignment-in-openacc-using-dummy-indices/134596/2 "2015-08-20T16:41:43Z")

</div>

Hi nrlugg,

> Is there any way to do this?

Add the “independent” clause or use the “parallel” compute construct instead of “kernels”.

With “kernels”, the compiler performs analysis to determine if the loop is parallelizable. Here since your indices are computed, the compiler can’t tell if all values of “jx” and “jy” are discrete. Hence it must assume the worst case where all values of “jx” and “jy” are the same and it can’t parallelize the loop. Adding “independent” asserts to the compiler that the loop is parallelizable.

With “parallel”, you’re telling the compiler which loops to parallelize so no dependency analysis is performed.

```plaintext
% cat test.f90
subroutine test_sub
implicit none
integer :: ix,iy,jx,jy,n
real, allocatable :: X(:,:)

n = 1000
allocate(X(n,n))

!$acc kernels loop independent copy(X)
do ix = -n/2,n/2
    !$acc loop independent private(jx,jy)
    do iy = -n/2,n/2
        jx = modulo(ix,n)+1
        jy = modulo(iy,n)+1

        X(jy,jx) = jx **2 + jy** 2
    end do
end do

end subroutine
% pgfortran -acc -Minfo=accel -c test.f90
test_sub:
      9, Generating copy(x(:,:))
     10, Loop is parallelizable
     12, Loop is parallelizable
         Accelerator kernel generated
         Generating Tesla code
         10, !$acc loop gang ! blockidx%y
         12, !$acc loop gang, vector(128) ! blockidx%x threadidx%x
             Interchanging generated vector loop outwards

```

Hope this helps,  
Mat

---

<div class="post-metadata">

**Author:** ![nrlugg](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@nrlugg](https://forums.developer.nvidia.com/u/nrlugg)\
**Post date:** [August 21, 2015, 2:03am UTC](https://forums.developer.nvidia.com/t/array-assignment-in-openacc-using-dummy-indices/134596/3 "2015-08-21T02:03:50Z")

</div>

Hi Mat,

Both the “independent” clause and “parallel” compute construct work perfectly! Thanks for your answer and explanation :)

As a side, I initially tried the parallel construct but it didn’t seem to parallalize. At that time I just had a single “parallel loop” directive around the outer loop. But trying it again with the “loop” directive on the inner loop (i.e., simply replacing “kernels” with “parallel” in my test\_sub) works, as you suggested.

Thanks again,  
Nath
