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.5 kB

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