From a matrix coordinate to a memory offset

CuTe's Layout pairs a shape, which defines the valid logical coordinates, with a stride, which tells you how far each coordinate step moves in memory. The same representation covers packed matrices, padded rows, and matrices stored tile by tile. Start with a \(2\times3\) matrix and follow the mapping as its storage changes.

For a zero-based coordinate \((r,c)\), \(r\) is the row and \(c\) is the column. The matrix has six logical coordinates, regardless of how its elements are stored.

The six logical coordinates of a 2-by-3 matrix, from (0, 0) through (1, 2).

A layout in these examples returns an element offset from the base pointer. For a C++ pointer T* base, base + offset is the address of the element. If you work in bytes instead, multiply the element offset by sizeof(T). For example, element offset 5 is 20 bytes from the base of a 32-bit float array.

Row-major storage

Pack each row's three elements together. Increasing \(c\) by one moves one element. Increasing \(r\) by one moves three elements to the same column of the next row.

\[ L_{\mathrm{row}}(r,c)=3r+c \]

Row-major element offsets. The first row contains 0, 1, 2 and the second contains 3, 4, 5.

Column-major storage

Pack each column's two elements together. A row step now moves one element, and a column step moves two. The coordinate \((1,0)\) maps to offset 1 here and offset 3 in the row-major buffer.

\[ L_{\mathrm{col}}(r,c)=r+2c \]

Column-major element offsets. The first row contains 0, 2, 4 and the second contains 1, 3, 5.

Row-major storage with padding

Image buffers and other vectorized workloads can use a row pitch larger than the logical width. Give each row four slots while keeping three logical columns. The row stride becomes 4, so \((1,0)\) maps to offset 4 and \((1,2)\) maps to offset 6. Offsets 3 and 7 are padding slots in this buffer.

\[ L_{\mathrm{pad}}(r,c)=4r+c \]

Padded row-major storage. The valid offsets are 0, 1, 2 and 4, 5, 6. Slots 3 and 7 are padding.

When the row pitch is chosen for byte alignment, account for both element size and base-pointer alignment. A stride of four elements is a 16-byte pitch for 32-bit floats.

One notation for the three storage orders

Write a layout as Shape:Stride. In the flat two-dimensional case, \((h,w):(s_r,s_c)\) means \(0\le r<h\) and \(0\le c<w\), with \(L(r,c)=r s_r+c s_c\). The dimensions bound valid coordinates. The strides are coefficients in the offset calculation.

Storage orderShapeStrideOffset at \((1,0)\)
Packed row-major(2,3)(3,1)3
Packed column-major(2,3)(1,2)1
Padded row-major(2,3)(4,1)4

The row stride equals the width in the first case because the rows are packed. The padded case shows why width and stride need separate fields. A kernel can use the same coordinate calculation for all three buffers by taking its strides from the layout.

Nested layouts for tiled matrices

Tiled matrix multiplication partitions the operands into blocks, multiplies the blocks along the shared dimension, and accumulates the partial products into each output tile. A layout can describe storage in which each tile occupies a contiguous region. It can also expose tile coordinates as a separate level for partitioning work.

Take a \(4\times6\) matrix broken into \(2\times2\) tiles. The figure shows element offsets. Elements within each tile are row-major, and the tiles themselves are ordered down each tile column before moving to the next tile column.

Offsets in a 4-by-6 matrix stored as 2-by-2 tiles. The first row of global offsets is 0, 1, 8, 9, 16, 17.

Write \((r_0,c_0)\) for the position inside a tile and \((r_1,c_1)\) for the tile's position in the \(2\times3\) grid. The two levels have different strides.

  • Inside the tile, moving down one local row advances by 2 elements. Moving across one local column advances by 1. The inner layout is (2,2):(2,1).
  • Across the tile grid, moving down one tile row advances by 4 elements. Moving across one tile column skips the two four-element tiles in the column, advancing by 8. The outer layout is (2,3):(4,8).

Every offset now has four terms.

\[ L=2r_0+4r_1+c_0+8c_1 \]

