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 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 set callbacks to empty
  36. void ClearCallbacks() { callbacks_.clear(); }
  37. /// \brief DatasetOp needs to call Init if it wishes to use callback, Init will set enabled_ to true
  38. /// \param[in] op, this pointer is used for Callback Manager to Pause Worker threads
  39. /// \return Status
  40. Status Init(DatasetOp *op);
  41. /// \brief callback function called at the start of the first row
  42. /// \return Status
  43. Status Begin(const CallbackParam &);
  44. /// \brief callback function called at the start of each epoch
  45. /// \return Status
  46. Status EpochBegin(const CallbackParam &);
  47. /// \brief callback function called at the start of each row
  48. /// \return Status
  49. Status StepBegin(const CallbackParam &);
  50. /// \brief callback function called after the last row is processed
  51. /// \return Status
  52. Status End(const CallbackParam &);
  53. /// \brief callback function called at the end of each epoch
  54. /// \return Status
  55. Status EpochEnd(const CallbackParam &);
  56. /// \brief callback function called at the the end of each row
  57. /// \return Status
  58. Status StepEnd(const CallbackParam &);
  59. private:
  60. bool enabled_; // flag to enable callback, if false, all functions would return immediately
  61. DatasetOp *op_; // back pointer to DatasetOp, raw pointer to avoid circular ownership
  62. std::vector<std::shared_ptr<DSCallback>> callbacks_; // list of callbacks the DatasetOp needs to call
  63. };
  64. } // namespace dataset
  65. } // namespace mindspore
  66. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_CALLBACK_MANAGER_H