Skip to content

Commit

Permalink
[dataflow][core] try to fix random crash : Graph
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasPaulin committed Feb 27, 2023
1 parent 62b0e96 commit 3d690a3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
42 changes: 19 additions & 23 deletions src/Dataflow/Core/DataflowGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,21 +274,15 @@ bool DataflowGraph::canAdd( const Node* newNode ) const {
}

std::pair<bool, Node*> DataflowGraph::addNode( std::unique_ptr<Node> newNode ) {
std::map<std::string, std::string> m_mapInputs;
// Check if the new node already exists (= same name and type)
if ( canAdd( newNode.get() ) ) {
if ( newNode->getInputs().empty() ) {
// it is a source node, add its interface port as input and data setter to the graph
auto& interfaces = newNode->buildInterfaces( this );
if ( newNode->getInputs().empty() || newNode->getOutputs().empty() ) {
bool ( DataflowGraph::*addGraphIOPort )( PortBase* ) = newNode->getInputs().empty()
? &DataflowGraph::addSetter
: &DataflowGraph::addGetter;
auto& interfaces = newNode->buildInterfaces( this );
for ( auto p : interfaces ) {
addSetter( p );
}
}
if ( newNode->getOutputs().empty() ) {
// it is a sink node, add its interface port as output to the graph
auto& interfaces = newNode->buildInterfaces( this );
for ( auto p : interfaces ) {
addGetter( p );
( this->*addGraphIOPort )( p );
}
}
auto addedNode = newNode.get();
Expand All @@ -310,14 +304,12 @@ bool DataflowGraph::removeNode( Node*& node ) {
int index = -1;
if ( ( index = findNode( node ) ) == -1 ) { return false; }
else {
if ( node->getInputs().empty() ) { // Check if it is a source node
for ( auto& port : node->getInterfaces() ) {
removeSetter( port->getName() );
}
}
if ( node->getOutputs().empty() ) { // Check if it is a sink node
for ( auto& port : node->getInterfaces() ) {
removeGetter( port->getName() );
if ( node->getInputs().empty() || node->getOutputs().empty() ) {
bool ( DataflowGraph::*removeGraphIOPort )( const std::string& ) =
node->getInputs().empty() ? &DataflowGraph::removeSetter
: &DataflowGraph::removeGetter;
for ( auto& p : node->getInterfaces() ) {
( this->*removeGraphIOPort )( p->getName() );
}
}
m_nodes.erase( m_nodes.begin() + index );
Expand Down Expand Up @@ -597,12 +589,16 @@ bool DataflowGraph::removeSetter( const std::string& setterName ) {

bool DataflowGraph::addGetter( PortBase* out ) {
if ( out->is_input() ) { return false; }
// This is very similar to addOutput, except the data can't be set, they will be in the init
// of any Sink
// This is very similar to addOutput, except the data can't be set.
// Data pointer must be set by any sink at compile time, in the init function to refer to the
// data fetched from the associated input link
bool found = false;
// TODO check if this verification is needed ?
for ( auto& output : m_outputs ) {
if ( output->getName() == out->getName() ) { found = true; }
if ( output->getName() == out->getName() ) {
found = true;
break;
}
}
if ( !found ) { m_outputs.emplace_back( out ); }
return !found;
Expand Down
23 changes: 12 additions & 11 deletions src/Dataflow/Core/DataflowGraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ class RA_DATAFLOW_API DataflowGraph : public Node
/// This flag is useless outside an load/edit/save scenario
bool m_shouldBeSaved { false };

/// \brief Creates an output port connected to the named input port of the graph.
/// \brief Gets an output port connected to the named input port of the graph.
/// Return the connected output port if success, sharing the ownership with the caller.
/// Allows to set data to the graph from the caller .
/// This output port could then be used through setter->setData( ptr ) to set the graph input
/// from the data pointer owned by the caller.
/// \note As ownership is shared with the caller, the graph must survive the returned
/// pointer to be able to use the dataSetter..
/// \params portName The name of the input port of the graph
Expand All @@ -141,10 +142,10 @@ class RA_DATAFLOW_API DataflowGraph : public Node
/// \brief connect the data setting port from its inputs.
bool activateDataSetter( const std::string& portName );

/// Returns an alias to the named output port of the graph.
/// \brief Returns an alias to the named output port of the graph.
/// Allows to get the data stored at this port after the execution of the graph.
/// \note ownership is left to the graph. The graph must survive the returned
/// pointer to be able to use the dataGetter..
/// \note ownership is left to the graph, not shared. The graph must survive the returned
/// pointer to be able to use the dataGetter.
/// \params portName the name of the output port
PortBase* getDataGetter( const std::string& portName );

Expand All @@ -157,17 +158,17 @@ class RA_DATAFLOW_API DataflowGraph : public Node
/// \brief Data getter descriptor.
/// A Data getter descriptor is composed of an output port (belonging to any node of the graph),
/// its name and its type.
/// Use getData on the output port to extract data from the graph
/// Use getData on the output port to extract data from the graph.
/// \note, a dataGetter is valid only after successful compilation of the graph.
/// \todo find a way to test the validity of the getter (invalid if no path exists from any
/// source port to the associated sink port)
using DataGetterDesc = std::tuple<PortBase*, std::string, std::string>;

/// Creates a vector that stores all the DataSetters (\see getDataSetter) of the graph.

/// \note If called multiple times for the same port, only the last returned result is
/// usable.
/// Creates a vector that stores all the existing DataSetters (\see getDataSetter) of the graph.
/// TODO : Verify why, when listing the data setters, they are connected ...
std::vector<DataSetterDesc> getAllDataSetters() const;

/// Creates a vector that stores all the DataGetters (\see getDataGetter) of the graph.
/// Creates a vector that stores all the existing DataGetters (\see getDataGetter) of the graph.
/// A tuple is composed of an output port belonging to the graph, its name its type.
std::vector<DataGetterDesc> getAllDataGetters() const;

Expand Down

0 comments on commit 3d690a3

Please sign in to comment.