# Why threadIdx().y shows some random value?

**URL:** <https://discourse.julialang.org/t/why-threadidx-y-shows-some-random-value/49659>\
**Category:** New to Julia\
**Tags:** gpu, cuda\
**Created:** [November 6, 2020, 1:33am UTC](https://discourse.julialang.org/t/why-threadidx-y-shows-some-random-value/49659 "2020-11-06T01:33:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [November 6, 2020, 1:33am UTC](https://discourse.julialang.org/t/why-threadidx-y-shows-some-random-value/49659/1 "2020-11-06T01:33:39Z")

</div>

Hi,

I am just new to CUDA and trying to learn. I tried to print thread id’s in x and y direction using the following code:

```julia
using CUDA
function test_kernel()
	@cuprintln("idx: %d, idy: %d",threadIdx().x, threadIdx().y)
	return nothing
end
@cuda threads=(4,4) blocks=1 test_kernel()

```

I am getting like following:

```julia
idx: 1, idy: 0117592186044417
idx: 2, idy: 0117592186044417
idx: 3, idy: 0117592186044417
idx: 4, idy: 0117592186044417
idx: 1, idy: 0217592186044418
idx: 2, idy: 0217592186044418
idx: 3, idy: 0217592186044418
idx: 4, idy: 0217592186044418
idx: 1, idy: 0317592186044419
idx: 2, idy: 0317592186044419
idx: 3, idy: 0317592186044419
idx: 4, idy: 0317592186044419
idx: 1, idy: 0417592186044420
idx: 2, idy: 0417592186044420
idx: 3, idy: 0417592186044420
idx: 4, idy: 0417592186044420

```

I got some weird value for thread id in y direction. I tried same with normal CUDA programming in visual studio, like below:

```julia
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>

__global__ void test_kernel() {
	printf("Idx: %d Idy: %d\n", threadIdx.x, threadIdx.y);
}

int main() {
	dim3 block(4,4);
	dim3 grid(1);
	test_kernel<< < grid, block >> > ();
	cudaDeviceSynchronize();
	cudaDeviceReset();
	return 0;
}

```

and I got like below:

```julia
Idx: 0 Idy: 0
Idx: 1 Idy: 0
Idx: 2 Idy: 0
Idx: 3 Idy: 0
Idx: 0 Idy: 1
Idx: 1 Idy: 1
Idx: 2 Idy: 1
Idx: 3 Idy: 1
Idx: 0 Idy: 2
Idx: 1 Idy: 2
Idx: 2 Idy: 2
Idx: 3 Idy: 2
Idx: 0 Idy: 3
Idx: 1 Idy: 3
Idx: 2 Idy: 3
Idx: 3 Idy: 3

```

Here I am getting the proper results.

What I did wrong?

Can anyone help me to solve this?

Thanks and Regards,  
Manu

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [November 6, 2020, 1:35am UTC](https://discourse.julialang.org/t/why-threadidx-y-shows-some-random-value/49659/2 "2020-11-06T01:35:04Z")

</div>

_Random_ is not precice, it’s just that it gets to run in a _indeterminate_ order. Actually, some of it is run in parallel and prints in an _indeterminate_ order

---

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [November 6, 2020, 1:37am UTC](https://discourse.julialang.org/t/why-threadidx-y-shows-some-random-value/49659/3 "2020-11-06T01:37:40Z")

</div>

> [@xiaodai](#):
>
> _Random_ is not precice

Hi @xiaodai: Thanks for your reply. How can I get the proper value for `threadIdx().y` ?

---

<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:** [November 6, 2020, 6:59am UTC](https://discourse.julialang.org/t/why-threadidx-y-shows-some-random-value/49659/4 "2020-11-06T06:59:21Z")

</div>

The problem is your print, `threadIdx().x` returns an Int, while `%d` is for `Int32`. Just use `@cuprintln` and interpolate, instead of using a formatting string.

---

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [November 6, 2020, 10:23am UTC](https://discourse.julialang.org/t/why-threadidx-y-shows-some-random-value/49659/5 "2020-11-06T10:23:30Z")

</div>

@maleadt: Thanks for your reply. It is working 🙂
