raz / commit ed7c3fe4

commit
ed7c3fe41495e975d6289e3a12432e23746a8644
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
c04ee33e
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.

patch · log

5 files changed, +205 -4

PLAN.md (history) modified +25 -4

@@ -5,10 +5,31 @@
55 stopping for the owner's review. A finished milestone is cut from this
66 file: git keeps the history, and the report's section 12 the language's.
77
8-No milestone is under way. v0.7.1, the manual, and v0.8.0, better code
9-again, are tagged, by the owner's word to finish both; all before
10-v0.7.1 is approved by the owner, and v0.7.1 and v0.8.0 wait for the owner's
11-review. Next comes what the owner chooses, of the list below.
8+Better code for reals is under way, by the owner's word. v0.7.1, the
9+manual, and v0.8.0, better code again, are tagged; all before v0.7.1
10+is approved by the owner, and v0.7.1 and v0.8.0 wait for the owner's
11+review.
12+
13+## Now: better code for reals
14+
15+For: code with reals near FPC `-O2`'s, as integer code is. raz keeps a
16+real in an integer register and copies it to xmm0 or xmm1 and back
17+for each operation; its variables are always in memory. A 2-D Jacobi
18+sweep took 3.1 times FPC's time. No change to the language. Each step
19+is measured on the kernels and on rat, as v0.8.0's were, and one that
20+buys less than 2% on both is reverted and noted here.
21+
22+1. Done: **Two kernels of reals.** `test/bench/Jacobi`, a sweep of a
23+ grid of `F64`s, each point the mean of its neighbours: loads, adds
24+ and a constant; and `NBody`, bodies under gravity: real variables,
25+ a square root and a division in the inner loop. raz 3.1 and 4.4
26+ times FPC's time.
27+2. **Reals on the stack in xmm registers.** A real on the value stack
28+ is held in xmm8 to xmm11, loaded and stored there with `movsd`, a
29+ constant read from read-only data, and an operation takes its right
30+ operand from memory as it is.
31+3. **Real variables in xmm registers**, where that pays: measured, and
32+ kept only if it does.
1233
1334 ## Next, when the owner says
1435

test/bench/Jacobi.raz (history) added +41 -0

@@ -0,0 +1,41 @@
1+(* A 2-D Jacobi sweep: each point of a grid becomes the mean of its four
2+ neighbours, the edges held. *)
3+module Jacobi;
4+import Out;
5+check off;
6+
7+const
8+ N = 512;
9+ Rounds = 200;
10+
11+var a, b: [N * N]F64;
12+
13+(* one sweep, from s to d *)
14+proc sweep(d, s: []F64);
15+var i, j: INT;
16+begin
17+ for i := 1 to N - 2 do
18+ for j := 1 to N - 2 do
19+ 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]);
20+ end;
21+ end;
22+end;
23+
24+proc main*;
25+var i, r: INT; sum: F64;
26+begin
27+ for i := 0 to N * N - 1 do
28+ a[i] := F64(i mod 7);
29+ b[i] := a[i];
30+ end;
31+ for r := 1 to Rounds do
32+ sweep(b, a);
33+ sweep(a, b);
34+ end;
35+ sum := 0.0;
36+ for i := 0 to N * N - 1 do sum := sum + a[i]; end;
37+ Out.int(INT(sum * 1000.0));
38+ Out.ln;
39+end;
40+
41+end Jacobi.

test/bench/NBody.raz (history) added +58 -0

