Benchmarks

Timings, so that changes can be measured against them, and profiles, so that it is known where the time goes. Both come from programs in crates/pdemagic/benches, without a framework (decision D24).

The benchmarks are not run in CI, because shared runners are too noisy for timings to mean anything. CI compiles them.

The suite

One or more cases per method, each a problem as a user states it, from the problem to the solution:

cargo bench -p pdemagic --bench suite

Argument

Effect

a word, such as box

only the cases with that word in their name

--profile

after each case, where the time of its solve goes

--save FILE

writes the timings

--add FILE

writes the timings of the cases that the file does not hold yet, and leaves the others

--compare FILE

prints the timings as fractions of saved ones

For each case the planner and the solve are timed, each a few times, and the fastest run counts. The solve uses the plan that is stored with the case, so that a timing means the same after the planner has learned to choose differently.

The cases are written by a script, from problems stated with sympy, into crates/pdemagic/benches/cases. The benchmark program reads them from there and needs no Python. Run the script again to add a case or to change one, and add its timing to the baseline with --add when you do:

uv run python scripts/benchmark-cases.py

To compare a change against the timings of the last milestone that recorded them:

cargo bench -p pdemagic --bench suite -- --compare benches/baseline.json

After milestone 81

Between walls and on regions the stepping takes five evaluations of its term in a step, where it took four (decision D81.1). A case that kept its order before pays for that, and one that lost it gains by far more than it pays. On the machine of this milestone, where the cases that did not change run at 0.76 to 0.91 of their baseline:

Case

Solve before

Solve now

Against the baseline

region_growth_2d, a reaction on a dish that lets nothing through

1 488 ms

1 757 ms

1.16

box_reaction_1d, a reaction between walls

174 ms

158 ms

0.90

box_sine_gordon_1d, second order in time, not changed

5.4 s

4.9 s

0.91

The baseline holds the new timing of region_growth_2d. What the change is for has no case in the suite: a system between walls with values on the walls that decay took 1 280 steps and takes 80, and a wave between walls that are shaken took 1 243 and takes 215.

After milestone 74

A nonlinear wave between walls has a case, box_sine_gordon_1d: a kink of the sine-Gordon equation on a line of the length 24 with free ends, followed for 40 units of time through its reflection, on 256 Chebyshev points. It is stepped with a matrix of two by two for each mode (decision D74.1).

Case

Plan

Solve

Steps

box_sine_gordon_1d

4.6 ms

5.4 s

740

Of the solve, the expressions take 78 % and the transforms between points and modes 18 %. The term that is stepped is one sine, evaluated 27 000 times at 256 points: the interpreter computes it in complex numbers, at 0.6 µs for a point. The matrices of the modes and their functions do not show. Generated kernels (C4) and real arithmetic for real problems are what this case waits for.

After milestone 62

The sparse system of a region is factorized as what it is (package C16, decision D62.1), and the points inside elements are eliminated in real numbers where the problem is real. Measured through the Python frontend, on four cores, on a rectangle with a ridge whose two corners with a singularity have 18 layers each (2 256 elements):

Problem

Degree

Before

After

\(-\nabla^2 u = 1\)

8

1.5 s, 0.9 GB

0.4 s, 0.3 GB

\(-\nabla^2 u = 1\)

12

4.8 s, 2.0 GB

1.3 s, 0.7 GB

\(-\nabla^2 u = 1\)

16

11 s, 3.7 GB

2.8 s, 1.3 GB

\(\nabla^2 u + 30u = 1\)

12

4.2 s, 2.0 GB

2.3 s, 1.1 GB

Two modes, no slope on the wall

8

2.9 s, 1.0 GB

1.0 s, 0.4 GB

Two modes, no slope on the wall

12

10.7 s, 2.9 GB

3.1 s, 1.0 GB

Where the time of the solve at the degree 12 goes:

Phase

Before

After

Sparse factorization

3.9 s

0.8 s

Elimination inside elements

0.8 s

0.6 s

