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

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

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