CUDA Asynchronous Copies: Distinguish Host-Device Transfers from In-Kernel Copies
CUDA asynchronous copy can mean either a host-to-device transfer submitted with cudaMemcpyAsync or an in-kernel global-to-shared copy. Learn how their memory paths, completion rules, pinned-buffer requirements, and possible overlap differ, then use a two-slot example and a verification checklist to assess correctness and end-to-end performance without assuming a universal speedup.
CUDA Asynchronous Copies: Distinguish Host-Device Transfers from In-Kernel Copies
In CUDA, “asynchronous copy” can refer to two different operations. Host-side cudaMemcpyAsync() submits a transfer between the CPU and GPU to a stream so the CPU does not have to wait. In-kernel cuda::memcpy_async() helps a GPU thread move data from global memory to shared memory while continuing other work. The operations have different execution locations, memory paths, and completion rules. An API call returning does not mean the data has arrived or that a transfer overlapped computation.
This article helps CUDA C++ developers choose a copy path and place safe completion points. For stream ordering and event dependencies, see How to Design Ordering and Events with CUDA Streams. Here the focus is the difference between the two copy paths and the conditions for overlap. The CUDA Programming Guide and CUDA C++ Best Practices Guide 13.5 were checked on November 22, 2026. No speedup or execution result for a particular GPU is claimed.
First distinguish the paths: CPU-GPU transfer or copy inside a kernel?
The first path sends CPU-prepared input to device memory or returns a GPU result to CPU memory. When cudaMemcpyAsync() is given a stream, the runtime places the transfer in that stream’s order. The call usually returns to the CPU before the operation completes, allowing the CPU to prepare other input or do independent work. This asynchrony describes the relationship between the CPU and GPU work; it does not guarantee that the transfer and a kernel using that data run at the same time.
The second path copies data from global memory to shared memory from inside a GPU kernel. It uses CUDA facilities such as cuda::memcpy_async() with a pipeline or barrier, and completion is awaited inside the kernel. A pipeline can fetch the next tile while the kernel computes on the current tile. This is a different mechanism from a host-device transfer started by calling cudaMemcpyAsync() on the CPU.
| Question | Host-device transfer | In-kernel copy |
|---|---|---|
| Where does the operation start? | CPU code calls cudaMemcpyAsync() | GPU kernel calls cuda::memcpy_async() or a related API |
| Typical data path | Pinned host memory ↔ device memory | Device global memory → shared memory |
| Where is completion checked? | CPU code synchronizes a stream or event, or another stream waits | An async barrier or pipeline inside the kernel; block synchronization may also be needed before other threads read shared data |
| Main purpose | Let the CPU do other work during a transfer, or overlap chunk transfers with kernels | Overlap movement of the next tile with computation on the current tile inside a kernel |
What does cudaMemcpyAsync() need for an asynchronous transfer?
On a discrete GPU, ordinary CPU buffers are usually pageable memory. The operating system can move their pages, so CUDA cannot always use their addresses directly and reliably throughout a transfer. For the intended asynchronous CPU-to-GPU or GPU-to-CPU transfer path, prepare the host buffer as pinned (page-locked) memory. Passing an ordinary buffer can cause the runtime to use an internal copy or a synchronous path, so the call may not behave as asynchronously as expected.
Pinned memory is not VRAM, and it does not mean the CPU and GPU automatically see the same data. In the common discrete-GPU path, a copy moves data between a pinned host buffer and a separate device buffer. Pinning many pages reduces memory available to the operating system, and pinning or unpinning also has a cost. Instead of repeatedly allocating and freeing memory for each transfer, prepare the required buffers once and reuse them. Where appropriate, compare combining small transfers into fewer larger ones.
Operations in one stream execute in the order they were enqueued. If a stream receives a host-to-device copy, a kernel, and then a device-to-host copy, the kernel follows the input copy and the output copy follows the kernel. This ordering establishes dependencies, but it does not make the copy and kernel for that same chunk run concurrently. With independent chunks in multiple streams, a GPU may overlap one chunk’s computation with another chunk’s transfer if its copy engines and resources allow it. The result depends on device capabilities, copy-engine count, memory bandwidth, kernel resource use, and dependencies; inspect the actual timeline.
How is cuda::memcpy_async() inside a kernel different?
An in-kernel asynchronous copy moves data from global memory to shared memory. It does not eliminate the CPU-GPU transfer. It prepares a tile that has already reached the GPU for repeated use by a kernel. This can be useful in kernels such as matrix multiplication or stencils that stage neighboring data in shared memory and reuse it across multiple operations.
The current CUDA Programming Guide describes the LDGSTS asynchronous-copy path for compute capability 8.0 and later. It supports the global-to-shared direction, copies of 4, 8, or 16 bytes, and pointer alignment appropriate to the copy size. The guide identifies 128-byte alignment as beneficial for performance, not a universal correctness requirement. Nor should you assume that every use of the high-level cuda::memcpy_async() API produces a particular hardware instruction. Check the compiler path and measurements when that distinction matters.
If a kernel reads shared memory before the copy finishes, it can consume incorrect data. Put a barrier or pipeline completion wait before the data is consumed. Even after one thread waits for its own copy, block synchronization may be needed if other threads read the same data. Frequent waits can reduce the benefit of overlap, so design the producer, consumer, and tile-reuse points together.
When do copies and computation actually overlap?
An asynchronous API expresses execution order and can reduce how long the calling thread waits; it is not a switch that guarantees concurrency. Host-device overlap needs pinned host buffers, independent work, suitable stream placement, and hardware support for copy engines. Inside a kernel, the next data movement must be independent of current computation, the tiles must fit in shared memory, and the copy must be awaited correctly. Too many waits or connected dependencies can leave no work to overlap.
This conceptual schedule shows two chunks using two streams and buffer slots. It is not a guaranteed execution result on a particular GPU; it is one possible arrangement when the work is independent and resources permit it.
Time ───────────────────────────────────────────────────────▶
Slot 0: [Chunk 0 H→D] [Kernel 0] [Chunk 0 D→H] [reuse]
Slot 1: [Chunk 1 H→D] [Kernel 1] [Chunk 1 D→H]
Possible overlap: Kernel 0 and transfer for chunk 1The schedule changes depending on whether a device can handle H→D and D→H transfers at the same time and whether it can overlap a transfer with a kernel. Do not overlap operations that need to use the same input or output buffer at the same time.
How can two-slot streaming be made safe?
Split a large input into chunks. For each slot, provide pinned host input and output buffers, device input and output buffers, a stream, and a completion event. Submit H→D copy, kernel, and D→H copy in order to the slot’s stream, then record an event after the D→H copy. The CPU must not overwrite that slot’s input or read its output until the event completes. Before reusing a slot, check the previous event, then fill the next input and collect the previous result.
The following is an ordering example that assumes two slots have already been allocated and initialized. processChunk is an application kernel; error handling, the size of the final partial chunk, and application-specific output bookkeeping are omitted. This code was not compiled or run on a GPU.
// hIn[slot], hOut[slot] are pinned host buffers.
// dIn[slot], dOut[slot], stream[slot], done[slot] are already created.
bool slotWasSubmitted[2] = {false, false};
for (int chunk = 0; chunk < chunkCount; ++chunk) {
int slot = chunk % 2;
if (chunk >= 2) {
cudaEventSynchronize(done[slot]);
consumeOutput(hOut[slot]); // Previous D2H copy is complete.
}
fillInput(hIn[slot], chunk); // Reuse only after prior H2D is complete.
cudaMemcpyAsync(dIn[slot], hIn[slot], bytes,
cudaMemcpyHostToDevice, stream[slot]);
processChunk<<<grid, block, 0, stream[slot]>>>(dIn[slot], dOut[slot]);
cudaMemcpyAsync(hOut[slot], dOut[slot], bytes,
cudaMemcpyDeviceToHost, stream[slot]);
cudaEventRecord(done[slot], stream[slot]);
slotWasSubmitted[slot] = true;
}
// Before reading final outputs, wait for each slot that was submitted.
for (int slot = 0; slot < 2; ++slot) {
if (slotWasSubmitted[slot]) {
cudaEventSynchronize(done[slot]);
consumeOutput(hOut[slot]);
}
}The application must track slotWasSubmitted and decide how it consumes outputs. cudaEventSynchronize() blocks the CPU, so compare it with polling for completion while doing other work or separating CPU tasks. Writing a pinned input before its transfer completes, or reading a D→H result before its event completes, creates a data race. See also CUDA Stream Ordering and Event Dependencies.
Why might there be no benefit, or the program get slower?
| Observation | First causes to check | Safe next step |
|---|---|---|
| The CPU waits for a long time after cudaMemcpyAsync() | Pageable host memory, implicit synchronization, or initialization cost | Check pinned buffers and synchronization points; measure transfer time separately from end-to-end latency |
| The timeline shows no transfer/kernel overlap | Sequential dependencies in one stream, event waits, or engine/resource limits | Check for independent chunks, device capabilities, and what the profiler records |
| The whole program slows after switching to pinned memory | Too much pinned memory, repeated allocation/free, or overhead from many small copies | Reuse a small number of buffers, combine transfers where suitable, and compare total time |
| Values become unstable after an in-kernel copy | Missing barrier/pipeline wait, missing synchronization before tile reuse, or alignment/size mismatch | Check completion ordering for every consumer thread, edge chunks, and alignment |
| An in-kernel copy does not make the kernel faster | Memory latency is not the bottleneck, shared-memory use rises, occupancy falls, or copy/wait overhead dominates | Compare against the baseline with identical input and measure tile size, stage count, and resource use |
Measure preparation and allocation, copies, kernels, result collection, and the full application interval separately. Do not estimate a host-device pipeline benefit from the time of one kernel alone. Compare end-to-end throughput and latency using the same data size and repetition count, and inspect the GPU timeline for actual transfer/kernel overlap. Do not generalize a result from one device to other GPUs or input sizes.
Which copy path should you try first?
If CPU data transfer to the GPU and result return take a substantial share of the time, investigate the host-device path. If data is already on the GPU and a kernel repeatedly fetches tiles from global memory, consider in-kernel copies and a pipeline. Some applications need both, but measure each layer and its completion points separately to see which change helped.
**Record where data lives and who consumes it.** Mark whether data travels from CPU memory to the GPU or from global to shared memory inside a kernel.
**Choose the completion point.** For host-device copies, reuse or read buffers only after stream/event completion. Inside a kernel, read shared data only after the barrier/pipeline and any required block synchronization.
**Measure a simple, safe baseline.** Record copy, kernel, allocation, result-collection, and total time under the same conditions.
**Make one layer asynchronous at a time.** Start with pinned host buffers and multiple streams, or an in-kernel tile pipeline, depending on the measured bottleneck.
**Recheck correctness and actual overlap.** Compare results with the baseline, inspect profiler overlap, waits, and resource contention, then decide using total latency and throughput.
Sources
[NVIDIA CUDA Programming Guide — Asynchronous Execution](https://docs.nvidia.com/cuda/cuda-programming-guide/02-basics/asynchronous-execution.html)
[NVIDIA CUDA Programming Guide — Asynchronous Data Copies](https://docs.nvidia.com/cuda/cuda-programming-guide/04-special-topics/async-copies.html)
[NVIDIA CUDA Programming Guide — Pipelines](https://docs.nvidia.com/cuda/cuda-programming-guide/04-special-topics/pipelines.html)
[NVIDIA CUDA C++ Best Practices Guide 13.5](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/)