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.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_MANAGER_H_
  17. #define MINDSPORE_CCSRC_RUNTIME_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 <unordered_set>
  25. #include <vector>
  26. #include "utils/ms_utils.h"
  27. #include "runtime/device/kernel_runtime.h"
  28. namespace mindspore {
  29. namespace device {
  30. using KernelRuntimeCreator = std::function<std::shared_ptr<KernelRuntime>()>;
  31. class KernelRuntimeManager {
  32. public:
  33. static KernelRuntimeManager &Instance() {
  34. static KernelRuntimeManager instance;
  35. return instance;
  36. }
  37. void Register(const std::string &device_name, KernelRuntimeCreator &&runtime_creator);
  38. KernelRuntime *GetKernelRuntime(const std::string &device_name, uint32_t device_id);
  39. KernelRuntime *GetSingleKernelRuntime(const std::string &device_name, uint32_t device_id);
  40. void ClearRuntimeResource();
  41. void ClearGraphResource(uint32_t graph_id, const std::vector<AnfNodePtr> &inputs,
  42. const std::unordered_set<ValueNodePtr> &value_nodes,
  43. const std::vector<CNodePtr> &execution_order);
  44. private:
  45. KernelRuntimeManager() = default;
  46. ~KernelRuntimeManager() = default;
  47. DISABLE_COPY_AND_ASSIGN(KernelRuntimeManager);
  48. std::map<std::string, std::shared_ptr<KernelRuntime> > runtime_map_;
  49. std::map<std::string, KernelRuntimeCreator> runtime_creators_;
  50. std::mutex lock_;
  51. };
  52. class KernelRuntimeRegistrar {
  53. public:
  54. KernelRuntimeRegistrar(const std::string &device_name, KernelRuntimeCreator &&runtime_creator) {
  55. KernelRuntimeManager::Instance().Register(device_name, std::move(runtime_creator));
  56. }
  57. ~KernelRuntimeRegistrar() = default;
  58. };
  59. #define MS_REG_KERNEL_RUNTIME(DEVICE_NAME, RUNTIME_CLASS) \
  60. static const KernelRuntimeRegistrar g_kernel_runtime_##DEVICE_NAME##_reg( \
  61. DEVICE_NAME, []() { return std::make_shared<RUNTIME_CLASS>(); });
  62. } // namespace device
  63. } // namespace mindspore
  64. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_MANAGER_H_