Skip to content

CG solver#83

Draft
sbiquard wants to merge 2 commits intomainfrom
sb/cg
Draft

CG solver#83
sbiquard wants to merge 2 commits intomainfrom
sb/cg

Conversation

@sbiquard
Copy link
Copy Markdown
Member

@sbiquard sbiquard commented Mar 12, 2026

This adds our own implementation of a conjugate gradient solver.

Comparison with lineax's CG solver:

  • Unlike lineax, we record the residual norm at every iteration to monitor convergence. (This is the main motivation for this implementation.)
  • Unlike lineax, we only use the standard convergence criterion in the 'b' space (given Ay=b), i.e. we do not check if the solution is still updating. (This may change.)
  • Like lineax, we stabilise the residual every 10 (by default) iteration steps to improve numerical stability.

Interestingly, this implementation seems to perform consistently better than lineax, at least according to this small benchmarking script which on my laptop produces the following results. The number of steps reported is not the same (not exactly sure why, I tested with the same convergence criterion and got the same results).

method=furax cg, n=100: 77.531 µs ± 9.35% (median of 7 runs, 5000 loops each)
method=lineax cg, n=100: 540.241 µs ± 12.84% (median of 7 runs, 1 loops each)
n=100:  furax iters=42  lineax iters=46

method=furax cg, n=1000: 482.796 µs ± 6.26% (median of 7 runs, 1000 loops each)
method=lineax cg, n=1000: 1.455 ms ± 15.64% (median of 7 runs, 500 loops each)
n=1000:  furax iters=57  lineax iters=68

method=furax cg, n=10000: 2.231 ms ± 3.94% (median of 7 runs, 200 loops each)
method=lineax cg, n=10000: 8.514 ms ± 21.21% (median of 7 runs, 100 loops each)
n=10000:  furax iters=56  lineax iters=71

┌───────────┬────────┬────────────────────────────┬───────────┬───────────────────────────┬───────────────────────┐
│ method    ┆ n      ┆ median_execution_time (ms) ┆ ± (%)     ┆ first_execution_time (ms) ┆ compilation_time (ms) │
╞═══════════╪════════╪════════════════════════════╪═══════════╪═══════════════════════════╪═══════════════════════╡
│ furax cg  ┆ 100    ┆ 0.077531                   ┆ 9.352299  ┆ 183.34523                 ┆ 0.53148               │
│ lineax cg ┆ 100    ┆ 0.540241                   ┆ 12.842919 ┆ 301.850555                ┆ 0.529054              │
│ furax cg  ┆ 1_000  ┆ 0.482796                   ┆ 6.264044  ┆ 177.919875                ┆ 0.612218              │
│ lineax cg ┆ 1_000  ┆ 1.455173                   ┆ 15.643046 ┆ 244.980239                ┆ 0.490225              │
│ furax cg  ┆ 10_000 ┆ 2.231452                   ┆ 3.941657  ┆ 174.208146                ┆ 0.483637              │
│ lineax cg ┆ 10_000 ┆ 8.513943                   ┆ 21.206212 ┆ 273.003013                ┆ 0.628656              │
└───────────┴────────┴────────────────────────────┴───────────┴───────────────────────────┴───────────────────────┘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant