0Pricing
CUDA Academy · Aula

Olá, GPU: seu primeiro arquivo .cu

Compile e execute um programa CUDA mínimo.

Olá, GPU: seu primeiro arquivo .cu é uma aula grátis de CUDA Academy no CoddyKit. Esta é a aula 4 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.

Your First .cu File

Time to build something real. A CUDA program lives in a .cu file, which can hold both CPU and GPU code side by side. 🎉

Include the Header

Most CUDA samples start by pulling in standard headers. The CUDA runtime header is included automatically by nvcc, so iostream is often enough.

#include <cstdio>

Marking a Kernel

A function that runs on the GPU is a kernel. You mark it with the __global__ qualifier so nvcc compiles it for the device.

__global__ void hello() {
}

Printing from the GPU

Inside the kernel you can call printf just like on the CPU. Each GPU thread that runs it produces its own line of output.

__global__ void hello() {
  printf("Hello from GPU\n");
}

The Launch Syntax

To run a kernel you use the triple-angle-bracket launch. The numbers inside choose how many blocks and threads will execute it.

hello<<<1, 1>>>();

Launches Are Asynchronous

A kernel launch returns immediately while the GPU works in the background. The CPU keeps going, which is powerful but can be surprising at first.

Waiting for the GPU

Because of that, you must synchronize before the program ends, or the printf output may never appear on screen.

cudaDeviceSynchronize();

Putting It Together

Your main function launches the kernel and then waits. That is the entire skeleton of a working CUDA program.

int main() {
  hello<<<1, 1>>>();
  cudaDeviceSynchronize();
}

Compile It

Save the file as hello.cu and build it with nvcc, naming the output program with the -o flag.

nvcc hello.cu -o hello

Run It

Now run the program. Seeing Hello from GPU printed means your code truly executed on the device. That is your first GPU output! 🚀

./hello

Many Threads at Once

Bump the launch to many threads and you will see the greeting printed several times, one line per thread. That is real parallelism in action.

hello<<<1, 8>>>();

Quick Check

One detail makes or breaks this program.

Recap

You wrote a real CUDA program: a __global__ kernel, a triple-bracket launch, and a sync. Compile with nvcc, run it, and the GPU greets you. Congrats! 🎓

Perguntas Frequentes

A aula “Olá, GPU: seu primeiro arquivo .cu” é grátis?

Sim — o texto completo de “Olá, GPU: seu primeiro arquivo .cu” é 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 “Olá, GPU: seu primeiro arquivo .cu”?

Compile e execute um programa CUDA mínimo. 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 4 de 4.

Quanto tempo leva a aula “Olá, GPU: seu primeiro arquivo .cu”?

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. Versões do controlador, ambiente de execução e kit de ferramentas
  2. Leia nvidia-smi como um profissional
  3. Compile com nvcc
  4. Olá, GPU: seu primeiro arquivo .cu
← Voltar para CUDA Academy