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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include "runtime/device/kernel_runtime_manager.h"
  17. #include "utils/log_adapter.h"
  18. namespace mindspore {
  19. namespace device {
  20. void KernelRuntimeManager::ClearRuntimeResource() {
  21. std::lock_guard<std::mutex> guard(lock_);
  22. for (auto &iter : runtime_map_) {
  23. MS_LOG(INFO) << "Release device " << iter.first;
  24. MS_EXCEPTION_IF_NULL(iter.second);
  25. iter.second->ReleaseDeviceRes();
  26. }
  27. runtime_map_.clear();
  28. }
  29. void KernelRuntimeManager::ClearGraphResource(uint32_t graph_id, const std::vector<AnfNodePtr> &inputs,
  30. const std::unordered_set<ValueNodePtr> &value_nodes,
  31. const std::vector<CNodePtr> &execution_order) {
  32. std::lock_guard<std::mutex> guard(lock_);
  33. for (auto &iter : runtime_map_) {
  34. MS_LOG(INFO) << "Clear device " << iter.first << " graph " << graph_id << " runtime resource";
  35. if (!iter.second) {
  36. MS_LOG(ERROR) << "Kernel runtime is nullptr";
  37. continue;
  38. }
  39. iter.second->ClearGraphRuntimeResource(graph_id, inputs, value_nodes, execution_order);
  40. }
  41. }
  42. void KernelRuntimeManager::Register(const std::string &device_name, KernelRuntimeCreator &&runtime_creator) {
  43. if (runtime_creators_.find(device_name) == runtime_creators_.end()) {
  44. (void)runtime_creators_.emplace(device_name, runtime_creator);
  45. }
  46. }
  47. std::string KernelRuntimeManager::GetDeviceKey(const std::string &device_name, uint32_t device_id) {
  48. std::string device_key = device_name + "_" + std::to_string(device_id);
  49. return device_key;
  50. }
  51. KernelRuntime *KernelRuntimeManager::GetSingleKernelRuntime(const std::string &device_name, uint32_t device_id) {
  52. auto runtime_key = GetDeviceKey(device_name, device_id);
  53. auto runtime_iter = runtime_map_.find(runtime_key);
  54. if (runtime_iter != runtime_map_.end()) {
  55. return runtime_iter->second.get();
  56. } else if (runtime_map_.size() > 0) {
  57. auto cur_runtime_key = runtime_map_.begin()->first;
  58. auto find_pos = cur_runtime_key.rfind('_');
  59. if (find_pos != std::string::npos) {
  60. if (cur_runtime_key.size() > find_pos + 1) {
  61. auto cur_device_id = cur_runtime_key.substr(find_pos + 1);
  62. MS_LOG(EXCEPTION) << "Can't change device id in runtime, already set device id: " << cur_device_id
  63. << ", set device id: " << device_id << " failed";
  64. } else {
  65. MS_LOG(EXCEPTION) << "Can't change device id in runtime, current runtime_key size error, set device id: "
  66. << device_id << " failed";
  67. }
  68. }
  69. }
  70. return GetKernelRuntime(device_name, device_id);
  71. }
  72. KernelRuntime *KernelRuntimeManager::GetKernelRuntime(const std::string &device_name, uint32_t device_id) {
  73. std::string runtime_key = GetDeviceKey(device_name, device_id);
  74. std::lock_guard<std::mutex> guard(lock_);
  75. auto runtime_iter = runtime_map_.find(runtime_key);
  76. if (runtime_iter != runtime_map_.end()) {
  77. return runtime_iter->second.get();
  78. }
  79. std::shared_ptr<KernelRuntime> kernel_runtime;
  80. auto creator_iter = runtime_creators_.find(device_name);
  81. if (creator_iter != runtime_creators_.end()) {
  82. MS_EXCEPTION_IF_NULL(creator_iter->second);
  83. kernel_runtime = (creator_iter->second)();
  84. kernel_runtime->set_device_id(device_id);
  85. MS_EXCEPTION_IF_NULL(kernel_runtime);
  86. runtime_map_[runtime_key] = kernel_runtime;
  87. } else {
  88. MS_LOG(EXCEPTION) << "No kernel runtime creator for " << device_name << " with device id " << device_id;
  89. }
  90. return kernel_runtime.get();
  91. }
  92. void KernelRuntimeManager::ReleaseKernelRuntime(const std::string &device_name, uint32_t device_id) {
  93. std::string runtime_key = GetDeviceKey(device_name, device_id);
  94. std::lock_guard<std::mutex> guard(lock_);
  95. auto runtime_iter = runtime_map_.find(runtime_key);
  96. if (runtime_iter == runtime_map_.end()) {
  97. return;
  98. }
  99. auto runtime = runtime_iter->second.get();
  100. if (runtime == nullptr) {
  101. return;
  102. }
  103. runtime->ReleaseDeviceRes();
  104. runtime_map_.erase(runtime_iter);
  105. }
  106. } // namespace device
  107. } // namespace mindspore