# Compute only specific cells in 3D Matrix

**URL:** <https://discourse.julialang.org/t/compute-only-specific-cells-in-3d-matrix/92476>\
**Category:** GPU\
**Tags:** parallel, indexing, cuda\
**Created:** [January 4, 2023, 12:50am UTC](https://discourse.julialang.org/t/compute-only-specific-cells-in-3d-matrix/92476 "2023-01-04T00:50:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bozaga\_Murat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bozaga_murat/32/34441_2.png) [@Bozaga\_Murat](https://discourse.julialang.org/u/Bozaga_Murat)\
**Post date:** [January 4, 2023, 12:50am UTC](https://discourse.julialang.org/t/compute-only-specific-cells-in-3d-matrix/92476/1 "2023-01-04T00:50:19Z")

</div>

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:

```julia

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!

---

<div class="post-metadata">

**Author:** ![dora\_ho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dora_ho/32/42582_2.png) [@dora\_ho](https://discourse.julialang.org/u/dora_ho)\
**Post date:** [January 5, 2023, 4:05am UTC](https://discourse.julialang.org/t/compute-only-specific-cells-in-3d-matrix/92476/2 "2023-01-05T04:05:47Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![luraess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luraess/32/16189_2.png) [@luraess](https://discourse.julialang.org/u/luraess)\
**Post date:** [January 5, 2023, 2:22pm UTC](https://discourse.julialang.org/t/compute-only-specific-cells-in-3d-matrix/92476/3 "2023-01-05T14:22:07Z")

</div>

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).
