liveBook · Manning

cudaMemcpy

As explained in Grokking GPU Programming

cudaMemcpy is a CUDA runtime API function that copies data between host and device memory or between device memory regions.

CUDA programs use cudaMemcpy to move data between host memory and device memory. The host and GPU keep separate copies, so input must be copied to the device before a kernel uses it, and results must be copied back for the host to use. section 2.16 section 2.18

Copy in, then copy out

Use destination, source, size, direction—in that order: cudaMemcpy(d_data, h_data, n * sizeof(int), cudaMemcpyHostToDevice); cudaMemcpy(h_data, d_data, n * sizeof(int), cudaMemcpyDeviceToHost); The direction must match the pointers. If it does not, the runtime may return an error and leave the destination unchanged; unchecked, this can look like stale or garbage data. section 2.16 section 2.18 section 2.22

Figure 2.10 Step 4 — Copy Out: bringing the results back to the host. cudaMemcpy transfers the computed results from device to host: the squared values (9, 25, 4, 49) are now back on the CPU, ready to be used.

See the squared results after the device-to-host copy, ready for use on the CPU. section 2.18

How Grokking GPU Programming covers it

How deep it goes
Defines argument order, direction constants, and common copy errors, but defers the complete memory reference.
Examples
Uses host and device arrays, including squared values returned to the CPU.
What it assumes
Assumes allocated host and device arrays while teaching copy argument order and direction constants.
What you can do after
Apply cudaMemcpy to transfer arrays in either direction and match the direction constant to the pointers.
How it connects
Connects host and device memory transfers with CUDA kernels and blocking synchronization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking GPU Programming. The section links point to the parts of the book it draws on.