# Using 2D and 3D for nested loops

**URL:** <https://forums.developer.nvidia.com/t/using-2d-and-3d-for-nested-loops/146904>\
**Category:** Legacy PGI Compilers (archived)\
**Tags:** cuda\
**Created:** [August 14, 2020, 8:08pm UTC](https://forums.developer.nvidia.com/t/using-2d-and-3d-for-nested-loops/146904 "2020-08-14T20:08:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cattaneo](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/cattaneo/32/25378_2.png) [@Cattaneo](https://forums.developer.nvidia.com/u/Cattaneo)\
**Post date:** [August 14, 2020, 8:08pm UTC](https://forums.developer.nvidia.com/t/using-2d-and-3d-for-nested-loops/146904/1 "2020-08-14T20:08:41Z")

</div>

I can’t quite figure out how to use dim3 effectively for creating nested loops. I understand that you define threads per block and blocks per grid and you can use blockIdx%x/y/z and other equivalents for nested loops, but I’m afraid I don’t quite understand how.  
I understand that the actual conversion is  
loop x for N1  
loop y for N2  
array[x,y]

which becomes  
array[(blockDim%x \* (blockIdx%x - 1) + threadIdx%x),(blockDim%y \* (blockIdx%y - 1) + threadIdx%y)]

but I don’t understand how one picks the actually size for dim3(x,y,z). My attempts to read into this haven’t helped very much. I can give a more specific example of code I’m trying to get working if that would help.