This mapping needs more than two constant strides over the global row and column coordinates. Along global row 0, for example, the offsets begin 0, 1, 8, 9. One column step advances by 1 inside a tile and by 7 across the tile boundary.

Group by matrix axis

Keep a row mode and a column mode. Within each mode, place the coordinate inside the tile first and the tile-grid coordinate second.

\[ L_1=((2,2),(2,3)):((2,4),(1,8)) \]

The argument now has the form \(((r_0,r_1),(c_0,c_1))\). To derive it from a global coordinate \((r,c)\), the local components are \(r_0=r\bmod 2\) and \(c_0=c\bmod 2\). The outer components are \(r_1=\lfloor r/2\rfloor\) and \(c_1=\lfloor c/2\rfloor\).

For global coordinate \((2,3)\), this gives \(((0,1),(1,1))\). The element offset is \(2\cdot0+4\cdot1+1\cdot1+8\cdot1=13\).

Group by position inside the tile and by tile index

When the code works on one whole tile at a time, put both local coordinates in the first mode and both tile-grid coordinates in the second mode.

\[ L_2=((2,2),(2,3)):((2,1),(4,8)) \]

The argument is now \(((r_0,c_0),(r_1,c_1))\). The grouping changes the argument structure. Global coordinate \((0,3)\) becomes \(((0,0),(1,1))\) for \(L_1\) and \(((0,1),(0,1))\) for \(L_2\). Both return 9 because each coordinate component is paired with the same stride in either representation.

Select one tile

With the tile index in a separate mode, hold \((r_1,c_1)\) fixed and keep the local mode. For tile \((1,2)\), the base offset is \(4\cdot1+8\cdot2=20\). The retained \(2\times2\) layout has strides \((2,1)\), giving relative offsets 0, 1, 2, and 3. Adding the tile base gives the following offsets in the original buffer.

\[ \begin{bmatrix} 20 & 21\\ 22 & 23 \end{bmatrix} \]

In CuTe C++, use cute::_ to keep a mode. slice_and_offset returns the retained layout and the base offset separately. When slicing a CuTe tensor, CuTe carries this offset into the tensor's data iterator.

#include <cute/layout.hpp>
#include <cassert>

int main() {
  using namespace cute;

  auto layout = make_layout(
      make_shape(make_shape(2, 2), make_shape(2, 3)),
      make_stride(make_stride(2, 1), make_stride(4, 8)));

  auto local = make_coord(0, 1);
  auto outer = make_coord(1, 1);
  assert(layout(make_coord(local, outer)) == 13);

  auto slice_result = slice_and_offset(
      make_coord(cute::_, make_coord(1, 2)), layout);
  auto tile = get<0>(slice_result);
  auto tile_base = get<1>(slice_result);
  assert(tile_base == 20);
  assert(tile_base + tile(make_coord(1, 1)) == 23);
}

Two more storage patterns

You can nest just one axis. In the first example below, each four-row by two-column tile is row-major. A global row step advances by 2, an in-tile column step advances by 1, and an outer column step advances by 8. This gives \((4,(2,2)):(2,(1,8))\). For global row 1 and global column 2, the nested coordinate is \((1,(0,1))\) and the offset is \(2+8=10\).

A 4-by-4 matrix in two 4-by-2 tiles. The second row has element offsets 2, 3, 10, 11.

You can also change the order both inside and across tiles. In the second example, each \(2\times2\) tile is packed column-major, while the tile grid is packed row-major. Grouping by axis gives \(((2,2),(2,2)):((1,8),(2,4))\). For global row 2 and global column 3, the axis-grouped coordinate is \(((0,1),(1,1))\). The offset is \(0+8+2+4=14\).

A 4-by-4 matrix with column-major tiles in a row-major tile grid. Global row 2 has offsets 8, 10, 12, 14.

To read a nested layout, match the coordinate structure to the shape, then multiply each coordinate leaf by the corresponding stride and add the products. The grouping tells you which coordinates you can hold fixed together, for example to select a tile or retain an entire matrix axis.

Further reading