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