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.1 kB

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

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