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.

cpu_resource_manager.cc 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "device/cpu/cpu_resource_manager.h"
  17. #include "session/anf_runtime_algorithm.h"
  18. namespace mindspore {
  19. namespace device {
  20. namespace cpu {
  21. CPUResourceManager::~CPUResourceManager() { MemFree(); }
  22. void CPUResourceManager::MemFree() {
  23. if (mem_ptr_ != nullptr) {
  24. free(mem_ptr_);
  25. mem_ptr_ = nullptr;
  26. mem_size_ = 0;
  27. }
  28. for (auto &&iter : dynamic_mem_) {
  29. free(iter.first);
  30. }
  31. dynamic_mem_.clear();
  32. }
  33. void CPUResourceManager::MemPlan(const session::KernelGraph *graph) {
  34. mem_plan_.MemPlan(graph);
  35. size_t graph_mem_size = mem_plan_.GetGraphMemSize(graph);
  36. if (graph_mem_size > mem_size_) {
  37. MemFree();
  38. mem_ptr_ = reinterpret_cast<uint8_t *>(malloc(graph_mem_size));
  39. if (mem_ptr_ != nullptr) {
  40. mem_size_ = graph_mem_size;
  41. dynamic_malloc_ = false;
  42. } else {
  43. MS_LOG(INFO) << "Switch to dynamic malloc";
  44. dynamic_malloc_ = true;
  45. }
  46. }
  47. }
  48. void CPUResourceManager::MemMalloc(const session::KernelGraph *graph) {
  49. if (dynamic_malloc_) {
  50. return;
  51. }
  52. mem_plan_.MemAssign(graph, mem_ptr_);
  53. }
  54. void *CPUResourceManager::MemMalloc(size_t mem_size) {
  55. void *ptr = malloc(mem_size);
  56. if (ptr != nullptr) {
  57. memset_s(ptr, mem_size, 0, mem_size);
  58. dynamic_mem_[ptr] = mem_size;
  59. return ptr;
  60. } else {
  61. MS_LOG(EXCEPTION) << "Malloc memory failed: size " << mem_size;
  62. }
  63. }
  64. void CPUResourceManager::MemFree(void *ptr) {
  65. auto iter = dynamic_mem_.find(ptr);
  66. if (iter != dynamic_mem_.end()) {
  67. (void)dynamic_mem_.erase(iter);
  68. free(ptr);
  69. }
  70. }
  71. void CPUResourceManager::IncreaseSummaryRefCount(const session::NamedSummaryOutputs &summary_outputs) {
  72. if (!dynamic_malloc_) {
  73. return;
  74. }
  75. if (summary_outputs.empty()) {
  76. return;
  77. }
  78. for (auto &output_item : summary_outputs) {
  79. auto node = output_item.second.first;
  80. size_t index = IntToSize(output_item.second.second);
  81. auto address = AnfAlgo::GetMutableOutputAddr(node, index);
  82. MS_EXCEPTION_IF_NULL(address);
  83. address->ref_count_++;
  84. }
  85. }
  86. void CPUResourceManager::DecreaseSummaryRefCount(const session::NamedSummaryOutputs &summary_outputs) {
  87. if (!dynamic_malloc_) {
  88. return;
  89. }
  90. if (summary_outputs.empty()) {
  91. return;
  92. }
  93. for (auto &output_item : summary_outputs) {
  94. auto node = output_item.second.first;
  95. size_t index = IntToSize(output_item.second.second);
  96. auto address = AnfAlgo::GetMutableOutputAddr(node, index);
  97. MS_EXCEPTION_IF_NULL(address);
  98. address->ref_count_--;
  99. if (address->ref_count_ == 0 && address->ptr_ != nullptr) {
  100. MemFree(address->ptr_);
  101. address->ptr_ = nullptr;
  102. }
  103. }
  104. }
  105. void CPUResourceManager::IncreaseAddressRefCount(const session::KernelGraph *graph) {
  106. if (!dynamic_malloc_) {
  107. return;
  108. }
  109. MS_EXCEPTION_IF_NULL(graph);
  110. auto kernels = graph->execution_order();
  111. for (const auto &kernel : kernels) {
  112. MS_EXCEPTION_IF_NULL(kernel);
  113. size_t input_num = AnfAlgo::GetInputTensorNum(kernel);
  114. for (size_t i = 0; i < input_num; ++i) {
  115. auto address = AnfAlgo::GetPrevNodeMutableOutputAddr(kernel, i);
  116. MS_EXCEPTION_IF_NULL(address);
  117. address->ref_count_++;
  118. }
  119. auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
  120. MS_EXCEPTION_IF_NULL(kernel_mod);
  121. for (size_t i = 0; i < kernel_mod->GetWorkspaceSizeList().size(); ++i) {
  122. auto address = AnfAlgo::GetWorkspaceAddr(kernel, i);
  123. MS_EXCEPTION_IF_NULL(address);
  124. address->ref_count_++;
  125. }
  126. }
  127. }
  128. void CPUResourceManager::DecreaseAddressRefCount(const AnfNodePtr &kernel) {
  129. if (!dynamic_malloc_) {
  130. return;
  131. }
  132. MS_EXCEPTION_IF_NULL(kernel);
  133. size_t input_num = AnfAlgo::GetInputTensorNum(kernel);
  134. for (size_t i = 0; i < input_num; ++i) {
  135. auto address = AnfAlgo::GetPrevNodeMutableOutputAddr(kernel, i);
  136. MS_EXCEPTION_IF_NULL(address);
  137. address->ref_count_--;
  138. if (address->ref_count_ == 0 && address->ptr_ != nullptr) {
  139. MemFree(address->ptr_);
  140. address->ptr_ = nullptr;
  141. }
  142. }
  143. auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
  144. MS_EXCEPTION_IF_NULL(kernel_mod);
  145. for (size_t i = 0; i < kernel_mod->GetWorkspaceSizeList().size(); ++i) {
  146. auto address = AnfAlgo::GetWorkspaceAddr(kernel, i);
  147. MS_EXCEPTION_IF_NULL(address);
  148. address->ref_count_--;
  149. if (address->ref_count_ == 0 && address->ptr_ != nullptr) {
  150. MemFree(address->ptr_);
  151. address->ptr_ = nullptr;
  152. }
  153. }
  154. }
  155. } // namespace cpu
  156. } // namespace device
  157. } // namespace mindspore