# Linear array indexing

**URL:** <https://forums.developer.nvidia.com/t/linear-array-indexing/11013>\
**Category:** CUDA Programming and Performance\
**Created:** [July 20, 2009, 11:13pm UTC](https://forums.developer.nvidia.com/t/linear-array-indexing/11013 "2009-07-20T23:13:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakeM](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jakeM](https://forums.developer.nvidia.com/u/jakeM)\
**Post date:** [July 20, 2009, 11:13pm UTC](https://forums.developer.nvidia.com/t/linear-array-indexing/11013/1 "2009-07-20T23:13:10Z")

</div>

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 ROWS_COLS of the number of blocks by the ROWS_COLS 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.

---

<div class="post-metadata">

**Author:** ![Nico](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Nico](https://forums.developer.nvidia.com/u/Nico)\
**Post date:** [July 21, 2009, 10:09am UTC](https://forums.developer.nvidia.com/t/linear-array-indexing/11013/2 "2009-07-21T10:09:13Z")

</div>

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

dim3 block(M,N);

[codebox]

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

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

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

array[index]=…

[/codebox]

N.
