# Grids & blocks

### Grids and Blocks

Grids and Blocks are basic CUDA concepts, that permit to organise threads

They are created as kernel launch:

* grids are 1, 2, (or 3) dimensional array of blocks
* blocks are 1, 2, (or 3) dimensionale array of threads

A thread block is a batch of threads that can cooperate with each other by:

* synchronising their execution using barriers
* efficiently sharing data through a low latency shared memory

Two threads from two different blocks cannot cooperate



### Thread block organisation

Threads are grouped into blocks

Blocks can have 1, 2, or 3 dimensions

Each block dimension can have a maximum number of threads (e.g. 512, 1024)

![Thread block organisation (1D/2D)](https://3718412349-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LcLzF9dv8k9-Sb-ui-A%2F-MJC-KqQEnWctoURT_CE%2F-MJWbiabR70AGZ6uljIj%2Fthread_block_organisation.jpeg?alt=media\&token=ee1c1ff0-1ad0-48c4-9573-00d6a9ace658)



#### Thread block

Block shape doesn't matter, but may be usefull for mapping threads to data

![Thread block vs array access](https://3718412349-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LcLzF9dv8k9-Sb-ui-A%2F-MJC-KqQEnWctoURT_CE%2F-MJWbtOzsryuLaHvn5gB%2Fthread_block_array.jpeg?alt=media\&token=c152c713-79c5-4215-9340-d5d056f39993)



Or mapping nested loops in the original code: each 2D-organised thread taking care of an (i,j) pair:

![Thread blocks - mapping loops](https://3718412349-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LcLzF9dv8k9-Sb-ui-A%2F-MJC-KqQEnWctoURT_CE%2F-MJWc1XNtTS_miFEDbJX%2Fthread_block_loop.jpeg?alt=media\&token=df8d4006-841a-455c-b36c-b3fc8f761081)



#### Block organisation

Threads are grouped into blocks

Blocks are organised in a grid

A grid can be 1D, 2D (or 3D)

![Grid vs blocks](https://3718412349-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LcLzF9dv8k9-Sb-ui-A%2F-MJWcGC_kGs55ArPLnuw%2F-MJWcq7DWKdsI8S2WvNa%2Fblock_organisation.jpeg?alt=media\&token=79ed03e8-2899-4e84-b65d-6f934168fb07)

