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

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