@@ -0,0 +1,58 @@
1+(* Bodies under gravity: each step every pair pulls on each other, then
2+ each body moves. Real variables, a square root and a division in the
3+ inner loop. *)
4+module NBody;
5+import Out, Math;
6+check off;
7+
8+const
9+ N = 64;
10+ Steps = 8000;
11+ Dt = 0.001;
12+
13+var x, y, z, vx, vy, vz, m: [N]F64;
14+
15+proc step;
16+var i, j: INT; dx, dy, dz, d2, mag: F64;
17+begin
18+ for i := 0 to N - 1 do
19+ for j := i + 1 to N - 1 do
20+ dx := x[i] - x[j];
21+ dy := y[i] - y[j];
22+ dz := z[i] - z[j];
23+ d2 := dx * dx + dy * dy + dz * dz + 0.01;
24+ mag := Dt / (d2 * Math.sqrt(d2));
25+ vx[i] := vx[i] - dx * m[j] * mag;
26+ vy[i] := vy[i] - dy * m[j] * mag;
27+ vz[i] := vz[i] - dz * m[j] * mag;
28+ vx[j] := vx[j] + dx * m[i] * mag;
29+ vy[j] := vy[j] + dy * m[i] * mag;
30+ vz[j] := vz[j] + dz * m[i] * mag;
31+ end;
32+ end;
33+ for i := 0 to N - 1 do
34+ x[i] := x[i] + Dt * vx[i];
35+ y[i] := y[i] + Dt * vy[i];
36+ z[i] := z[i] + Dt * vz[i];
37+ end;
38+end;
39+
40+proc main*;
41+var i, s: INT; e: F64;
42+begin
43+ for i := 0 to N - 1 do
44+ x[i] := F64(i mod 8);
45+ y[i] := F64(i div 8);
46+ z[i] := F64(i mod 5) * 0.5;
47+ m[i] := 1.0 + F64(i mod 3);
48+ end;
49+ for s := 1 to Steps do step; end;
50+ e := 0.0;
51+ for i := 0 to N - 1 do
52+ e := e + 0.5 * m[i] * (vx[i] * vx[i] + vy[i] * vy[i] + vz[i] * vz[i]);
53+ end;
54+ Out.int(INT(e * 1000000.0));
55+ Out.ln;
56+end;
57+
58+end NBody.

test/bench/jacobi.pas (history) added +32 -0

@@ -0,0 +1,32 @@
1+{ Jacobi.raz's twin: a 2-D Jacobi sweep over open arrays. }
2+program jacobi;
3+{$mode objfpc}
4+const
5+ N = 512;
6+ Rounds = 200;
7+var
8+ a, b: array[0..N * N - 1] of double;
9+ i, r: int64;
10+ sum: double;
11+
12+procedure Sweep(var d: array of double; const s: array of double);
13+var i, j: int64;
14+begin
15+ for i := 1 to N - 2 do
16+ for j := 1 to N - 2 do
17+ 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]);
18+end;
19+
20+begin
21+ for i := 0 to N * N - 1 do begin
22+ a[i] := i mod 7;
23+ b[i] := a[i];
24+ end;
25+ for r := 1 to Rounds do begin
26+ Sweep(b, a);
27+ Sweep(a, b);
28+ end;
29+ sum := 0.0;
30+ for i := 0 to N * N - 1 do sum := sum + a[i];
31+ writeln(trunc(sum * 1000.0));
32+end.

test/bench/nbody.pas (history) added +49 -0

@@ -0,0 +1,49 @@
1+{ NBody.raz's twin: bodies under gravity. }
2+program nbody;
3+{$mode objfpc}
4+const
5+ N = 64;
6+ Steps = 8000;
7+ Dt = double(0.001);
8+var
9+ x, y, z, vx, vy, vz, m: array[0..N - 1] of double;
10+ i, s: int64;
11+ e: double;
12+
13+procedure Step;
14+var i, j: int64; dx, dy, dz, d2, mag: double;
15+begin
16+ for i := 0 to N - 1 do
17+ for j := i + 1 to N - 1 do begin
18+ dx := x[i] - x[j];
19+ dy := y[i] - y[j];
20+ dz := z[i] - z[j];
21+ d2 := dx * dx + dy * dy + dz * dz + double(0.01);
22+ mag := Dt / (d2 * sqrt(d2));
23+ vx[i] := vx[i] - dx * m[j] * mag;
24+ vy[i] := vy[i] - dy * m[j] * mag;
25+ vz[i] := vz[i] - dz * m[j] * mag;
26+ vx[j] := vx[j] + dx * m[i] * mag;
27+ vy[j] := vy[j] + dy * m[i] * mag;
28+ vz[j] := vz[j] + dz * m[i] * mag;
29+ end;
30+ for i := 0 to N - 1 do begin
31+ x[i] := x[i] + Dt * vx[i];
32+ y[i] := y[i] + Dt * vy[i];
33+ z[i] := z[i] + Dt * vz[i];
34+ end;
35+end;
36+
37+begin
38+ for i := 0 to N - 1 do begin
39+ x[i] := i mod 8;
40+ y[i] := i div 8;
41+ z[i] := (i mod 5) * 0.5;
42+ m[i] := 1.0 + (i mod 3);
43+ end;
44+ for s := 1 to Steps do Step;
45+ e := 0.0;
46+ for i := 0 to N - 1 do
47+ e := e + 0.5 * m[i] * (vx[i] * vx[i] + vy[i] * vy[i] + vz[i] * vz[i]);
48+ writeln(trunc(e * 1000000.0));
49+end.