iMSTK
Interactive Medical Simulation Toolkit
imstkTbbTaskGraphController.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkTbbTaskGraphController.h"
8 #include "imstkMacros.h"
9 #include "imstkTaskGraph.h"
10 
11 DISABLE_WARNING_PUSH
12  DISABLE_WARNING_PADDING
13 #include <tbb/flow_graph.h>
14 DISABLE_WARNING_POP
15 
16 using namespace tbb::flow;
17 
18 namespace imstk
19 {
20 void
21 TbbTaskGraphController::execute()
22 {
23  using TbbContinueNode = continue_node<continue_msg>;
24 
25  graph g;
26 
27  broadcast_node<continue_msg> start(g);
28 
29  // Create a continue node for every TaskNode (except start)
30  std::unordered_map<std::shared_ptr<TaskNode>, TbbContinueNode> tbbNodes;
31  using NodeKeyValuePair = std::pair<std::shared_ptr<TaskNode>, TbbContinueNode>;
32 
33  const TaskNodeVector& nodes = m_graph->getNodes();
34  if (nodes.size() == 0)
35  {
36  return;
37  }
38  tbbNodes.reserve(nodes.size());
39  for (size_t i = 0; i < nodes.size(); i++)
40  {
41  if (m_graph->getSource() != nodes[i])
42  {
43  std::shared_ptr<TaskNode> node = nodes[i];
44  tbbNodes.insert(NodeKeyValuePair(node, TbbContinueNode(g,
45  [node](continue_msg) { node->execute(); })));
46  }
47  }
48 
49  const TaskNodeAdjList& adjList = m_graph->getAdjList();
50  for (const auto& i : adjList)
51  {
52  if (i.first == m_graph->getSource())
53  {
54  for (const auto& outputNode : i.second)
55  {
56  TbbContinueNode& tbbNode2 = tbbNodes.at(outputNode);
57  make_edge(start, tbbNode2);
58  }
59  }
60  else
61  {
62  TbbContinueNode& tbbNode1 = tbbNodes.at(i.first);
63  for (const auto& outputNode : i.second)
64  {
65  TbbContinueNode& tbbNode2 = tbbNodes.at(outputNode);
66  make_edge(tbbNode1, tbbNode2);
67  }
68  }
69  }
70 
71  start.try_put(continue_msg());
72  g.wait_for_all();
73 }
74 } // namespace imstk
Compound Geometry.