GPU by Example
14 / 16

Tile programming

8 min readChapter 14 of 16

Everything in this book so far is SIMT: you write per-thread code and decide what each thread does. CUDA 13 adds a second model. You write per-block code that operates on whole multidimensional arrays called tiles, and the compiler decides how to distribute the work across the block's threads.

Two ways to write a kernel#

SIMT (Chapters 2–13)Tile (this chapter)
You writeCode for one threadCode for one block
IndexingthreadIdx, blockIdxct::bid() only
ParallelismExplicit, per elementImplicit, per tile operation
DivergenceYour problemCompiler's problem
MemoryYou stage into shared memoryCompiler chooses registers/shared
ControlTotalHigher-level, some restrictions

A tile kernel is launched on a grid exactly like a SIMT kernel, but the second launch argument must be 1 — the compiler chooses the threads per block internally.

launch.cuCUDA C++
vec_add<<<num_blocks, 1>>>(a, b, out);

The types#

Tiles have a compile-time shape and element type. Every dimension must be a power of two:

CUDA C++
#include "cuda_tile.h"
namespace ct = cuda::tiles;

using f32x8   = ct::tile<float, ct::shape<8>>;      // 8 floats
using f32x4x4 = ct::tile<float, ct::shape<4, 4>>;   // 4x4 floats

Tiles are values, not pointers. Copying a tile copies its elements; operations produce new tiles. The compiler decides whether the data lives in registers, shared memory, or somewhere else. You never allocate or free a tile.

A tile vector add#

This is the smallest complete tile kernel. It adds 128-element chunks, one per block.

vec_add_tile.cuCUDA C++
#include "cuda_tile.h"

__tile_global__ void vec_add(float* __restrict__ a,
                             float* __restrict__ b,
                             float* __restrict__ out) {
    namespace ct = cuda::tiles;
    using namespace ct::literals;

    a   = ct::assume_aligned(a,   16_ic);
    b   = ct::assume_aligned(b,   16_ic);
    out = ct::assume_aligned(out, 16_ic);

    // Attach a shape to each raw pointer.
    auto aSpan = ct::tensor_span{a,   ct::extents{128_ic}};
    auto bSpan = ct::tensor_span{b,   ct::extents{128_ic}};
    auto oSpan = ct::tensor_span{out, ct::extents{128_ic}};

    // Partition each span into fixed 8-element tiles.
    auto aView = ct::partition_view{aSpan, ct::shape{8_ic}};
    auto bView = ct::partition_view{bSpan, ct::shape{8_ic}};
    auto oView = ct::partition_view{oSpan, ct::shape{8_ic}};

    int  bx    = ct::bid().x;         // this block's tile-space index
    auto aTile = aView.load(bx);      // load tile bx
    auto bTile = bView.load(bx);
    oView.store(aTile + bTile, bx);   // add whole tiles, store the result
}

Read it as block-level code:

  • __tile_global__ marks the entry point, the tile analogue of __global__.
  • ct::tensor_span{ptr, extents} pairs a raw pointer with a shape.
  • ct::partition_view{span, shape} cuts that array into a grid of 8-element tiles.
  • ct::bid().x is the block's position — no threadIdx.
  • load, +, and store operate on all 8 elements at once. There is no loop over elements.

Handling edges#

Real arrays are not always a multiple of the tile size. Tile-space loads come in two flavours: unmasked (undefined on an out-of-bounds tile) and masked (zero-filled reads, discarded writes).

edge_safe.cuCUDA C++
__tile_global__ void edge_safe(float* __restrict__ in,
                               float* __restrict__ out, int n) {
    namespace ct = cuda::tiles;
    using namespace ct::literals;

    in  = ct::assume_aligned(in,  16_ic);
    out = ct::assume_aligned(out, 16_ic);

    auto inView  = ct::partition_view{
        ct::tensor_span{in, ct::extents{n}}, ct::shape{128_ic}};
    auto outView = ct::partition_view{
        ct::tensor_span{out, ct::extents{n}}, ct::shape{128_ic}};

    int  bx   = ct::bid().x;
    auto tile = inView.load_masked(bx);     // OOB lanes become 0
    outView.store_masked(tile, bx);         // OOB writes are dropped
}

Launch it with a ceiling-divided grid:

CUDA C++
int blocks = (n + 127) / 128;
edge_safe<<<blocks, 1>>>(in, out, n);

The masked variant is slightly slower than the unmasked one. Use load()/store() when the size divides evenly, and release builds with load_masked()/store_masked() when it does not.

Tiles as values#

Factories build tiles without touching memory:

CUDA C++
auto z = ct::zeros<f32x4x4>();
auto o = ct::ones<f32x4x4>();
auto f = ct::full<f32x4x4>(3.14f);
auto s = ct::iota<i32x8>();       // 0,1,2,3,4,5,6,7

