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

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