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

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