You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

kernel_runtime_manager.h 2.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #ifndef MINDSPORE_MINDSPORE_CCSRC_DEVICE_KERNEL_RUNTIME_MANAGER_H_
  17. #define MINDSPORE_MINDSPORE_CCSRC_DEVICE_KERNEL_RUNTIME_MANAGER_H_
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <functional>
  22. #include <utility>
  23. #include <mutex>
  24. #include "common/utils.h"
  25. #include "device/kernel_runtime.h"
  26. namespace mindspore {
  27. namespace device {
  28. using KernelRuntimeCreator = std::function<std::shared_ptr<KernelRuntime>()>;
  29. class KernelRuntimeManager {
  30. public:
  31. static KernelRuntimeManager &Instance() {
  32. static KernelRuntimeManager instance;
  33. return instance;
  34. }
  35. void Register(const std::string &device_name, KernelRuntimeCreator &&runtime_creator);
  36. KernelRuntime *GetKernelRuntime(const std::string &device_name, uint32_t device_id);
  37. KernelRuntime *GetSingleKernelRuntime(const std::string &device_name, uint32_t device_id);
  38. void ClearRuntimeResource();
  39. private:
  40. KernelRuntimeManager() = default;
  41. ~KernelRuntimeManager() = default;
  42. DISABLE_COPY_AND_ASSIGN(KernelRuntimeManager);
  43. std::map<std::string, std::shared_ptr<KernelRuntime> > runtime_map_;
  44. std::map<std::string, KernelRuntimeCreator> runtime_creators_;
  45. std::mutex lock_;
  46. };
  47. class KernelRuntimeRegistrar {
  48. public:
  49. KernelRuntimeRegistrar(const std::string &device_name, KernelRuntimeCreator &&runtime_creator) {
  50. KernelRuntimeManager::Instance().Register(device_name, std::move(runtime_creator));
  51. }
  52. ~KernelRuntimeRegistrar() = default;
  53. };
  54. #define MS_REG_KERNEL_RUNTIME(DEVICE_NAME, RUNTIME_CLASS) \
  55. static const KernelRuntimeRegistrar g_kernel_runtime_##DEVICE_NAME##_reg( \
  56. DEVICE_NAME, []() { return std::make_shared<RUNTIME_CLASS>(); });
  57. } // namespace device
  58. } // namespace mindspore
  59. #endif // MINDSPORE_MINDSPORE_CCSRC_DEVICE_KERNEL_RUNTIME_MANAGER_H_