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.0 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. ptr = mem_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  67. }
  68. return ptr;
  69. }
  70. uint8_t *MemoryManager::MallocWorkSpaceMem(const AnfNodePtr &node, size_t index, int flag, size_t size) {
  71. if (flag == kReuseDynamicMem) {
  72. return mem_reuse_util_ptr_->GetNodeWorkSpacePtr(node, index);
  73. }
  74. return MallocDynamicMem(size, false);
  75. }
  76. uint8_t *MemoryManager::MallocMem(int flag, size_t size) {
  77. uint8_t *ptr = nullptr;
  78. if (flag == kStaticMem) {
  79. ptr = MallocStaticMem(size, false);
  80. } else if (flag == kDynamicMem) {
  81. ptr = MallocDynamicMem(size, false);
  82. }
  83. return ptr;
  84. }
  85. uint8_t *MemoryManager::MallocStaticMem(size_t size, bool communication_mem) {
  86. size_t align_size = 0;
  87. if (communication_mem) {
  88. align_size = GetCommunicationAlignSize(size);
  89. } else {
  90. align_size = GetCommonAlignSize(size);
  91. }
  92. if (static_mem_offset_ < align_size) {
  93. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  94. << "] static[" << total_static_size_ << "])"
  95. << " malloc [" << align_size << "] failed!";
  96. }
  97. total_static_size_ += align_size;
  98. auto offset = static_mem_offset_ - align_size;
  99. if (dynamic_mem_offset_ > offset) {
  100. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  101. << "] static[" << total_static_size_ << "])"
  102. << " malloc [" << align_size << "] failed!";
  103. }
  104. static_mem_offset_ = offset;
  105. if (communication_mem) {
  106. return device_mem_base_ + offset + kMemAlignSize;
  107. } else {
  108. return device_mem_base_ + offset;
  109. }
  110. }
  111. uint8_t *MemoryManager::MallocDynamicMem(size_t size, bool communication_mem) {
  112. size_t align_size = 0;
  113. if (communication_mem) {
  114. align_size = GetCommunicationAlignSize(size);
  115. } else {
  116. align_size = GetCommonAlignSize(size);
  117. }
  118. uint64_t offset = dynamic_mem_offset_;
  119. auto new_offset = dynamic_mem_offset_ + align_size;
  120. if (new_offset > static_mem_offset_) {
  121. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  122. << "] static[" << total_static_size_ << "])"
  123. << " malloc [" << align_size << "] failed!";
  124. }
  125. total_dynamic_size_ += align_size;
  126. dynamic_mem_offset_ = new_offset;
  127. if (communication_mem) {
  128. return device_mem_base_ + offset + kMemAlignSize;
  129. } else {
  130. return device_mem_base_ + offset;
  131. }
  132. }
  133. void MemoryManager::MallocMemFromMemPool(const DeviceAddressPtr address, size_t size) {
  134. auto device_ptr = MallocMemFromMemPool(size);
  135. MS_EXCEPTION_IF_NULL(device_ptr);
  136. address->ptr_ = device_ptr;
  137. address->from_mem_pool_ = true;
  138. }
  139. void *MemoryManager::MallocMemFromMemPool(size_t size) {
  140. if (size == 0) {
  141. MS_LOG(ERROR) << "MallocMemFromMemPool size is 0.";
  142. }
  143. return nullptr;
  144. }
  145. void MemoryManager::FreeMemFromMemPool(const DeviceAddressPtr address) {
  146. MS_EXCEPTION_IF_NULL(address);
  147. MS_EXCEPTION_IF_NULL(address->ptr_);
  148. FreeMemFromMemPool(address->ptr_);
  149. address->ptr_ = nullptr;
  150. }
  151. void MemoryManager::FreeMemFromMemPool(void *device_ptr) {
  152. if (device_ptr == nullptr) {
  153. MS_LOG(ERROR) << "FreeMemFromMemPool device_ptr is null.";
  154. }
  155. }
  156. void MemoryManager::MallocContinuousMemFromMemPool(const DeviceAddressPtrList addr_list, size_t total_size,
  157. std::vector<size_t> size_list) {
  158. auto device_ptr_list = MallocContinuousMemFromMemPool(total_size, size_list);
  159. if (addr_list.size() != device_ptr_list.size()) {
  160. MS_LOG(EXCEPTION) << "The size of device list is not equal to the size of address list.";
  161. }
  162. for (size_t i = 0; i < addr_list.size(); i++) {
  163. MS_EXCEPTION_IF_NULL(device_ptr_list[i]);
  164. MS_EXCEPTION_IF_NULL(addr_list[i]);
  165. addr_list[i]->ptr_ = device_ptr_list[i];
  166. addr_list[i]->from_mem_pool_ = true;
  167. }
  168. }
  169. std::vector<void *> MemoryManager::MallocContinuousMemFromMemPool(size_t total_size, std::vector<size_t> size_list) {
  170. if (total_size == 0) {
  171. MS_LOG(ERROR) << "MallocContinuousMemFromMemPool total_size is 0.";
  172. }
  173. std::vector<void *> device_ptr_list;
  174. device_ptr_list.emplace_back(nullptr);
  175. return device_ptr_list;
  176. }
  177. } // namespace device
  178. } // namespace mindspore