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