単純な行列乗算カーネル
2次元インデックスによる基準実装とその限界を学びます。
「単純な行列乗算カーネル」はCoddyKit上の無料CUDA Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはCUDA Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 CUDA Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Matrix Multiply, GPU Style
Matrix multiplication is the heart of graphics and AI. Today you build a naive GPU version first, then learn why it leaves speed on the table.
The Math in One Line
Each output cell C[row][col] is a dot product: multiply a full row of A by a full column of B and sum the results. 🧮
C[row][col] = sum over k of A[row][k] * B[k][col]One Thread per Output
The simplest plan gives each thread one output element of C. Thousands of cells get computed at the same time across the GPU.
A 2D Grid of Threads
Since C is a 2D grid, you launch threads in two dimensions. The x index maps to a column and the y index maps to a row.
dim3 threads(16, 16);
dim3 blocks((N+15)/16, (N+15)/16);Finding This Thread's Cell
Inside the kernel, each thread computes its own row and col from its block and thread indices, just like 1D indexing but on both axes.
int row = blockIdx.y*blockDim.y + threadIdx.y;
int col = blockIdx.x*blockDim.x + threadIdx.x;The Bounds Check
Grids round up, so some threads fall outside the matrix. Guard with if (row < N && col < N) before you touch memory.
if (row < N && col < N) {
// safe to compute
}The Inner Loop
Each thread runs a loop over k, accumulating products into a local sum. That local variable lives in a fast register.
float sum = 0.0f;
for (int k = 0; k < N; ++k)
sum += A[row*N+k] * B[k*N+col];Writing the Result
After the loop finishes, the thread stores its accumulated sum into C exactly once. One thread, one clean write.
C[row*N + col] = sum;Row-Major Flattening
The matrix is a flat 1D array, so you index it as row*N + col. Getting this layout right is half the battle in matmul.
Why It Works, But Slowly
This kernel is correct and easy to read, but every thread reads its row and column straight from global memory, the slowest space.
The Hidden Cost
Neighboring threads re-read the same A rows and B columns over and over. That wasted memory traffic is exactly what tiling will fix next.
Quick Check
Think about how the naive kernel maps work to threads.
Recap
You mapped one thread to one output cell, looped over k from global memory, and saw the redundant reads. Next you cut that traffic with tiling. 🚀
よくある質問
「単純な行列乗算カーネル」レッスンは無料ですか?
はい。「単純な行列乗算カーネル」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、CUDA Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 CUDA Academyコースには全4レッスンが含まれています。
「単純な行列乗算カーネル」で何を学びますか?
2次元インデックスによる基準実装とその限界を学びます。 ブラウザで直接実行するハンズオンコードでCUDA Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
CUDA Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのCUDA Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「単純な行列乗算カーネル」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このCUDA Academyレッスンでコードを書いて実行できますか?
はい。すべてのCUDA Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 単純な行列乗算カーネル
- 内積をタイル化する
- タイルのフェーズをループする
- 高速化の効果を測定する