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 8.3 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
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
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "runtime/device/memory_manager.h"
  17. #include "backend/session/anf_runtime_algorithm.h"
  18. #include "utils/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(const 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, MemType type, size_t size,
  45. const DeviceAddressPtr &address) {
  46. MS_EXCEPTION_IF_NULL(node);
  47. MS_EXCEPTION_IF_NULL(address);
  48. auto context_ptr = MsContext::GetInstance();
  49. MS_EXCEPTION_IF_NULL(context_ptr);
  50. uint8_t *ptr = nullptr;
  51. if (AnfAlgo::IsCommunicationOp(node)) {
  52. bool communication_mem = false;
  53. if (context_ptr->enable_hccl()) {
  54. communication_mem = true;
  55. }
  56. if (type == kStaticMem) {
  57. ptr = MallocStaticMem(size, communication_mem);
  58. address->from_mem_pool_ = true;
  59. if (communication_mem) {
  60. address->communication_ptr_ = ptr - kMemAlignSize;
  61. }
  62. } else if (type == kReuseDynamicCommMem) {
  63. MS_EXCEPTION_IF_NULL(mem_reuse_util_ptr_);
  64. ptr = mem_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  65. } else {
  66. ptr = MallocDynamicMem(size, communication_mem);
  67. }
  68. address->ptr_ = ptr;
  69. return ptr;
  70. }
  71. if (type == kStaticMem) {
  72. ptr = MallocStaticMem(size, false);
  73. address->from_mem_pool_ = true;
  74. } else if (type == kDynamicMem) {
  75. ptr = MallocDynamicMem(size, false);
  76. } else if (type == kReuseDynamicMem) {
  77. MS_EXCEPTION_IF_NULL(mem_reuse_util_ptr_);
  78. ptr = mem_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  79. }
  80. address->ptr_ = ptr;
  81. return ptr;
  82. }
  83. uint8_t *MemoryManager::MallocWorkSpaceMem(const AnfNodePtr &node, size_t index, MemType type, size_t size) {
  84. if (type == kReuseDynamicMem) {
  85. MS_EXCEPTION_IF_NULL(mem_reuse_util_ptr_);
  86. return mem_reuse_util_ptr_->GetNodeWorkSpacePtr(node, index);
  87. }
  88. return MallocDynamicMem(size, false);
  89. }
  90. uint8_t *MemoryManager::MallocMem(MemType type, size_t size, const DeviceAddressPtr &address) {
  91. MS_EXCEPTION_IF_NULL(address);
  92. uint8_t *ptr = nullptr;
  93. if (type == kStaticMem) {
  94. ptr = MallocStaticMem(size, false);
  95. address->from_mem_pool_ = true;
  96. } else if (type == kDynamicMem) {
  97. ptr = MallocDynamicMem(size, false);
  98. }
  99. address->ptr_ = ptr;
  100. return ptr;
  101. }
  102. uint8_t *MemoryManager::MallocStaticMem(size_t size, bool communication_mem) {
  103. size_t align_size = 0;
  104. if (communication_mem) {
  105. align_size = GetCommunicationAlignSize(size);
  106. } else {
  107. align_size = GetCommonAlignSize(size);
  108. }
  109. MS_LOG(INFO) << "Malloc Memory for Static: total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  110. << "] static[" << total_static_size_ << "])"
  111. << " malloc [" << align_size << "] communication_mem: " << communication_mem;
  112. if (static_mem_offset_ < align_size) {
  113. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  114. << "] static[" << total_static_size_ << "])"
  115. << " malloc [" << align_size << "] failed!";
  116. }
  117. total_static_size_ += align_size;
  118. auto offset = static_mem_offset_ - align_size;
  119. if (dynamic_mem_offset_ > offset) {
  120. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  121. << "] static[" << total_static_size_ << "])"
  122. << " malloc [" << align_size << "] failed!";
  123. }
  124. static_mem_offset_ = offset;
  125. if (communication_mem) {
  126. return device_mem_base_ + offset + kMemAlignSize;
  127. } else {
  128. return device_mem_base_ + offset;
  129. }
  130. }
  131. uint8_t *MemoryManager::MallocDynamicMem(size_t size, bool communication_mem) {
  132. size_t align_size = 0;
  133. if (communication_mem) {
  134. align_size = GetCommunicationAlignSize(size);
  135. } else {
  136. align_size = GetCommonAlignSize(size);
  137. }
  138. MS_LOG(INFO) << "Malloc Memory for Dynamic: total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  139. << "] static[" << total_static_size_ << "])"
  140. << " malloc [" << align_size << "] communication_mem: " << communication_mem;
  141. uint64_t offset = dynamic_mem_offset_;
  142. auto new_offset = dynamic_mem_offset_ + align_size;
  143. if (new_offset > static_mem_offset_) {
  144. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  145. << "] static[" << total_static_size_ << "])"
  146. << " malloc [" << align_size << "] failed!";
  147. }
  148. total_dynamic_size_ += align_size;
  149. dynamic_mem_offset_ = new_offset;
  150. if (communication_mem) {
  151. return device_mem_base_ + offset + kMemAlignSize;
  152. } else {
  153. return device_mem_base_ + offset;
  154. }
  155. }
  156. bool MemoryManager::MallocMemFromMemPool(const DeviceAddressPtr address, size_t size) {
  157. auto device_ptr = MallocMemFromMemPool(size);
  158. if (!device_ptr) {
  159. return false;
  160. }
  161. address->ptr_ = device_ptr;
  162. address->from_mem_pool_ = true;
  163. return true;
  164. }
  165. void *MemoryManager::MallocMemFromMemPool(size_t size) {
  166. if (size == 0) {
  167. MS_LOG(ERROR) << "MallocMemFromMemPool size is 0.";
  168. }
  169. return nullptr;
  170. }
  171. void MemoryManager::FreeMemFromMemPool(const DeviceAddressPtr address) {
  172. MS_EXCEPTION_IF_NULL(address);
  173. MS_EXCEPTION_IF_NULL(address->ptr_);
  174. FreeMemFromMemPool(address->ptr_);
  175. address->ptr_ = nullptr;
  176. }
  177. void MemoryManager::FreeMemFromMemPool(void *device_ptr) {
  178. if (device_ptr == nullptr) {
  179. MS_LOG(ERROR) << "FreeMemFromMemPool device_ptr is null.";
  180. }
  181. }
  182. bool MemoryManager::MallocContinuousMemFromMemPool(const DeviceAddressPtrList addr_list, size_t total_size,
  183. std::vector<size_t> size_list) {
  184. auto device_ptr_list = MallocContinuousMemFromMemPool(total_size, size_list);
  185. if (device_ptr_list.size() == 0) {
  186. return false;
  187. }
  188. if (addr_list.size() != device_ptr_list.size()) {
  189. MS_LOG(EXCEPTION) << "The size of device list is not equal to the size of address list.";
  190. }
  191. for (size_t i = 0; i < addr_list.size(); i++) {
  192. MS_EXCEPTION_IF_NULL(device_ptr_list[i]);
  193. MS_EXCEPTION_IF_NULL(addr_list[i]);
  194. addr_list[i]->ptr_ = device_ptr_list[i];
  195. addr_list[i]->from_mem_pool_ = true;
  196. }
  197. return true;
  198. }
  199. std::vector<void *> MemoryManager::MallocContinuousMemFromMemPool(size_t total_size, std::vector<size_t> size_list) {
  200. if (total_size == 0) {
  201. MS_LOG(ERROR) << "MallocContinuousMemFromMemPool total_size is 0.";
  202. }
  203. std::vector<void *> device_ptr_list;
  204. device_ptr_list.emplace_back(nullptr);
  205. return device_ptr_list;
  206. }
  207. } // namespace device
  208. } // namespace mindspore