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.h 7.3 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
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #ifndef GE_GRAPH_MANAGER_GRAPH_MEM_ALLOCATOR_H_
  17. #define GE_GRAPH_MANAGER_GRAPH_MEM_ALLOCATOR_H_
  18. #include <iostream>
  19. #include <map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <string>
  23. #include <vector>
  24. #include "framework/common/debug/ge_log.h"
  25. #include "framework/common/ge_inner_error_codes.h"
  26. #include "graph/manager/host_mem_allocator.h"
  27. #include "graph/node.h"
  28. #include "runtime/mem.h"
  29. namespace ge {
  30. class MemoryInfo {
  31. public:
  32. MemoryInfo() : memory_addr_(nullptr), memory_size_(0), memory_used_num_(0) {}
  33. MemoryInfo(uint8_t *memory_addr, size_t memory_size)
  34. : memory_addr_(memory_addr), memory_size_(memory_size), memory_used_num_(0) {}
  35. MemoryInfo &operator=(const MemoryInfo &op) {
  36. if (&op == this) {
  37. return *this;
  38. }
  39. this->memory_addr_ = op.memory_addr_;
  40. this->memory_size_ = op.memory_size_;
  41. this->memory_used_num_ = op.memory_used_num_;
  42. return *this;
  43. }
  44. MemoryInfo(const MemoryInfo &op) {
  45. this->memory_addr_ = op.memory_addr_;
  46. this->memory_size_ = op.memory_size_;
  47. this->memory_used_num_ = op.memory_used_num_;
  48. }
  49. virtual ~MemoryInfo() = default;
  50. uint8_t *memory_addr_;
  51. uint64_t memory_size_;
  52. int32_t memory_used_num_;
  53. };
  54. class MemoryAllocator {
  55. public:
  56. explicit MemoryAllocator(rtMemType_t memory_type) : memory_type_(memory_type), mem_malloced_(false) {}
  57. virtual ~MemoryAllocator() = default;
  58. ///
  59. /// @ingroup ge_graph
  60. /// @brief memory allocator init
  61. /// @param [in] options user config params
  62. /// @return void
  63. ///
  64. void Initialize(uint32_t device_id = 0);
  65. ///
  66. /// @ingroup ge_graph
  67. /// @brief memory allocator finalize
  68. /// @return void
  69. ///
  70. void Finalize(uint32_t device_id = 0);
  71. ///
  72. /// @ingroup ge_graph
  73. /// @brief malloc memory
  74. /// @param [in] purpose memory usage
  75. /// @param [in] size memory size
  76. /// @param [in] device_id device id
  77. /// @return memory address
  78. ///
  79. uint8_t *MallocMemory(const string &purpose, size_t memory_size, uint32_t device_id = 0) const;
  80. ///
  81. /// @ingroup ge_graph
  82. /// @brief free memory
  83. /// @param [in] device_id device id
  84. /// @param [out] memory_ptr memory address ptr
  85. /// @return Status result of function
  86. ///
  87. Status FreeMemory(uint8_t *memory_addr, uint32_t device_id = 0) const;
  88. ///
  89. /// @ingroup ge_graph
  90. /// @brief malloc memory
  91. /// @param [in] purpose memory usage
  92. /// @param [in] memory_key memory key
  93. /// @param [in] size memory size
  94. /// @param [in] device_id device id
  95. /// @return memory address
  96. ///
  97. uint8_t *MallocMemory(const string &purpose, const string &memory_key, size_t memory_size,
  98. uint32_t device_id = 0);
  99. ///
  100. /// @ingroup ge_graph
  101. /// @brief free memory
  102. /// @param [in] memory_key memory key
  103. /// @param [in] device_id device id
  104. /// @return Status result of function
  105. ///
  106. Status FreeMemory(const string &memory_key, uint32_t device_id = 0);
  107. ///
  108. /// @ingroup ge_graph
  109. /// @brief get memory address
  110. /// @param [in] memory_key memory key
  111. /// @param [in] device_id device id
  112. /// @return memory address (must not free memory by it)
  113. ///
  114. uint8_t *GetMemoryAddr(const string &memory_key, uint32_t device_id = 0);
  115. private:
  116. rtMemType_t memory_type_;
  117. bool mem_malloced_;
  118. map<string, MemoryInfo> memory_base_map_;
  119. };
  120. using MemoryAllocatorPtr = std::shared_ptr<MemoryAllocator>;
  121. class CachingAllocator;
  122. class RdmaPoolAllocator;
  123. class MemManager {
  124. public:
  125. MemManager();
  126. virtual ~MemManager();
  127. static MemManager &Instance();
  128. static MemoryAllocator *Instance(rtMemType_t memory_type);
  129. CachingAllocator &CachingInstance(rtMemType_t memory_type);
  130. RdmaPoolAllocator &RdmaPoolInstance(rtMemType_t memory_type);
  131. HostMemAllocator &HostMemInstance(rtMemType_t memory_type);
  132. MemManager(const MemManager &) = delete;
  133. MemManager &operator=(const MemManager &) = delete;
  134. ///
  135. /// @ingroup ge_graph
  136. /// @brief memory allocator manager init
  137. /// @param [in] options user config params
  138. /// @return Status result of function
  139. ///
  140. Status Initialize(const std::vector<rtMemType_t> &memory_type);
  141. ///
  142. /// @ingroup ge_graph
  143. /// @brief memory allocator finalize
  144. /// @return void
  145. ///
  146. void Finalize() noexcept;
  147. private:
  148. ///
  149. /// @ingroup ge_graph
  150. /// @brief ge memory allocator
  151. /// @param [in] memory_type memory type
  152. /// @return MemoryAllocator ptr
  153. ///
  154. MemoryAllocator *GetMemoryAllocator(rtMemType_t memory_type);
  155. ///
  156. /// @ingroup ge_graph
  157. /// @param [in] memory_type memory type
  158. /// @param [in] allocate_map memory allocator map
  159. /// @return Status result of function
  160. ///
  161. template <typename T>
  162. Status InitAllocator(const std::vector<rtMemType_t> &memory_type, std::map<rtMemType_t, T *> &allocate_map) {
  163. T *allocator = nullptr;
  164. for (unsigned int index : memory_type) {
  165. auto it = allocate_map.find(index);
  166. if (it == allocate_map.end()) {
  167. allocator = new (std::nothrow) T(index);
  168. if (allocator != nullptr) {
  169. allocate_map[index] = allocator;
  170. GELOGI("Create Allocator memory type[%u] success.", index);
  171. } else {
  172. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "Alloc Allocator failed.");
  173. }
  174. } else {
  175. allocator = it->second;
  176. }
  177. if (allocator == nullptr) {
  178. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "Create Allocator failed.");
  179. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  180. } else {
  181. if (allocator->Initialize() != SUCCESS) {
  182. return ACL_ERROR_GE_INTERNAL_ERROR;
  183. }
  184. }
  185. }
  186. return SUCCESS;
  187. }
  188. ///
  189. /// @ingroup ge_graph
  190. /// @param [in] memory_type memory type
  191. /// @param [in] allocate_map memory allocator map
  192. /// @return Allocator ptr
  193. ///
  194. template <typename T>
  195. T &GetAllocator(rtMemType_t memory_type, std::map<rtMemType_t, T *> allocate_map) {
  196. std::lock_guard<std::recursive_mutex> lock(allocator_mutex_);
  197. T *allocator = nullptr;
  198. auto it = allocate_map.find(memory_type);
  199. if (it != allocate_map.end()) {
  200. allocator = it->second;
  201. }
  202. // Usually impossible
  203. if (allocator == nullptr) {
  204. GELOGW("Get allocator failed, memory type is %u.", memory_type);
  205. static T default_allocator(RT_MEMORY_RESERVED);
  206. return default_allocator;
  207. }
  208. return *allocator;
  209. }
  210. std::map<rtMemType_t, MemoryAllocator *> memory_allocator_map_;
  211. std::map<rtMemType_t, CachingAllocator *> caching_allocator_map_;
  212. std::map<rtMemType_t, RdmaPoolAllocator *> rdma_allocator_map_;
  213. std::map<rtMemType_t, HostMemAllocator *> host_allocator_map_;
  214. std::recursive_mutex allocator_mutex_;
  215. };
  216. } // namespace ge
  217. #endif // GE_GRAPH_MANAGER_GRAPH_MEM_ALLOCATOR_H_

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