Skip to content
Systems programming for what’s next.
— Vex

Graph Functions and GPU Execution ​

A graph fn describes a computation graph candidate; it does not by itself define a manually launched GPU kernel or guarantee GPU execution. The compiler may lower a supported graph through SIR and choose an available path. See the tested graph example.

The thread coordinates, shared memory, and barriers below explain a general GPU algorithm. Vex expresses the computation through graph and tensor operations; these conceptual coordinates are not source-level launch syntax. Use the calibration example to see a complete Vex graph surrounded by ordinary application code.

Thread Hierarchy ​

For a conceptual two-dimensional workgroup of side T, local coordinates lx and ly range from 0 to T - 1, while bx and by identify the workgroup. An output element is at row = by * T + ly, col = bx * T + lx. If an API instead supplies a global coordinate, use it directly: adding the workgroup offset again double-counts it.

Shared Memory / Threadgroup Memory ​

A tiled matrix multiplication can reuse two workgroup-local tiles. For matrices A with shape M×K and B with shape K×N, an implementation must:

  1. Load each A and B tile with bounds checks on both dimensions, using zero for out-of-range elements, including the final partial K tile.
  2. Synchronize every workgroup lane after loading and before computing, and again before overwriting the tiles for the next iteration.
  3. Keep edge lanes participating in both barriers, even when their output row or column is out of range. Only the final C write is conditional on row < M and col < N.

The following complete Python program models those stages sequentially. Its two stages model the data dependencies of the barriers; they do not exercise GPU synchronization, Vex kernel syntax, a GPU compiler, or a device. It checks the tiled result against an independent triple-loop reference, including multiple workgroups and dimensions that are not multiples of 16.

python
def tiled(a, b, m, n, k, tile=16):
    out = [[0 for _ in range(n)] for _ in range(m)]
    for by in range((m + tile - 1) // tile):
        for bx in range((n + tile - 1) // tile):
            sums = [[0 for _ in range(tile)] for _ in range(tile)]
            for base in range(0, k, tile):
                # All lanes finish these loads before any lane computes.
                a_tile = [[
                    a[by * tile + ly][base + lx]
                    if by * tile + ly < m and base + lx < k else 0
                    for lx in range(tile)] for ly in range(tile)]
                b_tile = [[
                    b[base + ly][bx * tile + lx]
                    if base + ly < k and bx * tile + lx < n else 0
                    for lx in range(tile)] for ly in range(tile)]
                for ly in range(tile):
                    for lx in range(tile):
                        for inner in range(tile):
                            sums[ly][lx] += a_tile[ly][inner] * b_tile[inner][lx]
                # All lanes finish computing before either tile is replaced.
            for ly in range(tile):
                for lx in range(tile):
                    row, col = by * tile + ly, bx * tile + lx
                    if row < m and col < n:
                        out[row][col] = sums[ly][lx]
    return out


def reference(a, b, m, n, k):
    return [[sum(a[row][inner] * b[inner][col] for inner in range(k))
             for col in range(n)] for row in range(m)]


for m, n, k in [(1, 1, 1), (16, 16, 16), (17, 19, 23),
                (32, 17, 1), (33, 35, 37), (3, 5, 0)]:
    a = [[(row * 3 + inner) % 7 - 3 for inner in range(k)] for row in range(m)]
    b = [[(inner * 5 + col) % 11 - 5 for col in range(n)] for inner in range(k)]
    assert tiled(a, b, m, n, k) == reference(a, b, m, n, k)
    print(f"OK {m}x{n}x{k}")

Save it as tiled-reference.py and run python3 tiled-reference.py. Integer inputs allow exact comparison here; floating-point GPU results may require an appropriate tolerance and separate numerical validation.

Graph Function Restrictions ​

Graph lowering supports a subset of Vex, and restrictions depend on the compiler version and target. Do not assume that an arbitrary function, FFI call, or asynchronous body can be lowered as a graph. A graph function can return a value, as the array addition example demonstrates.

SIR Pipeline for Graph Functions ​

Supported graph bodies can be lowered from HIR to SIR and planned for an available backend. A backend's presence in the source tree does not establish that every operation or device is supported. Consult the SIR pipeline and backend reference, then validate the exact target.

Best Practices ​

Start with a verified graph example, check the compiler's diagnostics and the actual execution path, and compare output with a reference on your target. Measure performance only after correctness is established; tile sizes and memory behavior are device- and implementation-dependent.

Released under the MIT License.