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 9.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #include "mindspore/ccsrc/dataset/engine/perf/profiling.h"
  26. namespace mindspore {
  27. namespace dataset {
  28. // Forward declares
  29. class TaskGroup;
  30. class DatasetOp;
  31. class Monitor;
  32. class ExecutionTree {
  33. public:
  34. // Prepare flags used during tree prepare phase
  35. enum PrepareFlags {
  36. kDePrepNone = 0,
  37. kDePrepRepeat = 1 // Processing a repeat operation
  38. };
  39. // State flags for the lifecycle of the tree
  40. enum TreeState {
  41. kDeTStateInit = 0, // The freshly initialized state after construction
  42. kDeTStateBuilding, // The tree is being built, nodes are being added
  43. kDeTStatePrepare, // The tree has been assigned a root node and is pending prepare
  44. kDeTStateReady, // The tree has been prepared and is ready to be launched
  45. kDeTStateExecuting, // The tree has been launched and is executing
  46. kDeTStateFinished // The tree has been drained, dataset iterator received EOF
  47. };
  48. class Iterator {
  49. public:
  50. // Constructor
  51. // @param root The root node to start iterating from
  52. explicit Iterator(const std::shared_ptr<DatasetOp> &root = nullptr);
  53. // Destructor
  54. ~Iterator() {}
  55. Iterator &operator++() {
  56. ++ind_;
  57. return *this;
  58. } // prefix ++ overload
  59. Iterator operator++(int) {
  60. Iterator it = *this;
  61. it.ind_ = ind_;
  62. ind_++;
  63. return it;
  64. } // post-fix ++ overload
  65. Iterator &operator--() {
  66. --ind_;
  67. return *this;
  68. } // prefix -- overload
  69. Iterator operator--(int) {
  70. Iterator it = *this;
  71. it.ind_ = ind_;
  72. ind_--;
  73. return it;
  74. } // post-fix -- overload
  75. DatasetOp &operator*() { return *nodes_[ind_]; } // dereference operator
  76. std::shared_ptr<DatasetOp> operator->() { return nodes_[ind_]; }
  77. // getter function
  78. // @return Shared pointer to the current operator
  79. std::shared_ptr<DatasetOp> get() { return nodes_[ind_]; }
  80. bool operator!=(const Iterator &rhs) { return nodes_[ind_] != rhs.nodes_[rhs.ind_]; }
  81. private:
  82. int ind_; // the cur node our Iterator points to
  83. std::vector<std::shared_ptr<DatasetOp>> nodes_; // store the nodes in post order
  84. void PostOrderTraverse(const std::shared_ptr<DatasetOp> &);
  85. };
  86. // Constructor
  87. ExecutionTree();
  88. // Destructor
  89. ~ExecutionTree();
  90. // Associates a DatasetOp with this tree. This assigns a valid node id to the operator and
  91. // provides it with a link to the tree. A node cannot form any relationships (parent/child) with
  92. // other nodes unless they are associated with the same tree.
  93. // @param op - The operator to associate
  94. // @return Status - The error code return
  95. Status AssociateNode(const std::shared_ptr<DatasetOp> &op);
  96. // Sets the root node of the tree
  97. // @param op - The operator to assign as root
  98. // @return Status - The error code return
  99. Status AssignRoot(const std::shared_ptr<DatasetOp> &op);
  100. // Start the execution of the tree
  101. // @return Status - The error code return
  102. Status Launch();
  103. // A print method typically used for debugging
  104. // @param out - The output stream to write output to
  105. void Print(std::ostream &out) const;
  106. // Returns an iterator positioned at the start
  107. // @return Iterator - The iterator
  108. ExecutionTree::Iterator begin(const std::shared_ptr<DatasetOp> &root = nullptr) const {
  109. return Iterator(root == nullptr ? root_ : root);
  110. }
  111. // Returns an iterator positioned at the end
  112. // @return Iterator - The iterator
  113. ExecutionTree::Iterator end() const { return Iterator(nullptr); }
  114. // << Stream output operator overload
  115. // @notes This allows you to write the debug print info using stream operators
  116. // @param out - reference to the output stream being overloaded
  117. // @param exe_tree - reference to the execution tree to display
  118. // @return - the output stream must be returned
  119. friend std::ostream &operator<<(std::ostream &out, ExecutionTree &exe_tree) {
  120. exe_tree.Print(out);
  121. return out;
  122. }
  123. // Given the number of workers, launches the worker entry function for each. Essentially a
  124. // wrapper for the TaskGroup handling that is stored inside the execution tree.
  125. // @param num_workers - The number of workers to launch
  126. // @param func - The function entry point that workers will execute
  127. // @return Status - The error code return
  128. Status LaunchWorkers(int32_t num_workers, std::function<Status(uint32_t)> func);
  129. // Getter method
  130. // @return shared_ptr to the root operator
  131. std::shared_ptr<DatasetOp> root() const { return root_; }
  132. // Getter method
  133. // @return the prepare flags
  134. uint32_t PrepareFlags() const { return prepare_flags_; }
  135. // The driver of the prepare phase of the execution tree.
  136. // Prepare phase consists of three sub phases
  137. //
  138. // 1. PrepareTreePreAction()
  139. // Compulsory transformation/action pre optimization.
  140. // For example, CacheOp Insertion
  141. //
  142. // 2. Optimize()
  143. // Optimization transformation/action, optional
  144. // For example, MapOp Fusion
  145. //
  146. // 3. PrepareTreePostAction()
  147. // Compulsory transformation/action post optimization.
  148. // For example, repeatOp inlining
  149. //
  150. // @return Status - The error code return
  151. Status Prepare();
  152. // Compulsory transformation/action pre optimization.
  153. // @return Status - The error code return
  154. Status PrepareTreePreAction();
  155. // Compulsory transformation/action post optimization.
  156. // @return Status - The error code return
  157. Status PrepareTreePostAction();
  158. // Optimization transformation/action, optional.
  159. // @return Status - The error code return
  160. Status Optimize();
  161. // The DEPRECATED driver of the prepare phase of the execution tree. The prepare phase will recursively
  162. // walk the tree to perform modifications to the tree or specific nodes within the tree to get
  163. // it ready for execution.
  164. // @return Status - The error code return
  165. Status PrepareDeprecated();
  166. // Recursive function used during prepare phase to visit a node and drive any pre- and post-
  167. // node actions during a tree walk.
  168. // @param op - The dataset op to work on
  169. // @return Status - The error code return
  170. Status PrepareNode(const std::shared_ptr<DatasetOp> &dataset_op);
  171. // Adds an operator to the repeat stack during prepare phase.
  172. // @param op - The dataset op to work add to repeat stack
  173. // @return Status - The error code return
  174. void AddToRepeatStack(std::shared_ptr<DatasetOp> dataset_op);
  175. // Pops an operator from the repeat stack during prepare phase.
  176. // @return shared_ptr to the popped operator
  177. std::shared_ptr<DatasetOp> PopFromRepeatStack();
  178. // Return the pointer to the TaskGroup
  179. // @return raw pointer to the TaskGroup
  180. TaskGroup *AllTasks() const { return tg_.get(); }
  181. // Return if the ExecutionTree is finished (iterator receives EOF).
  182. // @return Bool - true is ExecutionTree is finished
  183. bool isFinished() const { return tree_state_ == TreeState::kDeTStateFinished; }
  184. // Set the ExecutionTree to Finished state.
  185. void SetFinished() { tree_state_ = TreeState::kDeTStateFinished; }
  186. // Getter for profiling manager, no ownership
  187. ProfilingManager *GetProfilingManager() { return profiling_manager_.get(); }
  188. private:
  189. // A helper functions for doing the recursive printing
  190. // @param dataset_op - The dataset op to print
  191. // @param indent - an indent string for aligning child levels in output
  192. // @param last - an indicator if it's the last child or not
  193. // @param detailed - should it display the detailed node output or the summary line
  194. void PrintNode(std::ostream &out, const std::shared_ptr<DatasetOp> &dataset_op, std::string indent, bool last,
  195. bool detailed) const;
  196. std::unique_ptr<TaskGroup> tg_; // Class for worker management
  197. std::shared_ptr<DatasetOp> root_; // The root node of the tree
  198. int32_t id_count_; // Counter for generating operator id's
  199. uint32_t prepare_flags_; // Flags used during tree prepare
  200. TreeState tree_state_; // Tracking the current tree state
  201. std::stack<std::shared_ptr<DatasetOp>> repeat_stack_; // A stack used during prepare phase
  202. std::unique_ptr<Monitor> perf_monitor_; // Performance Monitor
  203. std::unique_ptr<ProfilingManager> profiling_manager_; // Profiling manager
  204. };
  205. } // namespace dataset
  206. } // namespace mindspore
  207. #endif // DATASET_ENGINE_EXECUTION_TREE_H_