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

5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 ReleaseKernelRuntime(const std::string &device_name, uint32_t device_id);
  41. void ClearRuntimeResource();
  42. void ClearGraphResource(uint32_t graph_id, const std::vector<AnfNodePtr> &inputs,
  43. const std::unordered_set<ValueNodePtr> &value_nodes,
  44. const std::vector<CNodePtr> &execution_order);
  45. private:
  46. KernelRuntimeManager() = default;
  47. ~KernelRuntimeManager() = default;
  48. DISABLE_COPY_AND_ASSIGN(KernelRuntimeManager);
  49. std::string GetDeviceKey(const std::string &device_name, uint32_t device_id);
  50. std::map<std::string, std::shared_ptr<KernelRuntime> > runtime_map_;
  51. std::map<std::string, KernelRuntimeCreator> runtime_creators_;
  52. std::mutex lock_;
  53. };
  54. class KernelRuntimeRegistrar {
  55. public:
  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. #define MS_REG_KERNEL_RUNTIME(DEVICE_NAME, RUNTIME_CLASS) \
  62. static const KernelRuntimeRegistrar g_kernel_runtime_##DEVICE_NAME##_reg( \
  63. DEVICE_NAME, []() { return std::make_shared<RUNTIME_CLASS>(); });
  64. } // namespace device
  65. } // namespace mindspore
  66. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_MANAGER_H_