# 3D Thread ID

**URL:** <https://forums.developer.nvidia.com/t/3d-thread-id/3901>\
**Category:** CUDA Programming and Performance\
**Created:** [May 30, 2008, 9:22pm UTC](https://forums.developer.nvidia.com/t/3d-thread-id/3901 "2008-05-30T21:22:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xinchen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@xinchen](https://forums.developer.nvidia.com/u/xinchen)\
**Post date:** [May 30, 2008, 9:22pm UTC](https://forums.developer.nvidia.com/t/3d-thread-id/3901/1 "2008-05-30T21:22:17Z")

</div>

Codes as follows, THETA is a const, I can define outImage as 1 in GPU function. The expected output should be 1 but they are NaN.

I think that there is an error in 3D thread ID computation. But I don’t know where? So could you tell me?  
dim3 dimBlock(BLOCK\_SIZE, BLOCK\_SIZE, BLOCK\_SIZE);  
dim3 dimGrid(iRotated / dimBlock.x , iRotated / dimBlock.y, THETA/dimBlock.z);

```
rotationD<<<dimGrid, dimBlock>>> (imageRotatedDevice, iRotated, iRotated);

```

**global** void roatationD(float\* outImage, int widthR, int heightR)  
{

```
int ix = blockIdx.x * BLOCK_SIZE + threadIdx.x;
int iy = blockIdx.y * BLOCK_SIZE + threadIdx.y; 
 int iz = blockIdx.z * BLOCK_SIZE + threadIdx.z; 

            int idx = ix + iy * widthR + iz * widthR * heightR; // index

             outImage[idx] = 1.0f;

```

}

---

<div class="post-metadata">

**Author:** ![seibert](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/seibert/32/520688_2.png) [@seibert](https://forums.developer.nvidia.com/u/seibert)\
**Post date:** [May 30, 2008, 9:43pm UTC](https://forums.developer.nvidia.com/t/3d-thread-id/3901/2 "2008-05-30T21:43:11Z")

</div>

The z component of dimGrid must be 1 on current hardware. Blocks can be 3D, but grids must be 2D. Your kernel probably aborts with an error when you try to run it in this configuration.

---

<div class="post-metadata">

**Author:** ![xinchen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@xinchen](https://forums.developer.nvidia.com/u/xinchen)\
**Post date:** [May 30, 2008, 10:03pm UTC](https://forums.developer.nvidia.com/t/3d-thread-id/3901/3 "2008-05-30T22:03:41Z")

</div>

Thank you very much. How to fix it? Use loop in Z. I really hope to not use loop.

> [@](#):
>
> The z component of dimGrid must be 1 on current hardware.&nbsp; Blocks can be 3D, but grids must be 2D.&nbsp; Your kernel probably aborts with an error when you try to run it in this configuration.
> 
> [snapback]386106[/snapback]

---

<div class="post-metadata">

**Author:** ![Ailleur](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Ailleur](https://forums.developer.nvidia.com/u/Ailleur)\
**Post date:** [June 13, 2008, 1:50pm UTC](https://forums.developer.nvidia.com/t/3d-thread-id/3901/4 "2008-06-13T13:50:45Z")

</div>

Heres what i do to handle 3d data:

```auto
const dim3 dimBlock(192);

	int dim = ceil(sqrt((float)(DATA_W*DATA_D*DATA_H)/192.0f));

	const dim3 dimGrid(dim,dim);

	convolve<<<dimGrid, dimBlock,0>>>(d_Result);

```

```auto
const int idx = (blockIdx.y*blockDim.x*gridDim.x)+blockIdx.x*blockDim.x+threadIdx.x;

	const int z = idx/(DATA_W*DATA_H);

	const int y = (idx - z * DATA_W * DATA_H) / DATA_W;

	const int x =  (idx - z * DATA_W * DATA_H - y * DATA_W);

```

Theres a bit more overhead, but it works.

Obviously 192 should be a constant somewhere.
