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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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::ResetAddressRefCount(const session::KernelGraph *graph) {
  72. if (!dynamic_malloc_) {
  73. return;
  74. }
  75. MS_EXCEPTION_IF_NULL(graph);
  76. auto kernels = graph->execution_order();
  77. for (const auto &kernel : kernels) {
  78. MS_EXCEPTION_IF_NULL(kernel);
  79. size_t input_num = AnfAlgo::GetInputTensorNum(kernel);
  80. for (size_t i = 0; i < input_num; ++i) {
  81. auto address = AnfAlgo::GetPrevNodeMutableOutputAddr(kernel, i);
  82. MS_EXCEPTION_IF_NULL(address);
  83. address->ref_count_++;
  84. }
  85. auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
  86. MS_EXCEPTION_IF_NULL(kernel_mod);
  87. for (size_t i = 0; i < kernel_mod->GetWorkspaceSizeList().size(); ++i) {
  88. auto address = AnfAlgo::GetWorkspaceAddr(kernel, i);
  89. MS_EXCEPTION_IF_NULL(address);
  90. address->ref_count_++;
  91. }
  92. }
  93. }
  94. void CPUResourceManager::DecreaseAddressRefCount(const AnfNodePtr &kernel) {
  95. if (!dynamic_malloc_) {
  96. return;
  97. }
  98. MS_EXCEPTION_IF_NULL(kernel);
  99. size_t input_num = AnfAlgo::GetInputTensorNum(kernel);
  100. for (size_t i = 0; i < input_num; ++i) {
  101. auto address = AnfAlgo::GetPrevNodeMutableOutputAddr(kernel, i);
  102. MS_EXCEPTION_IF_NULL(address);
  103. address->ref_count_--;
  104. if (address->ref_count_ == 0 && address->ptr_ != nullptr) {
  105. MemFree(address->ptr_);
  106. address->ptr_ = nullptr;
  107. }
  108. }
  109. auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
  110. MS_EXCEPTION_IF_NULL(kernel_mod);
  111. for (size_t i = 0; i < kernel_mod->GetWorkspaceSizeList().size(); ++i) {
  112. auto address = AnfAlgo::GetWorkspaceAddr(kernel, i);
  113. MS_EXCEPTION_IF_NULL(address);
  114. address->ref_count_--;
  115. if (address->ref_count_ == 0 && address->ptr_ != nullptr) {
  116. MemFree(address->ptr_);
  117. address->ptr_ = nullptr;
  118. }
  119. }
  120. }
  121. } // namespace cpu
  122. } // namespace device
  123. } // namespace mindspore