# Compute only specific cells in 3D Matrix

Hey everybody,

I have a 3D Matrix and a array with 3D coordinates. My target is to compute die differences with the neighbour cells. Yes, its called finite difference. So, I know how I can compute a range in this 3D Matrix with the GPU, but no how only the specific cells in this 3D Matrix correspending to my coordinates array?

For example:

``````
e = CUDA.zeros(Float32,nx,ny,nz);
d = CUDA.rand(Float32,nx,ny,nz);

function diff(e,d)

ix = (blockIdx().x-1) * blockDim().x + threadIdx().x
iy = (blockIdx().y-1) * blockDim().y + threadIdx().y
iz = (blockIdx().z-1) * blockDim().z + threadIdx().z

if ix<size(e,1) && ......

e[ix,iy,iz] = d[ix,iy,iz+1] - d[ix,iy,iz-1]+....

# with cpu I can write e[linIndex0] = d[linIndex1] - d[linIndex2]+....

end

end
``````

I hope my question is clearly. I am newcomer in gpu topics.

Thanks for every help!

I think It quite depends on the condition of your “specific cell”
As you write the kernel function, the gpu will loop the whole array once. If you just want some element inside the array being updated, another if loop will serve the suppose.
For example

``````function diff(e,d)

ix = (blockIdx().x-1) * blockDim().x + threadIdx().x
iy = (blockIdx().y-1) * blockDim().y + threadIdx().y
iz = (blockIdx().z-1) * blockDim().z + threadIdx().z

if ix<size(e,1) && iy < size(e,2) && iz < size(e,3) #check inbound

de =  d[ix,iy,iz+1] - d[ix,iy,iz-1]
if de > 1 # check condition
e[ix,iy,iz]  = de
else
nothing
end
nothing

end

``````

Hi,

it is unclear what exactly you want to achieve.

1. If you want your kernel to compute a single cell of your 3D array, you could either launch the kernel using a single thread, or make the appropriate selection in the if condition within your kernel, selecting a single index instead of a range.
2. If you rather want to access your 3D array using a global index, you could compute that global index out of `ix,iy,iz` within the kernel and use it.

If you meant something else, best would be to post a 1D or 2D MWE on what you want to achieve (e.g., on the CPU).