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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. GELOGE(RT_FAILED, "Call rt api(rtMallocHostSharedMemory) failed, devid:[%u].", device_id);
  37. return GE_GRAPH_MEMORY_ALLOC_FAILED;
  38. }
  39. mem_info.fd = output_para.fd;
  40. mem_info.host_address = reinterpret_cast<uint8_t *>(output_para.ptr);
  41. mem_info.device_address = reinterpret_cast<uint8_t *>(output_para.devPtr);
  42. return SUCCESS;
  43. }
  44. Status SharedMemAllocator::DeAllocate(SharedMemInfo &mem_info) {
  45. GELOGD("SharedMemAllocator::DeAllocate");
  46. rtFreeHostSharedMemoryIn free_para = {mem_info.shm_name.c_str(), mem_info.mem_size, mem_info.fd,
  47. mem_info.host_address, mem_info.device_address};
  48. rtError_t rt_ret = rtFreeHostSharedMemory(&free_para);
  49. if (rt_ret != RT_ERROR_NONE) {
  50. GELOGE(RT_FAILED, "Call rt api(rtFreeHostSharedMemory) failed, ret: 0x%X.", rt_ret);
  51. return RT_FAILED;
  52. }
  53. return ge::SUCCESS;
  54. }
  55. HostMemManager &HostMemManager::Instance() {
  56. static HostMemManager mem_manager;
  57. return mem_manager;
  58. }
  59. Status HostMemManager::Initialize() {
  60. std::lock_guard<std::recursive_mutex> lock(mutex_);
  61. allocator_ = std::unique_ptr<SharedMemAllocator>(new (std::nothrow) SharedMemAllocator());
  62. if (allocator_ == nullptr) {
  63. GELOGE(GE_GRAPH_MALLOC_FAILED, "Shared memory allocator init failed!");
  64. return GE_GRAPH_MALLOC_FAILED;
  65. }
  66. return SUCCESS;
  67. }
  68. void HostMemManager::Finalize() noexcept {
  69. std::lock_guard<std::recursive_mutex> lock(mutex_);
  70. for (auto &it : var_memory_base_map_) {
  71. if (allocator_->DeAllocate(it.second) != SUCCESS) {
  72. GELOGW("Host %s mem release failed!", it.first.c_str());
  73. }
  74. }
  75. var_memory_base_map_.clear();
  76. }
  77. Status HostMemManager::MallocSharedMemory(SharedMemInfo &mem_info) {
  78. std::lock_guard<std::recursive_mutex> lock(mutex_);
  79. auto iter = var_memory_base_map_.find(mem_info.op_name);
  80. if (iter != var_memory_base_map_.end()) {
  81. GELOGE(FAILED, "Host shared memory for op %s has been malloced", mem_info.op_name.c_str());
  82. return FAILED;
  83. }
  84. mem_info.shm_name = OpNameToShmName(mem_info.op_name);
  85. GE_CHECK_NOTNULL(allocator_);
  86. GE_CHK_STATUS_RET(allocator_->Allocate(mem_info));
  87. var_memory_base_map_[mem_info.op_name] = mem_info;
  88. return SUCCESS;
  89. }
  90. Status HostMemManager::QueryVarMemInfo(const string &op_name, uint64_t &base_addr, uint64_t &data_size) {
  91. std::lock_guard<std::recursive_mutex> lock(mutex_);
  92. if (var_memory_base_map_.find(op_name) == var_memory_base_map_.end()) {
  93. GELOGE(INTERNAL_ERROR, "Find host base base_addr failed,node name:%s!", op_name.c_str());
  94. return INTERNAL_ERROR;
  95. }
  96. base_addr = reinterpret_cast<uint64_t>(reinterpret_cast<uintptr_t>(var_memory_base_map_[op_name].device_address));
  97. data_size = var_memory_base_map_[op_name].mem_size;
  98. return SUCCESS;
  99. }
  100. string HostMemManager::OpNameToShmName(const string &op_name) {
  101. string sh_name("Ascend_");
  102. std::hash<std::string> hash_str;
  103. sh_name.append(std::to_string(hash_str(op_name)));
  104. return sh_name;
  105. }
  106. } // namespace ge

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