HTGS  v2.0
The Hybrid Task Graph Scheduler
IRule.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 
14 #ifndef HTGS_IRULE_HPP
15 #define HTGS_IRULE_HPP
16 
17 #include <iostream>
18 #include <functional>
19 #include <list>
20 #include <mutex>
22 
23 #include <htgs/api/IData.hpp>
24 
25 namespace htgs {
26 
27 template<class T>
29 
99 template<class T, class U>
100 class IRule : public AnyIRule {
101  static_assert(std::is_base_of<IData, T>::value, "T must derive from IData");
102  static_assert(std::is_base_of<IData, U>::value, "U must derive from IData");
103  public:
104 
108  IRule() : AnyIRule() {
109  output = new std::list<std::shared_ptr<U>>();
110  }
111 
118  IRule(bool useLocks) : AnyIRule(useLocks) {
119  output = new std::list<std::shared_ptr<U>>();
120  }
121 
125 
129  virtual ~IRule() override {
130  if (output != nullptr) {
131  output->clear();
132  delete output;
133  output = nullptr;
134  }
135  }
136 
141  virtual bool canTerminateRule(size_t pipelineId) override { return false; }
142 
146  virtual void shutdownRule(size_t pipelineId) override {}
147 
151  virtual std::string getName() override {
152  return "Unnamed IRule";
153  }
154 
163  virtual void applyRule(std::shared_ptr<T> data, size_t pipelineId) = 0;
164 
165 
169 
178  std::list<std::shared_ptr<U>> *applyRuleFunction(std::shared_ptr<T> data, size_t pipelineId) {
179  this->output->clear();
180  applyRule(data, pipelineId);
181  return output;
182  }
183 
188  void addResult(std::shared_ptr<U> result) {
189  this->output->push_back(result);
190  }
191 
197  void addResult(U *result) {
198  this->output->push_back(std::shared_ptr<U>(result));
199  }
200 
208  return new StateContainer<std::shared_ptr<T>>(height, width, nullptr);
209  }
210 
219  template<class V>
220  StateContainer<V> *allocStateContainer(size_t height, size_t width, V defaultValue) {
221  return new StateContainer<V>(height, width, defaultValue);
222  }
223 
230  return new StateContainer<std::shared_ptr<T>>(size, 1, nullptr);
231  }
232 
240  template<class V>
241  StateContainer<V> *allocStateContainer(size_t size, V defaultValue) {
242  return new StateContainer<V>(size, 1, defaultValue);
243  }
244 
245  private:
246  std::list<std::shared_ptr<U>>
248 };
249 
270 template<class T>
271 class StateContainer {
272 
273  public:
281  StateContainer(size_t height, size_t width, T emptyData) {
282  this->width = width;
283  this->height = height;
284  this->emptyData = emptyData;
285  data = new T[width * height];
286 
287  for (size_t i = 0; i < width * height; i++) {
288  data[i] = this->emptyData;
289  }
290  }
291 
296  delete[]data;
297  }
298 
305  void set(size_t row, size_t col, T &value) const {
306  data[computeIndex(row, col)] = value;
307  }
308 
315  void assign(size_t row, size_t col, T value) const {
316  data[computeIndex(row, col)] = value;
317  }
318 
324  void set(size_t index, T &value) const {
325  data[index] = value;
326  }
327 
333  void assign(size_t index, T value) const {
334  data[index] = value;
335  }
336 
343  T &get(size_t row, size_t col) const {
344  return data[computeIndex(row, col)];
345  }
346 
352  T &get(size_t index) const {
353  return data[index];
354  }
355 
361  void remove(size_t row, size_t col) {
362  set(row, col, emptyData);
363  }
364 
369  void remove(size_t index) {
370  set(index, emptyData);
371  }
372 
382  bool has(size_t row, size_t col) const {
383  return data[computeIndex(row, col)] != emptyData;
384  }
385 
394  bool has(size_t index) const {
395  return data[index] != emptyData;
396  }
397 
403  void printState() {
404  for (size_t r = 0; r < height; r++) {
405  for (size_t c = 0; c < width; c++) {
406  if (this->has(r, c))
407  std::cout << "1";
408  else
409  std::cout << "0";
410  }
411  std::cout << std::endl;
412  }
413  }
414 
419  void printContents() {
420  for (size_t r = 0; r < height; r++) {
421  for (size_t c = 0; c < width; c++) {
422  std::cout << this->get(r, c) << " ";
423  }
424  std::cout << std::endl;
425  }
426  }
427 
428  private:
435  size_t computeIndex(size_t row, size_t col) const {
436  return row * width + col;
437  }
438 
439  T *data;
440  size_t width;
441  size_t height;
443 };
444 
445 }
446 
447 #endif //HTGS_IRULE_HPP
bool has(size_t row, size_t col) const
Checks whether the specified row column has data.
Definition: IRule.hpp:382
virtual std::string getName() override
Virtual function to get the name of the IRule.
Definition: IRule.hpp:151
Class to hold one/two dimensional state information.
Definition: IRule.hpp:28
void printContents()
Prints the contents of the state container.
Definition: IRule.hpp:419
virtual bool canTerminateRule(size_t pipelineId) override
Virtual function to determine if a rule is ready to be terminated.
Definition: IRule.hpp:141
size_t width
The width of the StateContainer.
Definition: IRule.hpp:440
virtual void applyRule(std::shared_ptr< T > data, size_t pipelineId)=0
Pure virtual function to process input data.
StateContainer< V > * allocStateContainer(size_t height, size_t width, V defaultValue)
Allocates a two dimensional state container using the template argument.
Definition: IRule.hpp:220
bool has(size_t index) const
Checks whether the specified index has data.
Definition: IRule.hpp:394
void addResult(U *result)
Adds a result value to the output.
Definition: IRule.hpp:197
bool useLocks
Will enable using the mutex to lock the rule to ensure this rule is only accessed by a thread at a ti...
Definition: AnyIRule.hpp:94
IRule()
Creates an IRule.
Definition: IRule.hpp:108
size_t computeIndex(size_t row, size_t col) const
Computes the one dimensional index from two dimension.
Definition: IRule.hpp:435
Provides an interface to send data along RuleManager edges for processing state and dependencies...
Definition: ExecutionPipeline.hpp:34
Base class for an htgs::IRule to hide the template arguments.
StateContainer< std::shared_ptr< T > > * allocStateContainer(size_t size)
Allocates a one dimensional state container using the input type of the IRule.
Definition: IRule.hpp:229
Base class for an htgs::IRule.
Definition: AnyIRule.hpp:25
StateContainer(size_t height, size_t width, T emptyData)
Constructs a state container with a width and height, and what it considers to be empty data...
Definition: IRule.hpp:281
void addResult(std::shared_ptr< U > result)
Adds a result value to the output.
Definition: IRule.hpp:188
Implements the IData class, which is used for all data types entering/leaving a task graph...
~StateContainer()
Destructor.
Definition: IRule.hpp:295
size_t height
The height of the StateContainer.
Definition: IRule.hpp:441
void assign(size_t row, size_t col, T value) const
Sets a value at a row column (uses assignment operator)
Definition: IRule.hpp:315
std::list< std::shared_ptr< U > > * output
The output data that is sent as soon as the applyRule has finished processing.
Definition: IRule.hpp:247
void assign(size_t index, T value) const
Sets a value at an index (uses assignment operator)
Definition: IRule.hpp:333
T emptyData
The data value that represents no data.
Definition: IRule.hpp:442
T * data
The pointer to data for the StateContainer.
Definition: IRule.hpp:439
IRule(bool useLocks)
Creates an IRule with locks specified.
Definition: IRule.hpp:118
void printState()
Prints the state of the state container.
Definition: IRule.hpp:403
virtual void shutdownRule(size_t pipelineId) override
Virtual function that handles when a rule is being shutdown for a particular pipelineId.
Definition: IRule.hpp:146
std::list< std::shared_ptr< U > > * applyRuleFunction(std::shared_ptr< T > data, size_t pipelineId)
Applies the virtual rule function and processes output.
Definition: IRule.hpp:178
StateContainer< V > * allocStateContainer(size_t size, V defaultValue)
Allocates a one dimensional state container using the input type of the IRule.
Definition: IRule.hpp:241
Definition: Bookkeeper.hpp:23
StateContainer< std::shared_ptr< T > > * allocStateContainer(size_t height, size_t width)
Allocates a two dimensional state container using the input type of the IRule.
Definition: IRule.hpp:207
virtual ~IRule() override
Destructor.
Definition: IRule.hpp:129