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_manager.h 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_MANAGER_H_
  17. #define GE_GRAPH_MANAGER_GRAPH_MEM_MANAGER_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/graph_mem_allocator.h"
  27. #include "graph/manager/graph_caching_allocator.h"
  28. #include "graph/manager/host_mem_allocator.h"
  29. #include "graph/manager/rdma_pool_allocator.h"
  30. #include "graph/manager/host_mem_allocator.h"
  31. #include "graph/manager/session_scope_mem_allocator.h"
  32. #include "graph/node.h"
  33. #include "runtime/mem.h"
  34. #include "common/mem_manager.h"
  35. namespace ge {
  36. class MemManager : public MemoryManager {
  37. public:
  38. MemManager();
  39. virtual ~MemManager();
  40. static MemManager &Instance();
  41. uint8_t *MallocMemory(rtMemType_t memory_type, const std::string &purpose, const std::string &memory_key,
  42. size_t memory_size, uint32_t device_id);
  43. Status FreeMemory(rtMemType_t memory_type, const std::string &memory_key, uint32_t device_id);
  44. uint8_t *GetMemoryBase(rtMemType_t memory_type, const std::string &memory_key, uint32_t device_id);
  45. uint8_t *GetMemoryAddr(rtMemType_t memory_type, const std::string &memory_key, uint32_t device_id);
  46. uint8_t *GetPoolMemory(rtMemType_t memory_type, size_t memory_size, uint32_t device_id);
  47. void FreeSessionMemory(uint64_t session_id, uint32_t device_id = 0);
  48. MemoryAllocator &MemInstance(rtMemType_t memory_type);
  49. CachingAllocator &CachingInstance(rtMemType_t memory_type);
  50. RdmaPoolAllocator &RdmaPoolInstance(rtMemType_t memory_type);
  51. HostMemAllocator &HostMemInstance(rtMemType_t memory_type);
  52. SessionScopeMemAllocator &SessionScopeMemInstance(rtMemType_t memory_type);
  53. ///
  54. /// @ingroup ge_graph
  55. /// @brief memory allocator manager init
  56. /// @param [in] options user config params
  57. /// @return Status result of function
  58. ///
  59. Status Initialize(const std::vector<rtMemType_t> &memory_type);
  60. ///
  61. /// @ingroup ge_graph
  62. /// @brief memory allocator finalize
  63. /// @return void
  64. ///
  65. void Finalize() noexcept;
  66. private:
  67. MemManager(const MemManager &) = delete;
  68. MemManager &operator=(const MemManager &) = delete;
  69. ///
  70. /// @ingroup ge_graph
  71. /// @param [in] memory_type memory type
  72. /// @param [in] allocate_map memory allocator map
  73. /// @return Status result of function
  74. ///
  75. template <typename T>
  76. Status InitAllocator(const std::vector<rtMemType_t> &memory_type, std::map<rtMemType_t, T *> &allocate_map) {
  77. T *allocator = nullptr;
  78. for (unsigned int index : memory_type) {
  79. auto it = allocate_map.find(index);
  80. if (it == allocate_map.end()) {
  81. allocator = new (std::nothrow) T(index);
  82. if (allocator != nullptr) {
  83. allocate_map[index] = allocator;
  84. GELOGI("Create Allocator memory type[%u] success.", index);
  85. } else {
  86. REPORT_CALL_ERROR("E19999", "New MemoryAllocator fail, index:%u", index);
  87. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "Alloc Allocator failed.");
  88. }
  89. } else {
  90. allocator = it->second;
  91. }
  92. if (allocator == nullptr) {
  93. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "Create Allocator failed.");
  94. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  95. } else {
  96. if (allocator->Initialize() != SUCCESS) {
  97. return ACL_ERROR_GE_INTERNAL_ERROR;
  98. }
  99. }
  100. }
  101. return SUCCESS;
  102. }
  103. ///
  104. /// @ingroup ge_graph
  105. /// @param [in] memory_type memory type
  106. /// @param [in] allocate_map memory allocator map
  107. /// @return Allocator ptr
  108. ///
  109. template <typename T>
  110. T &GetAllocator(rtMemType_t memory_type, std::map<rtMemType_t, T *> allocate_map) {
  111. std::lock_guard<std::recursive_mutex> lock(allocator_mutex_);
  112. T *allocator = nullptr;
  113. auto it = allocate_map.find(memory_type);
  114. if (it != allocate_map.end()) {
  115. allocator = it->second;
  116. }
  117. // Usually impossible
  118. if (allocator == nullptr) {
  119. GELOGW("Get allocator failed, memory type is %u.", memory_type);
  120. static T default_allocator(RT_MEMORY_RESERVED);
  121. return default_allocator;
  122. }
  123. return *allocator;
  124. }
  125. std::map<rtMemType_t, MemoryAllocator *> memory_allocator_map_;
  126. std::map<rtMemType_t, CachingAllocator *> caching_allocator_map_;
  127. std::map<rtMemType_t, RdmaPoolAllocator *> rdma_allocator_map_;
  128. std::map<rtMemType_t, HostMemAllocator *> host_allocator_map_;
  129. std::map<rtMemType_t, SessionScopeMemAllocator *> session_scope_allocator_map_;
  130. std::recursive_mutex allocator_mutex_;
  131. std::vector<rtMemType_t> memory_type_;
  132. bool init_ = false;
  133. };
  134. } // namespace ge
  135. #endif // GE_GRAPH_MANAGER_GRAPH_MEM_ALLOCATOR_H_

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