The first row is Cholesky’s method in real numbers against a general factorization in complex ones. Of its 0.8 s the factorization itself takes 0.3 s: the rest sorts the five million entries of the matrix into its two triangles and compares them. The elimination inside elements gains little from real numbers. The equation \(\nabla^2 u + 30u = 1\) is real with a matrix of both signs, and is factorized as a general real matrix.

After milestone 40

A mesh that shrinks towards corners (decision D40.1) adds one case, and the elimination inside elements runs on all cores (package C16, decision D40.3). The stationary cases on regions were timed again, on four cores, and their entries in the baseline replaced. All other entries stayed as they were: timed again on the same day, cases that this milestone does not touch came out between 0.4 and 1.7 of their saved times, which says more about the machine than about them.

Case

Elements, degree

Plan

Solve

Solve before

Largest phases

region_corner_2d

828, degree 12

2 789 ms

1 333 ms

new

sparse factorization 73 %, elimination inside elements 17 %

region_l_2d

306, degree 12

2 947 ms

347 ms

639 ms

sparse factorization 53 %, elimination inside elements 20 %, sampling on the grid 19 %

region_poisson_2d

111, degree 14

528 ms

347 ms

429 ms

region_medium_2d

111, degree 12

293 ms

217 ms

236 ms

region_bratu_2d

111, degree 14

544 ms

827 ms

992 ms

  • The elimination inside elements took 60 % of a solve and takes 20 % now: every element on its own thread, and the products by the library. What is left is the sparse factorization, on one core.

  • The planner of region_l_2d took 37 ms, because it stopped at the singularity of the corner. It now gives the mesh its layers and solves on them, which is the time of region_corner_2d. The case itself keeps its plan without layers.

  • The factors are not nearly full. Milestone 33 recorded 6.4 gigabytes for 27 000 points on the sides of elements, and a limit of 16 000 that followed from it. Measured again, the same solve takes 0.9 gigabytes and 2.9 s, and 110 000 such points take 4.9 gigabytes and 18 s. Memory grows about with the number of those points, not with its square. Where the figure of milestone 33 came from could not be found again.

After milestone 38

Terms that are stepped in time on a region (decision D38.1) add one case. Its timing was added to the baseline, and all other entries stayed as they were.

Case

Elements, degree

Plan

Solve

Largest phases

region_growth_2d

24, degree 6

1 ms

1 488 ms

mode transforms 81 %, eigendecompositions 10 %

  • The case is given its plan, with 913 points. The planner chooses the degree 8 for it, with 1 601 points, which takes three times as long.

  • 2 614 products with the matrix of the modes take four fifths of the time: two for every evaluation of the terms that are stepped. They grow with the square of the number of points, and are what package C17 would replace.

After milestone 36

Nonlinear stationary equations on a region, by Newton’s method (decision D36.1), add one case. Its timing was added to the baseline, and all other entries stayed as they were.

Case

Elements, degree

Plan

Solve

Largest phases

region_bratu_2d

111, degree 14

693 ms

992 ms

elimination inside elements 68 %, sparse factorization 20 %, sampling on the grid 8 %

  • The solve takes three steps of Newton’s method, each a linear solve with its own factorization.

  • The planner solves at six degrees and takes less than the one solve at the last of them: each degree starts from the solution at the one before, and the low degrees are cheap.

After milestone 35

Equations with coefficients that vary on a region (decision D35.1) add one case. Its timing was added to the baseline, and all other entries stayed as they were.

Case

Elements, degree

Plan

Solve

Largest phases

region_medium_2d

111, degree 12

325 ms

236 ms

elimination inside elements 48 %, sampling on the grid 26 %, sparse factorization 18 %

  • The matrix of each element is now built for any coefficients, in complex numbers. The two cases of milestone 33 did not slow down: their solves take 0.88 and 0.93 of the time before.

  • The table of milestone 33 gave the degree of region_poisson_2d as 10. The case has the degree 14, and always had.

After milestone 34

Problems in time on a region of the plane, in the modes of the shape (decision D34.1), add one case. Its timing was added to the baseline, and all other entries stayed as they were.

Case

Elements, degree

Plan

Solve

Largest phases

