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 7.6 kB

6 years ago
5 years ago
6 years ago
6 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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
4 years ago
6 years ago
4 years ago
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
4 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "debug/common.h"
  20. #ifdef ENABLE_DUMP_IR
  21. #include "debug/rdr/running_data_recorder.h"
  22. #endif
  23. #include "utils/ms_context.h"
  24. namespace mindspore {
  25. namespace device {
  26. constexpr size_t kAlignBytes = 32;
  27. size_t MemoryManager::GetCommonAlignSize(size_t input_size) {
  28. return (input_size + kMemAlignSize + kAlignBytes - 1) / kMemAlignSize * kMemAlignSize;
  29. }
  30. size_t MemoryManager::GetCommunicationAlignSize(size_t input_size) {
  31. return (input_size + kMemAlignSize - 1) / kMemAlignSize * kMemAlignSize + kTwiceMemAlignSize;
  32. }
  33. void MemoryManager::MallocSomasDynamicMem(const session::KernelGraph &graph) {
  34. SomasPtr somas_reuse_util_ptr = std::make_shared<somas::Somas>();
  35. MS_EXCEPTION_IF_NULL(somas_reuse_util_ptr);
  36. somas_reuse_util_ptr_ = somas_reuse_util_ptr;
  37. if (!(somas_reuse_util_ptr->Allocate(&graph))) {
  38. MS_LOG(EXCEPTION) << "Somas Allocate Failed.";
  39. }
  40. size_t total_allocated_size = somas_reuse_util_ptr->GetTotalMemSize();
  41. MS_LOG(INFO) << "Graph " << graph.graph_id() << ": TotalSomasReuseDynamicSize [" << total_allocated_size << "]";
  42. if (total_allocated_size > 0) {
  43. auto base_ptr = MallocDynamicMem(total_allocated_size, false);
  44. MS_LOG(INFO) << "Somas Reuse Memory Base Address [" << static_cast<void *>(base_ptr) << "], End Address ["
  45. << static_cast<void *>(base_ptr + total_allocated_size) << "]";
  46. somas_reuse_util_ptr->set_mem_base_addr(base_ptr);
  47. }
  48. auto context_ptr = MsContext::GetInstance();
  49. MS_EXCEPTION_IF_NULL(context_ptr);
  50. #ifdef ENABLE_DUMP_IR
  51. SubModuleId module = SubModuleId::SM_OPTIMIZER;
  52. std::string name = "somas_allocate_info." + std::to_string(graph.graph_id());
  53. (void)mindspore::RDR::RecordString(module, name, somas_reuse_util_ptr_->SomasInfo());
  54. name = "somas_mem_info." + std::to_string(graph.graph_id());
  55. (void)mindspore::RDR::RecordString(module, name, somas_reuse_util_ptr_->SomasMemory());
  56. #endif
  57. bool save_graphs = context_ptr->get_param<bool>(MS_CTX_SAVE_GRAPHS_FLAG);
  58. if (save_graphs) {
  59. std::string file_path = GetSaveGraphsPathName("somas_allocate_info_" + std::to_string(graph.graph_id()) + ".ir");
  60. somas_reuse_util_ptr_->DumpSomasInfoIR(file_path);
  61. std::string mem_file_path = GetSaveGraphsPathName("somas_mem_info_" + std::to_string(graph.graph_id()) + ".ir");
  62. somas_reuse_util_ptr_->DumpSomasMemoryIR(mem_file_path);
  63. }
  64. }
  65. uint8_t *MemoryManager::MallocOutputMem(const AnfNodePtr &node, size_t index, MemType type, size_t size,
  66. const DeviceAddressPtr &address, bool comm_mem) {
  67. MS_EXCEPTION_IF_NULL(node);
  68. MS_EXCEPTION_IF_NULL(address);
  69. auto context_ptr = MsContext::GetInstance();
  70. MS_EXCEPTION_IF_NULL(context_ptr);
  71. uint8_t *ptr = nullptr;
  72. if (comm_mem) {
  73. bool communication_mem = false;
  74. if (context_ptr->get_param<bool>(MS_CTX_ENABLE_HCCL)) {
  75. communication_mem = true;
  76. }
  77. if (type == kStaticMem) {
  78. ptr = MallocStaticMem(size, communication_mem);
  79. address->from_mem_pool_ = true;
  80. if (communication_mem) {
  81. address->communication_ptr_ = ptr - kMemAlignSize;
  82. }
  83. } else if (type == kSomasReuseDynamicMem) {
  84. MS_EXCEPTION_IF_NULL(somas_reuse_util_ptr_);
  85. ptr = somas_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  86. } else {
  87. ptr = MallocDynamicMem(size, communication_mem);
  88. }
  89. address->ptr_ = ptr;
  90. return ptr;
  91. }
  92. if (type == kStaticMem) {
  93. ptr = MallocStaticMem(size, false);
  94. address->from_mem_pool_ = true;
  95. } else if (type == kDynamicMem) {
  96. ptr = MallocDynamicMem(size, false);
  97. } else if (type == kSomasReuseDynamicMem) {
  98. MS_EXCEPTION_IF_NULL(somas_reuse_util_ptr_);
  99. ptr = somas_reuse_util_ptr_->GetNodeOutputPtr(node, index);
  100. }
  101. address->ptr_ = ptr;
  102. return ptr;
  103. }
  104. uint8_t *MemoryManager::MallocWorkSpaceMem(const AnfNodePtr &node, size_t index, MemType type, size_t size) {
  105. if (type == kSomasReuseDynamicMem) {
  106. MS_EXCEPTION_IF_NULL(somas_reuse_util_ptr_);
  107. return somas_reuse_util_ptr_->GetNodeWorkSpacePtr(node, index);
  108. }
  109. return MallocDynamicMem(size, false);
  110. }
  111. uint8_t *MemoryManager::MallocMem(MemType type, size_t size, const DeviceAddressPtr &address, uint32_t graph_id) {
  112. MS_EXCEPTION_IF_NULL(address);
  113. uint8_t *ptr = nullptr;
  114. if (type == kStaticMem) {
  115. ptr = MallocStaticMem(size, false, graph_id);
  116. address->from_mem_pool_ = true;
  117. } else if (type == kDynamicMem) {
  118. ptr = MallocDynamicMem(size, false);
  119. }
  120. address->ptr_ = ptr;
  121. return ptr;
  122. }
  123. uint8_t *MemoryManager::MallocDynamicMem(size_t size, bool communication_mem) {
  124. MS_LOG(INFO) << "Call default dynamic malloc " << size << " v " << communication_mem;
  125. return nullptr;
  126. }
  127. bool MemoryManager::MallocMemFromMemPool(const DeviceAddressPtr &address, size_t size) {
  128. MS_EXCEPTION_IF_NULL(address);
  129. auto device_ptr = MallocMemFromMemPool(size, address->from_persistent_mem_);
  130. if (!device_ptr) {
  131. return false;
  132. }
  133. MS_EXCEPTION_IF_NULL(address);
  134. address->ptr_ = device_ptr;
  135. address->size_ = size;
  136. address->from_mem_pool_ = true;
  137. return true;
  138. }
  139. void *MemoryManager::MallocMemFromMemPool(size_t size, bool from_persistent_mem) {
  140. if (size == 0) {
  141. MS_LOG(ERROR) << "MallocMemFromMemPool size is 0.";
  142. }
  143. return nullptr;
  144. }
  145. void MemoryManager::FreeMemFromMemPool(const DeviceAddressPtr address) {
  146. MS_EXCEPTION_IF_NULL(address);
  147. MS_EXCEPTION_IF_NULL(address->ptr_);
  148. FreeMemFromMemPool(address->ptr_);
  149. address->ptr_ = nullptr;
  150. }
  151. void MemoryManager::FreeMemFromMemPool(void *device_ptr) {
  152. if (device_ptr == nullptr) {
  153. MS_LOG(ERROR) << "FreeMemFromMemPool device_ptr is null.";
  154. }
  155. }
  156. bool MemoryManager::MallocContinuousMemFromMemPool(const DeviceAddressPtrList &addr_list, size_t total_size,
  157. std::vector<size_t> size_list) {
  158. auto device_ptr_list = MallocContinuousMemFromMemPool(total_size, size_list);
  159. if (device_ptr_list.empty()) {
  160. return false;
  161. }
  162. if (addr_list.size() != device_ptr_list.size()) {
  163. MS_LOG(EXCEPTION) << "The size of device list " << addr_list.size() << " is not equal to the size of address list "
  164. << device_ptr_list.size();
  165. }
  166. for (size_t i = 0; i < addr_list.size(); i++) {
  167. MS_EXCEPTION_IF_NULL(device_ptr_list[i]);
  168. MS_EXCEPTION_IF_NULL(addr_list[i]);
  169. addr_list[i]->ptr_ = device_ptr_list[i];
  170. addr_list[i]->size_ = size_list[i];
  171. addr_list[i]->from_mem_pool_ = true;
  172. }
  173. return true;
  174. }
  175. std::vector<void *> MemoryManager::MallocContinuousMemFromMemPool(size_t total_size, std::vector<size_t> size_list) {
  176. if (total_size == 0) {
  177. MS_LOG(ERROR) << "MallocContinuousMemFromMemPool total_size is 0.";
  178. }
  179. std::vector<void *> device_ptr_list;
  180. for (size_t i = 0; i < size_list.size(); ++i) {
  181. device_ptr_list.emplace_back(nullptr);
  182. }
  183. return device_ptr_list;
  184. }
  185. } // namespace device
  186. } // namespace mindspore