GPU Programming
In CUDA, how does the CPU-side program receive the results computed by a GPU kernel (a function marked __global__)?
- AThrough the kernel's return value, which sends the results of all threads back to the CPU together
- BThe kernel writes its results to an array in GPU memory, and after the kernel finishes the CPU side copies that array to CPU memory (or reads unified memory)
- CThe CPU-side program reads the values each thread printed with printf as variables
Show answer and explanation
Answer: B. The kernel writes its results to an array in GPU memory, and after the kernel finishes the CPU side copies that array to CPU memory (or reads unified memory)
A GPU kernel is executed by many threads at once, so it cannot return its results as a single value, and in CUDA a __global__ function must have a void return type. The CPU side allocates an array in GPU memory and passes its pointer as an argument, and each thread writes its result to the element it handles. Because kernel launches are asynchronous, the CPU side copies the array back with cudaMemcpy or similar after the kernel has finished (in the same stream, the copy waits for the preceding kernel to finish). printf inside a kernel is a debugging aid for displaying output, and the program cannot read those values back.
- A. A __global__ function must have a void return type.
- B. Correct. Results are received through memory.
- C. printf only displays output; the program cannot use it as values.