liveBook · Manning

Matrix

As explained in Grokking GPU Programming

A matrix is a two-dimensional arrangement of elements organized by rows and columns.

A matrix arranges elements in rows and columns, but its contents are stored in a one-dimensional memory array. In C and C++, rows are stored consecutively: this is row-major order. Column-major order stores each column consecutively instead. The layout determines which elements are near each other in memory. section 10.10

Figure 10.8 Row-major layout. Consecutive 1D indices fill each row before moving to the next. Each cell shows the 1D array index where that matrix element lives: row 0 holds [0] through [m−1], row 1 holds [m] through [2m−1], and so on, ending at [nm−1] in the bottom-right. The numbered arrows give the traversal order: across the columns first (1), then down to the next row (2).

See how consecutive memory indices fill one row before moving to the next. section 10.10

In CUDA, the layout and the threads’ access pattern together affect memory performance:

  • With row-major data, threads accessing consecutive columns read nearby elements, allowing coalesced memory access. section 10.10
  • A sumRows kernel assigns one thread to each row; at each load step, warp threads access different rows and widely separated addresses. A sumCols kernel assigns one thread to each column, so threads access consecutive elements of a row. section 10.10
  • CUDA does not enforce a matrix layout. Check the convention used by the code or library that created the data. section 10.10

How Grokking GPU Programming covers it

How deep it goes
Compares row-major and column-major storage and traces warp accesses through row and column sums.
Examples
Uses row-sum and column-sum kernels to contrast strided and coalesced accesses.
What you can do after
Evaluate matrix traversal choices by checking whether warp threads access nearby addresses.
How it connects
Connects matrices with CUDA, row-major order, column-major order, and memory coalescing.

Also explained in

Compare how 3 books explain matrix →

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.