Go to the documentation of this file.
22 #ifndef COMPUTATION_GRAPH_H
23 #define COMPUTATION_GRAPH_H
58 typedef std::vector<ComputationalGraph::vertex>
comp_layer;
69 if(this->_state > GraphState::EMPTY)
70 throw NRPException::logCreate(
"Inserting edges while the graph is being configured or is configured is not allowed");
76 throw NRPException::logCreate(
"Attempt to insert edge with Output node \"" + a->
id() +
"\" as source. This is not allowed");
78 throw NRPException::logCreate(
"Attempt to insert edge with Input node \"" + b->
id() +
"\" as target. This is not allowed");
88 if(this->_state == GraphState::CONFIGURING)
90 else if( this->_state == GraphState::COMPUTING)
96 this->_state = GraphState::EMPTY;
104 if(this->_state > GraphState::EMPTY)
105 throw NRPException::logCreate(
"Graph is already configured. Please reset the graph first by calling 'clear()'");
107 this->_state = GraphState::CONFIGURING;
111 for (
const auto &e: *
this)
112 e.first->configure();
118 for (
const auto &e: *
this)
119 e.first->setVisited(
false);
126 setFirstLayer(layer);
127 while (!layer.empty()) {
128 _compLayers.push_back(layer);
130 setNextLayer(_compLayers.back(), layer);
133 if (checkForCycles())
137 this->_state = GraphState::READY;
139 catch(
const std::exception& e) {
140 this->_state = GraphState::READY;
151 if (this->_state < GraphState::READY)
152 throw NRPException::logCreate(
"Graph is not configured. Please (re-)build the graph by calling 'configure()'");
153 else if (this->_state == GraphState::COMPUTING)
156 this->_state = GraphState::COMPUTING;
160 sendCycleStartSignal();
166 for (
auto &
node: _inputLayer)
171 for (
auto &layer: _compLayers)
172 for (
auto &
node: layer)
173 if (this->_execMode == ExecMode::ALL_NODES ||
node->
doCompute()) {
178 for (
auto &
node: _outputLayer)
179 if (this->_execMode == ExecMode::ALL_NODES ||
node->
doCompute()) {
184 this->_state = GraphState::READY;
186 catch(
const std::exception& e) {
187 this->_state = GraphState::READY;
196 {
return this->_state; }
200 if( this->_state == GraphState::COMPUTING)
207 {
return _execMode; }
211 void sendCycleStartSignal()
215 for (
auto &
node: _outputLayer) {
219 if(this->_execMode == ExecMode::OUTPUT_DRIVEN &&
node->
doCompute())
220 propagateExecSignalBack(
node);
230 propagateExecSignalBack(
node);
240 _outputLayer.clear();
249 for(
const auto &e : *
this) {
250 if(!e.first->isVisited() && e.first->type() == ComputationalNode::NodeType::Input) {
251 e.first->setVisited(
true);
252 _inputLayer.push_back(e.first);
255 if(!e.first->isVisited() && e.first->type() == ComputationalNode::NodeType::Output) {
256 e.first->setVisited(
true);
257 _outputLayer.push_back(e.first);
269 for(
const auto &e : *
this) {
271 if(v->
isVisited() || v->
type() != ComputationalNode::NodeType::Functional)
276 if(i->type() != ComputationalNode::NodeType::Input) {
297 for(
const auto &v : prev_layer)
301 for(
const auto &v : output_set) {
302 if(v->
type() == ComputationalNode::NodeType::Output)
307 if(i->type() != ComputationalNode::NodeType::Input && !i->isVisited()) {
319 bool checkForCycles()
321 for(
const auto &e : *
this)
322 if(!e.first->isVisited())
330 std::vector<comp_layer> _compLayers;
334 ExecMode _execMode = ExecMode::ALL_NODES;
340 #endif //COMPUTATION_GRAPH_H
void clear()
Clear graph.
Definition: computational_graph.h:86
void insert_edge(const vertex &a, const vertex &b)
Insert edge.
Definition: computational_graph.h:67
ExecMode getExecMode()
Definition: computational_graph.h:206
void compute()
Executes all nodes in the graph in order.
Definition: computational_graph.h:149
void setExecMode(ExecMode mode)
Definition: computational_graph.h:198
void setDoCompute(bool doCompute)
Sets a value for the node 'doCompute' property, used in some graph execution modes.
Definition: computational_node.h:87
NodeType type() const
Returns the node 'type'.
Definition: computational_node.h:63
@ ALL_NODES
Definition: computational_graph.h:62
void setVisited(bool visited)
Sets a value for the node 'visited' property, used for graph traversing.
Definition: computational_node.h:75
@ Output
Definition: computational_node.h:37
const std::string & id() const
Returns the node 'id'.
Definition: computational_node.h:57
bool isVisited() const
Returns true if the node has been marked as visited, false otherwise.
Definition: computational_node.h:81
iterator begin()
Definition: ngraph.hpp:139
@ COMPUTING
Definition: computational_graph.h:60
@ EMPTY
Definition: computational_graph.h:60
void clear()
Definition: ngraph.hpp:144
virtual void compute()=0
Requests the node to execute its computation.
@ OUTPUT_DRIVEN
Definition: computational_graph.h:62
std::set< vertex > vertex_set
Definition: ngraph.hpp:89
static const vertex & node(const_iterator p)
Definition: ngraph.hpp:700
ExecMode
Definition: computational_graph.h:62
virtual bool doCompute() const
Tells if this node should be executed in this graph execution cycle, used in some graph execution mod...
Definition: computational_node.h:96
static EXCEPTION logCreate(LOG_EXCEPTION_T &exception, const std::string &msg, NRPLogger::spdlog_out_fcn_t spdlogCall=NRPLogger::critical)
Definition: nrp_exceptions.h:73
@ READY
Definition: computational_graph.h:60
void insert_edge(iterator pa, iterator pb)
Definition: ngraph.hpp:282
const vertex_set & in_neighbors(const vertex &a) const
Definition: ngraph.hpp:213
void configure()
Creates the graph execution structure and call 'configure' on each node.
Definition: computational_graph.h:102
@ CONFIGURING
Definition: computational_graph.h:60
Definition: ngraph.hpp:80
GraphState getState() const
Returns true if the graph is configured, false otherwise.
Definition: computational_graph.h:195
Definition: computational_graph.h:53
virtual void graphCycleStartCB()
Function called by the Computational Graph at the beginning of a new execution cycle.
Definition: computational_node.h:136
@ Input
Definition: computational_node.h:36
const vertex_set & out_neighbors(const vertex &a) const
Definition: ngraph.hpp:218
iterator end()
Definition: ngraph.hpp:141
Base class implementing a node in the computational graph.
Definition: computational_node.h:31
GraphState
Definition: computational_graph.h:60
std::vector< ComputationalGraph::vertex > comp_layer
Definition: computational_graph.h:58