/** * Copyright 2019 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef MINDSPORE_MINDSPORE_CCSRC_DEVICE_KERNEL_RUNTIME_MANAGER_H_ #define MINDSPORE_MINDSPORE_CCSRC_DEVICE_KERNEL_RUNTIME_MANAGER_H_ #include #include #include #include #include #include #include "common/utils.h" #include "device/kernel_runtime.h" namespace mindspore { namespace device { using KernelRuntimeCreator = std::function()>; class KernelRuntimeManager { public: static KernelRuntimeManager &Instance() { static KernelRuntimeManager instance; return instance; } void Register(const std::string &device_name, KernelRuntimeCreator &&runtime_creator); KernelRuntime *GetKernelRuntime(const std::string &device_name, uint32_t device_id); KernelRuntime *GetSingleKernelRuntime(const std::string &device_name, uint32_t device_id); void ClearRuntimeResource(); void ClearGraphResource(uint32_t graph_id); private: KernelRuntimeManager() = default; ~KernelRuntimeManager() = default; DISABLE_COPY_AND_ASSIGN(KernelRuntimeManager); std::map > runtime_map_; std::map runtime_creators_; std::mutex lock_; }; class KernelRuntimeRegistrar { public: KernelRuntimeRegistrar(const std::string &device_name, KernelRuntimeCreator &&runtime_creator) { KernelRuntimeManager::Instance().Register(device_name, std::move(runtime_creator)); } ~KernelRuntimeRegistrar() = default; }; #define MS_REG_KERNEL_RUNTIME(DEVICE_NAME, RUNTIME_CLASS) \ static const KernelRuntimeRegistrar g_kernel_runtime_##DEVICE_NAME##_reg( \ DEVICE_NAME, []() { return std::make_shared(); }); } // namespace device } // namespace mindspore #endif // MINDSPORE_MINDSPORE_CCSRC_DEVICE_KERNEL_RUNTIME_MANAGER_H_