liveBook · Manning

Ceiling division

As explained in Grokking GPU Programming

Ceiling division is division rounded upward to the smallest integer greater than or equal to the exact quotient.

When dividing a workload into fixed-size groups, ordinary integer division rounds down and can leave items uncovered. Ceiling division rounds up to the next whole group when there is a remainder. In CUDA C/C++, calculate the block count with (n + threadsPerBlock - 1) / threadsPerBlock. section 3.14 section 3.9

    int numBlocks = (n + threadsPerBlock - 1) / threadsPerBlock;    #A

Use this C/C++ expression to calculate the number of blocks. section 3.9

Covering an array

For 1,000 elements and 256 threads per block, the formula gives 4 blocks; plain division gives only 3, leaving 232 elements unprocessed. For 1,024 elements, it still gives 4 blocks. If n is 0, the formula gives 0 blocks, so skip the CUDA launch; CUDA rejects a zero-block launch. section 3.14 section 3.9

How Grokking GPU Programming covers it

How deep it goes
Derives the formula with remainder and exact-multiple examples, and notes empty-input handling and an alternative formula’s zero-case failure.
Examples
Works through 1,000 elements with blocks of 256, an exact multiple of 1,024, and a four-seat taxi analogy.
What you can do after
Apply the C/C++ formula to choose a CUDA block count that covers every array element.
How it connects
Links integer division to CUDA kernel launches, block sizing, and bounds checks.

Also explained in

Compare how 2 books explain ceiling division →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking GPU Programming. The section links point to the parts of the book it draws on.