# Trying to understand 3d indexing

**URL:** <https://discourse.julialang.org/t/trying-to-understand-3d-indexing/67525>\
**Category:** GPU\
**Created:** [September 1, 2021, 6:56pm UTC](https://discourse.julialang.org/t/trying-to-understand-3d-indexing/67525 "2021-09-01T18:56:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakub\_Mitura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_mitura/32/19496_2.png) [@Jakub\_Mitura](https://discourse.julialang.org/u/Jakub_Mitura)\
**Post date:** [September 1, 2021, 6:56pm UTC](https://discourse.julialang.org/t/trying-to-understand-3d-indexing/67525/1 "2021-09-01T18:56:38Z")

</div>

I prepared kernel with thread dimensions 4, 4,1 and iterating over array of dimensionality 16,16,2 and 32 blocks as far as my calculations all should work - all divides without remainder but I get constantly ERROR: Out-of-bounds array access

```julia

using CUDA

goldBoolGPU= CuArray(falses(16,16,2));

segmBoolGPU= CuArray(falses(16,16,2));

fn = CuArray([0])

function kernelFunct(goldBoolGPU::CuDeviceArray{Bool, 3, 1}, segmBoolGPU::CuDeviceArray{Bool, 3, 1},fn)

    i= (blockIdx().x) * blockDim().x + threadIdx().x

    j = (blockIdx().y) * blockDim().y + threadIdx().y

    z = (blockIdx().z) * blockDim().z + threadIdx().z 

    if (goldBoolGPU[i,j,z] & !segmBoolGPU[i,j,z] )

        @atomic fn[]+=1    

        end

    return  

    end

@cuda threads=(4, 4,1) blocks=32 kernelFunct(goldBoolGPU,segmBoolGPU,fn)

```

error

> ERROR: Out-of-bounds array access.

Thanks For Help

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [September 6, 2021, 6:23am UTC](https://discourse.julialang.org/t/trying-to-understand-3d-indexing/67525/2 "2021-09-06T06:23:59Z")

</div>

How did you try debugging this? If I just add a single print statement (`@cuprintln "goldBoolGPU[$i,$j,$z]"`) it’s obvious that you’re just actually indexing your arrays out-of-bounds. You probably want to do `(blockIdx().z - 1) * blockDim().z` since those values are 1-inded. Generally you also may not be able to perfectly divide your iteration space across blocks, so you’d want a branch that checks if the indices are in bounds.

---

<div class="post-metadata">

**Author:** ![Jakub\_Mitura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_mitura/32/19496_2.png) [@Jakub\_Mitura](https://discourse.julialang.org/u/Jakub_Mitura)\
**Post date:** [September 6, 2021, 9:48am UTC](https://discourse.julialang.org/t/trying-to-understand-3d-indexing/67525/3 "2021-09-06T09:48:16Z")

</div>

Thank You @maleadt for response , and your time for again responding for my question .

I know that I am getting out of bounds, as stated in question , but I just do not understand why, and as far as I was debugging it I got consistently out of bounds for index I but not for j and z.

And i will try to include this minus 1 to indexing as you suggested Thanks !

I did not included bounds checking , as this code will work on medical images where slices are squares with edge of length 256,512 or 1024 so in theory when I iterate with 16 by 16 square over square where edge length is divisible by 16 it should be possible to go through all entries without any remainder if I understand it correctly

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [September 6, 2021, 9:51am UTC](https://discourse.julialang.org/t/trying-to-understand-3d-indexing/67525/4 "2021-09-06T09:51:53Z")

</div>

> [@Jakub\_Mitura](#):
>
> I just do not understand why

You’re creating a 16x16x2 array, but launching 32 blocks of each 4x4x1 threads, while (incorrectly) computing indices that go up to 132 (4x32+4). For `y` and `z` the block dimension is 1, so it still goes out of bounds but only by 1.

I recommend adding some print statements to understand how these values are affected by the launch configuration.
