Why threadIdx().y shows some random value?

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:

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:

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:

#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:

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

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

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

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.

1 Like

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