# CUDAnative: kernel multidimensional access

**URL:** <https://discourse.julialang.org/t/cudanative-kernel-multidimensional-access/1864>\
**Category:** GPU\
**Tags:** cudanative\
**Created:** [February 3, 2017, 10:29am UTC](https://discourse.julialang.org/t/cudanative-kernel-multidimensional-access/1864 "2017-02-03T10:29:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin\_Beckingham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colin_beckingham/32/4878_2.png) [@Colin\_Beckingham](https://discourse.julialang.org/u/Colin_Beckingham)\
**Post date:** [February 3, 2017, 10:29am UTC](https://discourse.julialang.org/t/cudanative-kernel-multidimensional-access/1864/1 "2017-02-03T10:29:02Z")

</div>

I have a toy example where the goal is to process a 3x3 matrix of integers on the GPU and do an element wise doubling of the numbers. I have no problem doing this allowing CUDAnative to linearize the array to a vector, but attempting to process the array as a 3x3 on the GPU is puzzling. Here is my toy example which produces the right answer but for the wrong reason.

```julia
using CUDAdrv, CUDAnative

function kernel_mmul(a, c)
    i = (blockIdx().x-1) * blockDim().x + threadIdx().x
    j = (blockIdx().y-1) * blockDim().y + threadIdx().y
    c[i,j] = a[i,j].*2
    @cuprintf(" %d %d %d %d\n",i,j,c[i,j],threadIdx().y)
    return nothing
end

dev = CuDevice(0)
ctx = CuContext(dev)
a = Int32[1 2 3; 2 3 1; 3 1 2]
d_a = CuArray(a)
d_c = similar(d_a) 
@cuda ((1,1),(3,3)) kernel_mmul(d_a, d_c)
c = Array(d_c)
println(a)
println(c)
destroy(ctx)

```

For some reason the index j in the kernel is always zero. So I guess multiple blocks of i are processed to get the answer. The count of iterations is correct each time and the result is correct as long as the process does not end in error due to poor choice of grid and block combinations. Of note is that blockDim seems to be zero which is counter intuitive.

---

<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:** [February 3, 2017, 4:37pm UTC](https://discourse.julialang.org/t/cudanative-kernel-multidimensional-access/1864/2 "2017-02-03T16:37:39Z")

</div>

Your index calculation is correct, but by doing `-1` it gets promoted to `Int64`, which means your format specifier is wrong. Either use `%ld`, or do `-Int32(1)`.

Relevant issue: [https://github.com/JuliaGPU/CUDAnative.jl/issues/25](https://github.com/JuliaGPU/CUDAnative.jl/issues/25)

---

<div class="post-metadata">

**Author:** ![Colin\_Beckingham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colin_beckingham/32/4878_2.png) [@Colin\_Beckingham](https://discourse.julialang.org/u/Colin_Beckingham)\
**Post date:** [February 3, 2017, 5:47pm UTC](https://discourse.julialang.org/t/cudanative-kernel-multidimensional-access/1864/3 "2017-02-03T17:47:32Z")

</div>

Wow! You are right. The format string " %d %ld %d %d\n" works. So it might be informative that the variable “i” seems to be ambivalent about Int32 or Int64, but the variable “j” is very sensitive to that setting, even using the exact same value/type for the -1 component.

---

<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:** [February 3, 2017, 9:56pm UTC](https://discourse.julialang.org/t/cudanative-kernel-multidimensional-access/1864/4 "2017-02-03T21:56:24Z")

</div>

> [@Colin\_Beckingham](#):
>
> So it might be informative that the variable “i” seems to be ambivalent about Int32 or Int64, but the variable “j” is very sensitive to that setting, even using the exact same value/type for the -1 component.

Huh, curious. You should be using `%ld` for both though. Or even better, keep `i` and `j` 32-bits (although it doesn’t matter much in this case).
