Sobes.tech
Senior

What is CUDA and what are its main concepts (kernel, grid, block, thread)?

sobes.tech AI

Answer from AI

CUDA (Compute Unified Device Architecture) is a platform and API from NVIDIA for parallel computing on GPUs.

Key concepts:

  • Kernel — a function that executes in parallel on the GPU. Multiple copies of the kernel are launched, each processing a part of the data.

  • Thread — an individual execution thread of the kernel. Each thread executes one copy of the kernel.

  • Block — a group of threads that can interact with each other through shared memory and synchronize.

  • Grid — a collection of blocks that together form the entire workload for the kernel.

The kernel launch structure looks like this: you specify the number of blocks in the grid and the number of threads in each block. This allows efficient parallelization of computations across thousands of threads.

Example of launching a kernel in CUDA (pseudo-code):

__global__ void add(int *a, int *b, int *c) {
  int idx = threadIdx.x + blockIdx.x * blockDim.x;
  c[idx] = a[idx] + b[idx];
}

int main() {
  // memory allocation, data copying, etc.
  add<<<gridSize, blockSize>>>(dev_a, dev_b, dev_c);
  // synchronization and copying results
}

Thus, CUDA enables efficient utilization of GPU parallelism to accelerate computations.