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.

dataset_op.h 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_DATASETOPS_DATASET_OP_H_
  17. #define DATASET_ENGINE_DATASETOPS_DATASET_OP_H_
  18. #include <memory>
  19. #include <mutex>
  20. #include <string>
  21. #include <unordered_map>
  22. #include <vector>
  23. #include "dataset/core/constants.h"
  24. #include "dataset/engine/db_connector.h"
  25. #include "dataset/util/status.h"
  26. namespace mindspore {
  27. namespace dataset {
  28. // Forward declare
  29. class ExecutionTree;
  30. class DataBuffer;
  31. // The base class DatasetOp is the main tree node. It is an abstract class, so
  32. // the actual implementation of the operators will be derived from here.
  33. class DatasetOp : public std::enable_shared_from_this<DatasetOp> {
  34. // Allow execution tree to access internal members
  35. friend class ExecutionTree;
  36. public:
  37. static constexpr int32_t kInvalidOperatorId = -1;
  38. // Flags that control operator runtime behaviours
  39. enum OpControlFlags {
  40. kDeOpNone = 0,
  41. kDeOpRepeated = 1, // Operator is a leaf node in a repeat path
  42. kDeOpLastRepeat = 1 << 1 // We are in the last repeat loop
  43. };
  44. // Flags that control operator runtime behaviours
  45. enum OpState { kDeOpRunning = 0, kDeOpIdle = 1 };
  46. // Constructor
  47. // @param op_connector_size - The size for the output connector of this operator.
  48. explicit DatasetOp(int32_t op_connector_size);
  49. // Destructor
  50. virtual ~DatasetOp() { tree_ = nullptr; }
  51. // Adds a operator to become our child.
  52. // @param child - shared pointer to the child to add.
  53. Status AddChild(std::shared_ptr<DatasetOp> child);
  54. // Getter function to get a shared pointer to our child
  55. // @param child_index - An operator can have n children. Indicates choose which child to return.
  56. std::shared_ptr<DatasetOp> child(int32_t child_index) const;
  57. // Creates the connector within this operator
  58. // @param num_producers - number of threads that write into this connector
  59. // @param num_consumers - number of threads that read from this connector
  60. void CreateConnector(int32_t num_producers, int32_t num_consumers);
  61. // A print method typically used for debugging
  62. // @param out - The output stream to write output to
  63. // @param show_all - A bool to control if you want to show all info or just a summary
  64. virtual void Print(std::ostream &out, bool show_all) const;
  65. // << Stream output operator overload
  66. // @notes This allows you to write the debug print info using stream operators
  67. // @param out - reference to the output stream being overloaded
  68. // @param dO - reference to the DatasetOp to display
  69. // @return - the output stream must be returned
  70. friend std::ostream &operator<<(std::ostream &out, const DatasetOp &dO) {
  71. dO.Print(out, false);
  72. return out;
  73. }
  74. // Class functor operator ().
  75. // DatasetOps operate by launching a thread (see ExecutionTree).
  76. // This pure virtual version makes the requirement that derived classes must provide a functor
  77. // that will execute their main runtime loop code.
  78. // @return Status - The error code return
  79. virtual Status operator()() = 0;
  80. // Gets the next buffer from the given child
  81. // @notes See GetNextInput for similar function that has built-in message handling
  82. // @param p_buffer - The shared pointer for the fetched buffer to return (by reference)
  83. // @param worker_id - The worker id
  84. // @return Status - The error code return
  85. virtual Status GetNextBuffer(std::unique_ptr<DataBuffer> *p_buffer, int32_t worker_id) {
  86. return GetNextBuffer(p_buffer, worker_id, false);
  87. }
  88. // Gets the next buffer from the given child
  89. // @notes See GetNextInput for similar function that has built-in message handling
  90. // @param p_buffer - The shared pointer for the fetched buffer to return (by reference)
  91. // @return Status - The error code return
  92. virtual Status GetNextBuffer(std::unique_ptr<DataBuffer> *p_buffer) { return GetNextBuffer(p_buffer, 0, false); }
  93. // Gets the next buffer from the given child
  94. // @notes See GetNextInput for similar function that has built-in message handling
  95. // @param p_buffer - The shared pointer for the fetched buffer to return (by reference)
  96. // @param worker_id - The worker id
  97. // @param retry_if_eoe Set this flag to true to allow calling pop() again after the first pop() returns EOE.
  98. // @return Status - The error code return
  99. virtual Status GetNextBuffer(std::unique_ptr<DataBuffer> *p_buffer, int32_t worker_id, bool retry_if_eoe);
  100. // Gets the next buffer from the given child . This function also has built-in eoe and eof
  101. // message handling so that child classes don't have to manually code pass-through logic when
  102. // those messages are received.
  103. // @param p_buffer - The shared pointer for the fetched buffer to return (by reference)
  104. // @param worker_id - The worker id
  105. // @return Status - The error code return
  106. Status GetNextInput(std::unique_ptr<DataBuffer> *p_buffer, int32_t worker_id = 0, int32_t child_index = 0);
  107. // Performs handling for when an eoe message is received.
  108. // The base class implementation simply flows the eoe message to output. Derived classes
  109. // may override if they need to perform special eoe handling.
  110. // @param worker_id - The worker id
  111. // @return Status - The error code return
  112. virtual Status EoeReceived(int32_t worker_id);
  113. // Performs handling for when an eof message is received.
  114. // The base class implementation simply flows the eof message to output. Derived classes
  115. // may override if they need to perform special eof handling.
  116. // @param worker_id - The worker id
  117. // @return Status - The error code return
  118. virtual Status EofReceived(int32_t worker_id);
  119. // Derived classes may implement the reset function if the operator is stateful and needs
  120. // specific reset handling that is not contained in this common code version of the reset
  121. // @return Status - The error code return
  122. virtual Status Reset();
  123. // This calls the reset function on this subtree in pre-order
  124. // @return Status - The error code return
  125. virtual Status ResetSubtree() {
  126. RETURN_IF_NOT_OK(Reset());
  127. for (const auto &c : child_) {
  128. RETURN_IF_NOT_OK(c->ResetSubtree());
  129. }
  130. return Status::OK();
  131. }
  132. // During tree prepare phase, operators may have specific pre-operations to perform depending on
  133. // their role.
  134. // @notes Derived versions of this function should always call it's superclass version first
  135. // before providing their own implementations.
  136. virtual Status PrepareNodePreAction();
  137. // During tree prepare phase, operators may have specific post-operations to perform depending on
  138. // their role.
  139. // @notes Derived versions of this function should always call it's superclass version first
  140. // before providing their own implementations.
  141. virtual Status PrepareNodePostAction();
  142. // Getter function
  143. // @return The operator id
  144. int32_t id() const { return operator_id_; }
  145. // Getter function
  146. // @return The prepare flags
  147. virtual uint32_t PrepareFlags() const;
  148. // Getter function
  149. // @return The number of workers in this op
  150. virtual int32_t num_workers() const = 0;
  151. // Getter function
  152. // @return The number of threads consuming from previous op.
  153. virtual int32_t num_consumers() const = 0;
  154. // Getter function
  155. // @return The number of threads producing to the output connector.
  156. virtual int32_t num_producers() const = 0;
  157. // Getter function
  158. // @return T/F if this is an inlined operator
  159. bool inlined() const { return (oc_queue_size_ == 0); }
  160. // Setter function
  161. // @return Sets the control flags
  162. void set_control_flag(uint64_t flag) { BitSet(&op_ctrl_flags_, flag); }
  163. // Register the internal worker connectors. No op unless it is a parallel op
  164. // @return Status
  165. virtual Status RegisterWorkerConnectors() { return Status::OK(); }
  166. // Getter for the column name mapping
  167. // @return The returned map
  168. std::unordered_map<std::string, int32_t> column_name_id_map() const { return column_name_id_map_; }
  169. // Checks if the column name map has been set up yet for this op
  170. // @return - T/F if the operator has the map set up
  171. bool HasColumnNameMap() const { return (column_name_id_map_.empty()); }
  172. // gives a string output for the column map for handy debug printing
  173. // @return - the column name map as a string
  174. std::string ColumnNameMapAsString() const;
  175. protected:
  176. // Adds a parent operator to this operator
  177. // @notes External callers do not have access to this function.
  178. // @param parent - The parent node to add
  179. void AddParent(const DatasetOp *parent);
  180. // A helper function for providing an assignment of the column name map.
  181. // This grabs the map from child 0 and assigns it into this op.
  182. // Can only be used if number of children is 1.
  183. // @return - Status
  184. Status AssignColMapFromChild();
  185. std::vector<std::shared_ptr<DatasetOp>> child_; // Child nodes
  186. std::vector<const DatasetOp *> parent_; // Parent nodes. No ownership and read-only
  187. int32_t oc_queue_size_; // Capacity for each out_connector_
  188. int32_t operator_id_; // Generated id for the node
  189. ExecutionTree *tree_; // Back pointer to our tree.
  190. OpState state_; // The state of the operator, Running, Idle, Terminated
  191. uint32_t op_ctrl_flags_; // Flags for the operator
  192. std::unique_ptr<DbConnector> out_connector_; // Output Connector
  193. std::unordered_map<std::string, int32_t> column_name_id_map_; // Mapping between col index and col name
  194. bool first_fetch_; // For use when setting column map
  195. std::mutex column_name_map_mutex_; // For protecting shared access to the column map
  196. private:
  197. // Sets the operator id.
  198. // @notes No public interface. Only the class itself, or it's friend the execution tree can set
  199. // this
  200. // @param op_id - the Id value to set into the operator
  201. void set_id(int32_t op_id) { operator_id_ = op_id; }
  202. // Sets the tree into the op so that the operator has a back pointer to the tree.
  203. // @param tree - the tree to assign to the op.
  204. void set_tree(ExecutionTree *tree) { tree_ = tree; }
  205. };
  206. } // namespace dataset
  207. } // namespace mindspore
  208. #endif // DATASET_ENGINE_DATASETOPS_DATASET_OP_H_