Interface Documentation
Version: invalid
dag.hh
Go to the documentation of this file.
1 /*
2  @@@@@@@@ @@ @@@@@@ @@@@@@@@ @@
3  /@@///// /@@ @@////@@ @@////// /@@
4  /@@ /@@ @@@@@ @@ // /@@ /@@
5  /@@@@@@@ /@@ @@///@@/@@ /@@@@@@@@@/@@
6  /@@//// /@@/@@@@@@@/@@ ////////@@/@@
7  /@@ /@@/@@//// //@@ @@ /@@/@@
8  /@@ @@@//@@@@@@ //@@@@@@ @@@@@@@@ /@@
9  // /// ////// ////// //////// //
10 
11  Copyright (c) 2016, Triad National Security, LLC
12  All rights reserved.
13  */
14 #pragma once
15 
18 #include <flecsi-config.h>
19 
20 #include "flecsi/flog.hh"
21 
22 #if defined(FLECSI_ENABLE_GRAPHVIZ)
23 #include "flecsi/util/graphviz.hh"
24 #endif
25 
26 #include <algorithm>
27 #include <iostream>
28 #include <list>
29 #include <map>
30 #include <queue>
31 #include <sstream>
32 #include <vector>
33 
34 namespace flecsi {
35 namespace util {
36 
37 namespace dag_impl {
38 
39 /*
40  */
41 
42 template<typename NodePolicy>
43 struct node : NodePolicy, std::list<node<NodePolicy> const *> {
44 
45  template<typename... Args>
46  node(std::string const & label, Args &&... args)
47  : NodePolicy(std::forward<Args>(args)...), label_(label) {
48  const void * address = static_cast<const void *>(this);
49  std::stringstream ss;
50  ss << address;
51  identifier_ = ss.str();
52  }
53 
54  std::string const & identifier() const {
55  return identifier_;
56  }
57 
58  std::string const & label() const {
59  return label_;
60  }
61 
62 private:
63  std::string identifier_;
64  std::string label_;
65 };
66 
67 } // namespace dag_impl
68 
73 template<typename NodePolicy>
74 struct dag : std::vector<dag_impl::node<NodePolicy> *> {
75 
77 
78  dag(const char * label = "empty") : label_(label) {}
79 
84  std::string const & label() const {
85  return label_;
86  }
87 
94  std::vector<node_type const *> sort() {
95  std::vector<node_type const *> sorted;
96 
97  // Create a temporary list of the nodes.
98  std::list<node_type *> nodes;
99  for(auto n : *this) {
100  nodes.push_back(n);
101  } // for
102 
103  // Create a tally of the number of dependencies of each node
104  // in the graph. Remove nodes from the temporary list that do not
105  // have any depenendencies.
106  std::queue<node_type *> q;
107  for(auto n = nodes.begin(); n != nodes.end();) {
108  if((*n)->size() == 0) {
109  q.push(*n);
110  n = nodes.erase(n);
111  }
112  else {
113  ++n;
114  } // if
115  } // for
116 
117  size_t count{0};
118  while(!q.empty()) {
119  const auto root = q.front();
120  sorted.push_back(root);
121  q.pop();
122 
123  for(auto n = nodes.begin(); n != nodes.end();) {
124  auto it = std::find_if((*n)->begin(),
125  (*n)->end(),
126  [&root](const auto p) { return root == p; });
127 
128  if(it != (*n)->end()) {
129  (*n)->erase(it);
130 
131  if(!(*n)->size()) {
132  q.push(*n);
133  n = nodes.erase(n);
134  } // if
135  }
136  else {
137  ++n;
138  } // if
139  } // for
140 
141  ++count;
142  } // while
143 
144  flog_assert(count == this->size(), "sorting failed. This is not a DAG!!!");
145 
146  return sorted;
147  } // sort
148 
149 #if defined(FLECSI_ENABLE_GRAPHVIZ)
150 
154  void add(graphviz & gv, const char * color = "#c5def5") {
155  std::map<uintptr_t, Agnode_t *> node_map;
156 
157  for(auto n : *this) {
158 
159  auto * node = gv.add_node(n->identifier().c_str(), n->label().c_str());
160  node_map[uintptr_t(n)] = node;
161 
162  gv.set_node_attribute(node, "color", "black");
163  gv.set_node_attribute(node, "style", "filled");
164  gv.set_node_attribute(node, "fillcolor", color);
165  } // for
166 
167  for(auto n : *this) {
168  for(auto e : *n) {
169  auto * edge =
170  gv.add_edge(node_map[uintptr_t(e)], node_map[uintptr_t(n)]);
171  gv.set_edge_attribute(edge, "penwidth", "1.5");
172  } // for
173  } // for
174  } // add
175 
176 #endif // FLECSI_ENABLE_GRAPHVIZ
177 
178 private:
179  std::string label_;
180 }; // struct dag
181 
182 } // namespace util
183 } // namespace flecsi
Definition: graphviz.hh:91
#define flog_assert(test, message)
Definition: flog.hh:411
Definition: dag.hh:43
std::string const & label() const
Definition: dag.hh:84
size_t color()
Definition: execution.hh:326
Definition: dag.hh:74
std::vector< node_type const * > sort()
Definition: dag.hh:94
Definition: control.hh:31