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_ASYNC_TASK_H_ 18 #define MINDSPORE_MINDSPORE_CCSRC_RUNTIME_PYNATIVE_ASYNC_TASK_H_ 19 20 #include <cstdint> 21 #include <exception> 22 #include "include/backend/visible.h" 23 24 namespace mindspore { 25 namespace runtime { 26 enum TaskType { 27 kUnknownTask = 0, 28 kDeviceOpTask, 29 kDeviceOpBuildTask, 30 kPyBoostOpTask, 31 kBpropTask, 32 kFrontendTask, 33 kBackendTask, 34 kKernelTask, 35 kExitTask, 36 kWaitTask 37 }; 38 39 enum class KernelTaskType { kNORMAL_VIEW_TASK = 0, kCONTIGUOUS_TASK, kCOPY_TASK }; 40 41 class BACKEND_EXPORT AsyncTask { 42 public: AsyncTask(TaskType task_type)43 explicit AsyncTask(TaskType task_type) : task_type_(task_type) {} 44 virtual ~AsyncTask() = default; 45 virtual void Run() = 0; RunWithRet()46 virtual bool RunWithRet() { return false; } SetException(const std::exception_ptr &)47 virtual void SetException(const std::exception_ptr & /* e */) {} 48 task_type()49 TaskType task_type() const { return task_type_; } task_id()50 uint64_t task_id() const { return task_id_; } set_task_id(uint64_t task_id)51 void set_task_id(uint64_t task_id) { task_id_ = task_id; } 52 static uint64_t MakeId(); 53 54 protected: 55 TaskType task_type_; 56 uint64_t task_id_{MakeId()}; 57 }; 58 59 class ExitTask : public AsyncTask { 60 public: ExitTask()61 ExitTask() : AsyncTask(kExitTask) {} 62 ~ExitTask() override = default; Run()63 void Run() override {} 64 }; 65 66 class WaitTask : public AsyncTask { 67 public: WaitTask()68 WaitTask() : AsyncTask(kWaitTask) {} 69 ~WaitTask() override = default; Run()70 void Run() override {} 71 }; 72 } // namespace runtime 73 } // namespace mindspore 74 75 #endif // MINDSPORE_MINDSPORE_CCSRC_RUNTIME_PYNATIVE_ASYNC_TASK_H_ 76