La canalización de doble búfer
Divida los datos en bloques para mantener ocupada la GPU.
La canalización de doble búfer es una lección gratuita de CUDA Academy en CoddyKit. Esta es la lección 4 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.
The Idle GPU Problem
Copy a huge array, then run a kernel, then copy back. During each copy the GPU sits idle, and during compute the transfer engines sit idle.
Split the Work
The fix starts with breaking one giant array into smaller chunks. Each chunk can be copied and processed on its own, opening the door to overlap.
Two Buffers, Two Lanes
Double buffering uses two device buffers and two streams. While stream A computes on one chunk, stream B copies in the next.
Overlap Copy and Compute
The magic is concurrency: a transfer and a kernel run at the same time on different chunks. Transfer time hides behind useful compute.
Pinned Memory Required
This only works if the host data lives in pinned memory. Pageable buffers force blocking copies, and the whole pipeline collapses back to serial.
The Pipeline Loop
You iterate over chunks, and on each step you issue an async copy in, a kernel, and an async copy out, all on the same stream.
cudaMemcpyAsync(d_in, h_in + off, csz, H2D, s);
proc<<<g, b, 0, s>>>(d_in, d_out);
cudaMemcpyAsync(h_out + off, d_out, csz, D2H, s);Alternate the Streams
Assign each chunk to a stream by alternating them. Even chunks go to stream 0, odd chunks to stream 1, so neighbors overlap cleanly.
cudaStream_t s = streams[i % 2];Why Two Is Enough
Two streams already overlap copy with compute. More buffers add depth but bring diminishing returns and extra memory cost, so start with two.
Drain at the End
After queuing every chunk, wait for all work to finish before reading results. A simple cudaDeviceSynchronize drains every stream at once.
cudaDeviceSynchronize();The Speedup
With copy hidden behind compute, total time drops toward the cost of just the longer of the two, not their sum. That is the real win. 🚀
When It Wins Most
Double buffering shines when transfer time and compute time are roughly balanced. If one utterly dominates, focus your effort on shrinking that side first.
Quick Check
What is the core benefit of a double-buffering pipeline?
Recap
Chunk the data, use two pinned buffers and streams, and overlap copy with compute. Sync at the end and watch transfer time vanish. 🎉
Preguntas frecuentes
¿La lección «La canalización de doble búfer» es gratis?
Sí — el texto completo de «La canalización de doble búfer» 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 «La canalización de doble búfer»?
Divida los datos en bloques para mantener ocupada la GPU. 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 4 de 4.
¿Cuánto tiempo toma la lección «La canalización de doble búfer»?
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