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.

take_op.h 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_TAKE_OP_H_
  17. #define DATASET_ENGINE_DATASETOPS_TAKE_OP_H_
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include "dataset/engine/datasetops/pipeline_op.h"
  22. namespace mindspore {
  23. namespace dataset {
  24. class TakeOp : public PipelineOp {
  25. public:
  26. // The nested builder class inside of the TakeOp is used to help manage all of the arguments
  27. // for constructing it. This take op is very simple though, so this builder is really just
  28. // provided for a consistent look and feel for creators of Dataset operators overall.
  29. class Builder {
  30. public:
  31. // Builder constructor. Creates the builder object.
  32. // @note No default args
  33. // @param count - The number of takes to do
  34. // @return This is a constructor.
  35. explicit Builder(int32_t count);
  36. // Default destructor
  37. ~Builder() = default;
  38. // The builder "build" method creates the final object.
  39. // @return shared_ptr to the new StorageOp object
  40. Status Build(std::shared_ptr<TakeOp> *);
  41. private:
  42. int32_t build_max_takes_;
  43. Status SanityCheck() const;
  44. };
  45. // Constructor of the TakeOp.
  46. // @note The builder class should be used to call it
  47. // @param count - The number of takes to do
  48. explicit TakeOp(int32_t count);
  49. // Destructor
  50. ~TakeOp() = default;
  51. // A print method typically used for debugging
  52. // @param out - The output stream to write output to
  53. // @param show_all - A bool to control if you want to show all info or just a summary
  54. void Print(std::ostream &out, bool show_all) const override;
  55. // << Stream output operator overload
  56. // @notes This allows you to write the debug print info using stream operators
  57. // @param out - reference to the output stream being overloaded
  58. // @param ro - reference to the TakeOp to display
  59. // @return - the output stream must be returned
  60. friend std::ostream &operator<<(std::ostream &out, const TakeOp &ro) {
  61. ro.Print(out, false);
  62. return out;
  63. }
  64. // Class functor operator () override.
  65. // Most dataset ops operate by launching a thread (see ExecutionTree).
  66. // However, the TakeOp is defined as a inlined operator, so it is invalid to launch the
  67. // functor since this op runs inlined inside another operator. The function is overloaded to
  68. // ensure that it is not called by mistake (it will generate an error).
  69. // @return Status - The error code return
  70. Status operator()() override;
  71. // Gets a buffer from the child node. The caller is typically our parent node.
  72. // @note This function sets the `retryIfEoe` flag when popping from the child connector. This way,
  73. // this function will retry to pop the connector again and will get the non-EOE buffer if any.
  74. // @param p_buffer - output pointer to the buffer that it will fetch.
  75. // @param worker_id - The worker id
  76. // @param retry_if_eoe Set this flag to true to allow calling pop() again after the first pop() returns EOE.
  77. // @return Status - The error code return
  78. Status GetNextBuffer(std::unique_ptr<DataBuffer> *p_buffer, int32_t worker_id, bool retry_if_eoe) override;
  79. // During tree prepare phase, operators may have specific post-operations to perform depending on
  80. // their role.
  81. // @notes Derived versions of this function should always call it's superclass version first
  82. // before providing their own implementations.
  83. Status PrepareNodePostAction() override;
  84. private:
  85. int32_t max_takes_; // The number of takes that the user requested
  86. int32_t take_count_; // A counter for the current number of executed takes
  87. Status FillBuffer(std::unique_ptr<DataBuffer> *buffer, std::unique_ptr<DataBuffer> *data_buffer);
  88. };
  89. } // namespace dataset
  90. } // namespace mindspore
  91. #endif // DATASET_ENGINE_DATASETOPS_TAKE_OP_H_