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.

memory_manager.cc 7.2 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * Copyright 2019 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 "device/memory_manager.h"
  17. #include "session/anf_runtime_algorithm.h"
  18. #include "utils/context/ms_context.h"
  19. using mindspore::memreuse::BestFitMemReuse;
  20. using mindspore::memreuse::MemReuseUtilPtr;
  21. namespace mindspore {
  22. namespace device {
  23. size_t MemoryManager::GetCommonAlignSize(size_t input_size) const {
  24. return (input_size + kMemAlignSize + 31) / kMemAlignSize * kMemAlignSize;
  25. }
  26. size_t MemoryManager::GetCommunicationAlignSize(size_t input_size) const {
  27. return (input_size + kMemAlignSize - 1) / kMemAlignSize * kMemAlignSize + 2 * kMemAlignSize;
  28. }
  29. void MemoryManager::MallocReusedDynamicMem(session::KernelGraph *graph) {
  30. MS_EXCEPTION_IF_NULL(graph);
  31. MemReuseUtilPtr mem_reuse_util_ptr = std::make_shared<memreuse::MemReuseUtil>();
  32. MS_EXCEPTION_IF_NULL(mem_reuse_util_ptr);
  33. // set all infos
  34. mem_reuse_util_ptr->SetAllInfo(graph);
  35. auto bestfit_mem_reuse = std::make_shared<BestFitMemReuse>();
  36. MS_EXCEPTION_IF_NULL(bestfit_mem_reuse);
  37. bestfit_mem_reuse->Reuse(mem_reuse_util_ptr.get());
  38. size_t total_allocated_size = bestfit_mem_reuse->GetAllocatedSize();
  39. MS_LOG(INFO) << "TotalReuseDynamicSize [" << total_allocated_size << "]";
  40. mem_reuse_util_ptr_ = mem_reuse_util_ptr;
  41. auto base_ptr = MallocDynamicMem(total_allocated_size, false);
  42. mem_reuse_util_ptr_->set_mem_base(base_ptr);
  43. }
  44. uint8_t *MemoryManager::MallocOutputMem(const AnfNodePtr &node, size_t index, int flag, size_t size) {
  45. MS_EXCEPTION_IF_NULL(node);
  46. auto context_ptr = MsContext::GetInstance();
  47. MS_EXCEPTION_IF_NULL(context_ptr);
  48. uint8_t *ptr = nullptr;
  49. if (AnfAlgo::IsCommunicationOp(node)) {
  50. bool communication_mem = false;
  51. if (context_ptr->enable_hccl()) {
  52. communication_mem = true;
  53. }
  54. if (flag == kStaticMem) {
  55. ptr = MallocStaticMem(size, communication_mem);
  56. } else {
  57. ptr = MallocDynamicMem(size, communication_mem);
  58. }
  59. return ptr;
  60. }
  61. if (flag == kStaticMem) {
  62. ptr = MallocStaticMem(size, false);
  63. } else if (flag == kDynamicMem) {
  64. ptr = MallocDynamicMem(size, false);
  65. } else if (flag == kReuseDynamicMem) {
  66. MS_EXCEPTION_IF_NULL(mem_reuse_util_ptr_);
  67. ptr = mem_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  68. }
  69. return ptr;
  70. }
  71. uint8_t *MemoryManager::MallocWorkSpaceMem(const AnfNodePtr &node, size_t index, int flag, size_t size) {
  72. if (flag == kReuseDynamicMem) {
  73. MS_EXCEPTION_IF_NULL(mem_reuse_util_ptr_);
  74. return mem_reuse_util_ptr_->GetNodeWorkSpacePtr(node, index);
  75. }
  76. return MallocDynamicMem(size, false);
  77. }
  78. uint8_t *MemoryManager::MallocMem(int flag, size_t size) {
  79. uint8_t *ptr = nullptr;
  80. if (flag == kStaticMem) {
  81. ptr = MallocStaticMem(size, false);
  82. } else if (flag == kDynamicMem) {
  83. ptr = MallocDynamicMem(size, false);
  84. }
  85. return ptr;
  86. }
  87. uint8_t *MemoryManager::MallocStaticMem(size_t size, bool communication_mem) {
  88. size_t align_size = 0;
  89. if (communication_mem) {
  90. align_size = GetCommunicationAlignSize(size);
  91. } else {
  92. align_size = GetCommonAlignSize(size);
  93. }
  94. if (static_mem_offset_ < align_size) {
  95. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  96. << "] static[" << total_static_size_ << "])"
  97. << " malloc [" << align_size << "] failed!";
  98. }
  99. total_static_size_ += align_size;
  100. auto offset = static_mem_offset_ - align_size;
  101. if (dynamic_mem_offset_ > offset) {
  102. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  103. << "] static[" << total_static_size_ << "])"
  104. << " malloc [" << align_size << "] failed!";
  105. }
  106. static_mem_offset_ = offset;
  107. if (communication_mem) {
  108. return device_mem_base_ + offset + kMemAlignSize;
  109. } else {
  110. return device_mem_base_ + offset;
  111. }
  112. }
  113. uint8_t *MemoryManager::MallocDynamicMem(size_t size, bool communication_mem) {
  114. size_t align_size = 0;
  115. if (communication_mem) {
  116. align_size = GetCommunicationAlignSize(size);
  117. } else {
  118. align_size = GetCommonAlignSize(size);
  119. }
  120. uint64_t offset = dynamic_mem_offset_;
  121. auto new_offset = dynamic_mem_offset_ + align_size;
  122. if (new_offset > static_mem_offset_) {
  123. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  124. << "] static[" << total_static_size_ << "])"
  125. << " malloc [" << align_size << "] failed!";
  126. }
  127. total_dynamic_size_ += align_size;
  128. dynamic_mem_offset_ = new_offset;
  129. if (communication_mem) {
  130. return device_mem_base_ + offset + kMemAlignSize;
  131. } else {
  132. return device_mem_base_ + offset;
  133. }
  134. }
  135. bool MemoryManager::MallocMemFromMemPool(const DeviceAddressPtr address, size_t size) {
  136. auto device_ptr = MallocMemFromMemPool(size);
  137. if (!device_ptr) {
  138. return false;
  139. }
  140. address->ptr_ = device_ptr;
  141. address->from_mem_pool_ = true;
  142. return true;
  143. }
  144. void *MemoryManager::MallocMemFromMemPool(size_t size) {
  145. if (size == 0) {
  146. MS_LOG(ERROR) << "MallocMemFromMemPool size is 0.";
  147. }
  148. return nullptr;
  149. }
  150. void MemoryManager::FreeMemFromMemPool(const DeviceAddressPtr address) {
  151. MS_EXCEPTION_IF_NULL(address);
  152. MS_EXCEPTION_IF_NULL(address->ptr_);
  153. FreeMemFromMemPool(address->ptr_);
  154. address->ptr_ = nullptr;
  155. }
  156. void MemoryManager::FreeMemFromMemPool(void *device_ptr) {
  157. if (device_ptr == nullptr) {
  158. MS_LOG(ERROR) << "FreeMemFromMemPool device_ptr is null.";
  159. }
  160. }
  161. bool MemoryManager::MallocContinuousMemFromMemPool(const DeviceAddressPtrList addr_list, size_t total_size,
  162. std::vector<size_t> size_list) {
  163. auto device_ptr_list = MallocContinuousMemFromMemPool(total_size, size_list);
  164. if (device_ptr_list.size() == 0) {
  165. return false;
  166. }
  167. if (addr_list.size() != device_ptr_list.size()) {
  168. MS_LOG(EXCEPTION) << "The size of device list is not equal to the size of address list.";
  169. }
  170. for (size_t i = 0; i < addr_list.size(); i++) {
  171. MS_EXCEPTION_IF_NULL(device_ptr_list[i]);
  172. MS_EXCEPTION_IF_NULL(addr_list[i]);
  173. addr_list[i]->ptr_ = device_ptr_list[i];
  174. addr_list[i]->from_mem_pool_ = true;
  175. }
  176. return true;
  177. }
  178. std::vector<void *> MemoryManager::MallocContinuousMemFromMemPool(size_t total_size, std::vector<size_t> size_list) {
  179. if (total_size == 0) {
  180. MS_LOG(ERROR) << "MallocContinuousMemFromMemPool total_size is 0.";
  181. }
  182. std::vector<void *> device_ptr_list;
  183. device_ptr_list.emplace_back(nullptr);
  184. return device_ptr_list;
  185. }
  186. } // namespace device
  187. } // namespace mindspore