cudaMemcpyAsync en un stream
Transferencias no bloqueantes que se solapan.
cudaMemcpyAsync en un stream es una lección gratuita de CUDA Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de CUDA Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de CUDA Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Blocking by Default
Plain cudaMemcpy stops your CPU until the copy finishes. Your host thread just waits, doing nothing useful while bytes move.
The Async Cousin
Meet cudaMemcpyAsync. It queues the copy and returns to your CPU immediately, so the host can keep working while the transfer happens.
It Needs a Stream
The async copy takes an extra argument: a stream. The stream is the ordered queue where the transfer waits its turn to run.
cudaMemcpyAsync(d_data, h_data, bytes, cudaMemcpyHostToDevice, stream);Pinned or Bust
Here is the catch: async copies only run truly non-blocking from pinned host memory. Pass a pageable buffer and CUDA silently falls back to a blocking copy.
Returns, Does Not Finish
When the call returns, the copy has only been scheduled, not completed. The data may still be in flight, so do not read it yet.
Order Within a Stream
Work in one stream runs in order, one item after another. So a copy issued before a kernel in the same stream is guaranteed to finish first.
Wait When You Must
Before touching the results on the CPU, make sure the queue drained. Call cudaStreamSynchronize to block until that stream's work is done.
cudaStreamSynchronize(stream);The Whole Point: Overlap
Async copies let a transfer in one stream run while a kernel in another stream computes. That concurrency is how you hide transfer time.
Beware the Default Stream
If you pass stream 0, the legacy default, it can serialize against everything else. Use your own created streams to actually get overlap.
Keep the Buffer Alive
Since the copy finishes later, the host buffer must stay valid until then. Free it too early and the in-flight transfer reads garbage.
A Typical Pattern
The classic flow is async copy in, launch the kernel, then async copy out, all in one stream. Sync only at the very end.
cudaMemcpyAsync(d_in, h_in, bytes, cudaMemcpyHostToDevice, stream);
kernel<<<grid, block, 0, stream>>>(d_in, d_out);Quick Check
What does cudaMemcpyAsync require to be genuinely non-blocking?
Recap
cudaMemcpyAsync queues a copy in a stream and returns at once, but needs pinned memory to overlap. Sync the stream before reading results. ⚡
Preguntas frecuentes
¿La lección «cudaMemcpyAsync en un stream» es gratis?
Sí — el texto completo de «cudaMemcpyAsync en un stream» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de CUDA Academy, actualiza a CoddyKit PRO. El curso de CUDA Academy incluye 4 lecciones en total.
¿Qué aprenderé en «cudaMemcpyAsync en un stream»?
Transferencias no bloqueantes que se solapan. Practicas CUDA Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar CUDA Academy?
No se requiere experiencia previa. CUDA Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.
¿Cuánto tiempo toma la lección «cudaMemcpyAsync en un stream»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de CUDA Academy?
Sí. Cada lección de CUDA Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Por qué la memoria paginable es lenta
- Memoria fijada con cudaMallocHost
- cudaMemcpyAsync en un stream
- La canalización de doble búfer