region_drum_2d

18, degree 10

2 ms

957 ms

eigendecompositions 90 %, sampling on the grid 4 %

  • The case is given its plan, with 1 881 points. The planner chooses the degree 12 for it, with 2 689 points, which takes three times as long.

  • The time is that of one dense eigenproblem, on one core, and grows with the cube of the number of points: package C17.

  • The planner solves nothing, and takes milliseconds.

After milestone 33

Regions of the plane, by spectral elements (decision D33.6), add two cases. Their timings were added to the baseline, and all other entries stayed as they were. The method uses one core so far.

Case

Elements, degree

Plan

Solve

Largest phases

region_poisson_2d

111, degree 14

631 ms

398 ms

elimination inside elements 63 %, sampling on the grid 23 %, sparse factorization 13 %

region_l_2d

306, degree 12

38 ms

627 ms

elimination inside elements 60 %, sparse factorization 26 %, sampling on the grid 10 %

  • The planner of the first case solves at four degrees, which is why it takes longer than the solve. The second case is given its plan, and its planner ends early at the singularity of the corner.

  • Most of the time goes into eliminating the points inside each element: a dense factorization for each, and products that are plain loops. The elements are independent, so this is work for all cores, and for the library’s matrix product.

  • Before the points inside were eliminated, the sparse factorization took all of the time: 2.2 s for the first case.

  • The factors of the sparse matrix are nearly full. That limits the size of a mesh by memory before time: package C16.

  • The mesh and the geometry of the elements take less than 3 %.

After milestones 27 to 29

Disks, balls and cylinders with boundary conditions (decisions D77 to D79) add two cases each. Their timings were added to the baseline, and all other entries stayed as they were. On four cores:

Case

Grid

Plan

Solve

Largest phases

disk_screened_2d

128 × 256

0.7 ms

274 ms

eigendecompositions 92 %

disk_drum_2d, 21 times

64 × 128

35 ms

39 ms

eigendecompositions 55 %, mode transforms 19 %

ball_screened_3d

48 × 48 × 96

3.6 ms

59 ms

expressions 24 %, harmonic transforms 17 %, eigendecompositions 13 %

ball_heat_3d, 21 times

32 × 32 × 64

24 ms

90 ms

harmonic transforms 21 %, mode transforms 17 %

can_screened_3d

48 × 96 × 96

47 ms

115 ms

mode transforms 35 %, expressions 19 %

can_heat_3d, 21 times

24 × 48 × 48

42 ms

73 ms

mode transforms 36 %

  • A ball has one matrix along the radius for every degree of the harmonics, 48 in the first case, where a disk has one for every wave. That is why a ball costs less than a disk of the same resolution along the radius.

  • In a cylinder the transforms to the modes cost most: along the radius they are a plain loop, one matrix for every wave along the angle, where the axis of a box uses the library’s product.

  • Half of ball_heat_3d is in no phase. Part of it is the tables of the harmonics, which are computed again for every measurement of a spectrum.

  • A disk has one matrix along the radius for every wave along the angle, 129 of them in the first case, and each is diagonalised. They are independent, and run side by side on all cores. The profile counts them as one phase, by the clock.

  • The planner of the drum solves on three trial grids, which is why it takes as long as the solve.

  • A stationary solve does not need the modes: one factorization per wave would do. The modes are kept because the planner, the stationary solve and time then share everything else.

After milestones 22 to 25

Milestone 22 replaced the iteration for a permittivity by the method for any coefficients that vary (decision D71), and added two cases for it. Milestone 23 added Newton’s method on top of it (decision D72), milestone 24 waves in a medium (decision D73), and milestone 25 potentials in equations in time (decision D74), with one case each. The baseline holds the timings of these six cases, and those of milestone 21 for all others, which did not change beyond what timings scatter. Measured on the same machine, on four cores.

Case

Solve, M21

Solve now

Largest phases

permittivity_3d

10.05 s

7.77 s

transforms 59 %, derivatives 24 %

well_free_3d

1.41 s

transforms 70 %, derivatives 12 %

varying_periodic_2d

72 ms

