Prélecture avec cudaMemPrefetchAsync
Déplacez les pages avant qu’elles ne soient nécessaires.
Prélecture avec cudaMemPrefetchAsync est une leçon CUDA Academy gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage CUDA Academy, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours CUDA Academy comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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! ✨
Questions Fréquemment Posées
La leçon « Prélecture avec cudaMemPrefetchAsync » est-elle gratuite ?
Oui — le texte complet de « Prélecture avec cudaMemPrefetchAsync » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours CUDA Academy, passe à CoddyKit PRO. Le cours CUDA Academy comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Prélecture avec cudaMemPrefetchAsync » ?
Déplacez les pages avant qu’elles ne soient nécessaires. Tu pratiques CUDA Academy avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer CUDA Academy ?
Aucune expérience préalable n'est requise. CUDA Academy sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.
Combien de temps prend la leçon « Prélecture avec cudaMemPrefetchAsync » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon CUDA Academy ?
Oui. Chaque leçon CUDA Academy inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Un pointeur, deux côtés
- Migration des pages à la demande
- Prélecture avec cudaMemPrefetchAsync
- Indications via cudaMemAdvise