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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "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) {
  30. std::lock_guard<std::mutex> guard(lock_);
  31. for (auto &iter : runtime_map_) {
  32. MS_LOG(INFO) << "Clear device " << iter.first << " graph " << graph_id << " runtime resource";
  33. if (!iter.second) {
  34. MS_LOG(ERROR) << "Kernel runtime is nullptr";
  35. continue;
  36. }
  37. iter.second->ClearGraphRuntimeResource(graph_id);
  38. }
  39. }
  40. void KernelRuntimeManager::Register(const std::string &device_name, KernelRuntimeCreator &&runtime_creator) {
  41. if (runtime_creators_.find(device_name) == runtime_creators_.end()) {
  42. (void)runtime_creators_.emplace(device_name, runtime_creator);
  43. }
  44. }
  45. KernelRuntime *KernelRuntimeManager::GetSingleKernelRuntime(const std::string &device_name, uint32_t device_id) {
  46. std::string runtime_key = device_name + "_" + std::to_string(device_id);
  47. auto runtime_iter = runtime_map_.find(runtime_key);
  48. if (runtime_iter != runtime_map_.end()) {
  49. return runtime_iter->second.get();
  50. } else if (runtime_map_.size() > 0) {
  51. auto cur_runtime_key = runtime_map_.begin()->first;
  52. if (!cur_runtime_key.empty()) {
  53. auto cur_device_id = cur_runtime_key.substr(cur_runtime_key.rfind('_') + 1);
  54. MS_LOG(EXCEPTION) << "Can't change device id in runtime, already set device id: " << cur_device_id
  55. << ", set device id: " << device_id << " failed";
  56. }
  57. }
  58. return GetKernelRuntime(device_name, device_id);
  59. }
  60. KernelRuntime *KernelRuntimeManager::GetKernelRuntime(const std::string &device_name, uint32_t device_id) {
  61. std::lock_guard<std::mutex> guard(lock_);
  62. std::string runtime_key = device_name + "_" + std::to_string(device_id);
  63. auto runtime_iter = runtime_map_.find(runtime_key);
  64. if (runtime_iter != runtime_map_.end()) {
  65. return runtime_iter->second.get();
  66. }
  67. std::shared_ptr<KernelRuntime> kernel_runtime;
  68. auto creator_iter = runtime_creators_.find(device_name);
  69. if (creator_iter != runtime_creators_.end()) {
  70. MS_EXCEPTION_IF_NULL(creator_iter->second);
  71. kernel_runtime = (creator_iter->second)();
  72. kernel_runtime->set_device_id(device_id);
  73. MS_EXCEPTION_IF_NULL(kernel_runtime);
  74. runtime_map_[runtime_key] = kernel_runtime;
  75. } else {
  76. MS_LOG(EXCEPTION) << "No kernel runtime creator for " << device_name << " with device id " << device_id;
  77. }
  78. return kernel_runtime.get();
  79. }
  80. } // namespace device
  81. } // namespace mindspore