0Pricing
CUDA Academy · Aula

Um ponteiro, os dois lados

Veja como cudaMallocManaged funciona.

Um ponteiro, os dois lados é uma aula grátis de CUDA Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de CUDA Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de CUDA Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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

Perguntas Frequentes

A aula “Um ponteiro, os dois lados” é grátis?

Sim — o texto completo de “Um ponteiro, os dois lados” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de CUDA Academy, atualize para CoddyKit PRO. O curso de CUDA Academy inclui 4 aulas no total.

O que vou aprender em “Um ponteiro, os dois lados”?

Veja como cudaMallocManaged funciona. Você pratica CUDA Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar CUDA Academy?

Nenhuma experiência prévia é necessária. CUDA Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.

Quanto tempo leva a aula “Um ponteiro, os dois lados”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de CUDA Academy?

Sim. Cada aula de CUDA Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Um ponteiro, os dois lados
  2. Migração de páginas sob demanda
  3. Pré-busca com cudaMemPrefetchAsync
  4. Dicas por meio de cudaMemAdvise
← Voltar para CUDA Academy