liveBook · Manning

Integer division

As explained in Grokking GPU Programming

Integer division is division whose result is an integer obtained by truncating the fractional part of the quotient.

Integer division drops the fractional part of a quotient. When choosing CUDA blocks for an array, dividing its size by the threads per block can therefore leave elements uncovered if there is a remainder. section 3.14 section 3.9

A leftover needs another block

With 1,000 elements and 256 threads per block, plain division gives 3 blocks—only 768 threads. Ceiling division gives 4, enough to cover the array. For positive counts, use (n + threadsPerBlock - 1) / threadsPerBlock; exact multiples remain unchanged. section 3.9 section 3.14

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

Use this C/C++ expression to round the block count up when a remainder is present. section 3.9

How Grokking GPU Programming covers it

How deep it goes
Explains truncation and derives the ceiling-division adjustment with divisible and nondivisible array sizes.
Examples
Uses seven friends sharing four-seat taxis and arrays of 1,000 or 1,024 elements with 256 threads per block.
What you can do after
Apply it via C/C++ arithmetic to calculate a block count that covers every array element.
How it connects
Connects integer division to ceiling division and CUDA kernel launches.

Also explained in

Compare how 2 books explain integer 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.