 # Updating an array

Hi,

I have a problem with an array inside a double loop. I want to update this array but i don’t know if it is possible to do this in parallel.

``````!\$acc kernels reduction(+:k)
do j=1,N
do i= 1,M

!In the beginning SOME INDEPENDENT CALCULATIONS

!and now the part with the reduction variable and the array
k = k +1
array(1,k) = i
array(2,k) = j

enddo
enddo
``````

After the 2D loop my array should look like
k = 4
array(1,1) = 2 | array(1,2) = 3 | array(1,3) = 5 | array(1,4) = 5
array(2,1) = 3 | array(2,2) = 4 | array(2,3) = 160 | array(2,4) = 300

The problem is that with openACC my results are like
k=4 (CORRECT RESULT)
array(1,1) = 5 | array(1,2) = 0 | array(1,3) = 0 | array(1,4) = 0
array(2,1) = 160 | array(2,2) = 0 | array(2,3) = 0 | array(2,4) = 0

I’m familiar with the reason of this behaviour but i want to know if there is a way to solve this.

In CUDA i solved this problem using ATOMIC operations.

Thank you,
Sotiris

Hi, Sotiris!

Your problem is in “k” variable.
Try to substitute “k = k+1” with “k = j*M + i” and remove reduction clause.

Alexey

I did what you told me and now i have an array full of zeros and the pairs that i want in the positions k=j*M+i.

array(1,k) = SOMETHING
array(2,k) = SOMETHING ELSE
and in any other position array(1,l) = 0 and array(2,l) = 0

But now i want this pairs in the beginning of this array. For example if the total number of pairs is 5, i want the first 5 positions of the array to be filled with this pairs and all the other positions to be zeros.

Thanks
Sotiris

Hi Sotiris,

In CUDA i solved this problem using ATOMIC operations.

You might want to revisit this since it will cause your code to slow down as the threads block on updating “k”. Especially given, as Alexy points out, you can derive “k” from the index variables and loop bounds.

But now i want this pairs in the beginning of this array. For example if the total number of pairs is 5, i want the first 5 positions of the array to be filled with this pairs and all the other positions to be zeros.

Sorry, I’m not entirely sure what you mean, but it seems you might just need an if statement.

• Mat

Sorry, I’m not entirely sure what you mean, but it seems you might just need an if statement.

Ok, let me be more specific. I have a double loop and inside this loop i have an IF statement.Also i have a counter K which tells me how many times my program gets inside this IF statement.

``````!\$acc kernels
do j=1,L
array(j) = 0
enddo

!\$acc kernels reduction(+:k)
do j=1,N
do i= 1,M

!In the beginning SOME INDEPENDENT CALCULATIONS

If (condition) then
k = k +1
array(k) = i+j
end if

enddo
enddo
``````

The dimendsion of the loop is M=1000 and N=1000. But after the execution my counter k is around to 10(so i get inside if statement 10 times). So i want only the first K positions of my array to be filled. For example if the dimension of ARRAY is L=1000, after the loop i want something like that:
K = 10
ARRAY(1) = value1, ARRAY(2) = value2,…,ARRAY(10) = value10, ARRAY(11)=0,…,ARRAY(1000) = 0

I used Alexey’s way (k = jM + i) and after the execution i had the correct results (value1,value2,…,value10) but i had them in the wrong positions of my ARRAY(for those i’s and j’s in k=jM+i). So i want the correct results in the first K positions of the ARRAY, and i want this in parallel execution.

Thank you,
Sotiris

Hi Sotiris,

i have a counter K which tells me how many times my program gets inside this IF statement.

Unfortunately, this isn’t parallel since you now have dependency on “k”.

What I would recommend is to create a temp array to hold the found values, then perform the reduction into the array sequentially. How beneficial this is will depend upon how computationally intensive the “SOME INDEPENDENT CALCULATIONS” are.

Something like:

``````!\$acc data region create(value,found), copyout (array)
!\$acc kernels
do j=1,L
array(j) = 0
enddo

!\$acc kernels
do j=1,N
do i= 1,M

!In the beginning SOME INDEPENDENT CALCULATIONS

found(j,i) = 0
If (condition) then
found(j,i) = 1
value(j,i) = <value>
end if
enddo
enddo

!\$acc parallel
!\$acc loop seq
do j=1,N
do i= 1,M
if (found(j,i) .eq 1) then
k = k+1
array(k) = value(j,i)
endif
enddo
enddo

!\$acc end data region
``````

Hope this helps,
Mat

Thank you Mat,

This is exactly what i did as a solution. I can’t avoid the sequantial loop. Thabk you very much for your help.

Sotiris