Host and device: two computers, one program
A CUDA program runs on two processors at once:
- The host is the CPU. It runs
main(), manages files, allocates GPU memory, and launches work. - The device is the GPU. It runs the parallel kernels you write.
They have separate memory spaces (host RAM vs device VRAM) and separate execution. Host code calls into device code through kernel launches, then continues asynchronously while the GPU works.
int main() {
// host code here
myKernel<<<grid, block>>>(d_arr); // launch on device
cudaDeviceSynchronize(); // wait for device
// host code again
}
This split is the source of most CUDA bugs. Forget which side a pointer belongs to and you'll segfault or read garbage.
