Use a better grid layout to avoid those modulo operations.
Use the unique block index for the rows which is 64-bit range on latest Cuda.
Let the threads iterate in a loop over all elements and add the unique thread index!
Tiling input data is a general approach if calculated data is uniquely across a block (rows), especially for more complex calculations.
/*
* @param tileCount
*/
__global__ void addRowNumberToCells(int* inOutMat_g,
const unsigned long long int inColumnCount_s,
const int inTileCount_s)
{
//get unique block index
const unsigned long long int blockId = blockIdx.x //1D
+ blockIdx.y * gridDim.x //2D
+ gridDim.x * gridDim.y * blockIdx.z; //3D
/*
* check column ranges in case kernel is called
* with more blocks then columns
* (since its block wide following syncthreads are safe)
*/
if(blockId >= inColumnCount_s)
return;
//get unique thread index
const unsigned long long int threadId = blockId * blockDim.x + threadIdx.x;
/*
* calculate unique and 1 blockId
* maybe shared memory is overhead
* but it shows concept if calculation is more complex
*/
__shared__ unsigned long long int blockIdAnd1_s;
if(threadIdx.x == 0)
blockIdAnd1_s = blockId + 1;
__sycnthreads();
unsigned long long int idx;
//loop over tiles
for(int i = 0; i < inTileCount_s)
{
//calculate new offset for sequence thread writes
idx = i * blockDim.x + threadIdx.x;
//check new index range in case column count is no multiple of blockDim.x
if(idx >= inColumnCount_s)
break;
inOutMat_g[idx] = blockIdAnd1_s;
}
}
Example Cuda 2.0:
mat[131000][1000]
Necessary blockCount = 131000 / 65535 = 2 for blockDim.y rounded up!
inTileCount_s = 1000 / 192 = 6 rounded up!
(192 Threads per Block = 100 occupancy on Cuda 2.0)
<<(65535, 2, 1), (192, 1, 1)>>addRowNumberToCells(mat, 1000, 6)