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.cc 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include "dataset/engine/datasetops/parallel_op.h"
  17. #include <cstdint>
  18. #include <iostream>
  19. #include <map>
  20. #include <utility>
  21. #include "dataset/engine/data_schema.h"
  22. #include "dataset/engine/datasetops/dataset_op.h"
  23. #include "dataset/engine/execution_tree.h"
  24. #include "dataset/core/config_manager.h"
  25. #include "dataset/engine/db_connector.h"
  26. #include "dataset/engine/datasetops/source/storage_client.h"
  27. #include "dataset/util/task_manager.h"
  28. namespace mindspore {
  29. namespace dataset {
  30. // Constructor
  31. ParallelOp::ParallelOp(int32_t num_workers, int32_t op_connector_size)
  32. : DatasetOp(op_connector_size),
  33. num_workers_(num_workers),
  34. num_producers_(num_workers),
  35. worker_connector_size_(1),
  36. worker_connector_(nullptr) {}
  37. // Creates the internal worker connector for the parallel op if the derived class wants to use it
  38. Status ParallelOp::CreateWorkerConnector(int32_t worker_connector_size) {
  39. if (worker_connector_size == 0) {
  40. RETURN_STATUS_UNEXPECTED("Worker connector size 0 is invalid.");
  41. }
  42. num_producers_ = 1;
  43. worker_connector_size_ = worker_connector_size;
  44. // Instantiate the worker connector. This is the internal connector, not the operators
  45. // output connector. It has single master consuming from it (num producers is 1), and the number
  46. // of workers is the defined count from the op.
  47. worker_connector_ = std::make_unique<DbConnector>(num_workers_, num_producers_, worker_connector_size);
  48. return Status::OK();
  49. }
  50. // A print method typically used for debugging
  51. void ParallelOp::Print(std::ostream &out, bool show_all) const {
  52. // Call base class printer first
  53. DatasetOp::Print(out, show_all);
  54. // Then show our own stuff
  55. out << "ParallelOp:";
  56. out << "\n Num workers : " << num_workers_ << "\n";
  57. }
  58. // Override base class reset to provide reset actions specific to the ParallelOp class.
  59. Status ParallelOp::Reset() {
  60. RETURN_IF_NOT_OK(DatasetOp::Reset()); // Perform any super class reset work
  61. // ParallelOp is abstract, but we do own the connector between workers and master
  62. // (if the parallel op is configured for this). Reset that connector here.
  63. if (worker_connector_) {
  64. worker_connector_->Reset();
  65. }
  66. return Status::OK();
  67. }
  68. // Register the internal worker connectors
  69. Status ParallelOp::RegisterWorkerConnectors() {
  70. if (worker_connector_) {
  71. return (worker_connector_->Register(tree_->AllTasks()));
  72. }
  73. return Status::OK();
  74. }
  75. } // namespace dataset
  76. } // namespace mindspore