Determine Grid/Block Dimensions From within a kernel or device function

Is there a way to determine the Grid / Block geometry of the currently executing kernel from within the kernel itself or a function called from within the kernel?

Basically, I would like to do something like:

// If the time series is longer than then number of threads in the

  // block, we need to iterate.

  unsigned int i = (TS_LENGTH / THREADS_PER_BLOCK) + 1;

  while(i) {

    unsigned int j = i * threadIdx.x;

    if(j < TS_LENGTH) {

      sharedReferenceTimeSeries[j] = globalReferenceTimeSeries[j];

    }

    i--;

  }

But determine THREADS_PER_BLOCK dynamically, rather than have it defined as a constant or passed everywhere as a parameter.

How might I do this?

Thanks!

Is there a way to determine the Grid / Block geometry of the currently executing kernel from within the kernel itself or a function called from within the kernel?

Basically, I would like to do something like:

// If the time series is longer than then number of threads in the

  // block, we need to iterate.

  unsigned int i = (TS_LENGTH / THREADS_PER_BLOCK) + 1;

  while(i) {

    unsigned int j = i * threadIdx.x;

    if(j < TS_LENGTH) {

      sharedReferenceTimeSeries[j] = globalReferenceTimeSeries[j];

    }

    i--;

  }

But determine THREADS_PER_BLOCK dynamically, rather than have it defined as a constant or passed everywhere as a parameter.

How might I do this?

Thanks!

Have a look at gridDim and blockDim in the Programming guide.

Have a look at gridDim and blockDim in the Programming guide.

Yes, and artifact of dimwitted searching on my part.

For those who want to determine the “size” of a block (total number of “cells”):

__device__ unsigned int getBlockSize(void) {

 return blockDim.x * blockDim.y * blockDim.z;

}

And the “size” of a grid is

gridDim.x * gridDim.y * gridDim.z

OR

gridDim.x * gridDim.y

Since gridDim.z is not used, and therefore always “1”.

-CP

Yes, and artifact of dimwitted searching on my part.

For those who want to determine the “size” of a block (total number of “cells”):

__device__ unsigned int getBlockSize(void) {

 return blockDim.x * blockDim.y * blockDim.z;

}

And the “size” of a grid is

gridDim.x * gridDim.y * gridDim.z

OR

gridDim.x * gridDim.y

Since gridDim.z is not used, and therefore always “1”.

-CP