Skip to content

[LinearSolver.Direct] BTDLinearSolver: do not clear the dense inverse at each factorization - #6433

Merged
hugtalbot merged 2 commits into
sofa-framework:masterfrom
fredroy:fix_btd_minv_clear
Nov 6, 2026
Merged

hugtalbot merged 2 commits into
sofa-framework:masterfrom
fredroy:fix_btd_minv_clear

Conversation

@fredroy

@fredroy fredroy commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

invert() resized Minv at every factorization, and BlockFullMatrix::resize() always clears all its nb*nb blocks.

It appeared that clear() seems superfluous, as the block as always written before being read.
Empirical checks:

- Every write is a plain assignment.
- computeMinvBlock only reads blocks it has already written
- getMinvElement → computeMinvBlock before reading. 

and Claude tested by filling the Minv with NaN instead of clearing -> 0 NaN reads.

Not really a real proof but seems OK for me © 😅

Results: use case in BeamAdapter,
Iin 3instruments_collis, the default tools has 180 nodes, results are roughly similar

master: 5000 iterations done in 21.9278 s ( 228.021 FPS)
PR:     5000 iterations done in 21.4084 s ( 233.553 FPS)

if we increase the number of nodes (only in the mechanical object) to e.g 580 nodes

master 5000 iterations done in 34.5094 s ( 144.888 FPS).
PR     5000 iterations done in 26.2973 s ( 190.134 FPS).

and 1080 nodes

master 5000 iterations done in 62.2104 s ( 80.3724 FPS).
PR.    5000 iterations done in 33.0253 s ( 151.399 FPS)

my first version was introducing a Data to force clearing anyway (by default) to keep the compatibility, but removed it anyway as it seems totally useless.

Note that it will clear anyway if the size is different between factorization

[with-all-tests]


By submitting this pull request, I acknowledge that
I have read, understand, and agree SOFA Developer Certificate of Origin (DCO).


Reviewers will merge this pull-request only if

  • it builds with SUCCESS for all platforms on the CI.
  • it does not generate new warnings.
  • it does not generate new unit test failures.
  • it does not generate new scene test failures.
  • it does not break API compatibility.
  • it is more than 1 week old (or has fast-merge label).

@fredroy fredroy added pr: enhancement About a possible enhancement pr: status to review To notify reviewers to review this pull-request pr: AI-aided Label notifying the reviewers that part or all of the PR has been generated with the help of an AI labels Oct 29, 2026
… at each factorization

invert() resized Minv at every factorization, and BlockFullMatrix::resize()
always clears all its nb*nb blocks.

Clearing is not needed: a block of Minv is only read after
computeMinvBlock() has written it (tracked by nBlockComputedMinv, reset at
each factorization). Minv is now only resized when its size changes.
The previous behaviour can be restored with the new forceClearInverse Data.
Clearing the dense inverse is never needed: every block of Minv is
written (plain assignment) by computeMinvBlock() before being read, and
nBlockComputedMinv is reset at each factorization, whatever the topology.
A size change still goes through resize(), as before.

Checked by filling Minv with NaN instead of clearing it: no NaN read and
bit-identical trajectories on the BTD example scenes, including
subpartSolve and partial_solve (unbuilt Gauss-Seidel) paths.
@fredroy
fredroy force-pushed the fix_btd_minv_clear branch from a92b1c6 to e0aa6d5 Compare October 30, 2026 01:15

@EulalieCoevoet EulalieCoevoet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds and looks good to me. The method resize already checks if the resize is needed. However it also always clears the matrix, which can be expensive.
If I understood the code correctly, I agree with the comment that this clear is not necessary.

@hugtalbot hugtalbot added pr: status ready Approved a pull-request, ready to be squashed and removed pr: status to review To notify reviewers to review this pull-request labels Nov 6, 2026
@hugtalbot
hugtalbot merged commit 3cd174c into sofa-framework:master Nov 6, 2026
19 of 24 checks passed
@hugtalbot
hugtalbot deleted the fix_btd_minv_clear branch November 6, 2026 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: AI-aided Label notifying the reviewers that part or all of the PR has been generated with the help of an AI pr: enhancement About a possible enhancement pr: status ready Approved a pull-request, ready to be squashed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants