Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Simulation.Core] Refactor DefaultAnimationLoop + multithreading #3959

Merged
merged 10 commits into from
Aug 25, 2023

Conversation

alxbilger
Copy link
Contributor

@alxbilger alxbilger commented Jun 16, 2023

Split all pipeline steps into smaller functions

Each step of the animation loop now corresponds to a function. For example, the following code:

sofa::helper::AdvancedTimer::stepBegin("BehaviorUpdatePositionVisitor");
BehaviorUpdatePositionVisitor beh(params , dt);
gnode->execute ( beh );
sofa::helper::AdvancedTimer::stepEnd("BehaviorUpdatePositionVisitor");

is now in a function:

void DefaultAnimationLoop::behaviorUpdatePosition(const core::ExecParams* params, const SReal dt) const
{
    sofa::helper::ScopedAdvancedTimer timer("BehaviorUpdatePositionVisitor");
    BehaviorUpdatePositionVisitor beh(params, dt);
    gnode->execute(beh);
}

In the end, the pipeline is easier to read:

sofa::core::MechanicalParams mparams(*params);
mparams.setDt(dt);

behaviorUpdatePosition(params, dt);
updateInternalData(params);

resetConstraint(params);

beginIntegration(params, dt);
{
    collisionDetection(params);

    const core::ConstraintParams cparams;
    buildConstraintMatrix(cparams);
    accumulateMatrixDeriv(cparams);

    solve(params, dt);

    projectPositionAndVelocity(nextTime, mparams);
    propagateOnlyPositionAndVelocity(nextTime, mparams);
}
endIntegration(params, dt);
computeIsolatedForces(params, dt);

updateSimulationContext(params, dt, startTime);

AnimateVisitor

AnimateVisitor is no longer used. It was difficult to read its logic (the visitor called other visitors, it relies heavily on pruning which is easy to miss). Instead, I suggest to explicit all the steps from the AnimateVisitor into the DefaultAnimationLoop. This is closer to what is done in the FreeMotionAnimationLoop. Plus, we can find common steps, which makes the code more consistent.

Note also the introduction of ComputeIsolatedForceVisitor (see the comments in the code to understand its purpose).

Multithreading

Since AnimateVisitor is no longer used, SolveVisitor is used instead, providing optional multithreading, i.e. solving each ODE in parallel. Similar to what was done in the FreeMotionAnimationLoop.

Benchmark

Measured on examples/Component/SolidMechanics/FEM/TetrahedronFEMForceField.scn

Without parallelism

[INFO]    [BatchGUI] 1000 iterations done in 14.7031 s ( 68.0128 FPS).
 LEVEL   START    NUM      MIN     MAX   MEAN     DEV    TOTAL  PERCENT ID
   2       0.03    1      10.57   27.32   11.94    2.11   11.94   82.14 ..solve

With parallelism

[INFO]    [BatchGUI] 1000 iterations done in 7.13825 s ( 140.09 FPS).
 LEVEL   START    NUM      MIN     MAX   MEAN     DEV    TOTAL  PERCENT ID
   2       0.03    1       3.53    6.85    4.45    0.81    4.45   63.60 ..solve

The simulation is 2x faster. But there are 4 different objects, so we expected a speed-up of 4x. However, the speed-up is only about the solve function. Another time consuming function is the end event when computing the von Misses stresses. Still, the solve function only shows a speedup of 2.68x, instead of 4x. I cannot explain it for now...

[ci-depends-on https://github.com/sofa-framework/SofaPython3/pull/362]


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).

@alxbilger alxbilger added enhancement About a possible enhancement pr: status to review To notify reviewers to review this pull-request labels Jun 16, 2023
@alxbilger
Copy link
Contributor Author

[ci-build][with-all-tests]

@fredroy
Copy link
Contributor

fredroy commented Jun 19, 2023

[ci-build][with-all-tests]

1 similar comment
@alxbilger
Copy link
Contributor Author

[ci-build][with-all-tests]

@alxbilger
Copy link
Contributor Author

Another benchmark: examples/Demos/chainAll.scn

With parallelism

1000 iterations done in 41.8915 s ( 23.8712 FPS).

Without parallelism:

1000 iterations done in 91.9302 s ( 10.8778 FPS).

