Le noyau matmul naïf
Une base indexée en 2D et ses limites.
Le noyau matmul naïf est une leçon CUDA Academy gratuite sur CoddyKit. Ceci est la leçon 1 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage CUDA Academy, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours CUDA Academy comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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. 🚀
Questions Fréquemment Posées
La leçon « Le noyau matmul naïf » est-elle gratuite ?
Oui — le texte complet de « Le noyau matmul naïf » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours CUDA Academy, passe à CoddyKit PRO. Le cours CUDA Academy comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Le noyau matmul naïf » ?
Une base indexée en 2D et ses limites. Tu pratiques CUDA Academy avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer CUDA Academy ?
Aucune expérience préalable n'est requise. CUDA Academy sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 1 sur 4.
Combien de temps prend la leçon « Le noyau matmul naïf » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon CUDA Academy ?
Oui. Chaque leçon CUDA Academy inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Le noyau matmul naïf
- Tuiler le produit scalaire
- Parcourir les phases des tuiles
- Mesurer le gain de vitesse