Skip to main content

Tutorial

The tutorial teaches Tungsten and Paint, and how to think about the machine they program. It has two parts:

  • The main sequence: twelve lessons, in order. Together they cover the ideas every program uses:
    • how a PE computes;
    • how PEs exchange data;
    • when a transfer waits;
    • how a PE reacts to messages;
    • how rows and grids of PEs work together.
  • Extension topics: optional, and in no order. Each covers a feature or pattern that some programs need. Each topic starts with the lessons and topics it needs.

Every lesson and topic is a complete program. It builds, runs in the simulator and checks its own result.

The chapters, in order​

Read the four main groups in this order. The extension groups have no order: read a topic once you have read everything on its Needs line.

GroupWhat it teaches
Tutorial I – Single-PE ProgrammingLessons 1-3: the build-and-test loop, ordinary Tungsten code, and how a PE runs a loop and what it costs
Tutorial II – Communication Between PEsLessons 4-7: sockets, colors and routes; concurrent activities and FIFOs; blocking and deadlock; tasks started by messages
Tutorial III – Spatial ProgrammingLessons 8-10: one kernel on a row, a wavefront, broadcast and reduce, and switches
Tutorial IV – Whole-Grid ApplicationsLessons 11-12: a grid of blocks with halo exchange, and a complete iterative solver
Collective CommunicationThe fabric adder, self-restoring switch protocols, a barrier and a broadcast from switch positions, and collectives over messages
RoutingMessage passing, variable-length exchanges, eight-neighbour stencils, filters and fan-out, and a loop through every PE
NumericsPrecision and exact references, bit-level and wide integers, and hardware random numbers
Single-PE Programming TechniquesVector-step loops, memory placement and budget, and locks, until conditions and timers
AlgorithmsMatrix multiplication, sorting networks, physics on a grid of points, and command streams
Layouts and DebuggingLarger layouts in Paint, and debugging and profiling a multi-PE run
Write your own exampleA guide, with no program of its own: setting up, building and testing a program of your own

The main sequence​

#LessonYou will be able to
1Hello, wafer!build, run and check a one-PE program, and read what the build produced
2Values, control flow and functionswrite ordinary code with types, if, while and functions in a module, and load input data
3How a PE runs a loopmeasure a loop in cycles and write loops that run as vector instructions
4Sockets, wavelets, colors and routessend data between PEs, route it, and multicast it
5One core, many activitiesrun transfers concurrently with parallel, and buffer them in a FIFO
6Blocking, buffering and deadlocksay when a send or receive waits, recognize a deadlock, and remove it
7Taskswrite handlers that run when a message arrives
8Space and time: a wavefrontrun one kernel on a row with per-PE data, and map a recurrence onto PEs and steps
9Broadcast and reduce along a rowcompute on a passing stream, and return a result to every PE
10Switcheslet several PEs take turns on one color
11A grid of blockssplit a 2-D field across PEs, exchange halos, and budget colors, queues and memory
12A complete iterative solvercombine halo exchange, a grid-wide sum and a convergence test in one program

Extension topics​

A topic's Needs list names the lessons and topics it builds on directly. Each lesson in the main sequence assumes the lessons before it, so "lesson 9" includes lessons 1-8.

TopicNeeds
Reductions in the router: the fabric adderlesson 9
Switch protocolslesson 10
A barrier and a broadcast from switch positionslessons 10 and 11; Switch protocols
Collectives over messagesMessage passing; lesson 9
Message passing: routing by addresslessons 4, 6 and 10
Variable-length exchanges and teardownlessons 6 and 11
Eight neighbours: spiral routing on a budgetlessons 8 and 11
Data in and out: filters, fan-out and funnelslessons 7 and 10; Switch protocols
A route through every PElessons 10 and 11; Switch protocols
Precision: how the PE roundslessons 2 and 3
Bits and wide integerslesson 2
Hardware random numberslessons 2 and 3
Vector-step loops, predication and memory iteratorslesson 3
Memory: placing data and the memory budgetlessons 2 and 3
Locks, until conditions, rings and timer taskslessons 5 and 7
Matrix multiplication across PEslessons 9 and 10
Sorting networks along a rowlessons 2 and 6
Physics on a grid of pointslesson 11
Command streams: Tetris on a columnlessons 7 and 9
Large layouts: geovars, terminals and transportslessons 8, 9 and 11
Debugging and profiling a multi-PE runlessons 3 and 6

Which lesson for which dataflow​

Dataflow questionPatternWhere
One producer, one consumer?Point-to-point send and receiveLesson 4
A sequence of transforming stages?PipelineLesson 4
Two streams combined on one PE?parallel transfers and a FIFOLesson 5
Two PEs that both send and receive?A deadlock-free exchangeLesson 6
Indexed records, then a flush?DispatchLesson 7
A recurrence along a row?WavefrontLesson 8
One value for every PE of a row, or a sum over them?Broadcast and reduceLesson 9
Several producers sharing one color in turn?Serial drain with switchesLesson 10
Nearest-neighbour exchange on a grid?Halo exchangeLesson 11
A sum over a whole grid, every step?Grid allreduceLesson 12
Every PE waiting for all the others?Barrier from switch positionsBarrier and broadcast
Destinations known only at run time?Message passingMessage passing
A reduction done in the routers?Fabric adderThe fabric adder
Two matrices streamed across a grid?Systolic matrix multiplyMatrix multiplication

Prerequisites​

  • The experimental SDK, set up as Run the tutorials shows. With <sdk> the SDK's directory, set up means:
    • <sdk>/bin on $PATH, for carbide, paint, miget/miset, portrait and pipeline;
    • FLUTEMK=<sdk>/share/build/flute.mk and TESTMK=<sdk>/share/build/test.mk in the environment;
    • SIMFABRIC=simfabric-sdk, so that the build runs the Cerebras SDK simulator.
  • make, and python3 with numpy.
  • Familiarity with basic C-like syntax.

Paint compiles each lesson with Carbide.

Quick start​

Work in a copy of the SDK's tutorial, <sdk>/share/tutorial.

cp -r <sdk>/share/tutorial tutorial
cd tutorial/main-1-one-pe/01-hello-wafer

# Run the test in simulation mode:
make TEST=tutorial MODE=sim

# If the test passes, make prints a line ending in
# PASS tutorial [ N=4 ARCH=sdr ]. If it fails, the line says FAIL, and
# test-tutorial/make-log has the details.
  • make TEST=tutorial MODE=sim in tutorial/ runs the first row of every lesson and topic.
  • tungsten-tutorials runs them all, or the ones -f selects, and prints a scorecard: -f main for the main sequence, or -f main-1 for lessons 1-3.

After you edit a lesson's files, run make clean before make. Otherwise make does not rebuild an existing test-<label>/ directory, and it reports the old result.

Write a kernel with a coding agent​

The lessons and the language references are plain Markdown and source files, and every lesson checks its own result in the simulator. An agent can learn the languages from them and test the kernels it writes. Ask the agent to:

  1. read the Tungsten and Paint references, the main sequence, and the lesson or topic closest to your kernel. The table above maps dataflows to lessons.
  2. copy that lesson, or start from scratch as Write your own example describes. Then write the kernel (.w), its layout (bench.paint) and a Python reference for its output (ref.py).
  3. build and simulate it in the lesson's directory, as in the quick start, and repeat until the result matches the reference.

Also see​