Tile programming
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 write | Code for one thread | Code for one block |
| Indexing | threadIdx, blockIdx | ct::bid() only |
| Parallelism | Explicit, per element | Implicit, per tile operation |
| Divergence | Your problem | Compiler's problem |
| Memory | You stage into shared memory | Compiler chooses registers/shared |
| Control | Total | Higher-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.
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:
#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 floatsTiles 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.
#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().xis the block's position — nothreadIdx.load,+, andstoreoperate 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).
__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:
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:
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,7Because tiles are immutable values, expressions compose safely:
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:
#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:
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)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
floateven 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.