Choosing Threads per Block
Sensible defaults like 128 and 256.
Choosing Threads per Block is a free CUDA Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A Real Decision
When you launch a kernel, you must pick how many threads per block to use. This small choice affects real performance. 🎚️
Multiples of 32
The GPU runs threads in groups of 32 called warps, so always pick a multiple of 32. Odd sizes waste lanes in a partial warp.
Safe Defaults
Good first guesses are 128 or 256 threads per block. They are multiples of 32 and work well on almost every GPU.
int threadsPerBlock = 256;The Hard Upper Limit
A block can hold at most 1024 threads on current GPUs. Ask for more and the launch simply fails with an error.
Too Few Threads
Very small blocks, like 32, can leave the GPU underused. The scheduler has fewer warps to hide memory latency with.
Too Many Threads
Huge blocks may run out of registers or shared memory, so fewer blocks fit per multiprocessor. Bigger is not always better.
Computing the Block Count
Once threads per block is set, round up the number of blocks so every element is covered, even if it does not divide evenly.
int blocks = (n + threadsPerBlock - 1) / threadsPerBlock;Always Add a Bounds Check
Rounding up means a few extra threads exist. Guard your kernel with an if so they do not touch memory past the array.
if (i < n) out[i] = a[i] + b[i];Let CUDA Suggest a Size
You can ask CUDA for a good block size automatically with the occupancy helper, then tune from its suggestion.
cudaOccupancyMaxPotentialBlockSize(&grid, &block, kernel);Measure, Do Not Guess
The truly best size depends on your kernel and GPU. Start at 256, then benchmark a few values and keep the fastest.
A Practical Rule
For most beginner kernels, 256 threads per block plus a rounded-up block count is a reliable, fast starting point.
Quick Check
Pick the soundest choice for threads per block.
Recap: Sizing Your Blocks
You learned to size blocks: use multiples of 32, default to 256, stay under 1024, round up the block count, and benchmark. 🏁
Frequently asked questions
Is the “Choosing Threads per Block” lesson free?
Yes — the full text of “Choosing Threads per Block” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.
What will I learn in “Choosing Threads per Block”?
Sensible defaults like 128 and 256. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CUDA Academy?
No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Choosing Threads per Block” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CUDA Academy lesson?
Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Thread Hierarchy
- threadIdx, blockIdx, blockDim
- Why Blocks Exist
- Choosing Threads per Block