From 0cc78ade1220a3679b691a9d229be698b044eef7 Mon Sep 17 00:00:00 2001 From: Frederick Roy Date: Fri, 1 Dec 2023 10:38:53 +0900 Subject: [PATCH] restore and deprecate functions using localstorage --- .../sofa/simulation/BaseMechanicalVisitor.cpp | 58 +++++++++++++++++++ .../sofa/simulation/BaseMechanicalVisitor.h | 10 ++++ .../Core/src/sofa/simulation/Visitor.h | 12 ++++ 3 files changed, 80 insertions(+) diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/BaseMechanicalVisitor.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/BaseMechanicalVisitor.cpp index 1663a03fd19..be36afdee57 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/BaseMechanicalVisitor.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/BaseMechanicalVisitor.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -196,6 +197,63 @@ Visitor::Result BaseMechanicalVisitor::fwdInteractionConstraint(VisitorContext* return fwdConstraintSet(ctx->node, c); } + +Visitor::Result BaseMechanicalVisitor::processNodeTopDown(simulation::Node* node, LocalStorage* stack) +{ + if (root == nullptr) + { + root = node; + } + + VisitorContext ctx; + ctx.root = root; + ctx.node = node; + ctx.nodeData = rootData; + + const bool writeData = writeNodeData(); + if (writeData) + { + // create temporary accumulation buffer for parallel reductions (dot products) + if (node != root) + { + const SReal* parentData = stack->empty() ? rootData : (SReal*)stack->top(); + ctx.nodeData = new SReal(0.0); + setNodeData(node, ctx.nodeData, parentData); + stack->push(ctx.nodeData); + } + } + + return processNodeTopDown(node, &ctx); +} + + +void BaseMechanicalVisitor::processNodeBottomUp(simulation::Node* node, LocalStorage* stack) +{ + VisitorContext ctx; + ctx.root = root; + ctx.node = node; + ctx.nodeData = rootData; + SReal* parentData = rootData; + + const bool writeData = writeNodeData(); + + if (writeData) + { + // use temporary accumulation buffer for parallel reductions (dot products) + if (node != root) + { + ctx.nodeData = (SReal*)stack->pop(); + parentData = stack->empty() ? rootData : (SReal*)stack->top(); + } + } + + processNodeBottomUp(node, &ctx); + + if (writeData && parentData != ctx.nodeData) + addNodeData(node, parentData, ctx.nodeData); +} + + #ifdef SOFA_DUMP_VISITOR_INFO void BaseMechanicalVisitor::printReadVectors(core::behavior::BaseMechanicalState* mm) diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/BaseMechanicalVisitor.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/BaseMechanicalVisitor.h index 6137eabd050..bd25f9e25b7 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/BaseMechanicalVisitor.h +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/BaseMechanicalVisitor.h @@ -77,6 +77,11 @@ class SOFA_SIMULATION_CORE_API BaseMechanicalVisitor : public Visitor /// This method calls the fwd* methods during the forward traversal. You typically do not overload it. Result processNodeTopDown(simulation::Node* node) override; + /// Parallel version of processNodeTopDown. + /// This method calls the fwd* methods during the forward traversal. You typically do not overload it. + SOFA_ATTRIBUTE_DEPRECATED_LOCALSTORAGE() + Result processNodeTopDown(simulation::Node * node, LocalStorage * stack) override; + /// Process the OdeSolver virtual Result fwdOdeSolver(simulation::Node* /*node*/, sofa::core::behavior::OdeSolver* /*solver*/); @@ -163,6 +168,11 @@ class SOFA_SIMULATION_CORE_API BaseMechanicalVisitor : public Visitor /// This method calls the bwd* methods during the backward traversal. You typically do not overload it. void processNodeBottomUp(simulation::Node* node) override; + /// Parallel version of processNodeBottomUp. + /// This method calls the bwd* methods during the backward traversal. You typically do not overload it. + SOFA_ATTRIBUTE_DEPRECATED_LOCALSTORAGE() + void processNodeBottomUp(simulation::Node* /*node*/, LocalStorage * stack) override; + /// Process the BaseMechanicalState when it is not mapped from parent level virtual void bwdMechanicalState(simulation::Node* /*node*/,sofa::core::behavior::BaseMechanicalState* /*mm*/); diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/Visitor.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/Visitor.h index 20a292c8758..90b09b3da91 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/Visitor.h +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/Visitor.h @@ -37,6 +37,8 @@ namespace sofa::simulation { +class LocalStorage; + /// Base class for visitors propagated recursively through the scenegraph class SOFA_SIMULATION_CORE_API Visitor { @@ -156,6 +158,16 @@ class SOFA_SIMULATION_CORE_API Visitor /// Specify whether this visitor can be parallelized. virtual bool isThreadSafe() const { return false; } + /// Callback method called when decending to a new node. Recursion will stop if this method returns RESULT_PRUNE + /// This version is offered a LocalStorage to store temporary data + SOFA_ATTRIBUTE_DEPRECATED_LOCALSTORAGE() + virtual Result processNodeTopDown(simulation::Node* node, LocalStorage*) { return processNodeTopDown(node); } + + /// Callback method called after child node have been processed and before going back to the parent node. + /// This version is offered a LocalStorage to store temporary data + SOFA_ATTRIBUTE_DEPRECATED_LOCALSTORAGE() + virtual void processNodeBottomUp(simulation::Node* node, LocalStorage*) { processNodeBottomUp(node); } + typedef sofa::core::objectmodel::Tag Tag; typedef sofa::core::objectmodel::TagSet TagSet; /// list of the subsets