GPU Programming

In CUDA, how does the CPU-side program receive the results computed by a GPU kernel (a function marked __global__)?

  1. AThrough the kernel's return value, which sends the results of all threads back to the CPU together
  2. 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)
  3. 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.
Practice this topic Back to topic

Related calculators

Books on GPU Programming Ad

As an Amazon Associate I earn from qualifying purchases.

More GPU Programming questions

  1. On a GPU whose limit is 1024 threads per thread block, a CUDA kernel is launched with blockDim.x set to 2048. What generally happens?
  2. Assume an SM of a GPU has one issue unit that can issue instructions from one warp per cycle. Each warp repeatedly issues 20 cycles of instructions and then waits 400 cycles for the result of a memory load. If switching warps takes no time, at least how many warps are needed to eliminate idle time due to memory waits (latency hiding)?
  3. How do the design philosophies of CPUs and GPUs differ?
  4. Which characteristic best describes work that GPUs process quickly?
  5. Which processing tends to run inefficiently on a GPU?

Comparisons in this topic