@alxbilger alxbilger added the pr: highlighted in next release Highlight this contribution in the notes of the upcoming release label Jun 20, 2023
@alxbilger
Copy link
Contributor Author

[ci-build][with-all-tests][force-full-build]

@hugtalbot
Copy link
Contributor

[ci-build][with-all-tests] (check for MacOS)

@fredroy
Copy link
Contributor

fredroy commented Jul 12, 2023

[ci-build][with-all-tests][force-full-build]

Comment on lines 207 to 211
void DefaultAnimationLoop::buildConstraintMatrix(core::ConstraintParams cparams) const
{
sofa::helper::ScopedAdvancedTimer timer("buildConstraintMatrix");
unsigned int constraintId = 0;
mechanicalvisitor::MechanicalBuildConstraintMatrix buildConstraintMatrix(&cparams, core::MatrixDerivId::constraintJacobian(), constraintId );
buildConstraintMatrix.execute(gnode);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This function in a DefaultAnimationLoop does not make sense since only FF-based (weak) constraints can be handled with the DefaultAnimationLoop

What about removing it @alxbilger ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know what it is supposed to do. I guess we can try to remove it and see if CI complains

Copy link
Contributor

Choose a reason for hiding this comment

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

have you tested it @alxbilger ?

@alxbilger
Copy link
Contributor Author

I don't understand why the dashboard says 2 scenes are failing. When I see the result, I see only one

@hugtalbot
Copy link
Contributor

hugtalbot commented Jul 19, 2023

CI sees apparently twice an error in advancedtimer .. strange

[ERROR]   [AdvancedTimer::end] timer[Animate] does not correspond to last call to begin(cg_timer)

alxbilger and others added 8 commits July 21, 2023 14:19
The logic was difficult to read in AnimateVisitor. Instead, all the steps of the animation loop are now explicit in the the code of the animation loop.
Co-authored-by: Hugo <hugo.talbot@sofa-framework.org>
@alxbilger
Copy link
Contributor Author

There is an error in the test because timer ids changed in this PR

@sofabot
Copy link
Collaborator

sofabot commented Jul 24, 2023

[ci-depends-on] detected during build #13.

To unlock the merge button, you must

# Conflicts:
#	Sofa/framework/Simulation/Core/src/sofa/simulation/DefaultAnimationLoop.cpp
#	Sofa/framework/Simulation/Core/src/sofa/simulation/DefaultAnimationLoop.h
@sofabot
Copy link
Collaborator

sofabot commented Jul 27, 2023

[ci-depends-on] detected during build #14.

To unlock the merge button, you must

@alxbilger
Copy link
Contributor Author

The crash that happened on Windows also happens on the master branch on my computer. But I don't understand how it is not detected by the CI

@fredroy
Copy link
Contributor

fredroy commented Jul 30, 2023

The crash that happened on Windows also happens on the master branch on my computer. But I don't understand how it is not detected by the CI

Maybe it is random, it does not crash on mine (but I only tried like 2, 3 times) on msvc
Nevertheless, I dont think this scene is working as intended anyway.... (the particules do not interact with the static mesh)

@hugtalbot
Copy link
Contributor

It seems strange that the factorization of the mapping propagation at the end of the time step makes the scene crashes..
Have you investigated it further @alxbilger ?

@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 Aug 23, 2023
@alxbilger
Copy link
Contributor Author

[ci-build][with-all-tests][force-full-build]

@sofabot
Copy link
Collaborator

sofabot commented Aug 25, 2023

[ci-depends-on] detected during build #15.

To unlock the merge button, you must

@alxbilger
Copy link
Contributor Author

[ci-build][with-all-tests][force-full-build]

@sofabot
Copy link
Collaborator

sofabot commented Aug 25, 2023

[ci-depends-on] detected during build #16.

All dependencies are merged/closed and all ExternalProject pointers are up-to-date. Congrats! 👍

@alxbilger alxbilger merged commit e0d5432 into sofa-framework:master Aug 25, 2023
@hugtalbot hugtalbot added this to the v23.12 milestone Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement About a possible enhancement pr: highlighted in next release Highlight this contribution in the notes of the upcoming release pr: status ready Approved a pull-request, ready to be squashed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants