Hola, GPU: su primer archivo .cu
Compile y ejecute un programa CUDA mínimo.
Hola, GPU: su primer archivo .cu es una lección gratuita de CUDA Academy en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de CUDA Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de CUDA Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en 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 helloRun It
Now run the program. Seeing Hello from GPU printed means your code truly executed on the device. That is your first GPU output! 🚀
./helloMany 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! 🎓
Preguntas frecuentes
¿La lección «Hola, GPU: su primer archivo .cu» es gratis?
Sí — el texto completo de «Hola, GPU: su primer archivo .cu» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de CUDA Academy, actualiza a CoddyKit PRO. El curso de CUDA Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Hola, GPU: su primer archivo .cu»?
Compile y ejecute un programa CUDA mínimo. Practicas CUDA Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar CUDA Academy?
No se requiere experiencia previa. CUDA Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Hola, GPU: su primer archivo .cu»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de CUDA Academy?
Sí. Cada lección de CUDA Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Versiones del controlador, runtime y toolkit
- Leer nvidia-smi como un profesional
- Compilar con nvcc
- Hola, GPU: su primer archivo .cu