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.

parallel_op.h 5.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_PARALLEL_OP_H_
  17. #define DATASET_ENGINE_DATASETOPS_PARALLEL_OP_H_
  18. #include <memory>
  19. #include <vector>
  20. #include "dataset/core/constants.h"
  21. #include "dataset/engine/datasetops/dataset_op.h"
  22. #include "dataset/util/status.h"
  23. namespace mindspore {
  24. namespace dataset {
  25. // global const in our namespace
  26. constexpr int32_t kEndOfActions = -1;
  27. // Forward declares
  28. class DataBuffer;
  29. class DbConnector;
  30. // A ParallelOp provides a multi-threaded DatasetOp
  31. class ParallelOp : public DatasetOp {
  32. public:
  33. // Constructor
  34. // @param num_workers
  35. // @param op_connector_size - size of the output connector for this operator
  36. // @param sampler - The sampler for the op
  37. ParallelOp(int32_t num_workers, int32_t op_connector_size, std::shared_ptr<Sampler> sampler = nullptr);
  38. // Destructor
  39. ~ParallelOp() = default;
  40. // Creates the internal worker connector for the parallel op if the derived class wants to use it.
  41. // @notes This changes the number of producers of this op to 1, since it establishes a master/worker
  42. // relationship within the op, making all production flow through a single master.
  43. // @return Status - The error return code
  44. Status CreateWorkerConnector(int32_t worker_connector_size);
  45. // A print method typically used for debugging
  46. // @param out - The output stream to write output to
  47. // @param show_all - A bool to control if you want to show all info or just a summary
  48. void Print(std::ostream &out, bool show_all) const override;
  49. // << Stream output operator overload
  50. // @notes This allows you to write the debug print info using stream operators
  51. // @param out - reference to the output stream being overloaded
  52. // @param pO - reference to the ParallelOp to display
  53. // @return - the output stream must be returned
  54. friend std::ostream &operator<<(std::ostream &out, const ParallelOp &po) {
  55. po.Print(out, false);
  56. return out;
  57. }
  58. // During tree prepare phase, operators may have specific pre-operations to perform depending on
  59. // their role.
  60. // @notes Derived versions of this function should always call it's superclass version first
  61. // before providing their own implementations.
  62. // @return Status - The error return code
  63. Status PrepareNodePreAction() override {
  64. // Run common code from super class before adding ParallelOp specific logic
  65. return (DatasetOp::PrepareNodePreAction());
  66. }
  67. // During tree prepare phase, operators may have specific post-operations to perform depending on
  68. // their role.
  69. // @notes Derived versions of this function should always call it's superclass version first
  70. // before providing their own implementations.
  71. // @return Status - The error return code
  72. Status PrepareNodePostAction() override {
  73. // Run common code from super class before adding ParallelOp specific logic
  74. return (DatasetOp::PrepareNodePostAction());
  75. }
  76. // Override base class reset to provide reset actions specific to the ParallelOp class.
  77. // @return Status - The error code return
  78. Status Reset() override;
  79. // Getter
  80. // @return the number of workers
  81. int32_t num_workers() const override { return num_workers_; }
  82. // Getter
  83. // @return the number of threads consuming from the previous Connector
  84. int32_t num_consumers() const override { return num_workers_; }
  85. // Getter
  86. // @return the number of producers pushing to the output Connector
  87. // @notes The number of producers is commonly the same as number of workers, except in the case
  88. // when a worker connector is set up. In that case, there are n workers, and a single master
  89. // such that only 1 thread is a producer rather than the n workers.
  90. // @return the number of producers
  91. int32_t num_producers() const override { return num_producers_; }
  92. // Register the internal worker connectors.
  93. // @return Status
  94. Status RegisterWorkerConnectors() override;
  95. protected:
  96. // Interface for derived classes to implement. All derived classes must provide the entry
  97. // function with the main execution loop for worker threads.
  98. // @return Status - The error code return
  99. virtual Status WorkerEntry(int32_t workerId) = 0;
  100. int32_t num_workers_; // The number of worker threads
  101. int32_t num_producers_; // The number of threads pushing to the out_connector_
  102. int32_t worker_connector_size_;
  103. std::unique_ptr<DbConnector> worker_connector_; // The internal connector for worker threads
  104. };
  105. } // namespace dataset
  106. } // namespace mindspore
  107. #endif // DATASET_ENGINE_DATASETOPS_PARALLEL_OP_H_