HTGS  v2.0
The Hybrid Task Graph Scheduler
Bookkeeper.hpp
Go to the documentation of this file.
1 
2 // NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the software in any medium, provided that you keep intact this entire notice. You may improve, modify and create derivative works of the software or any portion of the software, and you may copy and distribute such modifications or works. Modified works should carry a notice stating that you changed the software and should note the date and nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the source of the software.
3 // NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE.
4 // You are solely responsible for determining the appropriateness of using and distributing the software and you assume all risks associated with its use, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of operation. This software is not intended to be used in any situation where a failure could cause risk of injury or damage to property. The software developed by NIST employees is not subject to copyright protection within the United States.
5 
15 #ifndef HTGS_BOOKKEEPER_HPP
16 #define HTGS_BOOKKEEPER_HPP
17 
18 #include <htgs/api/ITask.hpp>
19 #include <htgs/api/VoidData.hpp>
22 
23 namespace htgs {
24 
59 template<class T>
60 class Bookkeeper : public ITask<T, VoidData> {
61  static_assert(std::is_base_of<IData, T>::value, "T must derive from IData");
62 
63  public:
64 
69  this->ruleManagers = new std::list<AnyRuleManagerInOnly<T> *>();
70  }
71 
75  virtual ~Bookkeeper() override {
76  for (AnyRuleManagerInOnly<T> *ruleManager : *ruleManagers) {
77  delete ruleManager;
78  ruleManager = nullptr;
79  }
80  delete ruleManagers;
81  ruleManagers = nullptr;
82  }
83 
89  void addRuleManager(AnyRuleManager *ruleManager) {
90  AnyRuleManagerInOnly<T> *baseRuleMan = (AnyRuleManagerInOnly<T> *) ruleManager;
91 
92  HTGS_DEBUG_VERBOSE(this << "----" << this->getName() << " adding rule manager " << baseRuleMan->getName());
93 
94  ruleManagers->push_back(baseRuleMan);
95  ruleManagerInfo = ruleManagerInfo + " " + baseRuleMan->getName();
96  }
97 
103  template<class U>
104  void addRuleManager(RuleManager<T, U> *ruleManager) {
105 
106  HTGS_DEBUG_VERBOSE(this << "----" << this->getName() << " adding rule manager " << ruleManager->getName());
107 
108  ruleManagers->push_back(ruleManager);
109 
110  ruleManagerInfo = ruleManagerInfo + " " + ruleManager->getName();
111  }
112 
119  void executeTask(std::shared_ptr<T> data) override {
120 
121  for (AnyRuleManagerInOnly<T> *ruleManager : *ruleManagers) {
122 // HTGS_DEBUG_VERBOSE(this->getName() << " executing " + ruleManager->getName());
123  ruleManager->executeTask(data);
124  }
125  }
126 
131  std::string getName() override {
132  return "Bookkeeper -- " + std::to_string(this->ruleManagers->size()) + " rule(s): " +
133  this->ruleManagerInfo;
134  }
135 
140  virtual std::string getDotLabelName() override {
141  return "Bookkeeper";
142  }
143 
148  void debug() override {
149  HTGS_DEBUG(this->getName() << " Details:");
150  for (AnyRuleManagerInOnly<T> *ruleManager : *ruleManagers) {
151  HTGS_DEBUG("Executing rule manager: " << ruleManager->getName());
152  ruleManager->debug();
153  }
154 
155  }
156 
157  bool canTerminate(std::shared_ptr<AnyConnector> inputConnector) override {
158  // check input connector first
159  HTGS_ASSERT(inputConnector != nullptr, "A Bookkeeper does not appear to have any input, which results in this bookkeeper not doing anything");
160  assert(inputConnector != nullptr);
161  if (inputConnector->isInputTerminated())
162  return true;
163 
164  // Loop through each rule, if they can terminate, then initiate termination for that rule
165  // This is used to assist in terminating a cycle
166  for (auto ruleManager : *ruleManagers)
167  {
168  ruleManager->checkRuleTermination();
169  }
170 
171  return false;
172  }
173 
178  void initialize() override {
179  for (AnyRuleManagerInOnly<T> *ruleManager : *ruleManagers) {
180  ruleManager->initialize(this->getPipelineId(), this->getNumPipelines(), this->getAddress());
181  }
182  }
183 
188  void shutdown() {
189 
190  HTGS_DEBUG("Shutting down " << this->getName());
191  for (AnyRuleManagerInOnly<T> *ruleManager : *ruleManagers) {
192  ruleManager->shutdown();
193  }
194  }
195 
201  Bookkeeper<T> *copy() { return new Bookkeeper<T>(); }
202 
206  std::string genDot(int flags, std::string idStr) {
207  std::ostringstream oss;
208  for (AnyRuleManagerInOnly<T> *ruleMan : *ruleManagers) {
209 // std::ostringstream ruleManOss;
210 // ruleManOss <<
211 
212  std::string ruleManStr = ruleMan->getConnector()->getDotId();
213 // ruleManStr.erase(0, 1);
214  if ((flags & DOTGEN_FLAG_SHOW_CONNECTORS) != 0 || (flags & DOTGEN_FLAG_SHOW_CONNECTOR_VERBOSE) != 0) {
215  oss << idStr << " -> " << ruleManStr << "[label=\"" << ruleMan->getName(flags) << "\"];" << std::endl;
216  }
217  }
218 // std::string inOutLabel = (((flags & DOTGEN_FLAG_SHOW_IN_OUT_TYPES) != 0) ? ("\\nin: " + this->inTypeName()) : "");
219 
220  oss << idStr << ";" << std::endl; // + "[label=\"Bookkeeper" + inOutLabel + "\"];" << std::endl;
221 
222  return oss.str();
223  }
224 
225 
226  std::string genDotProducerEdgeToTask(std::map<std::shared_ptr<AnyConnector>, AnyITask *> &inputConnectorDotMap, int dotFlags) override
227  {
228  std::ostringstream oss;
229  for (AnyRuleManagerInOnly<T> *ruleMan : *ruleManagers) {
230  auto connectorPair = inputConnectorDotMap.find(ruleMan->getConnector());
231  if (connectorPair != inputConnectorDotMap.end())
232  {
233  oss << this->getDotId() << " -> " << connectorPair->second->getConsumerDotIds() << "[label=\"" << ruleMan->getName(dotFlags) << "\"];" << std::endl;
234  }
235  }
236 
237  return oss.str();
238  }
239 
240 
241  std::string genDotProducerEdgeFromConnector(std::shared_ptr<AnyConnector> connector, int flags)
242  {
243  std::ostringstream oss;
244  if (connector != nullptr)
245  {
246  for (AnyRuleManagerInOnly<T> *ruleMan : *ruleManagers) {
247  if (connector == ruleMan->getConnector())
248  {
249  oss << this->getDotId() << " -> " << connector->getDotId() << "[label=\"" << ruleMan->getName(flags) << "\"];" << std::endl;
250  }
251  }
252  }
253 
254  return oss.str();
255  }
256 
257  std::list<AnyRuleManagerInOnly<T> *> *getRuleManagers() {
258  return ruleManagers;
259  }
260 
261 // std::string genDotProducerEdgeWithoutConnector(std::string consumerDotId, int flags) override
262 // {
263 // std::cerr << "GENERATING CUSTOM DOT FILE FOR BOOKKEEEPER" << std::endl;
264 // std::ostringstream oss;
265 //
266 // for (AnyRuleManagerInOnly<T> *ruleMan : *ruleManagers) {
267 //
268 // oss << this->getDotId() << " -> " << consumerDotId << "[label=\"" << ruleMan->getName(flags) << "\"];" << std::endl;
269 // }
270 //
271 // return oss.str();
272 // }
273 
274 
275  private:
276  std::list<AnyRuleManagerInOnly<T> *> *ruleManagers;
277  std::string ruleManagerInfo;
278 };
279 }
280 
281 #endif //HTGS_BOOKKEEPER_HPP
virtual std::string getDotLabelName() override
Gets just the name "Bookkeeper" for the dot label.
Definition: Bookkeeper.hpp:140
bool canTerminate(std::shared_ptr< AnyConnector > inputConnector) override
Virtual function that is called when an ITask is checking if it can be terminated.
Definition: Bookkeeper.hpp:157
VoidData is used for data that is empty/void.
virtual ~Bookkeeper() override
Destructor destroys RuleManager memory.
Definition: Bookkeeper.hpp:75
Implements the parent ITask, which removes the template arguments of an ITask.
Definition: AnyITask.hpp:48
std::string getDotId()
Gets the id used for dot nodes.
Definition: AnyITask.hpp:497
std::string genDot(int flags, std::string idStr)
Generates the dot notation for the bookkeeper.
Definition: Bookkeeper.hpp:206
void shutdown()
Shuts down this bookkeeper and all of it&#39;s RuleManagers.
Definition: Bookkeeper.hpp:188
void addRuleManager(RuleManager< T, U > *ruleManager)
Adds rule manager to this bookkeeper.
Definition: Bookkeeper.hpp:104
Implements the base class for the rule manager, but only providing the input type.
void debug() override
Provides debug output for the rule manager.
Definition: Bookkeeper.hpp:148
std::list< AnyRuleManagerInOnly< T > * > * ruleManagers
The list of ruleManagers (one per consumer)
Definition: Bookkeeper.hpp:276
void executeTask(std::shared_ptr< T > data) override
Executes the bookkeeper on data.
Definition: Bookkeeper.hpp:119
void addRuleManager(AnyRuleManager *ruleManager)
Adds rule manager to this bookkeeper.
Definition: Bookkeeper.hpp:89
#define HTGS_DEBUG_VERBOSE(msg)
Prints a debug message to std:cerr with VERBOSE level.
Definition: debug_message.hpp:75
std::string ruleManagerInfo
A string representation of all rule managers.
Definition: Bookkeeper.hpp:277
Bookkeeper< T > * copy()
Creates a shallow copy of this bookkeeper.
Definition: Bookkeeper.hpp:201
std::string getName() override
Gets the name of this bookkeeper and all rule managers it controls.
Definition: Bookkeeper.hpp:131
size_t getNumPipelines() const
Sets the task graph communicator.
Definition: AnyITask.hpp:404
size_t getPipelineId()
Gets the pipeline ID.
Definition: AnyITask.hpp:367
Connects a Bookkeeper to another ITask using one IRule.
Definition: AnyRuleManager.hpp:53
std::string getAddress() override final
Definition: ITask.hpp:511
Implements the base class for the rule manager, but only provides the input type. ...
Definition: AnyRuleManagerInOnly.hpp:27
#define HTGS_DEBUG(msg)
Prints a debug message to std::cerr with standard level If DEBUG_FLAG is not defined, this equates to a no op Each message includes the file and line number for where the debug is called.
Definition: debug_message.hpp:65
std::string getName(int flags=0) override
Gets the name of the RuleManager and the names of all IRules that it manages.
Definition: RuleManager.hpp:112
An interface to process input data and forward results within a TaskGraph.
Definition: ITask.hpp:165
Connects a Bookkeeper to another ITask using one or more IRule(s).
Definition: RuleManager.hpp:57
void initialize() override
Initializes the bookkeeper and all RuleManagers.
Definition: Bookkeeper.hpp:178
#define HTGS_ASSERT(condition, message)
Prints a more meaningful assertion message and terminates if the condition fails. ...
Definition: debug_message.hpp:25
virtual std::string getName(int flags=0)=0
Gets the name of the RuleManager and the names of all IRules that it manages.
An interface to process input data and forward results within a TaskGraph.
Definition: Bookkeeper.hpp:23
Bookkeeper()
Constructs a bookkeeper.
Definition: Bookkeeper.hpp:68
#define DOTGEN_FLAG_SHOW_CONNECTOR_VERBOSE
Shows verbose information within each connector in the graph.
Definition: TaskGraphDotGenFlags.hpp:92
Implements a RuleManager, which connects a Bookkeeper to another ITask using an IRule.
Special task used to manage rules.
Definition: Bookkeeper.hpp:60