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.

host_mem_manager.cc 5.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Copyright 2019-2020 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 "graph/manager/host_mem_manager.h"
  17. #include <sstream>
  18. #include "graph/ge_context.h"
  19. #include "graph/utils/tensor_utils.h"
  20. #include "runtime/mem.h"
  21. namespace {
  22. const uint32_t kMallocHostMemFlag = 0;
  23. } // namespace
  24. namespace ge {
  25. Status SharedMemAllocator::Allocate(SharedMemInfo &mem_info) {
  26. auto device_id = GetContext().DeviceId();
  27. GELOGD("SharedMemAllocator::Malloc host mem size= %zu for devid:[%u].", mem_info.mem_size, device_id);
  28. auto dev_id = static_cast<int32_t>(device_id);
  29. GE_CHK_RT_RET(rtSetDevice(dev_id));
  30. // DeviceReset before memory finished!
  31. GE_MAKE_GUARD(not_used_var, [&] { GE_CHK_RT(rtDeviceReset(dev_id)); });
  32. rtMallocHostSharedMemoryIn input_para = {mem_info.shm_name.c_str(), mem_info.mem_size, kMallocHostMemFlag};
  33. rtMallocHostSharedMemoryOut output_para;
  34. rtError_t rt_ret = rtMallocHostSharedMemory(&input_para, &output_para);
  35. if (rt_ret != RT_ERROR_NONE) {
  36. REPORT_CALL_ERROR("E19999", "Call rtMallocHostSharedMemory fail, ret:0x%X", rt_ret);
  37. GELOGE(RT_FAILED, "[Call][RtMallocHostSharedMemory] failed, devid:[%u].", device_id);
  38. return GE_GRAPH_MEMORY_ALLOC_FAILED;
  39. }
  40. mem_info.fd = output_para.fd;
  41. mem_info.host_aligned_ptr = AlignedPtr::BuildFromAllocFunc(
  42. [&output_para](std::unique_ptr<uint8_t[], AlignedPtr::Deleter> &ptr) {
  43. ptr.reset(reinterpret_cast<uint8_t *>(output_para.ptr));
  44. },
  45. [](uint8_t *ptr) { ptr = nullptr; });
  46. mem_info.device_address = reinterpret_cast<uint8_t *>(output_para.devPtr);
  47. return SUCCESS;
  48. }
  49. Status SharedMemAllocator::DeAllocate(SharedMemInfo &mem_info) {
  50. GELOGD("SharedMemAllocator::DeAllocate");
  51. rtFreeHostSharedMemoryIn free_para = {mem_info.shm_name.c_str(), mem_info.mem_size, mem_info.fd,
  52. mem_info.host_aligned_ptr->MutableGet(), mem_info.device_address};
  53. rtError_t rt_ret = rtFreeHostSharedMemory(&free_para);
  54. if (rt_ret != RT_ERROR_NONE) {
  55. REPORT_CALL_ERROR("E19999", "Call rtFreeHostSharedMemory fail, ret:0x%X", rt_ret);
  56. GELOGE(RT_FAILED, "[Call][RtFreeHostSharedMemory] failed, ret:0x%X.", rt_ret);
  57. return RT_FAILED;
  58. }
  59. return ge::SUCCESS;
  60. }
  61. HostMemManager &HostMemManager::Instance() {
  62. static HostMemManager mem_manager;
  63. return mem_manager;
  64. }
  65. Status HostMemManager::Initialize() {
  66. std::lock_guard<std::recursive_mutex> lock(mutex_);
  67. allocator_ = std::unique_ptr<SharedMemAllocator>(new (std::nothrow) SharedMemAllocator());
  68. if (allocator_ == nullptr) {
  69. REPORT_CALL_ERROR("E19999", "New SharedMemAllocator fail");
  70. GELOGE(GE_GRAPH_MALLOC_FAILED, "[New][SharedMemAllocator] failed!");
  71. return GE_GRAPH_MALLOC_FAILED;
  72. }
  73. return SUCCESS;
  74. }
  75. void HostMemManager::Finalize() noexcept {
  76. std::lock_guard<std::recursive_mutex> lock(mutex_);
  77. for (auto &it : var_memory_base_map_) {
  78. if (allocator_->DeAllocate(it.second) != SUCCESS) {
  79. GELOGW("Host %s mem release failed!", it.first.c_str());
  80. }
  81. }
  82. var_memory_base_map_.clear();
  83. }
  84. Status HostMemManager::MallocSharedMemory(SharedMemInfo &mem_info) {
  85. std::lock_guard<std::recursive_mutex> lock(mutex_);
  86. auto iter = var_memory_base_map_.find(mem_info.op_name);
  87. if (iter != var_memory_base_map_.end()) {
  88. REPORT_INNER_ERROR("E19999", "Host shared memory for op %s has been malloced", mem_info.op_name.c_str());
  89. GELOGE(FAILED, "[Check][Param] Host shared memory for op %s has been malloced", mem_info.op_name.c_str());
  90. return FAILED;
  91. }
  92. mem_info.shm_name = OpNameToShmName(mem_info.op_name);
  93. GE_CHECK_NOTNULL(allocator_);
  94. GE_CHK_STATUS_RET(allocator_->Allocate(mem_info));
  95. var_memory_base_map_[mem_info.op_name] = mem_info;
  96. return SUCCESS;
  97. }
  98. Status HostMemManager::QueryVarMemInfo(const string &op_name, uint64_t &base_addr, uint64_t &data_size) {
  99. std::lock_guard<std::recursive_mutex> lock(mutex_);
  100. if (var_memory_base_map_.find(op_name) == var_memory_base_map_.end()) {
  101. REPORT_INNER_ERROR("E19999", "MemInfo.op_name:%s can't find in var_memory_base_map_", op_name.c_str());
  102. GELOGE(INTERNAL_ERROR, "[Check][Param] Find host base base_addr failed, node name:%s!", op_name.c_str());
  103. return INTERNAL_ERROR;
  104. }
  105. base_addr = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(var_memory_base_map_[op_name].device_address));
  106. data_size = var_memory_base_map_[op_name].mem_size;
  107. return SUCCESS;
  108. }
  109. string HostMemManager::OpNameToShmName(const string &op_name) {
  110. string sh_name("Ascend_");
  111. std::hash<std::string> hash_str;
  112. sh_name.append(std::to_string(hash_str(op_name)));
  113. return sh_name;
  114. }
  115. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示