transforms 39 %

poisson_boltzmann_3d

3.35 s

transforms 67 %, derivatives 14 %

lens_2d

191 ms

transforms 23 %, vectors of GMRES 12 %

schroedinger_barrier_2d

3.79 s

2.21 s

transforms 30 %, expressions 30 %

  • The iteration asks the reference solve for the derivatives that have a varying coefficient and for nothing else. For a permittivity those are the three components of the gradient: one transform there and three back per step, where the method of milestone 6 took four back.

  • The planner of permittivity_3d takes 256 ms where it took 92 ms: it now reads the equation on every candidate grid, to find out what the iteration will work with.

  • “Derivatives” is the multiplication of a transform by the wave numbers. A fifth of the time for that is more than the arithmetic costs: every derivative is a new array of the size of the padded transform.

  • schroedinger_barrier_2d evaluated its potential again at every stage of every step, although it does not depend on time: half of its 3.79 s. Since milestone 26 the parts of a stepped term that depend on neither the time nor the unknown are sampled once. What is left for expressions is the interpreter’s cost of one product per point.

  • Half of lens_2d is in no phase: it is copies of the padded arrays between the grid, the transform and the iteration.

  • poisson_boltzmann_3d takes 4 steps of Newton’s method and 9 of GMRES on 64³ points, and each step of GMRES and each trial of a step costs a solve with the reference operator. Its “derivatives” are copies: the linearized equation needs the solution itself and no derivative of it.

After milestone 21

The file crates/pdemagic/benches/baseline.json holds the numbers of the last column, except for the cases that later milestones added or changed, which the sections above name. Measured on a 4-core Intel Xeon at 2.1 GHz. Milestones 18 and 20 used one core, milestone 21 uses all four. The last column is the time of milestone 21 as a fraction of that of milestone 18, whose baseline the section after the next describes.

Case

Solve, M18

Solve, M20

Solve, M21

M21 / M18

poisson_free_3d

685 ms

251 ms

175 ms

0.26

poisson_slab

1.1 ms

0.8 ms

0.9 ms

0.78

screened_free_3d

497 ms

222 ms

163 ms

0.33

helmholtz_free_2d

53 ms

21 ms

14 ms

0.26

helmholtz_free_3d

4.99 s

1.95 s

1.09 s

0.22

periodic_fourth_order_2d

44 ms

43 ms

27 ms

0.61

permittivity_3d

30.93 s

16.21 s

10.05 s

0.33

ball

18 ms

18 ms

14 ms

0.78

cylinder

12 ms

12 ms

13 ms

1.03

bodies

1.45 s

1.42 s

596 ms

0.41

heat_free_2d

13 ms

13 ms

14 ms

1.07

wave_free_3d

3.83 s

2.20 s

1.54 s

0.40

schroedinger_free_2d

49 ms

50 ms

34 ms

0.71

maxwell_2d

99 ms

98 ms

94 ms

0.95

burgers_ring

52 ms

50 ms

53 ms

1.02

reaction_tile

1.53 s

1.56 s

1.64 s

1.07

gray_scott

1.44 s

1.33 s

1.49 s

1.03

box_poisson_3d

383 ms

59 ms

52 ms

0.14

box_heat_3d

1.13 s

217 ms

168 ms

0.15

box_drum_2d

331 ms

77 ms

63 ms

0.19

box_reaction_1d

284 ms

174 ms

174 ms

0.61

What milestone 21 changed

Threads (decision D69): transforms, expressions, kernels, propagation factors, the products of matrices with grid lines on boxes, and the potential of triangles at grid points run on all cores.

  • The large cases take 0.4 to 0.9 of the time of milestone 20 on four cores. That is far from a quarter: the transforms of padded arrays in three dimensions are limited by memory, not by arithmetic, and a part of every solve still runs on one thread.

  • Cases of a few milliseconds are not faster, and the smallest pay about a tenth of a millisecond to enter the pool.

  • Stepped problems on grids below 32768 points do not use threads: their loops are too short to share.

  • The planner gained as much as the solves, and on boxes more: building the modes of an axis and checking the resolution had costs that a count of instructions showed.

