GPUのHello World:最初の.cuファイル
最小構成のCUDAプログラムをビルドして実行します。
「GPUのHello World:最初の.cuファイル」はCoddyKit上の無料CUDA Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはCUDA Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 CUDA Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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! 🎓
よくある質問
「GPUのHello World:最初の.cuファイル」レッスンは無料ですか?
はい。「GPUのHello World:最初の.cuファイル」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、CUDA Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 CUDA Academyコースには全4レッスンが含まれています。
「GPUのHello World:最初の.cuファイル」で何を学びますか?
最小構成のCUDAプログラムをビルドして実行します。 ブラウザで直接実行するハンズオンコードでCUDA Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
CUDA Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのCUDA Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「GPUのHello World:最初の.cuファイル」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このCUDA Academyレッスンでコードを書いて実行できますか?
はい。すべてのCUDA Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ドライバー、ランタイム、Toolkitのバージョン
- nvidia-smiを使いこなす
- nvccでコンパイルする
- GPUのHello World:最初の.cuファイル