Global threadid, what I'm I missing

I’m using the code below to get the global threadid for a 3D Block and 3D Grid , but the first 256 threads always get the wrong Id, what I’m I missing?

blockId = (blockIdx%x-1) + (blockIdx%y-1)*gridDim%x &
                        + gridDim%x*gridDim%y*(blockIdx%z-1)
threadId = blockId*(blockDim%x*blockDim%y*blockDim%z) &
                        + (threadIdx%z*(blockDim%x*blockDim%y)) &
                        + (threadIdx%y*blockDim%x) + threadIdx%x

getGlobalId = threadId

Hi Godfred,

You forgot to subtract 1 from the threadIdx.

threadId = blockId*(blockDim%xblockDim%yblockDim%z) &

  • ((threadIdx%z-1)(blockDim%xblockDim%y)) &
  • ((threadIdx%y-1)*blockDim%x) + (threadIdx%x-1)

Hope this helps,
Mat