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.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * Copyright 2019-2021 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 MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_EXECUTION_TREE_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_EXECUTION_TREE_H_
  18. #include <functional>
  19. #include <memory>
  20. #include <stack>
  21. #include <string>
  22. #include <vector>
  23. #ifndef ENABLE_ANDROID
  24. #if !defined(_WIN32) && !defined(_WIN64) && !defined(__APPLE__)
  25. #include <sys/sysinfo.h>
  26. #include <opencv2/imgproc/imgproc.hpp>
  27. #endif
  28. #endif
  29. #include "minddata/dataset/engine/datasetops/dataset_op.h"
  30. #include "minddata/dataset/util/status.h"
  31. #include "mindspore/ccsrc/minddata/dataset/engine/perf/profiling.h"
  32. namespace mindspore {
  33. namespace dataset {
  34. // Forward declares
  35. class TaskGroup;
  36. class DatasetOp;
  37. class Pass;
  38. using OptPass = std::vector<std::unique_ptr<Pass>>;
  39. class ExecutionTree {
  40. public:
  41. // State flags for the lifecycle of the tree
  42. enum TreeState {
  43. kDeTStateInit = 0, // The freshly initialized state after construction
  44. kDeTStateBuilding, // The tree is being built, nodes are being added
  45. kDeTStatePrepared, // The tree has been prepared and is ready to be launched
  46. kDeTStateExecuting, // The tree has been launched and is executing
  47. kDeTStateEpochEnd, // The tree has been received end of epoch signal, just for profiling
  48. kDeTStateFinished // The tree has been drained, dataset iterator received EOF
  49. };
  50. class Iterator {
  51. public:
  52. // Constructor
  53. // @param root The root node to start iterating from
  54. explicit Iterator(const std::shared_ptr<DatasetOp> &root = nullptr);
  55. // Destructor
  56. ~Iterator() {}
  57. Iterator &operator++() {
  58. ++ind_;
  59. return *this;
  60. } // prefix ++ overload
  61. Iterator operator++(int) {
  62. Iterator it = *this;
  63. it.ind_ = ind_;
  64. ind_++;
  65. return it;
  66. } // post-fix ++ overload
  67. Iterator &operator--() {
  68. --ind_;
  69. return *this;
  70. } // prefix -- overload
  71. Iterator operator--(int) {
  72. Iterator it = *this;
  73. it.ind_ = ind_;
  74. ind_--;
  75. return it;
  76. } // post-fix -- overload
  77. DatasetOp &operator*() { return *nodes_[ind_]; } // dereference operator
  78. std::shared_ptr<DatasetOp> operator->() { return nodes_[ind_]; }
  79. // getter function
  80. // @return Shared pointer to the current operator
  81. std::shared_ptr<DatasetOp> get() { return nodes_[ind_]; }
  82. bool operator==(const Iterator &rhs) { return nodes_[ind_] == rhs.nodes_[rhs.ind_]; }
  83. bool operator!=(const Iterator &rhs) { return nodes_[ind_] != rhs.nodes_[rhs.ind_]; }
  84. int32_t NumNodes() { return nodes_.size(); }
  85. private:
  86. int32_t ind_; // the cur node our Iterator points to
  87. std::vector<std::shared_ptr<DatasetOp>> nodes_; // store the nodes in post order
  88. void PostOrderTraverse(const std::shared_ptr<DatasetOp> &);
  89. };
  90. // Constructor
  91. ExecutionTree();
  92. // Destructor
  93. ~ExecutionTree();
  94. /// \brief Associates a DatasetOp with this tree. This assigns a valid node id to the operator and
  95. /// provides it with a link to the tree. A node cannot form any relationships (parent/child) with
  96. /// other nodes unless they are associated with the same tree.
  97. /// \param op - The operator to associate
  98. /// \return Status The status code returned
  99. Status AssociateNode(const std::shared_ptr<DatasetOp> &op);
  100. /// \brief Set the root node of the tree
  101. /// \param op - The operator to assign as root
  102. /// \return Status The status code returned
  103. Status AssignRoot(const std::shared_ptr<DatasetOp> &op);
  104. /// \brief Start the execution of the tree
  105. /// \return Status The status code returned
  106. Status Launch();
  107. /// /brief A print method typically used for debugging
  108. /// \param out - The output stream to write output to
  109. void Print(std::ostream &out, const std::shared_ptr<DatasetOp> &op = nullptr) const;
  110. /// \brief Return an iterator positioned at the start
  111. /// \return Iterator - The iterator
  112. ExecutionTree::Iterator begin(const std::shared_ptr<DatasetOp> &root = nullptr) const {
  113. return Iterator(root == nullptr ? root_ : root);
  114. }
  115. /// \brief Return an iterator positioned at the end
  116. /// \return Iterator - The iterator
  117. ExecutionTree::Iterator end() const { return Iterator(nullptr); }
  118. /// \brief << Stream output operator overload
  119. /// \notes This allows you to write the debug print info using stream operators
  120. /// \param out - reference to the output stream being overloaded
  121. /// \param exe_tree - reference to the execution tree to display
  122. /// \return - the output stream must be returned
  123. friend std::ostream &operator<<(std::ostream &out, ExecutionTree &exe_tree) {
  124. exe_tree.Print(out);
  125. return out;
  126. }
  127. /// \brief Given the number of workers, launches the worker entry function for each. Essentially a
  128. /// wrapper for the TaskGroup handling that is stored inside the execution tree.
  129. /// \param num_workers - The number of workers to launch
  130. /// \param func - The function entry point that workers will execute
  131. /// \param name - The description of worker to launch
  132. /// \param op_id - The id of corresponding operator, if not inherit from dataset op then it is -1.
  133. /// \return Status The status code returned
  134. Status LaunchWorkers(int32_t num_workers, std::function<Status(uint32_t)> func, std::string name = "",
  135. int32_t operator_id = -1);
  136. /// \brief Getter method
  137. /// \return shared_ptr to the root operator
  138. std::shared_ptr<DatasetOp> root() const { return root_; }
  139. /// \brief The prepare phase walks the tree in post-order to perform modifications to get it ready for execution.
  140. /// \return Status The status code returned
  141. Status Prepare();
  142. /// \brief Return the pointer to the TaskGroup
  143. /// \return raw pointer to the TaskGroup
  144. TaskGroup *const AllTasks() const { return tg_.get(); }
  145. /// \brief Return if the ExecutionTree is at end of epoch status
  146. /// \return bool - true is ExecutionTree is end of epoch status
  147. bool IsEpochEnd() const { return tree_state_ == TreeState::kDeTStateEpochEnd; }
  148. /// \brief Set the ExecutionTree to EOE state
  149. void SetEpochEnd() { tree_state_ = TreeState::kDeTStateEpochEnd; }
  150. /// \brief Set the ExecutionTree to executing state
  151. void SetExecuting() { tree_state_ = TreeState::kDeTStateExecuting; }
  152. /// \brief Set the ExecutionTree to Finished state.
  153. void SetFinished() { tree_state_ = TreeState::kDeTStateFinished; }
  154. /// \brief Return if the ExecutionTree is finished (iterator receives EOF).
  155. /// \return Bool - true is ExecutionTree is finished
  156. bool isFinished() const { return tree_state_ == TreeState::kDeTStateFinished; }
  157. /// \brief Return if the ExecutionTree is ready.
  158. /// \return Bool - true is ExecutionTree is ready
  159. bool isPrepared() const {
  160. return tree_state_ == TreeState::kDeTStatePrepared || tree_state_ == TreeState::kDeTStateExecuting ||
  161. tree_state_ == TreeState::kDeTStateFinished;
  162. }
  163. /// \brief Getter for profiling manager, no ownership
  164. ProfilingManager *GetProfilingManager() { return profiling_manager_.get(); }
  165. private:
  166. /// \brief A helper functions for doing the recursive printing
  167. /// \param dataset_op - The dataset op to print
  168. /// \param indent - an indent string for aligning child levels in output
  169. /// \param last - an indicator if it's the last child or not
  170. /// \param detailed - should it display the detailed node output or the summary line
  171. void PrintNode(std::ostream &out, const std::shared_ptr<DatasetOp> &dataset_op, std::string indent, bool last,
  172. bool detailed) const;
  173. std::unique_ptr<TaskGroup> tg_; // Class for worker management
  174. std::shared_ptr<DatasetOp> root_; // The root node of the tree
  175. int32_t id_count_; // Counter for generating operator id's
  176. uint32_t prepare_flags_; // Flags used during tree prepare
  177. TreeState tree_state_; // Tracking the current tree state
  178. std::unique_ptr<ProfilingManager> profiling_manager_; // Profiling manager
  179. #if defined(ENABLE_GPUQUE) || defined(ENABLE_TDTQUE)
  180. // This rank_id is for numa and device_queue, one process work with only one rank_id,
  181. // for standalone scenario, this rank_id may come from env 'CUDA_VISIBLE_DEVICES',
  182. // but for distribute scenario, this rank_id come from _get_global_rank() in python
  183. int32_t rank_id_;
  184. bool numa_enable_;
  185. void *handle_;
  186. #endif
  187. };
  188. } // namespace dataset
  189. } // namespace mindspore
  190. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_EXECUTION_TREE_H_