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 4.8 kB

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

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