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

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