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.

callback_manager.h 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 MINDSPORE_CCSRC_MINDDATA_DATASET_CALLBACK_MANAGER_H
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_CALLBACK_MANAGER_H
  18. #include <memory>
  19. #include <vector>
  20. #include "minddata/dataset/callback/ds_callback.h"
  21. #include "minddata/dataset/util/status.h"
  22. namespace mindspore {
  23. namespace dataset {
  24. // forward declare to avoid cyclic include of dataset_op.h
  25. class DatasetOp;
  26. /// This class manages all the callbacks that are associated with a single DatasetOp. For now, only MapOp supports this.
  27. class CallbackManager {
  28. public:
  29. /// \brief CallbackManager default constructor. Init needs to be called before using the created instance.
  30. CallbackManager() : enabled_(false) {}
  31. ~CallbackManager() = default;
  32. /// \brief
  33. /// \param [in] callbacks list of callbacks to perform
  34. void AddCallbacks(std::vector<std::shared_ptr<DSCallback>> callbacks);
  35. /// \brief DatasetOp needs to call Init if it wishes to use callback, Init will set enabled_ to true
  36. /// \param[in] op, this pointer is used for Callback Manager to Pause Worker threads
  37. /// \return Status
  38. Status Init(DatasetOp *op);
  39. /// \brief callback function called at the start of the first row
  40. /// \return Status
  41. Status Begin(const CallbackParam &);
  42. /// \brief callback function called at the start of each epoch
  43. /// \return Status
  44. Status EpochBegin(const CallbackParam &);
  45. /// \brief callback function called at the start of each row
  46. /// \return Status
  47. Status StepBegin(const CallbackParam &);
  48. /// \brief callback function called after the last row is processed
  49. /// \return Status
  50. Status End(const CallbackParam &);
  51. /// \brief callback function called at the end of each epoch
  52. /// \return Status
  53. Status EpochEnd(const CallbackParam &);
  54. /// \brief callback function called at the the end of each row
  55. /// \return Status
  56. Status StepEnd(const CallbackParam &);
  57. private:
  58. bool enabled_; // flag to enable callback, if false, all functions would return immediately
  59. DatasetOp *op_; // back pointer to DatasetOp, raw pointer to avoid circular ownership
  60. std::vector<std::shared_ptr<DSCallback>> callbacks_; // list of callbacks the DatasetOp needs to call
  61. };
  62. } // namespace dataset
  63. } // namespace mindspore
  64. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_CALLBACK_MANAGER_H