Against the targets for four cores: box_heat_3d reached its target. poisson_free_3d, helmholtz_free_3d, wave_free_3d, permittivity_3d and reaction_tile did not. What is left for them is named under “Where the time goes now”.

What milestone 20 changed

Decision D68:

  • Kernels and propagation factors are evaluated once for a wave and its mirror images, and real fields are transformed with real transforms. That is the gain of the Poisson, Helmholtz and wave cases and of the iteration.

  • On boxes, grid lines are multiplied by matrices with the matrix product of the linear algebra library. The planner on boxes solves on trial grids, so planning there gained as much: the plan of box_poisson_3d fell from 515 ms to 90 ms.

  • Strongly damped modes no longer produce numbers below the normal range, which processors handle very slowly.

Against the targets below: poisson_free_3d (0.25 s), permittivity_3d (16 s) and box_heat_3d (0.22 s against 0.6 s) reached theirs for one core. helmholtz_free_3d (1.95 s against 1.5 s) and wave_free_3d (2.2 s against 2 s) fell short: their unknowns are complex, or propagated with complex factors, so real transforms do not apply.

Where the time goes now

What the next steps for speed are, in the order of what they would give:

  • The padded transforms. A free direction is padded to about 2.7 times the grid, twenty times as many points in three dimensions, and the transforms of such arrays wait for memory. A kernel on a box of twice the grid, computed once per grid, would cut the transforms of repeated solves by a factor of 2.5 (permittivity_3d, well_free_3d). Arrays that are reused from step to step would save a part of what the derivatives cost there.

  • Complex transforms of real fields in the propagators and in the stepping (wave_free_3d, reaction_tile).

  • Interpreted expressions, which are half of a stepped solve and are the reason why reaction_tile did not move (milestone C4).

  • The potential of many triangles (bodies, milestone C5).

  • The small matrices of systems (maxwell_2d, gray_scott), which are computed wave by wave on one thread.

Baseline after milestone 18

Measured on a 4-core Intel Xeon at 2.1 GHz, using one core. These are the numbers that the profiles and the targets below refer to.

Case

Method

Grid

Plan

Solve

poisson_free_3d

Fourier

64³

47 ms

0.69 s

poisson_slab

Fourier

11 × 11 × 64

10 ms

1.1 ms

screened_free_3d

Fourier

64³

47 ms

0.50 s

helmholtz_free_2d

Fourier

192 × 139

23 ms

53 ms

helmholtz_free_3d

Fourier

96³

108 ms

5.0 s

periodic_fourth_order_2d

Fourier

512²

0.2 ms

44 ms

permittivity_3d

Fourier, iterative

96 × 81 × 81

190 ms

30.9 s

ball

harmonics

32 × 32 × 64

18 ms

18 ms

cylinder

finite differences

24 × 8 × 48

0.3 ms

12 ms

bodies

boundary elements, 1000 triangles

24³

0 ms

1.45 s

heat_free_2d

propagator, 21 times

128 × 112

21 ms

13 ms

wave_free_3d

propagator, 21 times

96³

108 ms

3.8 s

schroedinger_free_2d

propagator, 21 times

192 × 188

24 ms

49 ms

maxwell_2d

propagator, three unknowns

87 × 96

21 ms

99 ms

burgers_ring

stepping

256

0 ms

52 ms

reaction_tile

stepping

128²

0.1 ms

1.53 s

gray_scott

stepping, two unknowns

96²

0.5 ms

1.44 s

box_poisson_3d

collocation

64³

515 ms

0.38 s

box_heat_3d

modes, 21 times

48³

27 ms

1.13 s

box_drum_2d

modes, 21 times

128²

171 ms

0.33 s

box_reaction_1d

stepping in modes

96

0.8 ms

0.28 s

On boxes the planner solves on trial grids, which is why planning costs there what a solve costs, or more.

Where the time goes

The core marks its heavy parts with spans (pdemagic::profile). While a recording is on, each span adds its time to a phase, and --profile prints the phases of one solve. The shares below are of the time of the solve.

