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 11 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
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 <string>
  18. #include "backend/session/anf_runtime_algorithm.h"
  19. #include "utils/ms_context.h"
  20. using mindspore::memreuse::BestFitMemReuse;
  21. using mindspore::memreuse::MemReuseUtilPtr;
  22. namespace mindspore {
  23. namespace device {
  24. size_t MemoryManager::GetCommonAlignSize(size_t input_size) const {
  25. return (input_size + kMemAlignSize + 31) / kMemAlignSize * kMemAlignSize;
  26. }
  27. size_t MemoryManager::GetCommunicationAlignSize(size_t input_size) const {
  28. return (input_size + kMemAlignSize - 1) / kMemAlignSize * kMemAlignSize + 2 * kMemAlignSize;
  29. }
  30. void MemoryManager::MallocReusedDynamicMem(const session::KernelGraph *graph) {
  31. MS_EXCEPTION_IF_NULL(graph);
  32. MemReuseUtilPtr mem_reuse_util_ptr = std::make_shared<memreuse::MemReuseUtil>();
  33. MS_EXCEPTION_IF_NULL(mem_reuse_util_ptr);
  34. // set all infos
  35. mem_reuse_util_ptr->SetAllInfo(graph);
  36. auto bestfit_mem_reuse = std::make_shared<BestFitMemReuse>();
  37. MS_EXCEPTION_IF_NULL(bestfit_mem_reuse);
  38. bestfit_mem_reuse->Reuse(mem_reuse_util_ptr.get());
  39. size_t total_allocated_size = bestfit_mem_reuse->GetAllocatedSize();
  40. MS_LOG(INFO) << "TotalReuseDynamicSize [" << total_allocated_size << "]";
  41. mem_reuse_util_ptr_ = mem_reuse_util_ptr;
  42. auto base_ptr = MallocDynamicMem(total_allocated_size, false);
  43. MS_LOG(INFO) << "Reuse Memory from [" << reinterpret_cast<void *>(base_ptr) << "] to ["
  44. << reinterpret_cast<void *>(base_ptr + total_allocated_size) << "]";
  45. mem_reuse_util_ptr_->set_mem_base(base_ptr);
  46. }
  47. void MemoryManager::MallocSomasDynamicMem(const session::KernelGraph *graph) {
  48. MS_EXCEPTION_IF_NULL(graph);
  49. SomasPtr somas_reuse_util_ptr = std::make_shared<somas::Somas>();
  50. MS_EXCEPTION_IF_NULL(somas_reuse_util_ptr);
  51. somas_reuse_util_ptr_ = somas_reuse_util_ptr;
  52. if (!(somas_reuse_util_ptr->Allocate(graph))) {
  53. MS_LOG(EXCEPTION) << "Somas Allocate Failed.";
  54. }
  55. size_t total_allocated_size = somas_reuse_util_ptr->GetTotalMemSize();
  56. MS_LOG(INFO) << "Graph " << graph->graph_id() << ": TotalSomasReuseDynamicSize [" << total_allocated_size << "]";
  57. auto base_ptr = MallocDynamicMem(total_allocated_size, false);
  58. MS_LOG(INFO) << "Somas Reuse Memory Base Address [" << static_cast<void *>(base_ptr) << "], End Address ["
  59. << static_cast<void *>(base_ptr + total_allocated_size) << "]";
  60. somas_reuse_util_ptr->set_mem_base_addr(base_ptr);
  61. auto context_ptr = MsContext::GetInstance();
  62. MS_EXCEPTION_IF_NULL(context_ptr);
  63. bool save_graphs = context_ptr->get_param<bool>(MS_CTX_SAVE_GRAPHS_FLAG);
  64. auto save_graphs_path = context_ptr->get_param<std::string>(MS_CTX_SAVE_GRAPHS_PATH);
  65. if (save_graphs_path.empty()) {
  66. save_graphs_path = ".";
  67. }
  68. if (save_graphs) {
  69. std::string file_path =
  70. save_graphs_path + "/" + "somas_after_allocate_" + std::to_string(graph->graph_id()) + ".ir";
  71. somas_reuse_util_ptr_->DumpSomasBasicIR(file_path);
  72. std::string mem_file_path = save_graphs_path + "/" + "somas_mem_info_" + std::to_string(graph->graph_id()) + ".ir";
  73. somas_reuse_util_ptr_->DumpSomasMemoryIR(mem_file_path);
  74. }
  75. }
  76. uint8_t *MemoryManager::MallocOutputMem(const AnfNodePtr &node, size_t index, MemType type, size_t size,
  77. const DeviceAddressPtr &address, bool comm_mem) {
  78. MS_EXCEPTION_IF_NULL(node);
  79. MS_EXCEPTION_IF_NULL(address);
  80. auto context_ptr = MsContext::GetInstance();
  81. MS_EXCEPTION_IF_NULL(context_ptr);
  82. uint8_t *ptr = nullptr;
  83. if (comm_mem) {
  84. bool communication_mem = false;
  85. if (context_ptr->get_param<bool>(MS_CTX_ENABLE_HCCL)) {
  86. communication_mem = true;
  87. }
  88. if (type == kStaticMem) {
  89. ptr = MallocStaticMem(size, communication_mem);
  90. address->from_mem_pool_ = true;
  91. if (communication_mem) {
  92. address->communication_ptr_ = ptr - kMemAlignSize;
  93. }
  94. } else if (type == kReuseDynamicCommMem) {
  95. MS_EXCEPTION_IF_NULL(mem_reuse_util_ptr_);
  96. ptr = mem_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  97. } else if (type == kSomasReuseDynamicMem) {
  98. MS_EXCEPTION_IF_NULL(somas_reuse_util_ptr_);
  99. ptr = somas_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  100. } else {
  101. ptr = MallocDynamicMem(size, communication_mem);
  102. }
  103. address->ptr_ = ptr;
  104. return ptr;
  105. }
  106. if (type == kStaticMem) {
  107. ptr = MallocStaticMem(size, false);
  108. address->from_mem_pool_ = true;
  109. } else if (type == kDynamicMem) {
  110. ptr = MallocDynamicMem(size, false);
  111. } else if (type == kReuseDynamicMem) {
  112. MS_EXCEPTION_IF_NULL(mem_reuse_util_ptr_);
  113. ptr = mem_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  114. } else if (type == kSomasReuseDynamicMem) {
  115. MS_EXCEPTION_IF_NULL(somas_reuse_util_ptr_);
  116. ptr = somas_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  117. }
  118. address->ptr_ = ptr;
  119. return ptr;
  120. }
  121. uint8_t *MemoryManager::MallocWorkSpaceMem(const AnfNodePtr &node, size_t index, MemType type, size_t size) {
  122. if (type == kReuseDynamicMem) {
  123. MS_EXCEPTION_IF_NULL(mem_reuse_util_ptr_);
  124. return mem_reuse_util_ptr_->GetNodeWorkSpacePtr(node, index);
  125. } else if (type == kSomasReuseDynamicMem) {
  126. MS_EXCEPTION_IF_NULL(somas_reuse_util_ptr_);
  127. return somas_reuse_util_ptr_->GetNodeWorkSpacePtr(node, index);
  128. }
  129. return MallocDynamicMem(size, false);
  130. }
  131. uint8_t *MemoryManager::MallocMem(MemType type, size_t size, const DeviceAddressPtr &address) {
  132. MS_EXCEPTION_IF_NULL(address);
  133. uint8_t *ptr = nullptr;
  134. if (type == kStaticMem) {
  135. ptr = MallocStaticMem(size, false);
  136. address->from_mem_pool_ = true;
  137. } else if (type == kDynamicMem) {
  138. ptr = MallocDynamicMem(size, false);
  139. }
  140. address->ptr_ = ptr;
  141. return ptr;
  142. }
  143. uint8_t *MemoryManager::MallocStaticMem(size_t size, bool communication_mem) {
  144. size_t align_size = 0;
  145. if (communication_mem) {
  146. align_size = GetCommunicationAlignSize(size);
  147. } else {
  148. align_size = GetCommonAlignSize(size);
  149. }
  150. MS_LOG(INFO) << "Malloc Memory for Static: total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  151. << "] static[" << total_static_size_ << "])"
  152. << " malloc [" << align_size << "] communication_mem: " << communication_mem;
  153. if (static_mem_offset_ < align_size) {
  154. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  155. << "] static[" << total_static_size_ << "])"
  156. << " malloc [" << align_size << "] failed!";
  157. }
  158. total_static_size_ += align_size;
  159. auto offset = static_mem_offset_ - align_size;
  160. if (dynamic_mem_offset_ > offset) {
  161. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  162. << "] static[" << total_static_size_ << "])"
  163. << " malloc [" << align_size << "] failed!";
  164. }
  165. static_mem_offset_ = offset;
  166. if (communication_mem) {
  167. return device_mem_base_ + offset + kMemAlignSize;
  168. } else {
  169. return device_mem_base_ + offset;
  170. }
  171. }
  172. uint8_t *MemoryManager::MallocDynamicMem(size_t size, bool communication_mem) {
  173. size_t align_size = 0;
  174. if (communication_mem) {
  175. align_size = GetCommunicationAlignSize(size);
  176. } else {
  177. align_size = GetCommonAlignSize(size);
  178. }
  179. MS_LOG(INFO) << "Malloc Memory for Dynamic: total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  180. << "] static[" << total_static_size_ << "])"
  181. << " malloc [" << align_size << "] communication_mem: " << communication_mem;
  182. uint64_t offset = dynamic_mem_offset_;
  183. auto new_offset = dynamic_mem_offset_ + align_size;
  184. if (new_offset > static_mem_offset_) {
  185. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  186. << "] static[" << total_static_size_ << "])"
  187. << " malloc [" << align_size << "] failed!";
  188. }
  189. total_dynamic_size_ += align_size;
  190. dynamic_mem_offset_ = new_offset;
  191. if (communication_mem) {
  192. return device_mem_base_ + offset + kMemAlignSize;
  193. } else {
  194. return device_mem_base_ + offset;
  195. }
  196. }
  197. bool MemoryManager::MallocMemFromMemPool(const DeviceAddressPtr address, size_t size) {
  198. auto device_ptr = MallocMemFromMemPool(size);
  199. if (!device_ptr) {
  200. return false;
  201. }
  202. address->ptr_ = device_ptr;
  203. address->size_ = size;
  204. address->from_mem_pool_ = true;
  205. return true;
  206. }
  207. void *MemoryManager::MallocMemFromMemPool(size_t size) {
  208. if (size == 0) {
  209. MS_LOG(ERROR) << "MallocMemFromMemPool size is 0.";
  210. }
  211. return nullptr;
  212. }
  213. void MemoryManager::FreeMemFromMemPool(const DeviceAddressPtr address) {
  214. MS_EXCEPTION_IF_NULL(address);
  215. MS_EXCEPTION_IF_NULL(address->ptr_);
  216. FreeMemFromMemPool(address->ptr_);
  217. address->ptr_ = nullptr;
  218. }
  219. void MemoryManager::FreeMemFromMemPool(void *device_ptr) {
  220. if (device_ptr == nullptr) {
  221. MS_LOG(ERROR) << "FreeMemFromMemPool device_ptr is null.";
  222. }
  223. }
  224. bool MemoryManager::MallocContinuousMemFromMemPool(const DeviceAddressPtrList addr_list, size_t total_size,
  225. std::vector<size_t> size_list) {
  226. auto device_ptr_list = MallocContinuousMemFromMemPool(total_size, size_list);
  227. if (device_ptr_list.size() == 0) {
  228. return false;
  229. }
  230. if (addr_list.size() != device_ptr_list.size()) {
  231. MS_LOG(EXCEPTION) << "The size of device list is not equal to the size of address list.";
  232. }
  233. for (size_t i = 0; i < addr_list.size(); i++) {
  234. MS_EXCEPTION_IF_NULL(device_ptr_list[i]);
  235. MS_EXCEPTION_IF_NULL(addr_list[i]);
  236. addr_list[i]->ptr_ = device_ptr_list[i];
  237. addr_list[i]->from_mem_pool_ = true;
  238. }
  239. return true;
  240. }
  241. std::vector<void *> MemoryManager::MallocContinuousMemFromMemPool(size_t total_size, std::vector<size_t> size_list) {
  242. if (total_size == 0) {
  243. MS_LOG(ERROR) << "MallocContinuousMemFromMemPool total_size is 0.";
  244. }
  245. std::vector<void *> device_ptr_list;
  246. device_ptr_list.emplace_back(nullptr);
  247. return device_ptr_list;
  248. }
  249. } // namespace device
  250. } // namespace mindspore