0Pricing
CUDA Academy · Lektion

Ein Pointer, beide Seiten

So funktioniert cudaMallocManaged.

Ein Pointer, beide Seiten ist eine kostenlose CUDA Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des CUDA Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

The Two-Pointer Headache

So far you juggled two pointers: one for the host, one for the device. Unified Memory replaces that pain with a single pointer both sides can use. 🙂

Meet cudaMallocManaged

You allocate managed memory with cudaMallocManaged. It hands back one address that works in your CPU code and inside your kernels alike.

float *data;
cudaMallocManaged(&data, n * sizeof(float));

No More cudaMemcpy

The big win: you usually skip the manual copies. With managed memory the runtime moves data for you, so cudaMemcpy often disappears from your code.

Write on the Host

You can fill the buffer with ordinary CPU code right after allocating. The same pointer you got is a normal address your loops can touch.

for (int i = 0; i < n; i++)
    data[i] = i;

Read on the Device

Pass that exact pointer to your kernel and the GPU threads dereference it directly. One address serves both worlds with no translation step.

kernel<<<blocks, threads>>>(data, n);

Sync Before You Read Back

After a kernel writes managed data, call cudaDeviceSynchronize before the CPU reads it. That guarantees the GPU finished and results are visible.

kernel<<<b, t>>>(data, n);
cudaDeviceSynchronize();

Free It Like Any Buffer

Managed memory is still device memory, so you release it with cudaFree. There is no special managed-free call to remember.

cudaFree(data);

Less Boilerplate, Fewer Bugs

Because you delete the alloc-copy-launch-copy-free dance, your programs shrink. Fewer copies means fewer chances to mix up directions or sizes.

Great for Prototyping

Unified Memory is perfect when you want a kernel running fast. You prototype quickly, then optimize transfers later only where they actually matter.

It Is Not Free Magic

The data still has to travel across PCIe under the hood. Convenience is real, but performance can lag hand-tuned copies until you add hints later.

When to Reach for It

Choose managed memory for simpler code, deep pointer structures, or oversubscribing GPU memory. It shines when clarity matters more than raw peak speed.

Quick Check

Let us confirm how managed allocation differs from the classic flow.

Recap: One Pointer, Both Sides

You learned that cudaMallocManaged hands you one pointer for host and device, dropping most copies. Sync before reading, free with cudaFree. Nice work! 🎉

Häufig gestellte Fragen

Ist die Lektion „Ein Pointer, beide Seiten“ kostenlos?

Ja — der vollständige Text von „Ein Pointer, beide Seiten“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des CUDA Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Ein Pointer, beide Seiten“?

So funktioniert cudaMallocManaged. Du übst CUDA Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um CUDA Academy zu starten?

Keine Vorkenntnisse erforderlich. CUDA Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Ein Pointer, beide Seiten“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser CUDA Academy-Lektion Code schreiben und ausführen?

Ja. Jede CUDA Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Ein Pointer, beide Seiten
  2. Seit迁移 bei Bedarf
  3. Prefetching mit cudaMemPrefetchAsync
  4. Hinweise mit cudaMemAdvise
← Zurück zu CUDA Academy