Skip to content

Commit

Permalink
restore and deprecate functions using localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
fredroy committed Dec 1, 2023
1 parent 1183ebe commit 0cc78ad
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <sofa/core/MechanicalParams.h>
#include <sofa/simulation/Node.h>
#include <sofa/simulation/LocalStorage.h>
#include <sofa/core/behavior/BaseMass.h>
#include <sofa/core/behavior/ConstraintSolver.h>
#include <sofa/core/behavior/BaseInteractionConstraint.h>
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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*/);

Expand Down Expand Up @@ -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*/);

Expand Down
12 changes: 12 additions & 0 deletions Sofa/framework/Simulation/Core/src/sofa/simulation/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
namespace sofa::simulation
{

class LocalStorage;

/// Base class for visitors propagated recursively through the scenegraph
class SOFA_SIMULATION_CORE_API Visitor
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0cc78ad

Please sign in to comment.