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.

barrier_op.h 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * Copyright 2020 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_BARRIER_OP_H_
  17. #define DATASET_ENGINE_DATASETOPS_BARRIER_OP_H_
  18. #include <memory>
  19. #include <string>
  20. #include <unordered_map>
  21. #include <utility>
  22. #include <vector>
  23. #include "dataset/core/tensor.h"
  24. #include "dataset/engine/dataset_iterator.h"
  25. #include "dataset/engine/datasetops/pipeline_op.h"
  26. #include "dataset/kernels/tensor_op.h"
  27. namespace mindspore {
  28. namespace dataset {
  29. // Forward declare
  30. class DataBuffer;
  31. class ExecutionTree;
  32. // BarrierOp class implements the Barrier operator. It will block sending of rows until a signal has
  33. // been received. This signal is given from python layer. The current barrier design respects the
  34. // rows per buffer design and will only output a buffer with rows once it has received rows per buffer
  35. // signals from python.
  36. class BarrierOp : public PipelineOp {
  37. public:
  38. // The nested builder class inside of the BarrierOp is used to help manage all of
  39. // the arguments for constructing it. Use the builder by setting each argument
  40. // with the provided set methods, and then finally call the build method to execute
  41. // the actual construction.
  42. class Builder {
  43. public:
  44. // Builder constructor. Creates the builder object.
  45. // @note No default args
  46. // @return This is a constructor.
  47. Builder();
  48. // Default destructor
  49. ~Builder() = default;
  50. // Setter method.
  51. // @return Builder setter method returns reference to the builder.
  52. Builder &SetRowsPerBuffer(int32_t rows_per_buffer) {
  53. builder_rows_per_buffer_ = rows_per_buffer;
  54. return *this;
  55. }
  56. // Setter method.
  57. // @param int32_t op_connector_size
  58. // @return Builder setter method returns reference to the builder.
  59. Builder &SetOpConnectorSize(int32_t op_connector_size) {
  60. builder_op_connector_size_ = op_connector_size;
  61. return *this;
  62. }
  63. // Setter method.
  64. // @param const std::string & condition_name
  65. // @return Builder setter method returns reference to the builder.
  66. Builder &SetConditionName(const std::string &condition_name) {
  67. builder_condition_name_ = condition_name;
  68. return *this;
  69. }
  70. // Setter method.
  71. // @param py::function condition_func - blocking condition function
  72. // @return Builder setter method returns reference to the builder.
  73. Builder &SetConditionFunc(py::function condition_func) {
  74. builder_condition_func_ = condition_func;
  75. return *this;
  76. }
  77. // The builder "build" method creates the BarrierOp dataset Operator.
  78. // @return shared_ptr to the new BarrierOp object
  79. Status Build(std::shared_ptr<BarrierOp> *);
  80. private:
  81. int32_t builder_rows_per_buffer_;
  82. int32_t builder_op_connector_size_;
  83. std::string builder_condition_name_;
  84. py::function builder_condition_func_;
  85. Status SanityCheck() const;
  86. };
  87. // Constructor for BarrierOp
  88. // @param rows_per_buffer - number of rows in output buffer
  89. // @param op_connector_size - connector size
  90. // @param condition_name - the condition name associated with this operator
  91. // @param condition_func - the blocking condition check per row
  92. // @note - currently rows_per_buffer should = 1 for barrier.
  93. // The reason for this is having other values would complicate how the pipeline behaves with other operators
  94. // One example of such case is having batch after barrier. Batch would be waiting for data and having
  95. // rows per buffer in this case can result in hanging
  96. BarrierOp(int32_t rows_per_buffer, int32_t op_connector_size, const std::string &condition_name,
  97. py::function condition_func);
  98. // Destructor
  99. ~BarrierOp();
  100. Status EofReceived(int32_t) override;
  101. Status EoeReceived(int32_t) override;
  102. // Print function for Barrier
  103. // @param out - output stream to print to
  104. // @param show_all - if it should print everything
  105. void Print(std::ostream &out, bool show_all) const override;
  106. // Provide stream operator for displaying it
  107. friend std::ostream &operator<<(std::ostream &out, const BarrierOp &bo) {
  108. bo.Print(out, false);
  109. return out;
  110. }
  111. // Class functor operator () override.
  112. // All dataset ops operate by launching a thread (see ExecutionTree). This class functor will
  113. // provide the master loop that drives the logic for performing the work
  114. // @return Status - The error code return
  115. Status operator()() override;
  116. // Handles preprocessing of the main loop, used when starting new epoch
  117. // @param table - a table of tensors to be moved into a buffer
  118. Status prepare(TensorQTable *const table);
  119. // This function calls takes a table repeatedly adds rows to it.
  120. // @param table - a table of tensors to be moved into a buffer
  121. Status fillBuffer(TensorQTable *const table);
  122. // Gets next tensor row and sets control signals
  123. Status getNextTensorRow(TensorRow *new_row);
  124. // This function runs the wait function on condition
  125. Status blockCond();
  126. private:
  127. // clean up variable to return imcomplete buffer
  128. bool clean_up_;
  129. // end of file state, we stop reading data and shut down
  130. bool eof_;
  131. // rows per buffer
  132. int32_t rows_per_buffer_;
  133. // buffer_id
  134. int32_t buffer_id_;
  135. // local variable to keep track of the buffer information
  136. std::unordered_map<std::string, int32_t> col_name_id_map_;
  137. // iterator to pull new rows, we only have one child
  138. std::unique_ptr<ChildIterator> child_iterator_;
  139. // condition name, to support multiple barriers
  140. std::string condition_name_;
  141. // Function pointer of blocking function
  142. py::function condition_function_;
  143. };
  144. } // namespace dataset
  145. } // namespace mindspore
  146. #endif // DATASET_ENGINE_DATASETOPS_BARRIER_OP_H_