Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
graph.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_H
22#define HEDGEHOG_GRAPH_H
23
24#include <ostream>
25#include <filesystem>
26
27#include "../../behavior/node.h"
28#include "../../behavior/copyable.h"
29#include "../../behavior/input_output/multi_senders.h"
30#include "../../behavior/input_output/multi_receivers.h"
31
32#include "../../core/nodes/core_graph.h"
33#include "../printer/options/color_scheme.h"
34#include "../printer/options/structure_options.h"
35#include "../printer/options/debug_options.h"
36#include "../printer/dot_printer.h"
37#include "../printer/jet_color.h"
38#include "result_visitor.h"
39
41namespace hh {
42
43#ifndef DOXYGEN_SHOULD_SKIP_THIS
47template<size_t Separator, class ...AllTypes>
48class AbstractExecutionPipeline;
49#endif //DOXYGEN_SHOULD_SKIP_THIS
50
133template<size_t Separator, class ...AllTypes>
134class Graph :
135 public behavior::Node,
136 public tool::BehaviorMultiReceiversTypeDeducer_t<tool::Inputs<Separator, AllTypes...>>,
137 public tool::BehaviorMultiSendersTypeDeducer_t<tool::Outputs<Separator, AllTypes...>> {
138 private:
139#ifndef DOXYGEN_SHOULD_SKIP_THIS
141 friend core::CoreExecutionPipeline<Separator, AllTypes...>;
143 friend AbstractExecutionPipeline<Separator, AllTypes...>;
144#endif //DOXYGEN_SHOULD_SKIP_THIS
145
146 std::shared_ptr<core::CoreGraph<Separator, AllTypes...>> const
147 coreGraph_ = nullptr;
148 std::unique_ptr<std::set<std::shared_ptr<Node>>>
149 nodes_ = nullptr;
150
151 public:
156 explicit Graph(
157 std::string const &name = "Graph",
158 std::unique_ptr<Scheduler> scheduler = std::make_unique<DefaultScheduler>()) :
159 behavior::Node(std::make_unique<core::CoreGraph<Separator, AllTypes...>>(name, std::move(scheduler), this)),
160 coreGraph_(std::dynamic_pointer_cast<core::CoreGraph<Separator, AllTypes...>>(this->core())),
161 nodes_(std::make_unique<std::set<std::shared_ptr<Node>>>()) {
162 if (coreGraph_ == nullptr) { throw std::runtime_error("The core used by the graph should be a CoreGraph."); }
163 }
164
166 ~Graph() override = default;
167
172 template<tool::CompatibleInputNode<typename core::GIM<Separator, AllTypes...>::inputs_t> InputNode_t>
173 void inputs(std::shared_ptr<InputNode_t> inputNode) {
174 auto node = std::static_pointer_cast<Node>(inputNode);
175 // Store shared_ptr in case user dereference it while it is still needed
176 nodes_->insert(node);
177 this->coreGraph_->template setInputForAllCommonTypes<typename InputNode_t::inputs_t>(node->core().get());
178 }
179
185 template<
186 class InputDataType,
188 typename core::GIM<Separator, AllTypes...>::inputs_t> InputNode_t>
189 void input(std::shared_ptr<InputNode_t> inputNode) {
190 auto node = std::static_pointer_cast<Node>(inputNode);
191 // Store shared_ptr in case user dereference it while it is still needed
192 nodes_->insert(node);
193 this->coreGraph_->template setInputForACommonType<InputDataType,
194 typename InputNode_t::inputs_t>(node->core().get());
195 }
196
201 template<tool::CompatibleOutputNode<typename core::GOM<Separator, AllTypes...>::outputs_t> OutputNode_t>
202 void outputs(std::shared_ptr<OutputNode_t> outputNode) {
203 auto node = std::static_pointer_cast<Node>(outputNode);
204 // Store shared_ptr in case user dereference it while it is still needed
205 nodes_->insert(node);
206
207 this->coreGraph_->template setOutputForAllCommonTypes<typename OutputNode_t::outputs_t>(node->core().get());
208 }
209
215 template<class OutputType,
217 typename core::GOM<Separator, AllTypes...>::outputs_t> OutputNode_t>
218 void output(std::shared_ptr<OutputNode_t> outputNode) {
219 auto node = std::static_pointer_cast<Node>(outputNode);
220 // Store shared_ptr in case user dereference it while it is still needed
221 nodes_->insert(node);
222 this->coreGraph_->template setOutputForACommonType<OutputType,
223 typename OutputNode_t::outputs_t>(node->core().get());
224 }
225
233 template<tool::SenderNode SenderNode_t, tool::ReceiverNode ReceiverNode_t>
234 void edges(std::shared_ptr<SenderNode_t> sender, std::shared_ptr<ReceiverNode_t> receiver) {
235 static_assert(
236 std::tuple_size_v<
238 > != 0, "The sender and the receiver nodes should at least share a type."
239 );
240 auto senderNode = std::static_pointer_cast<Node>(sender);
241 auto receiverNode = std::static_pointer_cast<Node>(receiver);
242 // Store shared_ptr in case user dereference it while it is still needed
243 nodes_->insert(senderNode);
244 nodes_->insert(receiverNode);
245
246 this->coreGraph_
247 ->template addEdgeForAllCommonTypes<typename SenderNode_t::outputs_t, typename ReceiverNode_t::inputs_t>
248 (senderNode->core().get(), receiverNode->core().get());
249 }
250
257 template<class CommonType,
259 void edge(std::shared_ptr<SenderNode_t> sender, std::shared_ptr<ReceiverNode_t> receiver) {
260 auto senderNode = std::static_pointer_cast<Node>(sender);
261 auto receiverNode = std::static_pointer_cast<Node>(receiver);
262 // Store shared_ptr in case user dereference it while it is still needed
263 nodes_->insert(senderNode);
264 nodes_->insert(receiverNode);
265
266 this->coreGraph_->template addEdgeForACommonType<
267 CommonType,
268 typename SenderNode_t::outputs_t, typename ReceiverNode_t::inputs_t>
269 (senderNode->core().get(), receiverNode->core().get());
270 }
271
275 void executeGraph(bool waitForInitialization = false) { coreGraph_->executeGraph(waitForInitialization); }
276
280 void finishPushingData() { coreGraph_->finishPushingData(); }
281
286 template<tool::MatchInputTypeConcept<tool::Inputs<Separator, AllTypes...>> CompatibleInputType_t>
287 void pushData(std::shared_ptr<CompatibleInputType_t> data) { this->coreGraph_->broadcastAndNotifyAllInputNodes(data); }
288
291 void waitForTermination() { coreGraph_->waitForTermination(); }
292
311 auto getBlockingResult() { return coreGraph_->getBlockingResult(); }
312
324 void createDotFile(std::filesystem::path const &dotFilePath,
325 ColorScheme colorScheme = ColorScheme::NONE,
326 StructureOptions structureOptions = StructureOptions::NONE,
327 DebugOptions debugOption = DebugOptions::NONE,
328 std::unique_ptr<ColorPicker> colorPicker = std::make_unique<JetColor>(),
329 bool verbose = false) {
331 DotPrinter
332 printer(
333 std::filesystem::absolute(dotFilePath), colorScheme, structureOptions, debugOption, core,
334 std::move(colorPicker), verbose);
335 core->visit(&printer);
336 }
337
340 void deviceId(int deviceId) { this->coreGraph_->deviceId(deviceId); }
341
345 void cleanGraph() {
346 this->coreGraph_->cleanGraph();
347 }
348};
349}
350#endif //HEDGEHOG_GRAPH_H
Hedgehog main namespace.
StructureOptions
Enum structural options.
@ NONE
No added structural options.
DebugOptions
Enum to enable debug printing.
Definition: debug_options.h:27
@ NONE
No added debug options.
ColorScheme
Enum color options.
Definition: color_scheme.h:27
@ NONE
No added coloration.
typename internals::Intersect< Tuple1, Tuple2 >::type Intersect_t
Helper getting the intersection of types between two type tuples.
typename internals::Splitter< delta, Types... >::Inputs Inputs
Helper getting the input types from a list of template types (variadic)
tool::GraphOutputsManagementAbstractionTypeDeducer_t< tool::Outputs< Separator, AllTypes... > > GOM
Type alias for an GraphOutputsManagementAbstraction from the list of template parameters.
Definition: core_graph.h:70
tool::GraphInputsManagementAbstractionTypeDeducer_t< tool::Inputs< Separator, AllTypes... > > GIM
Type alias for an GraphInputsManagementAbstraction from the list of template parameters.
Definition: core_graph.h:66
Execution pipeline abstraction.
Hedgehog graph abstraction.
Definition: graph.h:137
void createDotFile(std::filesystem::path const &dotFilePath, ColorScheme colorScheme=ColorScheme::NONE, StructureOptions structureOptions=StructureOptions::NONE, DebugOptions debugOption=DebugOptions::NONE, std::unique_ptr< ColorPicker > colorPicker=std::make_unique< JetColor >(), bool verbose=false)
Create a dot file representing a snapshot of the state of the graph at the moment of the call,...
Definition: graph.h:324
~Graph() override=default
Default graph destructor.
void inputs(std::shared_ptr< InputNode_t > inputNode)
Set an input node and connect the node to the graph's inputs for all common types.
Definition: graph.h:173
void pushData(std::shared_ptr< CompatibleInputType_t > data)
Push data into the graph.
Definition: graph.h:287
void executeGraph(bool waitForInitialization=false)
Execute the graph.
Definition: graph.h:275
Graph(std::string const &name="Graph", std::unique_ptr< Scheduler > scheduler=std::make_unique< DefaultScheduler >())
Default graph constructor, construct a graph with the name "Graph" and with a default scheduler.
Definition: graph.h:156
void output(std::shared_ptr< OutputNode_t > outputNode)
Set an output node and connect the node to the graph's output OutputDataType.
Definition: graph.h:218
void finishPushingData()
Indicate to the graph that no more input data are pushed to the graph.
Definition: graph.h:280
void edge(std::shared_ptr< SenderNode_t > sender, std::shared_ptr< ReceiverNode_t > receiver)
Create an edge between two nodes for a specific type.
Definition: graph.h:259
void waitForTermination()
Wait for the graph to terminate.
Definition: graph.h:291
auto getBlockingResult()
Get result data from the graph.
Definition: graph.h:311
void input(std::shared_ptr< InputNode_t > inputNode)
Set an input node and connect the node to the graph's input InputDataType.
Definition: graph.h:189
void edges(std::shared_ptr< SenderNode_t > sender, std::shared_ptr< ReceiverNode_t > receiver)
Create an edge between two nodes for all common types.
Definition: graph.h:234
std::shared_ptr< core::CoreGraph< Separator, AllTypes... > > const coreGraph_
Core of the graph.
Definition: graph.h:147
void outputs(std::shared_ptr< OutputNode_t > outputNode)
Set an output node and connect the node to the graph's outputs for all common types.
Definition: graph.h:202
void deviceId(int deviceId)
Set the device id (GPU ID) for all the nodes in a graph.
Definition: graph.h:340
std::unique_ptr< std::set< std::shared_ptr< Node > > > nodes_
Set of nodes given by the end user.
Definition: graph.h:149
void cleanGraph()
Clean the graph.
Definition: graph.h:345
Behavior abstraction for the base node.
Definition: node.h:32
std::string name() const
Node's name accessor.
Definition: node.h:53
Node(std::shared_ptr< hh::core::abstraction::NodeAbstraction > core)
Constructor's node.
Definition: node.h:40
std::shared_ptr< hh::core::abstraction::NodeAbstraction > const & core() const
Core accessor.
Definition: node.h:49
Graph core.
Definition: core_graph.h:81
Test if an input type is in the list of input types (tuple)
Definition: concepts.h:98
Test if a node is a sender for a type.
Definition: concepts.h:198
Test if a node is a receiver for a type.
Definition: concepts.h:207
Test if a node can be input of a graph.
Definition: concepts.h:216
Test if a node can be input of a graph for a type.
Definition: concepts.h:227
Test if a node can be output of a graph.
Definition: concepts.h:235
Test if a node can be output of a graph for a type.
Definition: concepts.h:246