Hallo GPU: Ihre erste .cu-Datei
Erstellen und führen Sie ein minimales CUDA-Programm aus.
Hallo GPU: Ihre erste .cu-Datei ist eine kostenlose CUDA Academy-Lektion auf CoddyKit. Dies ist Lektion 4 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.
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! 🎓
Häufig gestellte Fragen
Ist die Lektion „Hallo GPU: Ihre erste .cu-Datei“ kostenlos?
Ja — der vollständige Text von „Hallo GPU: Ihre erste .cu-Datei“ 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 „Hallo GPU: Ihre erste .cu-Datei“?
Erstellen und führen Sie ein minimales CUDA-Programm aus. 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 4 von 4.
Wie lange dauert die Lektion „Hallo GPU: Ihre erste .cu-Datei“?
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
- Versionen von Treiber, Runtime und Toolkit
- nvidia-smi wie ein Profi lesen
- Mit nvcc kompilieren
- Hallo GPU: Ihre erste .cu-Datei