Example: conjugate gradient solver
This page walks through a larger csl++ program: a solver for a linear system with the conjugate gradient method. It is the conjugate gradient benchmark of the Cerebras SDK, written with the libraries stencil_3d_7pts and allreduce of csl++. The page assumes the Tutorial.
The program is in website/examples/guides/cg of the repository.
Conjugate gradient method
Section titled “Conjugate gradient method”The conjugate gradient method solves A x = b for a symmetric positive definite matrix A. Starting from a guess x, it improves x along search directions p until the residual r = b − A x is small:
r = b − A xp = rρ = r · rwhile ρ > tol² and k < max_ite: w = A p α = ρ / (p · w) x = x + α p r = r − α w ρ' = r · r p = r + (ρ' / ρ) p ρ = ρ'The unknowns are the points of a three-dimensional grid, and A is a 7-point stencil: the value of A x at a point is a weighted sum of the values of x at the point and at its six neighbors. The grid is distributed over a rectangle of PEs. Each PE holds a column of n points in z, so the neighbors of a point in x and y are on the four neighboring PEs.
Every PE runs the loop on its columns of the vectors. The operations of the loop differ in how much of the fabric they use:
| Operation | Implementation | Provided by |
|---|---|---|
| A p | Each PE exchanges its column of p with its four neighbors and applies the stencil. | spmv of the library stencil_3d_7pts |
| r · r and p · w | Each PE computes the dot product of its columns. The results are summed over all PEs, and every PE receives the sum. | allreduce of the library allreduce |
| Vector updates | Local DSD operations. | The program |
Layout
Section titled “Layout”The layout file imports the layout part of each library:
// The libraries claim the colors they route. Each import is an instance// with colors of its own.const stencil = @import_module("stencil_3d_7pts/layout.csl", .{ .width = width, .height = height,});const reduce = @import_module("allreduce/layout.csl", .{ .width = width, .height = height,});Neither import has a color parameter, because the libraries claim the colors they route. The layout part of a library has a function get_params(px, py). It returns the parameters that the PE part of the library needs on the PE at (px, py), among them the colors of the library and whether the PE is at an edge of the rectangle. The layout file passes them to the PE program of every PE:
for (@range(i16, height)) |py| { for (@range(i16, width)) |px| { @set_tile_code(px, py, "pe.csl", .{ .memcpy_params = memcpy.get_params(px), .stencil_params = stencil.get_params(px, py), .reduce_params = reduce.get_params(px, py), .MAX_ZDIM = MAX_ZDIM, .BLOCK_SIZE = BLOCK_SIZE, }); }}Libraries in the PE program
Section titled “Libraries in the PE program”The PE program imports the PE part of each library with the parameters from the layout file:
// y = A x over the grid, and reductions over all PEs. The libraries claim// the queues and task IDs they need; only the DSRs are assigned here.const stencil = @import_module("stencil_3d_7pts/pe.csl", .{ .stencil_params = stencil_params, .BLOCK_SIZE = BLOCK_SIZE, .output_ut_id = 7, .dsr_ids = [2]u16{2, 3},});const reduce = @import_module("allreduce/pe.csl", .{ .reduce_params = reduce_params, .dsr_id = 1,});The two imports provide the async functions that the solver awaits:
stencil.spmv(n, coeff, x, y)computes y = A x for the column of the PE.coeffpoints to the seven stencil coefficients.reduce.allreduce(n, x, op)reduces the vectorx[0..n)over all PEs with the operationopand leaves the result inxon every PE.
The parameters dsr_ids and dsr_id select the DSRs into which the libraries load their DSDs, and output_ut_id selects the microthread of the stencil’s sends on WSE-3. The pages of the libraries describe them.
Vectors
Section titled “Vectors”Each PE holds its columns of the five vectors of the method, its stencil coefficients, and ρ:
// The local parts of the vectors of the algorithm.var b = @zeros([MAX_ZDIM]f32); // right-hand sidevar x = @zeros([MAX_ZDIM]f32); // initial guess, then the solutionvar p = @zeros([MAX_ZDIM]f32); // search directionvar w = @zeros([MAX_ZDIM]f32); // A pvar r = @zeros([MAX_ZDIM]f32); // residual
// The stencil coefficients of this PE: west, east, south, north, bottom,// top, center.var stencil_coeff = @zeros([7]f32);
// |r|^2 over all PEs, read back by the host after the solve.var rho = @zeros([1]f32);The host writes b, x and stencil_coeff before it launches the solver, and reads x and rho afterwards. The program also declares one memory DSD per vector, mem_b_dsd to mem_r_dsd, for the DSD operations of the loop.
A dot product over the grid is the sum of the dot products of the columns. The program computes the part of one PE with a @map over two DSDs:
// The local part of a dot product; the parts are summed over all PEs with// an allreduce.fn add_product(u_i: f32, v_i: f32, sum: *f32) f32 { return sum.* + u_i * v_i;}
fn dot(u: mem1d_dsd, v: mem1d_dsd) f32 { var sum: f32 = 0.0; @map(add_product, u, v, &sum, &sum); return sum;}Solver task
Section titled “Solver task”The loop of the method is one async task. It follows the pseudocode above:
async task cg_task() void { // r = b - A x, rho = r.r
var k: i16 = 0; var rho_old: f32 = 0.0; while (rho[0] > tol * tol and k < max_ite) { k += 1;
// p = r + beta p, with p = r in the first iteration // w = A p, eta = p.w // x += alpha p, r -= alpha w, rho = r.r }
sys_mod.unblock_cmd_stream();}Each comment stands for a few statements, which the following sections show. k and rho_old are local variables of the task, and they keep their values while the task is suspended in a library call. The host starts the task by launching the function solve, which stores its arguments in globals and activates the task.
Initial residual
Section titled “Initial residual”// r = b - A x, rho = r.r@await(stencil.spmv(n, &stencil_coeff, &x, &w));@fsubs(mem_r_dsd, mem_b_dsd, mem_w_dsd);rho[0] = dot(mem_r_dsd, mem_r_dsd);@await(reduce.allreduce(1, &rho, reduce.TYPE_BINARY_OP.ADD));stencil.spmv computes w = A x. When the @await returns, the PE has exchanged x with its neighbors, and w is complete. @fsubs then computes r = b − w.
The last two lines compute ρ = r · r. dot returns the part of this PE, and reduce.allreduce replaces it with the sum over all PEs. rho is declared as an array of one element because allreduce takes a pointer to the vector it reduces.
Search direction
Section titled “Search direction”// p = r + beta p, with p = r in the first iterationif (k == 1) { @fmovs(mem_p_dsd, mem_r_dsd);} else { const beta: f32 = rho[0] / rho_old; @fmacs(mem_p_dsd, mem_r_dsd, mem_p_dsd, beta);}The first iteration copies r to p. Every later iteration computes p = r + β p with one @fmacs, where β is the ratio of the current ρ and the previous one.
Step length
Section titled “Step length”// w = A p, eta = p.w@await(stencil.spmv(n, &stencil_coeff, &p, &w));var eta: f32 = dot(mem_p_dsd, mem_w_dsd);@await(reduce.allreduce(1, @ptrcast([*]f32, &eta), reduce.TYPE_BINARY_OP.ADD));This is the second call of stencil.spmv, now with p. The dot product p · w is reduced into the local variable eta; @ptrcast converts its address to the pointer type that allreduce takes. The library can write to a local variable of the task, because the local variables of an async task keep their addresses while the task is suspended.
Update of x and r
Section titled “Update of x and r”// x += alpha p, r -= alpha w, rho = r.rconst alpha: f32 = rho[0] / eta;@fmacs(mem_x_dsd, mem_x_dsd, mem_p_dsd, alpha);@fmacs(mem_r_dsd, mem_r_dsd, mem_w_dsd, -alpha);rho_old = rho[0];rho[0] = dot(mem_r_dsd, mem_r_dsd);@await(reduce.allreduce(1, &rho, reduce.TYPE_BINARY_OP.ADD));The two @fmacs add α p to x and subtract α w from r. The task then saves ρ in rho_old for the next iteration and computes the new ρ in the same way as for the initial residual.
Complete task
Section titled “Complete task”With these statements in place, the task is:
async task cg_task() void { // r = b - A x, rho = r.r @await(stencil.spmv(n, &stencil_coeff, &x, &w)); @fsubs(mem_r_dsd, mem_b_dsd, mem_w_dsd); rho[0] = dot(mem_r_dsd, mem_r_dsd); @await(reduce.allreduce(1, &rho, reduce.TYPE_BINARY_OP.ADD));
var k: i16 = 0; var rho_old: f32 = 0.0; while (rho[0] > tol * tol and k < max_ite) { k += 1;
// p = r + beta p, with p = r in the first iteration if (k == 1) { @fmovs(mem_p_dsd, mem_r_dsd); } else { const beta: f32 = rho[0] / rho_old; @fmacs(mem_p_dsd, mem_r_dsd, mem_p_dsd, beta); }
// w = A p, eta = p.w @await(stencil.spmv(n, &stencil_coeff, &p, &w)); var eta: f32 = dot(mem_p_dsd, mem_w_dsd); @await(reduce.allreduce(1, @ptrcast([*]f32, &eta), reduce.TYPE_BINARY_OP.ADD));
// x += alpha p, r -= alpha w, rho = r.r const alpha: f32 = rho[0] / eta; @fmacs(mem_x_dsd, mem_x_dsd, mem_p_dsd, alpha); @fmacs(mem_r_dsd, mem_r_dsd, mem_w_dsd, -alpha); rho_old = rho[0]; rho[0] = dot(mem_r_dsd, mem_r_dsd); @await(reduce.allreduce(1, &rho, reduce.TYPE_BINARY_OP.ADD)); }
sys_mod.unblock_cmd_stream();}Comparison with the SDK benchmark
Section titled “Comparison with the SDK benchmark”The libraries of the SDK benchmark report the completion of an operation by calling a callback. The loop of the benchmark is therefore a state machine: one task that is activated after every library call and continues with the state that was set before the call. This is a part of it, from benchmarks/conjugate-gradient/src/kernel_cg.csl of the SDK examples:
task f_state() void { // ... }else if (STATE_SPMV_AP == cur_state){ next_state = STATE_ETA; // w = A*p f_spmv_Ap();
}else if (STATE_ETA == cur_state){ next_state = STATE_UPDATE_X_R_RHO; // eta = np.dot(p,w) = (p_{k}, A*p_{k}) f_eta();
// ...}
fn f_trigger_state_machine() void { cur_state = next_state; // go to next state @activate(STATE);}f_trigger_state_machine is the callback that the program passes to both libraries. The state machine has ten states, and the loop of the method is the transition from the last state of an iteration back to the convergence check. The iteration counter, rho and rho_old are globals, because the task starts from its beginning on every activation.
The benchmark also chooses the colors, queues and task IDs of the libraries. Its layout file begins with a table of all of them and passes them to the libraries:
const reduce = @import_module( "../../benchmark-libs/allreduce/layout.csl", .{ .colors = [1]color{C8}, .entrypoints = [4]local_task_id{EN_REDUCE_1, EN_REDUCE_2, EN_REDUCE_3, EN_REDUCE_4}, .width = width, .height = height });Compiling and running
Section titled “Compiling and running”The directory of the program contains one script per architecture. commands_wse3.sh compiles the program for WSE-3 and runs the host program on the simulator:
#!/usr/bin/env bash
set -e
csl-pp ./src/layout.csl --arch=wse3 --fabric-dims=12,7 --fabric-offsets=4,1 \ --params=width:5,height:5,MAX_ZDIM:5 --import-path=../../../../csl-libs \ -o out --memcpy --channels=1cs_python ./run.py --name out--import-path names the csl-libs directory of the repository, so that the program is compiled with the libraries of the checkout. Without the option, an installed csl-pp uses the libraries of its installation.
The host program writes the inputs, launches solve, and compares x with the result of the same algorithm on the host:
3 iterations, |r|^2 = 6.766016960144043 on the device, 6.766015529632568 on the hostSUCCESS!run.py limits the solver to three iterations with max_ite, so that the simulation takes a few minutes. The residual is still far from the tolerance after three iterations. To run on a Cerebras system instead of the simulator, pass --cmaddr IP:port to run.py, as for the host programs of the SDK examples.
Simulator tracing looks at the trace of this run: the flamegraph shows spmv and allreduce under cg_task across all their suspends.
The benchmark version of the solver, with timing, is in test/Integration/coro-benchmarks/conjugate-gradient of the repository, next to ports of BiCGStab, the power method, and GEMM and GEMV with the collectives_2d library.