All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PRGraphVisitors.cpp
Go to the documentation of this file.
1 #include "PRGraphVisitors.h"
2 
3 namespace concurrency {
4 
5  //---------------------------------------------------------------------------
7 
8  if (node.m_graph->getNodeDecisions(m_slotNum)[node.getNodeIndex()] != 1)
9  return true;
10  return false;
11  }
12 
13  //---------------------------------------------------------------------------
15 
16  //std::cout << "1-st level Decision: " << node.getNodeName() << std::endl;
17  bool allChildDecisionsResolved = true;
18  for (auto child : node.getDaughters()) {
19  int& childDecision = child->m_graph->getNodeDecisions(m_slotNum)[child->getNodeIndex()];
20 
21  if (childDecision == 1 && node.m_modeOR && node.m_modePromptDecision) {
23  return true;
24  }
25 
26  if (childDecision == -1) {
27  allChildDecisionsResolved = false;
28  }
29  }
30 
31  if (allChildDecisionsResolved)
33 
34  return allChildDecisionsResolved;
35  }
36 
37  //---------------------------------------------------------------------------
39 
40  if (node.m_graph->getNodeDecisions(m_slotNum)[node.getNodeIndex()] != 1)
41  return true;
42  return false;
43  }
44 
45 
46 
47  //---------------------------------------------------------------------------
49 
50  if (node.m_graph->getNodeDecisions(m_slotNum)[node.getNodeIndex()] != 1)
51  return true;
52  return false;
53  }
54 
55  //--------------------------------------------------------------------------
57 
60  int& decision = decisions[node.getNodeIndex()];
61 
62  if (State::INITIAL == states[node.getAlgoIndex()]) {
63  states.updateState(node.getAlgoIndex(), State::CONTROLREADY);
65  states.updateState(node.getAlgoIndex(), State::DATAREADY);
66  states.updateState(node.getAlgoIndex(), State::SCHEDULED);
67  states.updateState(node.getAlgoIndex(), State::EVTACCEPTED);
68  decision = 1;
70  //std::cout << "Algorithm decided: " << node.getNodeName() << std::endl;
71  return true;
72  }
73  } else if (State::CONTROLREADY == states[node.getAlgoIndex()] && node.dataDependenciesSatisfied(m_slotNum)) {
74  states.updateState(node.getAlgoIndex(), State::DATAREADY);
75  states.updateState(node.getAlgoIndex(), State::SCHEDULED);
76  states.updateState(node.getAlgoIndex(), State::EVTACCEPTED);
77  decision = 1;
79  //std::cout << "Algorithm decided: " << node.getNodeName() << std::endl;
80  return true;
81  }
82 
83  return false;
84  }
85 
86 
87 
88  //---------------------------------------------------------------------------
89  bool Trigger::visitEnter(DecisionNode& node) const {
90 
91  if (node.m_graph->getNodeDecisions(m_slotNum)[node.getNodeIndex()] != -1)
92  return false;
93  return true;
94  }
95 
96  //---------------------------------------------------------------------------
98 
99  //std::cout << "1-st level Decision: " << node.getNodeName() << std::endl;
100  bool allChildDecisionsResolved = true;
101  for (auto child : node.getDaughters()) {
102  int& childDecision = child->m_graph->getNodeDecisions(m_slotNum)[child->getNodeIndex()];
103 
104  if (childDecision == 1 && node.m_modeOR && node.m_modePromptDecision) {
105  node.m_graph->getNodeDecisions(m_slotNum)[node.getNodeIndex()] = 1;
106  return true;
107  }
108 
109  if (childDecision == -1) {
110  allChildDecisionsResolved = false;
111  }
112  }
113 
114  if (allChildDecisionsResolved)
115  node.m_graph->getNodeDecisions(m_slotNum)[node.getNodeIndex()] = 1;
116 
117  return allChildDecisionsResolved;
118  }
119 
120 
121 
122  //---------------------------------------------------------------------------
123  bool Trigger::visitEnter(AlgorithmNode& node) const {
124 
125  if (node.m_graph->getNodeDecisions(m_slotNum)[node.getNodeIndex()] != -1)
126  return false;
127  return true;
128  }
129 
130  //--------------------------------------------------------------------------
132 
133  bool result = false;
134 
135  auto& decisions = node.m_graph->getNodeDecisions(m_slotNum);
136  auto& states = node.m_graph->getAlgoStates(m_slotNum);
137 
138  // Try to shift an algorithm with I->CF, and then, if successful, with CF->DF
139  if (node.promoteToControlReadyState(m_slotNum,states,decisions))
140  result = node.promoteToDataReadyState(m_slotNum);
141 
142  //returns true only when an algorithm is DF-ready
143  // i.e., the visitor reached its final goal with the algorithm
144  return result;
145  }
146 
147 
148  //---------------------------------------------------------------------------
150 
151  if (node.m_graph->getNodeDecisions(m_slotNum)[node.getNodeIndex()] != -1)
152  return false;
153  return true;
154  }
155 
156  //---------------------------------------------------------------------------
158 
159  bool foundNonResolvedChild = false;
160  bool foundNegativeChild = false;
161  bool foundPositiveChild = false;
162  int decision = -1;
163 
164  for (auto child : node.getDaughters()) {
165  int& childDecision = child->m_graph->getNodeDecisions(m_slotNum)[child->getNodeIndex()];
166 
167  if (childDecision == -1)
168  foundNonResolvedChild = true;
169  else if (childDecision == 1)
170  foundPositiveChild = true;
171  else
172  foundNegativeChild = true;
173 
174  if (node.m_modePromptDecision) {
175  if (node.m_modeOR && foundPositiveChild) {
176  decision = 1;
177  break;
178  } else if (!node.m_modeOR && foundNegativeChild) {
179  decision = 0;
180  break;
181  }
182  } else {
183  if (foundNonResolvedChild)
184  break;
185  }
186  } // end monitoring children
187 
188  if (!foundNonResolvedChild && decision == -1) {
189  if (node.m_modeOR) // OR
190  if (foundPositiveChild) decision = 1;
191  else decision = 0;
192  else // AND
193  if (foundNegativeChild) decision = 0;
194  else decision = 1;
195  }
196 
197  if (node.m_allPass && !foundNonResolvedChild)
198  decision = 1;
199 
200  if (decision != -1) {
201  node.m_graph->getNodeDecisions(m_slotNum)[node.getNodeIndex()] = decision;
202  return true;
203  }
204  return false;
205  }
206 
207 
208 
209  //---------------------------------------------------------------------------
211 
212  if (node.m_graph->getNodeDecisions(m_slotNum)[node.getNodeIndex()] != -1)
213  return false;
214  return true;
215  }
216 
217  //--------------------------------------------------------------------------
219 
220  bool result = false;
221 
222  auto& decisions = node.m_graph->getNodeDecisions(m_slotNum);
223  auto& states = node.m_graph->getAlgoStates(m_slotNum);
224 
225  // Try to shift an algorithm with I->CF, and then, if successful, with CF->DF
226  if (node.promoteToControlReadyState(m_slotNum,states,decisions))
227  result = node.promoteToDataReadyState(m_slotNum);
228 
229  //returns true only when an algorithm is DF-ready
230  // i.e., the visitor reached its final goal with the algorithm
231  return result;
232  }
233 
234 
235  //--------------------------------------------------------------------------
237 
238  auto& products = node.getOutputDataNodes();
239  float rank = 0;
240 
241  for (auto p : products)
242  rank += p->getConsumers().size();
243 
244  node.setRank(rank);
245  /*std::stringstream s;
246  s << node.getNodeName() << ", " << rank << "\n";
247  std::ofstream myfile;
248  myfile.open("AlgoRank.csv", std::ios::app);
249  myfile << s.str();
250  myfile.close();*/
251 
252  return true;
253  }
254 
255  //--------------------------------------------------------------------------
257 
258  std::ifstream myfile;
259  myfile.open("InputExecutionPlan.graphml", std::ios::in);
260 
261  boost::ExecPlan execPlan;
262 
263  boost::dynamic_properties dp;
264  dp.property("name", boost::get(&boost::AlgoNodeStruct::m_name, execPlan));
265  dp.property("index", boost::get(&boost::AlgoNodeStruct::m_index, execPlan));
266  dp.property("dataRank", boost::get(&boost::AlgoNodeStruct::m_rank, execPlan));
267  dp.property("runtime", boost::get(&boost::AlgoNodeStruct::m_runtime, execPlan));
268 
269  boost::read_graphml(myfile, execPlan, dp);
270 
271  typedef boost::graph_traits<boost::ExecPlan>::vertex_iterator itV;
273  typedef boost::graph_traits<boost::ExecPlan>::vertex_descriptor AlgoVertex;
274 
275  for (vp = boost::vertices(execPlan); vp.first != vp.second; ++vp.first) {
276  AlgoVertex v = *vp.first;
277  auto index = boost::get(&boost::AlgoNodeStruct::m_name, execPlan);
278  if (index[v] == node.getNodeName()) {
279  runThroughAdjacents(v,execPlan);
280  float rank = m_nodesSucceeded;
281  node.setRank(rank);
282  reset();
283  //std::cout << "Rank of " << index[v] << " is " << rank << std::endl;
284  }
285  }
286 
287  return true;
288  }
289 
290  //--------------------------------------------------------------------------
291  void RankerByCummulativeOutDegree::runThroughAdjacents(boost::graph_traits<boost::ExecPlan>::vertex_descriptor vertex,
293  typename boost::graph_traits<boost::ExecPlan>::adjacency_iterator itVB;
294  typename boost::graph_traits<boost::ExecPlan>::adjacency_iterator itVE;
295 
296  for (boost::tie(itVB, itVE) = adjacent_vertices(vertex, graph); itVB != itVE; ++itVB) {
297  m_nodesSucceeded += 1;
298  runThroughAdjacents(*itVB, graph);
299  }
300 
301  }
302 
303  //--------------------------------------------------------------------------
305 
306  std::ifstream myfile;
307  myfile.open("InputExecutionPlan.graphml", std::ios::in);
308 
309  boost::ExecPlan execPlan;
310 
311  boost::dynamic_properties dp;
312  dp.property("name", boost::get(&boost::AlgoNodeStruct::m_name, execPlan));
313  dp.property("index", boost::get(&boost::AlgoNodeStruct::m_index, execPlan));
314  dp.property("dataRank", boost::get(&boost::AlgoNodeStruct::m_rank, execPlan));
315  dp.property("runtime", boost::get(&boost::AlgoNodeStruct::m_runtime, execPlan));
316 
317  boost::read_graphml(myfile, execPlan, dp);
318 
319  typedef boost::graph_traits<boost::ExecPlan>::vertex_iterator itV;
321  typedef boost::graph_traits<boost::ExecPlan>::vertex_descriptor AlgoVertex;
322 
323  for (vp = boost::vertices(execPlan); vp.first != vp.second; ++vp.first) {
324  AlgoVertex v = *vp.first;
325  auto index = boost::get(&boost::AlgoNodeStruct::m_name, execPlan);
326  if (index[v] == node.getNodeName()) {
327  auto index_runtime = boost::get(&boost::AlgoNodeStruct::m_runtime, execPlan);
328  float rank = index_runtime[v];
329  node.setRank(rank);
330  //std::cout << "Rank of " << index[v] << " is " << rank << std::endl;
331  }
332  }
333  return true;
334  }
335 
336  //--------------------------------------------------------------------------
338 
339  std::ifstream myfile;
340  myfile.open("Eccentricity.graphml", std::ios::in);
341 
342  boost::ExecPlan execPlan;
343 
344  boost::dynamic_properties dp;
345  dp.property("name", boost::get(&boost::AlgoNodeStruct::m_name, execPlan));
346  dp.property("Eccentricity", boost::get(&boost::AlgoNodeStruct::m_eccentricity, execPlan));
347 
348  boost::read_graphml(myfile, execPlan, dp);
349 
350  typedef boost::graph_traits<boost::ExecPlan>::vertex_iterator itV;
352  typedef boost::graph_traits<boost::ExecPlan>::vertex_descriptor AlgoVertex;
353 
354  for (vp = boost::vertices(execPlan); vp.first != vp.second; ++vp.first) {
355  AlgoVertex v = *vp.first;
356  auto index = boost::get(&boost::AlgoNodeStruct::m_name, execPlan);
357  if (index[v] == node.getNodeName()) {
358  auto index_eccentricity = boost::get(&boost::AlgoNodeStruct::m_eccentricity, execPlan);
359  float rank = index_eccentricity[v];
360  node.setRank(rank);
361  //std::cout << "Rank of " << index[v] << " is " << rank << std::endl;
362  }
363  }
364  return true;
365  }
366 
367  //--------------------------------------------------------------------------
369 
370  // Find eccentricity of the node (only within the data realm of the execution flow graph)
371  recursiveVisit(node);
372 
373  float rank = m_maxKnownDepth;
374  node.setRank(rank);
375 
376  // Reset visitor for next nodes, if any
377  reset();
378 
379  return true;
380  }
381 
382  //--------------------------------------------------------------------------
384 
385  m_currentDepth += 1;
386 
387  auto& products = node.getOutputDataNodes();
388 
389  if (products.empty())
390  if ( (m_currentDepth - 1) > m_maxKnownDepth)
391  m_maxKnownDepth = m_currentDepth - 1;
392 
393  for (auto p : products)
394  for ( auto algoNode : p->getConsumers())
395  recursiveVisit(*algoNode);
396 
397  m_currentDepth -= 1;
398 
399  }
400 }
bool promoteToControlReadyState(const int &slotNum, AlgsExecutionStates &states, std::vector< int > &node_decisions) const override
XXX: CF tests.
const unsigned int & getAlgoIndex() const
XXX: CF tests.
T open(T...args)
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, AlgoNodeStruct > ExecPlan
bool visit(DecisionNode &node) override
bool dataDependenciesSatisfied(const int &slotNum) const
Method to check whether the Algorithm has its all data dependency satisfied.
void setRank(float &rank)
Set Algorithm rank.
void recursiveVisit(AlgorithmNode &node)
Depth-first node parser to calculate node eccentricity (only within the data realm of the precedence ...
const std::vector< DataNode * > & getOutputDataNodes() const
Get all supplier nodes.
bool visit(DecisionNode &) override
bool m_allPass
Whether always passing regardless of daughter results.
bool visit(DecisionNode &) override
std::vector< int > & getNodeDecisions(const int &slotNum) const
bool visit(DecisionNode &node) override
bool m_modeOR
Whether acting as "and" (false) or "or" node (true)
bool visitEnter(DecisionNode &node) const override
The AlgsExecutionStates encodes the state machine for the execution of algorithms within a single eve...
PrecedenceRulesGraph * m_graph
const std::vector< ControlFlowNode * > & getDaughters() const
graph_traits< ExecPlan >::vertex_descriptor AlgoVertex
bool visitEnter(DecisionNode &node) const override
bool m_modePromptDecision
Whether to evaluate the hub decision ASA its child decisions allow to do that.
bool visitLeave(DecisionNode &node) const override
AlgsExecutionStates & getAlgoStates(const int &slotNum) const
bool visit(DecisionNode &) override
void runThroughAdjacents(boost::graph_traits< boost::ExecPlan >::vertex_descriptor vertex, boost::ExecPlan graph)
bool visitEnter(DecisionNode &node) const override
bool visit(DecisionNode &) override
const std::string & getNodeName() const
const unsigned int & getNodeIndex() const
XXX: CF tests.
bool visit(DecisionNode &) override
STL class.
bool visit(DecisionNode &node) override
bool promoteToDataReadyState(const int &slotNum, const AlgorithmNode *requestor=nullptr) const
StatusCode updateState(unsigned int iAlgo, State newState)