1 /** 2 * Copyright 2022 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_MINDSPORE_CCSRC_RUNTIME_PYNATIVE_OP_EXECUTOR_H_ 18 #define MINDSPORE_MINDSPORE_CCSRC_RUNTIME_PYNATIVE_OP_EXECUTOR_H_ 19 20 #include <vector> 21 #include <memory> 22 #include <queue> 23 #include <map> 24 #include <string> 25 #include <set> 26 #include <utility> 27 #include "include/backend/kernel_graph.h" 28 #include "include/backend/anf_runtime_algorithm.h" 29 #include "include/common/utils/anfalgo.h" 30 #include "runtime/hardware/device_context.h" 31 #include "runtime/graph_scheduler/graph_scheduler.h" 32 #include "include/backend/visible.h" 33 #include "runtime/pipeline/task/device_task.h" 34 #include "runtime/pipeline/async_rqueue.h" 35 36 namespace mindspore::runtime { 37 class BACKEND_EXPORT OpExecutor { 38 public: 39 static OpExecutor &GetInstance(); 40 41 void RegisterForwardCallback(const std::function<void()> &callback); 42 43 void PushOpRunTask(const std::shared_ptr<DeviceOpRunTask> &op_run_task); 44 45 void PushOpRunTask(const std::shared_ptr<PyBoostDeviceTask> &op_run_task); 46 47 void PushSimpleOpRunTask(const std::shared_ptr<AsyncTask> &op_run_task); 48 49 bool RunQueueEmpty(); 50 51 // When an exception occurs, the state needs to be reset. 52 // Because we may sometimes have to ignore the exception and continue to run other tasks 53 void Reset(); 54 55 // Wait for all OpRunTasks to finish executing. 56 void Wait(); 57 58 void WaitAll(); 59 60 // Thread join before the process exit. 61 void WorkerJoin(); 62 63 // Child process reinitialize resource after fork. 64 void ChildAfterFork(); 65 set_async_for_graph(bool flag)66 void set_async_for_graph(bool flag) { async_for_graph_ = flag; } 67 async_for_graph()68 bool async_for_graph() const { return async_for_graph_; } 69 70 static bool NeedSync(); 71 72 static void DispatchLaunchTask(const std::function<void()> &func); 73 74 private: 75 OpExecutor(); 76 ~OpExecutor(); 77 DISABLE_COPY_AND_ASSIGN(OpExecutor); 78 79 void WaitForRun(); 80 std::function<void()> forward_callback_{nullptr}; 81 inline static bool async_for_graph_{false}; 82 }; 83 } // namespace mindspore::runtime 84 #endif // MINDSPORE_MINDSPORE_CCSRC_RUNTIME_PYNATIVE_OP_EXECUTOR_H_ 85