From ed7c3fe41495e975d6289e3a12432e23746a8644 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 13 Oct 2026 11:38:16 +0100 Subject: [PATCH] Two kernels of reals: Jacobi and NBody A 2-D Jacobi sweep over 512x512 F64s, and 64 bodies under gravity with real variables, sqrt and a division in the inner loop, each with its twin in Pascal. raz against FPC -O2: Jacobi 251 ms against 80, 3.1 times; NBody 375 against 85, 4.4 times. PLAN: better code for reals, under way. --- diff --git a/PLAN.md b/PLAN.md index 9066901..b9f4720 100644 --- a/PLAN.md +++ b/PLAN.md @@ -5,10 +5,31 @@ stopping for the owner's review. A finished milestone is cut from this file: git keeps the history, and the report's section 12 the language's. -No milestone is under way. v0.7.1, the manual, and v0.8.0, better code -again, are tagged, by the owner's word to finish both; all before -v0.7.1 is approved by the owner, and v0.7.1 and v0.8.0 wait for the owner's -review. Next comes what the owner chooses, of the list below. +Better code for reals is under way, by the owner's word. v0.7.1, the +manual, and v0.8.0, better code again, are tagged; all before v0.7.1 +is approved by the owner, and v0.7.1 and v0.8.0 wait for the owner's +review. + +## Now: better code for reals + +For: code with reals near FPC `-O2`'s, as integer code is. raz keeps a +real in an integer register and copies it to xmm0 or xmm1 and back +for each operation; its variables are always in memory. A 2-D Jacobi +sweep took 3.1 times FPC's time. No change to the language. Each step +is measured on the kernels and on rat, as v0.8.0's were, and one that +buys less than 2% on both is reverted and noted here. + +1. Done: **Two kernels of reals.** `test/bench/Jacobi`, a sweep of a + grid of `F64`s, each point the mean of its neighbours: loads, adds + and a constant; and `NBody`, bodies under gravity: real variables, + a square root and a division in the inner loop. raz 3.1 and 4.4 + times FPC's time. +2. **Reals on the stack in xmm registers.** A real on the value stack + is held in xmm8 to xmm11, loaded and stored there with `movsd`, a + constant read from read-only data, and an operation takes its right + operand from memory as it is. +3. **Real variables in xmm registers**, where that pays: measured, and + kept only if it does. ## Next, when the owner says diff --git a/test/bench/Jacobi.raz b/test/bench/Jacobi.raz new file mode 100644 index 0000000..40242ef --- /dev/null +++ b/test/bench/Jacobi.raz @@ -0,0 +1,41 @@ +(* A 2-D Jacobi sweep: each point of a grid becomes the mean of its four + neighbours, the edges held. *) +module Jacobi; +import Out; +check off; + +const + N = 512; + Rounds = 200; + +var a, b: [N * N]F64; + +(* one sweep, from s to d *) +proc sweep(d, s: []F64); +var i, j: INT; +begin + for i := 1 to N - 2 do + for j := 1 to N - 2 do + d[i * N + j] := 0.25 * (s[i * N + j - 1] + s[i * N + j + 1] + s[(i - 1) * N + j] + s[(i + 1) * N + j]); + end; + end; +end; + +proc main*; +var i, r: INT; sum: F64; +begin + for i := 0 to N * N - 1 do + a[i] := F64(i mod 7); + b[i] := a[i]; + end; + for r := 1 to Rounds do + sweep(b, a); + sweep(a, b); + end; + sum := 0.0; + for i := 0 to N * N - 1 do sum := sum + a[i]; end; + Out.int(INT(sum * 1000.0)); + Out.ln; +end; + +end Jacobi. diff --git a/test/bench/NBody.raz b/test/bench/NBody.raz new file mode 100644 index 0000000..98f9010 --- /dev/null +++ b/test/bench/NBody.raz @@ -0,0 +1,58 @@ +(* Bodies under gravity: each step every pair pulls on each other, then + each body moves. Real variables, a square root and a division in the + inner loop. *) +module NBody; +import Out, Math; +check off; + +const + N = 64; + Steps = 8000; + Dt = 0.001; + +var x, y, z, vx, vy, vz, m: [N]F64; + +proc step; +var i, j: INT; dx, dy, dz, d2, mag: F64; +begin + for i := 0 to N - 1 do + for j := i + 1 to N - 1 do + dx := x[i] - x[j]; + dy := y[i] - y[j]; + dz := z[i] - z[j]; + d2 := dx * dx + dy * dy + dz * dz + 0.01; + mag := Dt / (d2 * Math.sqrt(d2)); + vx[i] := vx[i] - dx * m[j] * mag; + vy[i] := vy[i] - dy * m[j] * mag; + vz[i] := vz[i] - dz * m[j] * mag; + vx[j] := vx[j] + dx * m[i] * mag; + vy[j] := vy[j] + dy * m[i] * mag; + vz[j] := vz[j] + dz * m[i] * mag; + end; + end; + for i := 0 to N - 1 do + x[i] := x[i] + Dt * vx[i]; + y[i] := y[i] + Dt * vy[i]; + z[i] := z[i] + Dt * vz[i]; + end; +end; + +proc main*; +var i, s: INT; e: F64; +begin + for i := 0 to N - 1 do + x[i] := F64(i mod 8); + y[i] := F64(i div 8); + z[i] := F64(i mod 5) * 0.5; + m[i] := 1.0 + F64(i mod 3); + end; + for s := 1 to Steps do step; end; + e := 0.0; + for i := 0 to N - 1 do + e := e + 0.5 * m[i] * (vx[i] * vx[i] + vy[i] * vy[i] + vz[i] * vz[i]); + end; + Out.int(INT(e * 1000000.0)); + Out.ln; +end; + +end NBody. diff --git a/test/bench/jacobi.pas b/test/bench/jacobi.pas new file mode 100644 index 0000000..9a06e9f --- /dev/null +++ b/test/bench/jacobi.pas @@ -0,0 +1,32 @@ +{ Jacobi.raz's twin: a 2-D Jacobi sweep over open arrays. } +program jacobi; +{$mode objfpc} +const + N = 512; + Rounds = 200; +var + a, b: array[0..N * N - 1] of double; + i, r: int64; + sum: double; + +procedure Sweep(var d: array of double; const s: array of double); +var i, j: int64; +begin + for i := 1 to N - 2 do + for j := 1 to N - 2 do + d[i * N + j] := 0.25 * (s[i * N + j - 1] + s[i * N + j + 1] + s[(i - 1) * N + j] + s[(i + 1) * N + j]); +end; + +begin + for i := 0 to N * N - 1 do begin + a[i] := i mod 7; + b[i] := a[i]; + end; + for r := 1 to Rounds do begin + Sweep(b, a); + Sweep(a, b); + end; + sum := 0.0; + for i := 0 to N * N - 1 do sum := sum + a[i]; + writeln(trunc(sum * 1000.0)); +end. diff --git a/test/bench/nbody.pas b/test/bench/nbody.pas new file mode 100644 index 0000000..e991746 --- /dev/null +++ b/test/bench/nbody.pas @@ -0,0 +1,49 @@ +{ NBody.raz's twin: bodies under gravity. } +program nbody; +{$mode objfpc} +const + N = 64; + Steps = 8000; + Dt = double(0.001); +var + x, y, z, vx, vy, vz, m: array[0..N - 1] of double; + i, s: int64; + e: double; + +procedure Step; +var i, j: int64; dx, dy, dz, d2, mag: double; +begin + for i := 0 to N - 1 do + for j := i + 1 to N - 1 do begin + dx := x[i] - x[j]; + dy := y[i] - y[j]; + dz := z[i] - z[j]; + d2 := dx * dx + dy * dy + dz * dz + double(0.01); + mag := Dt / (d2 * sqrt(d2)); + vx[i] := vx[i] - dx * m[j] * mag; + vy[i] := vy[i] - dy * m[j] * mag; + vz[i] := vz[i] - dz * m[j] * mag; + vx[j] := vx[j] + dx * m[i] * mag; + vy[j] := vy[j] + dy * m[i] * mag; + vz[j] := vz[j] + dz * m[i] * mag; + end; + for i := 0 to N - 1 do begin + x[i] := x[i] + Dt * vx[i]; + y[i] := y[i] + Dt * vy[i]; + z[i] := z[i] + Dt * vz[i]; + end; +end; + +begin + for i := 0 to N - 1 do begin + x[i] := i mod 8; + y[i] := i div 8; + z[i] := (i mod 5) * 0.5; + m[i] := 1.0 + (i mod 3); + end; + for s := 1 to Steps do Step; + e := 0.0; + for i := 0 to N - 1 do + e := e + 0.5 * m[i] * (vx[i] * vx[i] + vy[i] * vy[i] + vz[i] * vz[i]); + writeln(trunc(e * 1000000.0)); +end.