You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

execution_tree.h 8.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef DATASET_ENGINE_EXECUTION_TREE_H_
  17. #define DATASET_ENGINE_EXECUTION_TREE_H_
  18. #include <functional>
  19. #include <memory>
  20. #include <stack>
  21. #include <string>
  22. #include <vector>
  23. #include "dataset/engine/datasetops/dataset_op.h"
  24. #include "dataset/util/status.h"
  25. namespace mindspore {
  26. namespace dataset {
  27. // Forward declares
  28. class TaskGroup;
  29. class DatasetOp;
  30. class ExecutionTree {
  31. public:
  32. // Prepare flags used during tree prepare phase
  33. enum PrepareFlags {
  34. kDePrepNone = 0,
  35. kDePrepRepeat = 1 // Processing a repeat operation
  36. };
  37. // State flags for the lifecycle of the tree
  38. enum TreeState {
  39. kDeTStateInit = 0, // The freshly initialized state after construction
  40. kDeTStateBuilding, // The tree is being built, nodes are being added
  41. kDeTStatePrepare, // The tree has been assigned a root node and is pending prepare
  42. kDeTStateReady, // The tree has been prepared and is ready to be launched
  43. kDeTStateExecuting // The tree has been launched and is executing
  44. };
  45. class Iterator {
  46. public:
  47. // Constructor
  48. // @param root The root node to start iterating from
  49. explicit Iterator(const std::shared_ptr<DatasetOp> &root = nullptr);
  50. // Destructor
  51. ~Iterator() {}
  52. Iterator &operator++() {
  53. ++ind_;
  54. return *this;
  55. } // prefix ++ overload
  56. Iterator operator++(int) {
  57. Iterator it = *this;
  58. it.ind_ = ind_;
  59. ind_++;
  60. return it;
  61. } // post-fix ++ overload
  62. Iterator &operator--() {
  63. --ind_;
  64. return *this;
  65. } // prefix -- overload
  66. Iterator operator--(int) {
  67. Iterator it = *this;
  68. it.ind_ = ind_;
  69. ind_--;
  70. return it;
  71. } // post-fix -- overload
  72. DatasetOp &operator*() { return *nodes_[ind_]; } // dereference operator
  73. std::shared_ptr<DatasetOp> operator->() { return nodes_[ind_]; }
  74. // getter function
  75. // @return Shared pointer to the current operator
  76. std::shared_ptr<DatasetOp> get() { return nodes_[ind_]; }
  77. bool operator!=(const Iterator &rhs) { return nodes_[ind_] != rhs.nodes_[rhs.ind_]; }
  78. private:
  79. int ind_; // the cur node our Iterator points to
  80. std::vector<std::shared_ptr<DatasetOp>> nodes_; // store the nodes in post order
  81. void PostOrderTraverse(const std::shared_ptr<DatasetOp> &);
  82. };
  83. // Constructor
  84. ExecutionTree();
  85. // Destructor
  86. ~ExecutionTree();
  87. // Associates a DatasetOp with this tree. This assigns a valid node id to the operator and
  88. // provides it with a link to the tree. A node cannot form any relationships (parent/child) with
  89. // other nodes unless they are associated with the same tree.
  90. // @param op - The operator to associate
  91. // @return Status - The error code return
  92. Status AssociateNode(const std::shared_ptr<DatasetOp> &op);
  93. // Sets the root node of the tree
  94. // @param op - The operator to assign as root
  95. // @return Status - The error code return
  96. Status AssignRoot(const std::shared_ptr<DatasetOp> &op);
  97. // Start the execution of the tree
  98. // @return Status - The error code return
  99. Status Launch();
  100. // A print method typically used for debugging
  101. // @param out - The output stream to write output to
  102. void Print(std::ostream &out) const;
  103. // Returns an iterator positioned at the start
  104. // @return Iterator - The iterator
  105. ExecutionTree::Iterator begin(const std::shared_ptr<DatasetOp> &root = nullptr) const {
  106. return Iterator((root == nullptr) ? root_ : root);
  107. }
  108. // Returns an iterator positioned at the end
  109. // @return Iterator - The iterator
  110. ExecutionTree::Iterator end() const { return Iterator(nullptr); }
  111. // << Stream output operator overload
  112. // @notes This allows you to write the debug print info using stream operators
  113. // @param out - reference to the output stream being overloaded
  114. // @param exe_tree - reference to the execution tree to display
  115. // @return - the output stream must be returned
  116. friend std::ostream &operator<<(std::ostream &out, ExecutionTree &exe_tree) {
  117. exe_tree.Print(out);
  118. return out;
  119. }
  120. // Given the number of workers, launches the worker entry function for each. Essentially a
  121. // wrapper for the TaskGroup handling that is stored inside the execution tree.
  122. // @param num_workers - The number of workers to launch
  123. // @param func - The function entry point that workers will execute
  124. // @return Status - The error code return
  125. Status LaunchWorkers(int32_t num_workers, std::function<Status(uint32_t)> func);
  126. // Getter method
  127. // @return shared_ptr to the root operator
  128. std::shared_ptr<DatasetOp> root() const { return root_; }
  129. // Getter method
  130. // @return the prepare flags
  131. uint32_t PrepareFlags() const { return prepare_flags_; }
  132. // The driver of the prepare phase of the execution tree.
  133. // Prepare phase consists of three sub phases
  134. //
  135. // 1. PrepareTreePreAction()
  136. // Compulsory transformation/action pre optimization.
  137. // For example, CacheOp Insertion
  138. //
  139. // 2. Optimize()
  140. // Optimization transformation/action, optional
  141. // For example, MapOp Fusion
  142. //
  143. // 3. PrepareTreePostAction()
  144. // Compulsory transformation/action post optimization.
  145. // For example, repeatOp inlining
  146. //
  147. // @return Status - The error code return
  148. Status Prepare();
  149. // Compulsory transformation/action pre optimization.
  150. // @return Status - The error code return
  151. Status PrepareTreePreAction();
  152. // Compulsory transformation/action post optimization.
  153. // @return Status - The error code return
  154. Status PrepareTreePostAction();
  155. // Optimization transformation/action, optional.
  156. // @return Status - The error code return
  157. Status Optimize();
  158. // The DEPRECATED driver of the prepare phase of the execution tree. The prepare phase will recursively
  159. // walk the tree to perform modifications to the tree or specific nodes within the tree to get
  160. // it ready for execution.
  161. // @return Status - The error code return
  162. Status PrepareDeprecated();
  163. // Recursive function used during prepare phase to visit a node and drive any pre- and post-
  164. // node actions during a tree walk.
  165. // @param op - The dataset op to work on
  166. // @return Status - The error code return
  167. Status PrepareNode(const std::shared_ptr<DatasetOp> &dataset_op);
  168. // Adds an operator to the repeat stack during prepare phase.
  169. // @param op - The dataset op to work add to repeat stack
  170. // @return Status - The error code return
  171. void AddToRepeatStack(std::shared_ptr<DatasetOp> dataset_op);
  172. // Pops an operator from the repeat stack during prepare phase.
  173. // @return shared_ptr to the popped operator
  174. std::shared_ptr<DatasetOp> PopFromRepeatStack();
  175. // Return the pointer to the TaskGroup
  176. // @return raw pointer to the TaskGroup
  177. TaskGroup *AllTasks() const { return tg_.get(); }
  178. private:
  179. // A helper functions for doing the recursive printing
  180. // @param dataset_op - The dataset op to print
  181. // @param indent - an indent string for aligning child levels in output
  182. // @param last - an indicator if it's the last child or not
  183. // @param detailed - should it display the detailed node output or the summary line
  184. void PrintNode(std::ostream &out, const std::shared_ptr<DatasetOp> &dataset_op, std::string indent, bool last,
  185. bool detailed) const;
  186. std::unique_ptr<TaskGroup> tg_; // Class for worker management
  187. std::shared_ptr<DatasetOp> root_; // The root node of the tree
  188. int32_t id_count_; // Counter for generating operator id's
  189. uint32_t prepare_flags_; // Flags used during tree prepare
  190. TreeState tree_state_; // Tracking the current tree state
  191. std::stack<std::shared_ptr<DatasetOp>> repeat_stack_; // A stack used during prepare phase
  192. };
  193. } // namespace dataset
  194. } // namespace mindspore
  195. #endif // DATASET_ENGINE_EXECUTION_TREE_H_