• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019 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_RUNTIME_DEVICE_KERNEL_RUNTIME_MANAGER_H_
18 #define MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_MANAGER_H_
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <functional>
23 #include <utility>
24 #include <mutex>
25 #include <unordered_set>
26 #include <vector>
27 #include "utils/ms_utils.h"
28 #include "runtime/device/kernel_runtime.h"
29 namespace mindspore {
30 namespace device {
31 using KernelRuntimeCreator = std::function<std::shared_ptr<KernelRuntime>()>;
32 
33 class KernelRuntimeManager {
34  public:
35   static KernelRuntimeManager &Instance();
36   void Register(const std::string &device_name, KernelRuntimeCreator &&runtime_creator);
37   KernelRuntime *GetKernelRuntime(const std::string &device_name, uint32_t device_id);
38   KernelRuntime *GetCurrentKernelRuntime();
39   KernelRuntime *GetSingleKernelRuntime(const std::string &device_name, uint32_t device_id);
40   void ReleaseKernelRuntime(const std::string &device_name, uint32_t device_id);
41   void ClearRuntimeResource();
42   void ClearGraphResource(uint32_t graph_id);
43 
44  private:
45   KernelRuntimeManager() = default;
46   ~KernelRuntimeManager() = default;
47   DISABLE_COPY_AND_ASSIGN(KernelRuntimeManager);
48   std::string GetDeviceKey(const std::string &device_name, uint32_t device_id);
49   std::map<std::string, std::shared_ptr<KernelRuntime> > runtime_map_;
50   std::map<std::string, KernelRuntimeCreator> runtime_creators_;
51   std::mutex lock_;
52 };
53 
54 class KernelRuntimeRegistrar {
55  public:
KernelRuntimeRegistrar(const std::string & device_name,KernelRuntimeCreator && runtime_creator)56   KernelRuntimeRegistrar(const std::string &device_name, KernelRuntimeCreator &&runtime_creator) {
57     KernelRuntimeManager::Instance().Register(device_name, std::move(runtime_creator));
58   }
59   ~KernelRuntimeRegistrar() = default;
60 };
61 
62 #define MS_REG_KERNEL_RUNTIME(DEVICE_NAME, RUNTIME_CLASS)                   \
63   static const KernelRuntimeRegistrar g_kernel_runtime_##DEVICE_NAME##_reg( \
64     DEVICE_NAME, []() { return std::make_shared<RUNTIME_CLASS>(); });
65 }  // namespace device
66 }  // namespace mindspore
67 #endif  // MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_MANAGER_H_
68