🏗️ Chapter 3: The Structure of Your GPU Army
💡 Story: An army isn't just thousands of random soldiers. It's organized! Soldiers form Squads (Threads → Blocks), squads form Platoons (Blocks), and platoons form a Division (Grid). This hierarchy makes commanding 10,000+ soldiers manageable!
The 3-Level CUDA Hierarchy:
Why have blocks at all? Why not just one giant block?
#include <stdio.h>
int main() {
int numBlocks = 2, threadsPerBlock = 3;
int total = numBlocks * threadsPerBlock;
printf("=== CUDA Thread Hierarchy ===\n");
printf("GRID: %d blocks x %d threads = %d total threads\n", numBlocks, threadsPerBlock, total);
for (int b = 0; b < numBlocks; b++) {
printf(" Block %d:\n", b);
for (int t = 0; t < threadsPerBlock; t++) {
int globalId = t + b * threadsPerBlock;
printf(" Thread %d (Global ID: %d)\n", t, globalId);
}
}
return 0;
}