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

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. auto find_pos = cur_runtime_key.rfind('_');
  53. if (find_pos != std::string::npos) {
  54. auto cur_device_id = cur_runtime_key.substr(find_pos + 1);
  55. MS_LOG(EXCEPTION) << "Can't change device id in runtime, already set device id: " << cur_device_id
  56. << ", set device id: " << device_id << " failed";
  57. }
  58. }
  59. return GetKernelRuntime(device_name, device_id);
  60. }
  61. KernelRuntime *KernelRuntimeManager::GetKernelRuntime(const std::string &device_name, uint32_t device_id) {
  62. std::lock_guard<std::mutex> guard(lock_);
  63. std::string runtime_key = device_name + "_" + std::to_string(device_id);
  64. auto runtime_iter = runtime_map_.find(runtime_key);
  65. if (runtime_iter != runtime_map_.end()) {
  66. return runtime_iter->second.get();
  67. }
  68. std::shared_ptr<KernelRuntime> kernel_runtime;
  69. auto creator_iter = runtime_creators_.find(device_name);
  70. if (creator_iter != runtime_creators_.end()) {
  71. MS_EXCEPTION_IF_NULL(creator_iter->second);
  72. kernel_runtime = (creator_iter->second)();
  73. kernel_runtime->set_device_id(device_id);
  74. MS_EXCEPTION_IF_NULL(kernel_runtime);
  75. runtime_map_[runtime_key] = kernel_runtime;
  76. } else {
  77. MS_LOG(EXCEPTION) << "No kernel runtime creator for " << device_name << " with device id " << device_id;
  78. }
  79. return kernel_runtime.get();
  80. }
  81. } // namespace device
  82. } // namespace mindspore