L2 Intermediate · Module 4: CUDA programming fundamentals
4.1A kernel is not a function
A CUDA kernel looks like a C++ function, but calling it starts thousands of copies at once, returns before any of them has run, gives nothing back, and can't see your CPU's memory. Get these four differences right and the rest of CUDA follows.
By the end you will be able to
- ✓Write, launch and error-check a CUDA kernel
- ✓Explain the four ways a kernel differs from a normal function
- ✓Show that kernel launches are asynchronous, and synchronize correctly
- ✓Build and run a checked vector add on a GPU or on Colab
Here is a complete CUDA kernel:
__global__ void vector_add(const float* a, const float* b, float* c, int n) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < n) c[i] = a[i] + b[i];}And here is how you call it:
vector_add<<<blocks, threads>>>(a, b, c, n);It looks like a function call with some odd brackets. It isn’t. Four things are different.
1. One call, many copies
<<<blocks, threads>>> is the launch configuration. The call runs the kernel body blocks × threads times, once per thread, all
at the same time on the GPU’s cores. With 262,144 blocks of 256 threads, that is 67 million copies of the two lines above.
Each copy finds out which one it is from built-in variables: blockIdx (which block), threadIdx (which thread inside the block) and
blockDim (threads per block). From those it computes which element to work on. Chapter 4.2
covers indexing in full.
2. It returns before it runs
A launch only enqueues the work. The CPU gets control back after a few microseconds, while the GPU may not have started yet. From the lab:
2) launches are asynchronous launch call returned after 4.6 us kernel finished after 5060.9 usTo wait for the GPU, call cudaDeviceSynchronize(), or do something that implicitly waits, such as copying the result back with
cudaMemcpy. Forgetting this is the most common source of “my kernel takes 0 ms” benchmarks. Chapter
4.6 shows how to use this asynchrony on purpose.
3. No return value, and errors arrive late
A kernel’s return type is always void. Results are written to memory through pointers. Errors don’t come back from the launch either:
- A bad launch configuration (say, 2,000 threads per block, over the 1,024 limit) is reported by
cudaGetLastError()right after the launch. - A fault inside the kernel, such as an out-of-bounds write, shows up at the next synchronizing call, possibly far away in your code.
So every lab here wraps API calls in a checking macro:
#define CUDA_CHECK(call) \ do { \ cudaError_t err_ = (call); \ if (err_ != cudaSuccess) { \ fprintf(stderr, "CUDA error %s at %s:%d: %s\n", #call, \ __FILE__, __LINE__, cudaGetErrorString(err_)); \ exit(1); \ } \ } while (0)4. It lives in a different memory
On a discrete GPU, the kernel can only touch device memory (the GPU’s HBM or GDDR). Pointers from malloc or new point into CPU
memory, and dereferencing them in a kernel is an error. You allocate device memory with cudaMalloc and move data with cudaMemcpy
(4.3).
The qualifiers
| Qualifier | Runs on | Called from |
|---|---|---|
__global__ | GPU | CPU (a launch), returns void |
__device__ | GPU | GPU code only: a normal helper function |
__host__ | CPU | CPU (the default for ordinary functions) |
Lab: your first kernel 🔴
Every Module 4 lab is a single .cu file. On your own NVIDIA GPU, or on Colab with a free T4 (see labs/m04/README.md):
cd labs/m04/4.1nvcc -O2 -arch=native vector_add.cu -o vector_add && ./vector_addGPU: NVIDIA GeForce RTX 3050 6GB Laptop GPU (sm_86, 20 SMs)
1) hello<<<3, 32>>>: blocks run in any order hello from block 0, thread 0 hello from block 0, thread 1 hello from block 2, thread 0 hello from block 2, thread 1 hello from block 1, thread 0 hello from block 1, thread 1
2) launches are asynchronous launch call returned after 4.6 us kernel finished after 5060.9 us
3) vector add, 67108864 elements, 262144 blocks x 256 threads kernel time 5.073 ms -> 159 GB/s (3 x 256 MB moved) check against CPU: PASSThe vector add moved 768 MB in 5.1 ms, which is 159 GB/s, about 95% of this laptop GPU’s ~168 GB/s spec. With 1 FLOP per 12 bytes it is completely memory-bound (3.1), and reaching 95% of bandwidth is as good as it gets.
Sources
Check your understanding
0 / 4 answered1.How many times does the body of kernel<<<100, 128>>>(...) run?
2.What happens when the CPU launches a kernel?
3.How does a kernel give back its result?
4.Block 7 needs a value computed by block 3 of the same kernel. What is the correct approach?