Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
default_scheduler.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_DEFAULT_SCHEDULER_H
22#define HEDGEHOG_DEFAULT_SCHEDULER_H
23
24#include <thread>
25#include <sstream>
26#include <vector>
27
28#include "scheduler.h"
29#include "../../core/abstractions/base/node/task_node_abstraction.h"
30#include "../../core/abstractions/base/node/execution_pipeline_node_abstraction.h"
31#include "../../core/abstractions/base/node/graph_node_abstraction.h"
32
34namespace hh {
35
39 private:
40 std::unique_ptr<std::vector<std::thread>> const
41 threads_ = nullptr;
42
43 std::unique_ptr<std::vector<core::abstraction::GraphNodeAbstraction *>>
44 innerGraphs_ = nullptr;
45
46 public:
49 threads_(std::make_unique<std::vector<std::thread>>()),
50 innerGraphs_(std::make_unique<std::vector<core::abstraction::GraphNodeAbstraction *>>()) {}
51
53 ~DefaultScheduler() override = default;
54
57 [[nodiscard]] std::unique_ptr<Scheduler> create() const override { return std::make_unique<DefaultScheduler>(); }
58
63 void spawnThreads(std::set<core::abstraction::NodeAbstraction *> const &cores, bool waitForInitialization) override {
64 std::vector<core::abstraction::TaskNodeAbstraction *> taskExec{};
65
66 for (auto &core : cores) {
67 if (auto exec = dynamic_cast<core::abstraction::TaskNodeAbstraction *>(core)) {
68 try {
70 taskExec.push_back(exec);
71 } catch (std::exception const &e) {
72 std::ostringstream oss;
73 oss << "Can not create thread for node \"" << core->name() << "\" because of error: " << e.what();
74 throw std::runtime_error(oss.str());
75 }
76 } else if (auto graph = dynamic_cast<core::abstraction::GraphNodeAbstraction *>(core)) {
77 innerGraphs_->push_back(graph);
78 } else {
79 std::ostringstream oss;
80 oss
81 << "Node " << core->name() << "/" << core->id()
82 << " does not derive from the right abstraction to be handled properly by the default scheduler.";
83 throw std::runtime_error(oss.str());
84 }
85 }
86
88 if (waitForInitialization) {
89 while (!std::all_of(taskExec.cbegin(), taskExec.cend(),
90 [](auto const &exec) { return exec->isInitialized(); })) {}
91 }
92
93 }
94
96 void joinAll() override {
97 std::for_each(threads_->begin(), threads_->end(), [](std::thread &t) { t.join(); });
98 for (core::abstraction::GraphNodeAbstraction *innerGraph : *(this->innerGraphs_)) {
99 innerGraph->joinThreads();
100 }
101 }
102
103};
104}
105#endif //HEDGEHOG_DEFAULT_SCHEDULER_H
Hedgehog main namespace.
Default scheduler use in Hedgehog graph.
std::unique_ptr< std::vector< core::abstraction::GraphNodeAbstraction * > > innerGraphs_
Scheduler's graph.
~DefaultScheduler() override=default
Default destructor.
std::unique_ptr< Scheduler > create() const override
Definition of virtual constructor.
void joinAll() override
Wait for all inside nodes to join and join the threads of all inside graphs.
void spawnThreads(std::set< core::abstraction::NodeAbstraction * > const &cores, bool waitForInitialization) override
Spawn the threads for all graph's nodes.
DefaultScheduler()
Default constructor.
std::unique_ptr< std::vector< std::thread > > const threads_
Vector of threads for the graph nodes.
Scheduler abstraction to manage graph's threads.
Definition: scheduler.h:32
Task core abstraction used to define some method for task-like behaving cores like CoreExecutionPipel...
virtual void run()=0
Run method, called when thread is attached.