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

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