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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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) {
  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 name = "somas_allocate_info." + std::to_string(graph->graph_id());
  69. mindspore::RDR::RecordString(module, name, somas_reuse_util_ptr_->SomasInfo());
  70. name = "somas_mem_info." + std::to_string(graph->graph_id());
  71. mindspore::RDR::RecordString(module, name, somas_reuse_util_ptr_->SomasMemory());
  72. #endif
  73. bool save_graphs = context_ptr->get_param<bool>(MS_CTX_SAVE_GRAPHS_FLAG);
  74. auto save_graphs_path = context_ptr->get_param<std::string>(MS_CTX_SAVE_GRAPHS_PATH);
  75. if (save_graphs_path.empty()) {
  76. save_graphs_path = ".";
  77. }
  78. if (save_graphs) {
  79. std::string file_path = save_graphs_path + "/" + "somas_allocate_info_" + std::to_string(graph->graph_id()) + ".ir";
  80. somas_reuse_util_ptr_->DumpSomasInfoIR(file_path);
  81. std::string mem_file_path = save_graphs_path + "/" + "somas_mem_info_" + std::to_string(graph->graph_id()) + ".ir";
  82. somas_reuse_util_ptr_->DumpSomasMemoryIR(mem_file_path);
  83. }
  84. }
  85. uint8_t *MemoryManager::MallocOutputMem(const AnfNodePtr &node, size_t index, MemType type, size_t size,
  86. const DeviceAddressPtr &address, bool comm_mem) {
  87. MS_EXCEPTION_IF_NULL(node);
  88. MS_EXCEPTION_IF_NULL(address);
  89. auto context_ptr = MsContext::GetInstance();
  90. MS_EXCEPTION_IF_NULL(context_ptr);
  91. uint8_t *ptr = nullptr;
  92. if (comm_mem) {
  93. bool communication_mem = false;
  94. if (context_ptr->get_param<bool>(MS_CTX_ENABLE_HCCL)) {
  95. communication_mem = true;
  96. }
  97. if (type == kStaticMem) {
  98. ptr = MallocStaticMem(size, communication_mem);
  99. address->from_mem_pool_ = true;
  100. if (communication_mem) {
  101. address->communication_ptr_ = ptr - kMemAlignSize;
  102. }
  103. } else if (type == kReuseDynamicCommMem) {
  104. MS_EXCEPTION_IF_NULL(mem_reuse_util_ptr_);
  105. ptr = mem_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  106. } else if (type == kSomasReuseDynamicMem) {
  107. MS_EXCEPTION_IF_NULL(somas_reuse_util_ptr_);
  108. ptr = somas_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  109. } else {
  110. ptr = MallocDynamicMem(size, communication_mem);
  111. }
  112. address->ptr_ = ptr;
  113. return ptr;
  114. }
  115. if (type == kStaticMem) {
  116. ptr = MallocStaticMem(size, false);
  117. address->from_mem_pool_ = true;
  118. } else if (type == kDynamicMem) {
  119. ptr = MallocDynamicMem(size, false);
  120. } else if (type == kReuseDynamicMem) {
  121. MS_EXCEPTION_IF_NULL(mem_reuse_util_ptr_);
  122. ptr = mem_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  123. } else if (type == kSomasReuseDynamicMem) {
  124. MS_EXCEPTION_IF_NULL(somas_reuse_util_ptr_);
  125. ptr = somas_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  126. }
  127. address->ptr_ = ptr;
  128. return ptr;
  129. }
  130. uint8_t *MemoryManager::MallocWorkSpaceMem(const AnfNodePtr &node, size_t index, MemType type, size_t size) {
  131. if (type == kReuseDynamicMem) {
  132. MS_EXCEPTION_IF_NULL(mem_reuse_util_ptr_);
  133. return mem_reuse_util_ptr_->GetNodeWorkSpacePtr(node, index);
  134. } else if (type == kSomasReuseDynamicMem) {
  135. MS_EXCEPTION_IF_NULL(somas_reuse_util_ptr_);
  136. return somas_reuse_util_ptr_->GetNodeWorkSpacePtr(node, index);
  137. }
  138. return MallocDynamicMem(size, false);
  139. }
  140. uint8_t *MemoryManager::MallocMem(MemType type, size_t size, const DeviceAddressPtr &address, uint32_t graph_id) {
  141. MS_EXCEPTION_IF_NULL(address);
  142. uint8_t *ptr = nullptr;
  143. if (type == kStaticMem) {
  144. ptr = MallocStaticMem(size, false, graph_id);
  145. address->from_mem_pool_ = true;
  146. } else if (type == kDynamicMem) {
  147. ptr = MallocDynamicMem(size, false);
  148. }
  149. address->ptr_ = ptr;
  150. return ptr;
  151. }
  152. uint8_t *MemoryManager::MallocStaticMem(size_t size, bool communication_mem, uint32_t graph_id) {
  153. size_t align_size = 0;
  154. if (communication_mem) {
  155. align_size = GetCommunicationAlignSize(size);
  156. } else {
  157. align_size = GetCommonAlignSize(size);
  158. }
  159. MS_LOG(INFO) << "Malloc Memory for Static: total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  160. << "] static[" << total_static_size_ << "])"
  161. << " malloc [" << align_size << "] communication_mem: " << communication_mem;
  162. if (static_mem_offset_ < align_size) {
  163. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  164. << "] static[" << total_static_size_ << "])"
  165. << " malloc [" << align_size << "] failed!";
  166. }
  167. total_static_size_ += align_size;
  168. auto offset = static_mem_offset_ - align_size;
  169. if (dynamic_mem_offset_ > offset) {
  170. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  171. << "] static[" << total_static_size_ << "])"
  172. << " malloc [" << align_size << "] failed!";
  173. }
  174. static_mem_offset_ = offset;
  175. if (communication_mem) {
  176. return device_mem_base_ + offset + kMemAlignSize;
  177. } else {
  178. return device_mem_base_ + offset;
  179. }
  180. }
  181. uint8_t *MemoryManager::MallocDynamicMem(size_t size, bool communication_mem) {
  182. size_t align_size = 0;
  183. if (communication_mem) {
  184. align_size = GetCommunicationAlignSize(size);
  185. } else {
  186. align_size = GetCommonAlignSize(size);
  187. }
  188. MS_LOG(INFO) << "Malloc Memory for Dynamic: total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  189. << "] static[" << total_static_size_ << "])"
  190. << " malloc [" << align_size << "] communication_mem: " << communication_mem;
  191. uint64_t offset = dynamic_mem_offset_;
  192. auto new_offset = dynamic_mem_offset_ + align_size;
  193. if (new_offset > static_mem_offset_) {
  194. MS_LOG(EXCEPTION) << "Out of memory!!! total[" << device_mem_size_ << "](dynamic[" << total_dynamic_size_
  195. << "] static[" << total_static_size_ << "])"
  196. << " malloc [" << align_size << "] failed!";
  197. }
  198. total_dynamic_size_ += align_size;
  199. dynamic_mem_offset_ = new_offset;
  200. if (communication_mem) {
  201. return device_mem_base_ + offset + kMemAlignSize;
  202. } else {
  203. return device_mem_base_ + offset;
  204. }
  205. }
  206. bool MemoryManager::MallocMemFromMemPool(const DeviceAddressPtr address, size_t size) {
  207. auto device_ptr = MallocMemFromMemPool(size);
  208. if (!device_ptr) {
  209. return false;
  210. }
  211. address->ptr_ = device_ptr;
  212. address->size_ = size;
  213. address->from_mem_pool_ = true;
  214. return true;
  215. }
  216. void *MemoryManager::MallocMemFromMemPool(size_t size) {
  217. if (size == 0) {
  218. MS_LOG(ERROR) << "MallocMemFromMemPool size is 0.";
  219. }
  220. return nullptr;
  221. }
  222. void MemoryManager::FreeMemFromMemPool(const DeviceAddressPtr address) {
  223. MS_EXCEPTION_IF_NULL(address);
  224. MS_EXCEPTION_IF_NULL(address->ptr_);
  225. FreeMemFromMemPool(address->ptr_);
  226. address->ptr_ = nullptr;
  227. }
  228. void MemoryManager::FreeMemFromMemPool(void *device_ptr) {
  229. if (device_ptr == nullptr) {
  230. MS_LOG(ERROR) << "FreeMemFromMemPool device_ptr is null.";
  231. }
  232. }
  233. bool MemoryManager::MallocContinuousMemFromMemPool(const DeviceAddressPtrList addr_list, size_t total_size,
  234. std::vector<size_t> size_list) {
  235. auto device_ptr_list = MallocContinuousMemFromMemPool(total_size, size_list);
  236. if (device_ptr_list.size() == 0) {
  237. return false;
  238. }
  239. if (addr_list.size() != device_ptr_list.size()) {
  240. MS_LOG(EXCEPTION) << "The size of device list is not equal to the size of address list.";
  241. }
  242. for (size_t i = 0; i < addr_list.size(); i++) {
  243. MS_EXCEPTION_IF_NULL(device_ptr_list[i]);
  244. MS_EXCEPTION_IF_NULL(addr_list[i]);
  245. addr_list[i]->ptr_ = device_ptr_list[i];
  246. addr_list[i]->from_mem_pool_ = true;
  247. }
  248. return true;
  249. }
  250. std::vector<void *> MemoryManager::MallocContinuousMemFromMemPool(size_t total_size, std::vector<size_t> size_list) {
  251. if (total_size == 0) {
  252. MS_LOG(ERROR) << "MallocContinuousMemFromMemPool total_size is 0.";
  253. }
  254. std::vector<void *> device_ptr_list;
  255. device_ptr_list.emplace_back(nullptr);
  256. return device_ptr_list;
  257. }
  258. } // namespace device
  259. } // namespace mindspore