Linear array indexing

Ok, so ive been racking my brain for some algorithm to map blockIdx.x, blockIdx.y for a two dim grid and threadIdX.x, threadIdx.y for a two dim block in the grid to a linear array allocated in memory. This way if you multiply the ROWSCOLS of the number of blocks by the ROWSCOLS of the number of threads in each block it will equal the size of the array. That way each index of the array has a specific thread dedicated to it and you can assign a value to each index with the maped thread. My problem is that I cannot for the life of me figure out how to index the array. (I.E map the 2d blockId’s and the 2d threadId’s to the 1D linear array in the device memory). Anyone done anything like this or just have some pointers? Your help is greatly appreciated. Thank you.

dim3 grid(COLS/M,ROWS/N);

dim3 block(M,N);


int x = blockIdx.x*blockDim.x+threadIdx.x;

int y = blockIdx.y*blockDim.y+threadIdx.y;

int index = y*(gridDim.x*blockDim.x)+x;