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.0 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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, uint32_t device_id = 0);
  97. ///
  98. /// @ingroup ge_graph
  99. /// @brief free memory
  100. /// @param [in] memory_key memory key
  101. /// @param [in] device_id device id
  102. /// @return Status result of function
  103. ///
  104. Status FreeMemory(const string &memory_key, uint32_t device_id = 0);
  105. ///
  106. /// @ingroup ge_graph
  107. /// @brief get memory address
  108. /// @param [in] memory_key memory key
  109. /// @param [in] device_id device id
  110. /// @return memory address (must not free memory by it)
  111. ///
  112. uint8_t *GetMemoryAddr(const string &memory_key, uint32_t device_id = 0);
  113. private:
  114. rtMemType_t memory_type_;
  115. bool mem_malloced_;
  116. map<string, MemoryInfo> memory_base_map_;
  117. };
  118. using MemoryAllocatorPtr = std::shared_ptr<MemoryAllocator>;
  119. class CachingAllocator;
  120. class RdmaPoolAllocator;
  121. class MemManager {
  122. public:
  123. MemManager();
  124. virtual ~MemManager();
  125. static MemManager &Instance();
  126. static MemoryAllocator *Instance(rtMemType_t memory_type);
  127. CachingAllocator &CachingInstance(rtMemType_t memory_type);
  128. RdmaPoolAllocator &RdmaPoolInstance(rtMemType_t memory_type);
  129. MemManager(const MemManager &) = delete;
  130. MemManager &operator=(const MemManager &) = delete;
  131. ///
  132. /// @ingroup ge_graph
  133. /// @brief memory allocator manager init
  134. /// @param [in] options user config params
  135. /// @return Status result of function
  136. ///
  137. Status Initialize(const std::vector<rtMemType_t> &memory_type);
  138. ///
  139. /// @ingroup ge_graph
  140. /// @brief memory allocator finalize
  141. /// @return void
  142. ///
  143. void Finalize() noexcept;
  144. private:
  145. ///
  146. /// @ingroup ge_graph
  147. /// @brief ge memory allocator
  148. /// @param [in] memory_type memory type
  149. /// @return MemoryAllocator ptr
  150. ///
  151. MemoryAllocator *GetMemoryAllocator(rtMemType_t memory_type);
  152. ///
  153. /// @ingroup ge_graph
  154. /// @param [in] memory_type memory type
  155. /// @param [in] allocate_map memory allocator map
  156. /// @return Status result of function
  157. ///
  158. template <typename T>
  159. Status InitAllocator(const std::vector<rtMemType_t> &memory_type, std::map<rtMemType_t, T *> &allocate_map) {
  160. T *allocator = nullptr;
  161. for (unsigned int index : memory_type) {
  162. auto it = allocate_map.find(index);
  163. if (it == allocate_map.end()) {
  164. allocator = new (std::nothrow) T(index);
  165. if (allocator != nullptr) {
  166. allocate_map[index] = allocator;
  167. GELOGI("Create Allocator memory type[%u] success.", index);
  168. } else {
  169. GELOGE(INTERNAL_ERROR, "Alloc Allocator failed.");
  170. }
  171. } else {
  172. allocator = it->second;
  173. }
  174. if (allocator == nullptr) {
  175. GELOGE(INTERNAL_ERROR, "Create Allocator failed.");
  176. return INTERNAL_ERROR;
  177. } else {
  178. if (allocator->Initialize() != SUCCESS) {
  179. return INTERNAL_ERROR;
  180. }
  181. }
  182. }
  183. return SUCCESS;
  184. }
  185. ///
  186. /// @ingroup ge_graph
  187. /// @param [in] memory_type memory type
  188. /// @param [in] allocate_map memory allocator map
  189. /// @return Allocator ptr
  190. ///
  191. template <typename T>
  192. T &GetAllocator(rtMemType_t memory_type, std::map<rtMemType_t, T *> allocate_map) {
  193. std::lock_guard<std::recursive_mutex> lock(allocator_mutex_);
  194. T *allocator = nullptr;
  195. auto it = allocate_map.find(memory_type);
  196. if (it != allocate_map.end()) {
  197. allocator = it->second;
  198. }
  199. // Usually impossible
  200. if (allocator == nullptr) {
  201. GELOGE(ge::INTERNAL_ERROR, "Get allocator failed, memory type is %u.", memory_type);
  202. static T default_allocator(RT_MEMORY_RESERVED);
  203. return default_allocator;
  204. }
  205. return *allocator;
  206. }
  207. std::map<rtMemType_t, MemoryAllocator *> memory_allocator_map_;
  208. std::map<rtMemType_t, CachingAllocator *> caching_allocator_map_;
  209. std::map<rtMemType_t, RdmaPoolAllocator *> rdma_allocator_map_;
  210. std::recursive_mutex allocator_mutex_;
  211. };
  212. } // namespace ge
  213. #endif // GE_GRAPH_MANAGER_GRAPH_MEM_ALLOCATOR_H_

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