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

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