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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. void ClearGraphResource(uint32_t graph_id);
  40. private:
  41. KernelRuntimeManager() = default;
  42. ~KernelRuntimeManager() = default;
  43. DISABLE_COPY_AND_ASSIGN(KernelRuntimeManager);
  44. std::map<std::string, std::shared_ptr<KernelRuntime> > runtime_map_;
  45. std::map<std::string, KernelRuntimeCreator> runtime_creators_;
  46. std::mutex lock_;
  47. };
  48. class KernelRuntimeRegistrar {
  49. public:
  50. KernelRuntimeRegistrar(const std::string &device_name, KernelRuntimeCreator &&runtime_creator) {
  51. KernelRuntimeManager::Instance().Register(device_name, std::move(runtime_creator));
  52. }
  53. ~KernelRuntimeRegistrar() = default;
  54. };
  55. #define MS_REG_KERNEL_RUNTIME(DEVICE_NAME, RUNTIME_CLASS) \
  56. static const KernelRuntimeRegistrar g_kernel_runtime_##DEVICE_NAME##_reg( \
  57. DEVICE_NAME, []() { return std::make_shared<RUNTIME_CLASS>(); });
  58. } // namespace device
  59. } // namespace mindspore
  60. #endif // MINDSPORE_MINDSPORE_CCSRC_DEVICE_KERNEL_RUNTIME_MANAGER_H_