A reverse! for CuArrays and Nd arrays

Dear All,

I have a 3d array V = zeros(N,N,N) and I would like to reverse it with respect to the last component. I achieve this by using the following function. This works well using CuArray (on master, see link) for A but this is very inefficient as N~100. Hence the gpu is wasted doing such a small reverse.

I am wondering if any of you has an idea to speed this up on GPU?

Thank you very much

Best regards

function myreverse!(A) 
	n = size(A)
	for jj = 1:n[2], ii = 1:n[1] 
		reverse!(A[ii,jj,:])
	end
end

Are you sure this works? I think you may need view(A, ii,jj,:) in there.

I see that my wish for a 1D reverse was granted, but really one should write version which takes dims=3 too I guess.

Yes with CuArrays#master

In search for a quick fix waiting for the correct implementation, I tried

A0 = rand(10,10,10)
A = copy(A0)
myreverse!(A)
A2 = A[:,:,10:-1:1]
norm(A-A2)

but to my surprise, it does not give the same result…

It remains my opinion that myreverse! does nothing, because A[...] makes a copy, without touching A.

But indexing with a range does seem to work on CuArrays, so this is a plausible way to go. I haven’t timed it to see if it’s quick:

A = collect(reshape(1:27, 3,3,3))
A0 = copy(A)

myreverse!(A)
A
A == A0

A[:,:,3:-1:1]
view(A, :,:,3:-1:1) # or view(A, :, :, reverse(axes(A,3)) )

using CuArrays
cA = cu(A)

CuArrays.allowscalar(false)
cA[:,:,3:-1:1]
view(cA, :,:,3:-1:1)
cA .= view(cA, :,:,3:-1:1)

You were right… Thank you

function myreverse!(A) 
               n = size(A)
               for jj = 1:n[2], ii = 1:n[1] 
                       reverse!(view(A,ii,jj,:))
               end
       end

For those who are interested, I use the following which does the trick

function myreverse!(A::AbstractArray)
		n = size(A)
		A .= A[:,:,n[3]:-1:1]
	end