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