CUDA : Global unique thread index in a 3D Grid

cuda

Solution

// unique block index inside a 3D block grid
const unsigned long long int blockId = blockIdx.x //1D
        + blockIdx.y * gridDim.x //2D
        + gridDim.x * gridDim.y * blockIdx.z; //3D

// global unique thread index, block dimension uses only x-coordinate
const unsigned long long int threadId = blockId * blockDim.x + threadIdx.x;

Problem

As the question states, If I have a 3D Grid of Blocks what is the formula to get a global unique index for one thread ? Lets keep the block itself as one dimensional.

Original source

Related problems