0Pricing
CUDA Academy · Lesson

One Pointer, Both Sides

How cudaMallocManaged works.

One Pointer, Both Sides is a free CUDA Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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! 🎉

Frequently asked questions

Is the “One Pointer, Both Sides” lesson free?

Yes — the full text of “One Pointer, Both Sides” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.

What will I learn in “One Pointer, Both Sides”?

How cudaMallocManaged works. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CUDA Academy?

No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “One Pointer, Both Sides” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CUDA Academy lesson?

Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. One Pointer, Both Sides
  2. On-Demand Page Migration
  3. Prefetching with cudaMemPrefetchAsync
  4. Hints via cudaMemAdvise
← Back to CUDA Academy