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

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