Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
graph_node_abstraction.h
Go to the documentation of this file.
1// NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the
2// software in any medium, provided that you keep intact this entire notice. You may improve, modify and create
3// derivative works of the software or any portion of the software, and you may copy and distribute such modifications
4// or works. Modified works should carry a notice stating that you changed the software and should note the date and
5// nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the
6// source of the software. NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND,
7// EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
8// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR
9// WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE
10// CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS
11// THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE. You
12// are solely responsible for determining the appropriateness of using and distributing the software and you assume
13// all risks associated with its use, including but not limited to the risks and costs of program errors, compliance
14// with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of
15// operation. This software is not intended to be used in any situation where a failure could cause risk of injury or
16// damage to property. The software developed by NIST employees is not subject to copyright protection within the
17// United States.
18
19
20
21#ifndef HEDGEHOG_GRAPH_NODE_ABSTRACTION_H
22#define HEDGEHOG_GRAPH_NODE_ABSTRACTION_H
23
24#include "node_abstraction.h"
27
29namespace hh {
31namespace core {
33namespace abstraction {
34
37 public:
39 enum Status { INIT, EXEC, TERM, INS };
40 private:
41 std::chrono::time_point<std::chrono::system_clock> const
42 graphStartCreation_ = std::chrono::system_clock::now();
43 std::chrono::nanoseconds
44 graphConstructionDuration_ = std::chrono::nanoseconds::zero();
45
46 int deviceId_ = 0;
47 size_t graphId_ = 0;
48
49 protected:
50 std::unique_ptr<std::map<NodeAbstraction *, std::vector<NodeAbstraction *>>> const
52
54
55 public:
58 explicit GraphNodeAbstraction(std::string const &name) :
60 insideNodesAndGroups_(std::make_unique<std::map<NodeAbstraction *, std::vector<NodeAbstraction *>>>()) {}
61
63 ~GraphNodeAbstraction() override = default;
64
67 [[nodiscard]] int deviceId() const override { return deviceId_; }
68
71 [[nodiscard]] size_t graphId() const override { return graphId_; }
72
75 [[nodiscard]] Status graphStatus() const { return graphStatus_; }
76
79 [[nodiscard]] std::chrono::time_point<std::chrono::system_clock> const &graphStartCreation() const {
81 }
82
85 [[nodiscard]] std::chrono::nanoseconds const &graphConstructionDuration() const { return graphConstructionDuration_; }
86
90
93 void graphId(size_t graphId) { graphId_ = graphId; }
94
97 void graphConstructionDuration(std::chrono::nanoseconds const &graphConstructionDuration) {
99 }
100
105 setInside();
106 }
107
110 [[nodiscard]] std::pair<std::chrono::nanoseconds, std::chrono::nanoseconds> minMaxExecutionDuration() const {
111 std::pair<std::chrono::nanoseconds, std::chrono::nanoseconds> minMaxExecDuration = {
112 std::chrono::nanoseconds::max(), std::chrono::nanoseconds::min()
113 };
114
116
117 for (auto insideNode : *(this->insideNodesAndGroups_)) {
118 node = insideNode.first;
119
121 auto nodeAsGraph = dynamic_cast<GraphNodeAbstraction *>(node);
122 auto nodeAsEP = dynamic_cast<ExecutionPipelineNodeAbstraction *>(node);
123
124 if (nodeAsGraph) {
125 auto graphMinMax = nodeAsGraph->minMaxExecutionDuration();
126 minMaxExecDuration.first = std::min(minMaxExecDuration.first, graphMinMax.first);
127 minMaxExecDuration.second = std::max(minMaxExecDuration.second, graphMinMax.second);
128 }
129 if (nodeAsEP) {
130 auto epMinMax = nodeAsEP->minMaxExecutionDuration();
131 minMaxExecDuration.first = std::min(minMaxExecDuration.first, epMinMax.first);
132 minMaxExecDuration.second = std::max(minMaxExecDuration.second, epMinMax.second);
133 } else {
134 auto execDuration = node->executionDuration();
135 minMaxExecDuration.first = std::min(minMaxExecDuration.first, execDuration);
136 minMaxExecDuration.second = std::max(minMaxExecDuration.second, execDuration);
137 std::vector<NodeAbstraction *> &group = insideNode.second;
138 if (!group.empty()) {
139 auto [minElement, maxElement] = std::minmax_element(
140 group.begin(), group.end(),
141 [](auto lhs, auto rhs) { return lhs->executionDuration() < rhs->executionDuration(); }
142 );
143 minMaxExecDuration.first = std::min(minMaxExecDuration.first, (*minElement)->executionDuration());
144 minMaxExecDuration.second = std::max(minMaxExecDuration.second, (*maxElement)->executionDuration());
145 }
146 }
147 }
148 }
149
150 return minMaxExecDuration;
151 }
152
156 [[nodiscard]] std::pair<std::chrono::nanoseconds, std::chrono::nanoseconds> minMaxWaitDuration() const {
157 std::pair<std::chrono::nanoseconds, std::chrono::nanoseconds> minMaxWaitDuration = {
158 std::chrono::nanoseconds::max(), std::chrono::nanoseconds::min()
159 };
160
162
163 for (auto insideNode : *(this->insideNodesAndGroups_)) {
164 node = insideNode.first;
165
167 auto nodeAsGraph = dynamic_cast<GraphNodeAbstraction *>(node);
168 auto nodeAsEP = dynamic_cast<ExecutionPipelineNodeAbstraction *>(node);
169
170 if (nodeAsGraph) {
171 auto graphMinMax = nodeAsGraph->minMaxWaitDuration();
172 minMaxWaitDuration.first = std::min(minMaxWaitDuration.first, graphMinMax.first);
173 minMaxWaitDuration.second = std::max(minMaxWaitDuration.second, graphMinMax.second);
174 }
175 if (nodeAsEP) {
176 auto epMinMax = nodeAsEP->minMaxWaitDuration();
177 minMaxWaitDuration.first = std::min(minMaxWaitDuration.first, epMinMax.first);
178 minMaxWaitDuration.second = std::max(minMaxWaitDuration.second, epMinMax.second);
179 } else if (auto task = dynamic_cast<TaskNodeAbstraction *>(node)) {
180 auto waitDuration = task->waitDuration();
181 minMaxWaitDuration.first = std::min(minMaxWaitDuration.first, waitDuration);
182 minMaxWaitDuration.second = std::max(minMaxWaitDuration.second, waitDuration);
183 std::vector<NodeAbstraction *> &group = insideNode.second;
184 if (!group.empty()) {
185 auto [minElement, maxElement] = std::minmax_element(
186 group.begin(), group.end(),
187 [](auto lhs, auto rhs) {
188 auto lhsAsTask = dynamic_cast<TaskNodeAbstraction const *>(lhs);
189 auto rhsAsTask = dynamic_cast<TaskNodeAbstraction const *>(rhs);
190 if (lhsAsTask && rhsAsTask) {
191 return lhsAsTask->waitDuration() < rhsAsTask->waitDuration();
192 } else {
193 throw std::runtime_error(
194 "All the nodes in a group should be of the same type, the representative is of"
195 " type TaskNodeAbstraction but not the nodes in the group.");
196 }
197 }
198 );
199 auto minTask = dynamic_cast<TaskNodeAbstraction *>(*minElement);
200 auto maxTask = dynamic_cast<TaskNodeAbstraction *>(*maxElement);
201 if (minTask && maxTask) {
202 minMaxWaitDuration.first = std::min(minMaxWaitDuration.first, minTask->waitDuration());
203 minMaxWaitDuration.second = std::max(minMaxWaitDuration.second, maxTask->waitDuration());
204 } else {
205 throw std::runtime_error("All the nodes in a group should be of the same type, the representative is of"
206 " type TaskNodeAbstraction but not the nodes in the group.");
207 }
208 }
209 } else {
210 return {std::chrono::nanoseconds::zero(), std::chrono::nanoseconds::zero()};
211 }
212 }
213 }
214
215 return minMaxWaitDuration;
216 }
217
219 virtual void joinThreads() = 0;
220
222 virtual void setInside() = 0;
223
226 virtual void createInnerGroupsAndLaunchThreads(bool waitForInitialization) = 0;
227
231};
232}
233}
234}
235#endif //HEDGEHOG_GRAPH_NODE_ABSTRACTION_H
Hedgehog main namespace.
virtual std::pair< std::chrono::nanoseconds, std::chrono::nanoseconds > minMaxExecutionDuration() const =0
Getter to the min max execution duration from the nodes inside the graphs in the execution pipeline.
virtual std::pair< std::chrono::nanoseconds, std::chrono::nanoseconds > minMaxWaitDuration() const =0
Getter to the min max wait duration from the nodes inside the graphs in the execution pipeline.
int deviceId() const override
Device id accessor.
std::chrono::time_point< std::chrono::system_clock > const graphStartCreation_
graph creation timestamp
virtual void setInside()=0
Set a graph inside another one.
virtual void createInnerGroupsAndLaunchThreads(bool waitForInitialization)=0
Create the groups and launch the threads of the inside nodes.
std::chrono::nanoseconds const & graphConstructionDuration() const
Graph construction duration accessor.
std::pair< std::chrono::nanoseconds, std::chrono::nanoseconds > minMaxWaitDuration() const
Accessor to the min / max wait duration of the nodes in the graph.
~GraphNodeAbstraction() override=default
Default destructor.
void graphId(size_t graphId)
Setter to the graph id.
GraphNodeAbstraction(std::string const &name)
Base graph abstraction.
size_t graphId() const override
Graph id accessor.
void registerNode(GraphNodeAbstraction *belongingGraph) override
Register a graph inside a graph.
std::pair< std::chrono::nanoseconds, std::chrono::nanoseconds > minMaxExecutionDuration() const
Accessor to the min / max execution duration of the nodes in the graph.
Status graphStatus() const
Graph status accessor.
virtual void joinThreads()=0
Interface to join the threads inside of a graph, called by the Default scheduler.
std::chrono::time_point< std::chrono::system_clock > const & graphStartCreation() const
Graph start creation timestamp accessor.
virtual void registerNodeInsideGraph(NodeAbstraction *core)=0
Register a core inside a graph.
std::unique_ptr< std::map< NodeAbstraction *, std::vector< NodeAbstraction * > > > const insideNodesAndGroups_
All nodes of the graph, mapped to their group nodes.
Status
Graph status (INITialisation, EXECution, TERMination, INSide)
int deviceId_
Device Id used for computation on devices.
size_t graphId_
Graph Id used to identify a graph in an execution pipeline.
std::chrono::nanoseconds graphConstructionDuration_
Node creation duration.
void graphConstructionDuration(std::chrono::nanoseconds const &graphConstructionDuration)
Setter to the graph construction duration.
void deviceId(int deviceId)
Setter to the device id.
virtual void registerNode(GraphNodeAbstraction *belongingGraph)
Register node to the given graph.
virtual behavior::Node * node() const =0
Node accessor.
GraphNodeAbstraction * belongingGraph() const
Belonging graph accessor.
std::string const & name() const
Accessor to the core's name.
Task core abstraction used to define some method for task-like behaving cores like CoreExecutionPipel...
PrintAbstraction, used to determine if the node should be visited by the printer and colored.