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.cc 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include "graph/manager/graph_mem_manager.h"
  17. #include <string>
  18. namespace ge {
  19. MemManager::MemManager() {}
  20. MemManager::~MemManager() { Finalize(); }
  21. MemManager &MemManager::Instance() {
  22. static MemManager mem_manager;
  23. return mem_manager;
  24. }
  25. Status MemManager::Initialize(const std::vector<rtMemType_t> &memory_type) {
  26. std::lock_guard<std::recursive_mutex> lock(allocator_mutex_);
  27. if (init_) {
  28. GELOGW("MemManager has been inited.");
  29. return SUCCESS;
  30. }
  31. auto ret = InitAllocator(memory_type, memory_allocator_map_);
  32. if (ret != SUCCESS) {
  33. GELOGE(ret, "Create MemoryAllocator failed.");
  34. return ret;
  35. }
  36. ret = InitAllocator(memory_type, caching_allocator_map_);
  37. if (ret != SUCCESS) {
  38. GELOGE(ret, "Create CachingAllocator failed.");
  39. return ret;
  40. }
  41. ret = InitAllocator(memory_type, rdma_allocator_map_);
  42. if (ret != SUCCESS) {
  43. GELOGE(ret, "Create RdmaAllocator failed.");
  44. return ret;
  45. }
  46. ret = InitAllocator(memory_type, host_allocator_map_);
  47. if (ret != SUCCESS) {
  48. GELOGE(ret, "Create HostMemAllocator failed.");
  49. return ret;
  50. }
  51. ret = InitAllocator(memory_type, session_scope_allocator_map_);
  52. if (ret != SUCCESS) {
  53. GELOGE(ret, "Create HostMemAllocator failed.");
  54. return ret;
  55. }
  56. init_ = true;
  57. memory_type_ = memory_type;
  58. return SUCCESS;
  59. }
  60. template <typename T>
  61. void FinalizeAllocatorMap(std::map<rtMemType_t, T *> &allocate_map) {
  62. for (auto &allocator : allocate_map) {
  63. if (allocator.second != nullptr) {
  64. allocator.second->Finalize();
  65. delete allocator.second;
  66. allocator.second = nullptr;
  67. }
  68. }
  69. allocate_map.clear();
  70. }
  71. void MemManager::Finalize() noexcept {
  72. GELOGI("Finalize.");
  73. std::lock_guard<std::recursive_mutex> lock(allocator_mutex_);
  74. // caching and rdma allocator use memory allocator, so finalize them first
  75. FinalizeAllocatorMap(session_scope_allocator_map_);
  76. FinalizeAllocatorMap(caching_allocator_map_);
  77. FinalizeAllocatorMap(rdma_allocator_map_);
  78. FinalizeAllocatorMap(host_allocator_map_);
  79. FinalizeAllocatorMap(memory_allocator_map_);
  80. init_ = false;
  81. memory_type_.clear();
  82. }
  83. MemoryAllocator &MemManager::MemInstance(rtMemType_t memory_type) {
  84. return GetAllocator(memory_type, memory_allocator_map_);
  85. }
  86. CachingAllocator &MemManager::CachingInstance(rtMemType_t memory_type) {
  87. return GetAllocator(memory_type, caching_allocator_map_);
  88. }
  89. RdmaPoolAllocator &MemManager::RdmaPoolInstance(rtMemType_t memory_type) {
  90. return GetAllocator(memory_type, rdma_allocator_map_);
  91. }
  92. HostMemAllocator &MemManager::HostMemInstance(rtMemType_t memory_type) {
  93. return GetAllocator(memory_type, host_allocator_map_);
  94. }
  95. SessionScopeMemAllocator &MemManager::SessionScopeMemInstance(rtMemType_t memory_type) {
  96. return GetAllocator(memory_type, session_scope_allocator_map_);
  97. }
  98. } // namespace ge

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