Case

Largest phases

poisson_free_3d

kernels 58 %, transforms 29 %

helmholtz_free_3d

kernels 76 %, transforms 13 %

wave_free_3d

propagation factors 50 %, transforms 20 %

permittivity_3d

transforms 68 %

maxwell_2d

matrix functions 78 %

reaction_tile

expressions 53 %, transforms 28 %

gray_scott

expressions 38 %, transforms 15 %, matrix functions 10 %

burgers_ring

transforms 53 %, expressions 27 %

box_poisson_3d

mode transforms 91 %

box_heat_3d

mode transforms 87 %

box_drum_2d

mode transforms 81 %, eigendecompositions 6 %

box_reaction_1d

mode transforms 50 %, expressions 45 %

bodies

potential at the grid points 77 %, charges 23 %

What the phases are:

  • Kernels. The transform of the Green’s function is evaluated for every wave of the padded box, one by one. It depends on the length of the wave vector alone, and for Poisson’s equation it is the same for every solve on a grid.

  • Propagation factors. What each wave does over a step, computed once per step length, wave by wave. For waves these are divided differences, which are evaluated by quadrature.

  • Transforms. FFTs along every axis, complex to complex, also for real fields, on one thread.

  • Mode transforms. On a box, every grid line times a dense complex matrix, also for a real problem.

  • Expressions. Sources and nonlinear terms are interpreted at every grid point, at every evaluation.

  • Matrix functions. In a system, the functions of the small matrix of each wave.

Targets

From the profiles, for the same machine. The next two milestones of the speed campaign, C2 and C3, are measured against them.

What

Now

Target

By

Kernels of Poisson, screened Poisson and Helmholtz

58 to 76 % of a solve

below 10 %

evaluating them once per distinct length of the wave vector, C2

Propagation factors of waves

50 % of a solve

below 10 %

the same, C2

Transforms of real fields

complex to complex

half the time and memory

real transforms, C2

Mode transforms of real problems

complex

half the time

real matrices where the modes are real, C2

poisson_free_3d

0.69 s

0.25 s on one core, 0.1 s on four

C2, C3

helmholtz_free_3d

5.0 s

1.5 s on one core, 0.5 s on four

C2, C3

wave_free_3d

3.8 s

2 s on one core, 0.7 s on four

C2, C3

permittivity_3d

30.9 s

16 s on one core, 6 s on four

C2, C3

box_heat_3d

1.13 s

0.6 s on one core, 0.2 s on four

C2, C3

reaction_tile

1.53 s

0.5 s on four cores

C3

Left to later milestones of the campaign: expressions compiled to machine code (C4), and a fast method for the potential of many triangles (C5).

Earlier baselines

The convolution alone, after milestone 2

Measured on a 4-core Intel Xeon at 2.1 GHz, using one core. Times are the fastest run.

Case

Time

Plan, 1D Gaussian

0.10 ms

Plan and solve, 1D Gaussian

0.12 ms

Plan, 2D Gaussian

15 ms

Plan and solve, 2D Gaussian

19 ms

Plan, 3D Gaussian

52 ms

Plan and solve, 3D Gaussian (64³ grid)

716 ms

Convolution, 1D, 4096 points

0.70 ms

Convolution, 2D, 256² points

54 ms

Convolution, 2D, 1024² points

1.08 s

Convolution, 3D, 32³ points

60 ms

Convolution, 3D, 64³ points

611 ms

Convolution, 3D, 96³ points

2.13 s

Boundary elements, after milestone 7

Measured from Python on the same machine, one core. A conducting sphere, no free charge.

Case

Time

1000 triangles, solution on 8³ points

0.2 s

1000 triangles, solution on 32³ points

1.2 s

1000 triangles, solution on 64³ points

7.9 s

4000 triangles, solution on 8³ points

4.6 s

5000 triangles, solution on 32³ points

10.6 s

Evaluating the solution costs about 30 ns per triangle and grid point, and dominates on fine grids.

“Convolution” is the method alone on a given grid. It is still measured, by cargo bench -p pdemagic --bench free_space.