Hedgehog  0.0.0
A library to generate hybrid pipeline workflow systems
default_scheduler.h
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 #ifndef HEDGEHOG_DEFAULT_SCHEDULER_H
21 #define HEDGEHOG_DEFAULT_SCHEDULER_H
22 
23 #include <thread>
24 #include <vector>
25 #include <algorithm>
26 #include "abstract_scheduler.h"
28 namespace hh {
31  private:
32  std::unique_ptr<std::vector<std::thread>>
33  threads_ = nullptr;
34  std::unique_ptr<std::vector<std::shared_ptr<core::CoreNode>>>
35  innerGraphs_ = nullptr;
36 
37  public:
40  this->threads_ = std::make_unique<std::vector<std::thread>>();
41  this->innerGraphs_ = std::make_unique<std::vector<std::shared_ptr<core::CoreNode>>>();
42  }
43 
46  [[nodiscard]] std::unique_ptr<AbstractScheduler> create() const override {
47  return std::make_unique<DefaultScheduler>();
48  }
49 
51  ~DefaultScheduler() override = default;
52 
55  void spawnThreads(std::vector<std::shared_ptr<core::CoreNode>> &insideCores) override {
56  // Iterate over inside nodes
57  for (auto &core : insideCores) {
58  // If the node is not a graph
59  if (core->type() != core::NodeType::Graph) {
60  // Create the thread for the run
61  threads_->emplace_back(&core::CoreNode::run, core);
62  // If the node is a graph
63  } else { innerGraphs_->push_back(core); }
64  }
65  }
66 
68  void joinAll() override {
69  std::for_each(threads_->begin(), threads_->end(), [](std::thread &t) { t.join(); });
70  for (std::shared_ptr<core::CoreNode> &innerGraph : *(this->innerGraphs_)) { innerGraph->joinThreads(); }
71  }
72 
73 };
74 }
75 #endif //HEDGEHOG_DEFAULT_SCHEDULER_H
std::unique_ptr< std::vector< std::thread > > threads_
List of threads spawned.
void joinAll() override
Wait for all inside nodes to join and join the threads of all inside graphs.
~DefaultScheduler() override=default
Default destructor.
Hedgehog main namespace.
Default scheduler for Hedgehog graph.
std::unique_ptr< AbstractScheduler > create() const override
Definition of virtual constructor.
Abstract Hedgehog Scheduler interface, define per graph how the threads are bound to the nodes...
virtual void run()
Run method, main execution.
Definition: core_node.h:497
void spawnThreads(std::vector< std::shared_ptr< core::CoreNode >> &insideCores) override
Spawn the threads for all graph&#39;s inside node.
DefaultScheduler()
Default constructor.
std::unique_ptr< std::vector< std::shared_ptr< core::CoreNode > > > innerGraphs_
List of inner graphs, all graphs could have it owns AbstractScheduler.