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.

graph_mem_allocator.cc 9.2 kB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/graph_mem_allocator.h"
  17. #include <string>
  18. #include "graph/manager/graph_caching_allocator.h"
  19. #include "graph/manager/rdma_pool_allocator.h"
  20. #include "graph/manager/host_mem_allocator.h"
  21. namespace ge {
  22. void MemoryAllocator::Initialize(uint32_t device_id) {
  23. GELOGI("MemoryAllocator::Initialize");
  24. // when redo Initialize free memory
  25. for (auto &it : memory_base_map_) {
  26. if (FreeMemory(it.second.memory_addr_, device_id) != ge::SUCCESS) {
  27. GELOGW("Initialize: FreeMemory failed");
  28. }
  29. }
  30. memory_base_map_.clear();
  31. }
  32. void MemoryAllocator::Finalize(uint32_t device_id) {
  33. GELOGI("MemoryAllocator::Finalize");
  34. // free memory
  35. for (auto &it : memory_base_map_) {
  36. if (FreeMemory(it.second.memory_addr_, device_id) != ge::SUCCESS) {
  37. GELOGW("Finalize: FreeMemory failed");
  38. }
  39. }
  40. memory_base_map_.clear();
  41. }
  42. uint8_t *MemoryAllocator::MallocMemory(const string &purpose, size_t memory_size, uint32_t device_id) const {
  43. uint8_t *memory_addr = nullptr;
  44. if (rtMalloc(reinterpret_cast<void **>(&memory_addr), memory_size, memory_type_) != RT_ERROR_NONE) {
  45. REPORT_CALL_ERROR("E19999", "Call rtMalloc fail, purpose:%s, size:%zu, device_id:%u, when MemoryAllocator %s",
  46. purpose.c_str(), memory_size, device_id, __FUNCTION__);
  47. GELOGE(ge::INTERNAL_ERROR,
  48. "MemoryAllocator::MallocMemory device_id = %u,"
  49. " size= %lu",
  50. device_id, memory_size);
  51. return nullptr;
  52. }
  53. GELOGI("MemoryAllocator::MallocMemory device_id = %u, size= %lu", device_id, memory_size);
  54. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, purpose.c_str(), memory_size)
  55. return memory_addr;
  56. }
  57. Status MemoryAllocator::FreeMemory(uint8_t *memory_addr, uint32_t device_id) const {
  58. GELOGI("MemoryAllocator::FreeMemory device_id = %u", device_id);
  59. auto rtRet = rtFree(memory_addr);
  60. if (rtRet != RT_ERROR_NONE) {
  61. REPORT_CALL_ERROR("E19999", "Call rtFree fail, device_id:%u, when MemoryAllocator %s", device_id, __FUNCTION__);
  62. GELOGE(rtRet, "MemoryAllocator::MallocMemory device_id = %u", device_id);
  63. return RT_ERROR_TO_GE_STATUS(rtRet);
  64. }
  65. memory_addr = nullptr;
  66. return ge::SUCCESS;
  67. }
  68. uint8_t *MemoryAllocator::MallocMemory(const string &purpose, const string &memory_key, size_t memory_size,
  69. uint32_t device_id) {
  70. auto it = memory_base_map_.find(memory_key);
  71. if (it != memory_base_map_.end()) {
  72. it->second.memory_used_num_++;
  73. return it->second.memory_addr_;
  74. }
  75. uint8_t *memory_addr = MallocMemory(purpose, memory_size, device_id);
  76. if (memory_addr == nullptr) {
  77. REPORT_CALL_ERROR("E19999", "Malloc Memory fail, purpose:%s, memory_key:%s, memory_size:%zu, device_id:%u, "
  78. "when MemoryAllocator %s", purpose.c_str(), memory_key.c_str(),
  79. memory_size, device_id, __FUNCTION__);
  80. GELOGE(ge::INTERNAL_ERROR,
  81. "MemoryAllocator::MallocMemory failed,"
  82. " memory_key[%s], size = %lu.",
  83. memory_key.c_str(), memory_size);
  84. return nullptr;
  85. }
  86. MemoryInfo memory_info(memory_addr, memory_size);
  87. memory_info.memory_used_num_++;
  88. memory_base_map_[memory_key] = memory_info;
  89. mem_malloced_ = true;
  90. return memory_addr;
  91. }
  92. Status MemoryAllocator::FreeMemory(const string &memory_key, uint32_t device_id) {
  93. auto it = memory_base_map_.find(memory_key);
  94. if (it == memory_base_map_.end()) {
  95. if (mem_malloced_) {
  96. GELOGW(
  97. "MemoryAllocator::FreeMemory failed,"
  98. " memory_key[%s] was not exist, device_id = %u.",
  99. memory_key.c_str(), device_id);
  100. }
  101. return ge::INTERNAL_ERROR;
  102. }
  103. if (it->second.memory_used_num_ > 1) {
  104. GELOGW("MemoryAllocator::FreeMemory memory_key[%s] should not be released, reference count %d", memory_key.c_str(),
  105. it->second.memory_used_num_);
  106. // reference count greater than 1 represnt that static memory is used by
  107. // someone else, reference count decrement
  108. it->second.memory_used_num_--;
  109. return ge::SUCCESS;
  110. }
  111. if (FreeMemory(it->second.memory_addr_, device_id) != ge::SUCCESS) {
  112. REPORT_CALL_ERROR("E19999", "Free Memory fail, memory_key:%s, device_id:%u, when MemoryAllocator %s",
  113. memory_key.c_str(), device_id, __FUNCTION__);
  114. GELOGE(ge::INTERNAL_ERROR,
  115. "MemoryAllocator::FreeMemory rtFree failed,"
  116. " memory_key[%s]",
  117. memory_key.c_str());
  118. return ge::INTERNAL_ERROR;
  119. }
  120. GELOGI("MemoryAllocator::FreeMemory device_id = %u", device_id);
  121. memory_base_map_.erase(it);
  122. return ge::SUCCESS;
  123. }
  124. uint8_t *MemoryAllocator::GetMemoryAddr(const string &memory_key, uint32_t device_id) {
  125. auto it = memory_base_map_.find(memory_key);
  126. if (it == memory_base_map_.end()) {
  127. GELOGW(
  128. "MemoryAllocator::GetMemoryAddr failed,"
  129. " memory_key[%s] was not exist, device_id = %u.",
  130. memory_key.c_str(), device_id);
  131. return nullptr;
  132. }
  133. return it->second.memory_addr_;
  134. }
  135. MemManager::MemManager() {}
  136. MemManager::~MemManager() { Finalize(); }
  137. MemManager &MemManager::Instance() {
  138. static MemManager mem_manager;
  139. return mem_manager;
  140. }
  141. MemoryAllocator *MemManager::Instance(rtMemType_t memory_type) { return Instance().GetMemoryAllocator(memory_type); }
  142. Status MemManager::Initialize(const std::vector<rtMemType_t> &memory_type) {
  143. std::lock_guard<std::recursive_mutex> lock(allocator_mutex_);
  144. MemoryAllocator *memory_allocator = nullptr;
  145. for (unsigned int index : memory_type) {
  146. auto it = memory_allocator_map_.find(index);
  147. if (it == memory_allocator_map_.end()) {
  148. memory_allocator = new (std::nothrow) MemoryAllocator(index);
  149. if (memory_allocator != nullptr) {
  150. memory_allocator_map_[index] = memory_allocator;
  151. GELOGI("Create MemoryAllocator memory type[%u] success.", index);
  152. } else {
  153. REPORT_CALL_ERROR("E19999", "New MemoryAllocator fail, index:%u, when MemoryAllocator %s", index, __FUNCTION__);
  154. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "Alloc MemoryAllocator failed.");
  155. }
  156. } else {
  157. memory_allocator = it->second;
  158. }
  159. if (memory_allocator == nullptr) {
  160. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "Create MemoryAllocator failed.");
  161. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  162. } else {
  163. memory_allocator->Initialize(0);
  164. }
  165. }
  166. auto ret = InitAllocator(memory_type, caching_allocator_map_);
  167. if (ret != SUCCESS) {
  168. GELOGE(ret, "Create CachingAllocator failed.");
  169. return ret;
  170. }
  171. ret = InitAllocator(memory_type, rdma_allocator_map_);
  172. if (ret != SUCCESS) {
  173. GELOGE(ret, "Create RdmaAllocator failed.");
  174. return ret;
  175. }
  176. ret = InitAllocator(memory_type, host_allocator_map_);
  177. if (ret != SUCCESS) {
  178. GELOGE(ret, "Create HostMemAllocator failed.");
  179. return ret;
  180. }
  181. return SUCCESS;
  182. }
  183. template <typename T>
  184. void FinalizeAllocatorMap(std::map<rtMemType_t, T *> &allocate_map) {
  185. for (auto &allocator : allocate_map) {
  186. if (allocator.second != nullptr) {
  187. allocator.second->Finalize();
  188. delete allocator.second;
  189. allocator.second = nullptr;
  190. }
  191. }
  192. allocate_map.clear();
  193. }
  194. void MemManager::Finalize() noexcept {
  195. GELOGI("Finalize.");
  196. std::lock_guard<std::recursive_mutex> lock(allocator_mutex_);
  197. // caching and rdma allocator use memory allocator, so finalize them first
  198. FinalizeAllocatorMap(caching_allocator_map_);
  199. FinalizeAllocatorMap(rdma_allocator_map_);
  200. FinalizeAllocatorMap(host_allocator_map_);
  201. FinalizeAllocatorMap(memory_allocator_map_);
  202. }
  203. MemoryAllocator *MemManager::GetMemoryAllocator(rtMemType_t memory_type) {
  204. std::lock_guard<std::recursive_mutex> lock(allocator_mutex_);
  205. MemoryAllocator *memory_allocator = nullptr;
  206. auto it = memory_allocator_map_.find(memory_type);
  207. if (it != memory_allocator_map_.end()) {
  208. memory_allocator = it->second;
  209. }
  210. // Usually impossible
  211. if (memory_allocator == nullptr) {
  212. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR, "GetMemoryAllocator failed, memory type is %u.", memory_type);
  213. static MemoryAllocator default_memory_allocator(RT_MEMORY_RESERVED);
  214. return &default_memory_allocator;
  215. }
  216. return memory_allocator;
  217. }
  218. CachingAllocator &MemManager::CachingInstance(rtMemType_t memory_type) {
  219. return Instance().GetAllocator(memory_type, caching_allocator_map_);
  220. }
  221. RdmaPoolAllocator &MemManager::RdmaPoolInstance(rtMemType_t memory_type) {
  222. return Instance().GetAllocator(memory_type, rdma_allocator_map_);
  223. }
  224. HostMemAllocator &MemManager::HostMemInstance(rtMemType_t memory_type) {
  225. return Instance().GetAllocator(memory_type, host_allocator_map_);
  226. }
  227. } // namespace ge

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