Prefetching con cudaMemPrefetchAsync
Mueva las páginas antes de necesitarlas.
Prefetching con cudaMemPrefetchAsync 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.
Stop Paying for Faults
Instead of waiting for slow first-touch faults, you can move pages early. cudaMemPrefetchAsync sends managed data to a device before the kernel needs it.
The Basic Call
You name the pointer, the byte count, and the destination device. This one prefetch migrates the whole range up front in a single efficient move.
cudaMemPrefetchAsync(data, n * sizeof(float), 0);Pick the Destination
The third argument is the device id. Pass a GPU number to stage data on that GPU, ready for the kernel you are about to launch.
int dev = 0;
cudaMemPrefetchAsync(data, bytes, dev);Prefetch Back to the CPU
Use the special id cudaCpuDeviceId to pull results back to the host. Do it before CPU code reads them to avoid a wave of faults.
cudaMemPrefetchAsync(data, bytes, cudaCpuDeviceId);It Is Asynchronous
The Async in the name is real: the call returns immediately and runs in a stream. Your CPU keeps working while pages migrate in the background.
Overlap With Compute
Because it rides a stream, a prefetch can overlap with other kernels. Stage the next chunk while the current one is still being processed.
cudaMemPrefetchAsync(next, bytes, dev, stream);One Move Beats Many Faults
A single bulk prefetch is far cheaper than thousands of tiny faults. You trade scattered overhead for one contiguous high-bandwidth transfer.
Prefetch the Right Range
Only stage what the kernel actually touches. Prefetching a huge buffer the kernel barely reads just wastes bandwidth and GPU memory.
A Two-Sided Pattern
A clean rhythm emerges: prefetch to the GPU, launch the kernel, prefetch results back. This keeps migration off the critical path on both ends.
Measure, Do Not Guess
Add a prefetch, then check Nsight for fewer faults and tighter timelines. Let profiling confirm the win rather than trusting intuition.
Convenience Plus Control
Prefetching keeps the single-pointer ease of managed memory while giving you back control over timing. You get the best of both styles.
Quick Check
Let us confirm what prefetching buys you.
Recap: Prefetching
You learned to stage pages early with cudaMemPrefetchAsync, picking a GPU or cudaCpuDeviceId. It overlaps in streams and beats faulting. Great job! ✨
Preguntas frecuentes
¿La lección «Prefetching con cudaMemPrefetchAsync» es gratis?
Sí — el texto completo de «Prefetching con cudaMemPrefetchAsync» 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 «Prefetching con cudaMemPrefetchAsync»?
Mueva las páginas antes de necesitarlas. 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 «Prefetching con cudaMemPrefetchAsync»?
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
- Un puntero para ambos lados
- Migración de páginas bajo demanda
- Prefetching con cudaMemPrefetchAsync
- Sugerencias mediante cudaMemAdvise