Because tiles are immutable values, expressions compose safely:

CUDA C++
auto y = ct::full<f32x8>(2.0f) * x + ct::ones<f32x8>();

The compiler fuses these into registers/shared memory as it sees fit. You do not manage the storage.

Tiled matrix multiply#

The payoff is matrix multiply. One block computes a 32×32 tile of C, looping over K tiles with an FP32 accumulator:

gemm_tile.cuCUDA C++
#include <cstddef>
#include <cuda_fp16.h>
#include "cuda_tile.h"

__tile_global__ void gemm(const __half* __restrict__ A,
                          const __half* __restrict__ B,
                          float* __restrict__ C,
                          std::size_t M, std::size_t K, std::size_t N) {
    namespace ct = cuda::tiles;
    using namespace ct::literals;
    using f32_acc = ct::tile<float, ct::shape<32, 32>>;

    A = ct::assume_aligned(A, 16_ic);
    B = ct::assume_aligned(B, 16_ic);
    C = ct::assume_aligned(C, 16_ic);

    constexpr auto tm = 32_ic;
    constexpr auto tn = 32_ic;
    constexpr auto tk = 16_ic;

    auto aView = ct::partition_view{ct::tensor_span{A, ct::extents{M, K}},
                                    ct::shape{tm, tk}};
    auto bView = ct::partition_view{ct::tensor_span{B, ct::extents{K, N}},
                                    ct::shape{tk, tn}};
    auto cView = ct::partition_view{ct::tensor_span{C, ct::extents{M, N}},
                                    ct::shape{tm, tn}};

    auto [bx, by, bz] = ct::bid();
    auto acc = ct::full<f32_acc>(0.0f);

    std::size_t num_k = (K + tk - 1) / tk;
    for (auto k : ct::irange(std::size_t{0}, num_k)) {
        acc = ct::mma(aView.load_masked(bx, k),   // zero-pad partial K tile
                      bView.load_masked(k, by),
                      acc);                       // acc += a @ b
    }
    cView.store_masked(acc, bx, by);              // drop OOB edge lanes
}

Everything that was manual in the SIMT version is gone: no shared-memory arrays, no __syncthreads(), no per-thread accumulator. ct::mma is the matrix multiply-accumulate, and the compiler maps it onto the hardware's matrix units.

The Python version#

The same model exists in Python as cuTile. The vector add collapses to four lines:

vec_add.pyPython
import cuda.tile as ct

@ct.kernel
def vec_add(a, b, c, TILE: ct.Constant[int]):
    bx = ct.bid(0)
    a_tile = ct.load(a, index=(bx,), shape=(TILE,))
    b_tile = ct.load(b, index=(bx,), shape=(TILE,))
    ct.store(c, index=(bx,), tile=a_tile + b_tile)
Python
ct.launch(stream, grid, vec_add, (a, b, c, 128))

ct.Constant[int] embeds the tile size in the compiled kernel; shapes must be compile-time constants in both languages.

SIMT or tiles?#

Use tiles when:

  • Your algorithm is naturally expressed as elementwise, reduction, or matmul operations on rectangular blocks.
  • You want portability across GPU generations without retuning thread mappings.
  • You are already on CUDA 13+ and a supported GPU.
  • The compiler's choice of data placement is good enough (it usually is for dense linear algebra).

Stay with SIMT when:

  • You need precise control over registers, shared memory, and warp behaviour.
  • The access pattern is irregular (sparse, graph, particle).
  • You are targeting older hardware or toolchains.
  • You need warp-level primitives or inline PTX.

Both models coexist in one file and one application. The choice is per kernel.

Requirements and restrictions#

  • CUDA Toolkit 13.0 or newer, with cuda_tile.h.
  • A GPU supported by the tile backend; check the CUDA Tile documentation for the current list.
  • Tile dimensions must be powers of two and known at compile time.
  • Tiles cannot be passed as kernel arguments; they exist only inside tile code.
  • __tile__ functions cannot call __device__ functions, and vice versa (currently).
  • Control flow is per block: conditionals and loops use scalar values, and there is no warp divergence to reason about.
  • Some constructs are restricted — for example, returning from inside a loop is not allowed.

Exercises#

  • Change the tile shape from 8 to 16 in the vector add. What changes in the launch? What happens at the array boundary?
  • Write a tile kernel that computes out = a * b + c (fused multiply-add) for 128-element chunks.
  • In the GEMM, why is the accumulator float even though the inputs are __half? What would change if you accumulated in __half?
  • Rewrite the SIMT tiled matmul from Chapter 8 as a tile kernel. Which lines disappear?
  • Read the tile kernel's PTX with nvcc -ptx. Can you identify the matrix instructions?

Next: what changes when one GPU is not enough.