Suppose eight students each grade one exam. Instead of giving one student a loop over all eight exams, give every student the same instruction and let them work concurrently. A CUDA kernel is that instruction: a function launched on the GPU for many threads to execute. section 2.3
Mark a kernel function with __global__. It is typically called from the host (CPU) and runs on the device (GPU). Each thread executes the same kernel body independently; the launch determines how many threads run it. A CUDA kernel is not an operating system kernel. section 2.3 section 2.1
#include <stdio.h>
#include <cuda_runtime.h>
__global__ void helloKernel() { #A
printf("Hello, Parallel World!\n"); #B
}
int main() {
helloKernel<<<1, 8>>>(); #C
cudaDeviceSynchronize(); #D
return 0;
}This launch starts one block of eight threads. Each thread runs the single printf statement, producing eight lines without a loop in the kernel body. section 2.1 section 2.3
A launch returns before the GPU necessarily finishes. Call cudaDeviceSynchronize() before relying on the results. For data processing, a common sequence is to allocate GPU memory, send data to the device, launch and synchronize, retrieve the results, and free memory. section 2.24
Threads need an index to find their own work. In one block, threadIdx.x identifies a thread. Across multiple blocks, blockIdx.x * blockDim.x + threadIdx.x gives its global position. Round up the block count when necessary, and guard accesses with if (idx < n) so extra threads do not go past the data. section 2.24 section 3.16
When a launch or layout goes wrong
A block with more than 1,024 threads fails to launch, so no threads run and data can come back unchanged. Call cudaGetLastError() immediately after the launch to see the error. Also, do not assume blocks finish in order. In a tiled matrix multiplication kernel where each thread loads one tile element, the block dimensions must match the tile dimensions: too few threads leave slots unloaded; too many can corrupt memory. section 3.16 section 12.4
Figure 5.3 The tree reduction kernel in three acts: load, reduce, write back. Act 1 copies global memory into shared memory so the fast reduction can happen on-chip. Act 2 runs three rounds of stride-based addition (stride 4, 2, then 1), collapsing eight values down to one. Act 3 uses atomicAdd to safely write the block's result (36) back to global memory.

A reduction kernel can do more than assign one independent output per thread: here, a block loads values, combines them in stages, and writes a subtotal. section 5.10

