カーネル内のprintf
デバイススレッドからの出力を確認します。
「カーネル内のprintf」はCoddyKit上の無料CUDA Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはCUDA Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 CUDA Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Printing from the GPU
Believe it or not, you can call printf right inside a kernel. It is the simplest way to peek at what your threads are doing. 👀
__global__ void hi() {
printf("Hello from the GPU\n");
}Every Thread Prints
Remember the kernel runs in every thread, so a single printf line fires once per thread. Launch 256 threads and you get 256 lines.
hi<<<1, 256>>>(); // 256 hellosIdentify Each Thread
Include the threadIdx in your message so you can tell threads apart. Otherwise the output is just a wall of identical lines.
printf("thread %d\n", threadIdx.x);Output Order Is Not Fixed
Threads run in parallel, so the printed order is unpredictable. Do not rely on lines arriving in index sequence.
Format Strings Work
Device printf supports the usual format specifiers like %d, %f, and %s. It feels just like host printf.
printf("i=%d val=%f\n", i, x[i]);Output Goes to a Buffer
Device output is staged in a GPU buffer and flushed to your console later, not the instant printf runs. That is normal.
You Must Wait to See It
Because the launch is async, you will not see prints until the GPU finishes. Call cudaDeviceSynchronize to flush them.
hi<<<1, 4>>>();
cudaDeviceSynchronize();Guard Heavy Printing
Printing from millions of threads floods the buffer. Guard it so only thread 0 prints, or only a few do.
if (threadIdx.x == 0) printf("block done\n");Great for Quick Debugging
printf is your fastest debugging tool: drop one in to check an index or a value, confirm the bug, then remove it.
printf("i=%d should be < n=%d\n", i, n);It Slows Kernels Down
Heavy printing hurts performance badly. Use it to find a problem, then delete it before you measure real speed.
Old GPUs May Differ
Device printf needs a reasonably modern compute capability (2.0 and up). Almost every current GPU supports it just fine.
Quick Check
Check what you know about device printf.
Recap: Kernel printf
Use printf to peek inside threads, add threadIdx to tell them apart, sync to flush, and remove it before timing. Handy tool! 🎉
よくある質問
「カーネル内のprintf」レッスンは無料ですか?
はい。「カーネル内のprintf」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、CUDA Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 CUDA Academyコースには全4レッスンが含まれています。
「カーネル内のprintf」で何を学びますか?
デバイススレッドからの出力を確認します。 ブラウザで直接実行するハンズオンコードでCUDA Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
CUDA Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのCUDA Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「カーネル内のprintf」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このCUDA Academyレッスンでコードを書いて実行できますか?
はい。すべてのCUDA Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- カーネルの構造
- 三重山括弧による起動
- カーネル内のprintf
- cudaDeviceSynchronizeの仕組み