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

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