• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 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 
17 #include "plugin/device/cpu/hal/device/cpu_memory_manager.h"
18 #include "include/backend/anf_runtime_algorithm.h"
19 #include "include/common/utils/anfalgo.h"
20 #include "utils/ms_context.h"
21 #include "include/common/utils/convert_utils.h"
22 namespace mindspore {
23 namespace device {
24 namespace cpu {
MemMalloc(size_t size)25 uint8_t *CPUMemoryManager::MemMalloc(size_t size) {
26   auto block = std::make_shared<std::vector<uint8_t>>();
27   try {
28     block->resize(size, 0);
29     auto ptr = block->data();
30     mem_block_map_[ptr] = block;
31     return ptr;
32   } catch (const std::exception &e) {
33     MS_LOG(EXCEPTION) << "Malloc memory failed: size " << size;
34   }
35 }
36 
MallocStaticMem(size_t size,bool,uint32_t)37 uint8_t *CPUMemoryManager::MallocStaticMem(size_t size, bool, uint32_t) {
38   auto ptr = MemMalloc(size);
39   static_mem_[ptr] = size;
40   return ptr;
41 }
42 
MallocDynamicMem(size_t size,bool)43 uint8_t *CPUMemoryManager::MallocDynamicMem(size_t size, bool) {
44   void *ptr = nullptr;
45   size_t min_size = 0;
46   // first find the smallest cached_mem_ which fits the size
47   for (auto &&iter : cached_mem_) {
48     if (iter.second >= size) {
49       if (min_size == 0 || iter.second < min_size) {
50         ptr = iter.first;
51         min_size = iter.second;
52       }
53     }
54   }
55   if (ptr != nullptr) {
56     if (memset_s(ptr, size, 0, size) != EOK) {
57       free(ptr);
58       MS_LOG(EXCEPTION) << "Failed to init memory.";
59     }
60     dynamic_mem_[ptr] = min_size;
61     (void)cached_mem_.erase(ptr);
62     return reinterpret_cast<uint8_t *>(ptr);
63   }
64   // if not found, malloc
65   auto new_ptr = MemMalloc(size);
66   dynamic_mem_[new_ptr] = size;
67   return new_ptr;
68 }
69 
ResetDynamicMemory()70 void CPUMemoryManager::ResetDynamicMemory() {
71   // don't free, for multi graph
72   for (auto &&iter : dynamic_mem_) {
73     cached_mem_[iter.first] = iter.second;
74   }
75   dynamic_mem_.clear();
76 }
77 
~CPUMemoryManager()78 CPUMemoryManager::~CPUMemoryManager() { MemFree(); }
79 
MemFree()80 void CPUMemoryManager::MemFree() noexcept {
81   if (mem_ptr_ != nullptr) {
82     mem_ptr_ = nullptr;
83     mem_size_ = 0;
84   }
85   static_mem_.clear();
86   dynamic_mem_.clear();
87   cached_mem_.clear();
88   mem_block_map_.clear();
89 }
90 
AssignMemory(const session::KernelGraph * graph)91 void CPUMemoryManager::AssignMemory(const session::KernelGraph *graph) {
92   size_t graph_mem_size = mem_plan_.MemPlan(graph);
93   if (graph_mem_size > mem_size_) {
94     if (mem_size_ > 0) {
95       dynamic_mem_[mem_ptr_] = mem_size_;
96       mem_size_ = 0;
97     }
98     mem_ptr_ = MemMalloc(graph_mem_size);
99     if (mem_ptr_ != nullptr) {
100       MS_LOG(INFO) << "Simple MemPlan GraphMemSize [" << graph_mem_size << "]";
101       mem_size_ = graph_mem_size;
102       dynamic_malloc_ = false;
103     } else {
104       MS_LOG(INFO) << "Switch to dynamic malloc";
105       dynamic_malloc_ = true;
106     }
107   }
108   if (dynamic_malloc_) {
109     return;
110   }
111   mem_plan_.MemAssign(graph, mem_ptr_);
112 }
113 
StaticMemMalloc(size_t mem_size)114 void *CPUMemoryManager::StaticMemMalloc(size_t mem_size) {
115   auto ptr = MemMalloc(mem_size);
116   if (ptr != nullptr) {
117     static_mem_[ptr] = mem_size;
118     return ptr;
119   } else {
120     MS_LOG(EXCEPTION) << "Malloc memory failed: size " << mem_size;
121   }
122 }
123 
MemFree(void * ptr)124 void CPUMemoryManager::MemFree(void *ptr) {
125   auto iter = static_mem_.find(ptr);
126   if (iter != static_mem_.end()) {
127     (void)static_mem_.erase(iter);
128     auto block_iter = mem_block_map_.find(ptr);
129     if (block_iter != mem_block_map_.end()) {
130       (void)mem_block_map_.erase(block_iter);
131     }
132   }
133 }
134 
IncreaseSummaryRefCount(const session::NamedSummaryOutputs & summary_outputs) const135 void CPUMemoryManager::IncreaseSummaryRefCount(const session::NamedSummaryOutputs &summary_outputs) const {
136   if (!dynamic_malloc_) {
137     return;
138   }
139   if (summary_outputs.empty()) {
140     return;
141   }
142   for (auto &output_item : summary_outputs) {
143     auto node = output_item.second.first;
144     size_t index = IntToSize(output_item.second.second);
145     auto address = AnfAlgo::GetMutableOutputAddr(node, index);
146     MS_EXCEPTION_IF_NULL(address);
147     address->set_ref_count(address->ref_count() + 1);
148   }
149 }
150 
DecreaseSummaryRefCount(const session::NamedSummaryOutputs & summary_outputs)151 void CPUMemoryManager::DecreaseSummaryRefCount(const session::NamedSummaryOutputs &summary_outputs) {
152   if (!dynamic_malloc_) {
153     return;
154   }
155   if (summary_outputs.empty()) {
156     return;
157   }
158   for (auto &output_item : summary_outputs) {
159     auto node = output_item.second.first;
160     size_t index = IntToSize(output_item.second.second);
161     auto address = AnfAlgo::GetMutableOutputAddr(node, index);
162     MS_EXCEPTION_IF_NULL(address);
163     address->DecreaseRefCount();
164     if (address->ref_count() == 0 && address->GetDevicePtr() != nullptr) {
165       MemFree(address->GetDevicePtr());
166       address->SetDevicePtr(nullptr);
167     }
168   }
169 }
170 
IncreaseAddressRefCount(const session::KernelGraph * graph) const171 void CPUMemoryManager::IncreaseAddressRefCount(const session::KernelGraph *graph) const {
172   if (!dynamic_malloc_) {
173     return;
174   }
175   MS_EXCEPTION_IF_NULL(graph);
176   auto kernels = graph->execution_order();
177   for (const auto &kernel : kernels) {
178     MS_EXCEPTION_IF_NULL(kernel);
179     size_t input_num = common::AnfAlgo::GetInputTensorNum(kernel);
180     for (size_t i = 0; i < input_num; ++i) {
181       auto address = AnfAlgo::GetPrevNodeMutableOutputAddr(kernel, i);
182       MS_EXCEPTION_IF_NULL(address);
183       address->set_ref_count(address->ref_count() + 1);
184     }
185     auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
186     MS_EXCEPTION_IF_NULL(kernel_mod);
187     for (size_t i = 0; i < kernel_mod->GetWorkspaceSizeList().size(); ++i) {
188       auto address = AnfAlgo::GetWorkspaceAddr(kernel, i);
189       MS_EXCEPTION_IF_NULL(address);
190       address->set_ref_count(address->ref_count() + 1);
191     }
192   }
193 }
194 
DecreaseAddressRefCount(const AnfNodePtr & kernel)195 void CPUMemoryManager::DecreaseAddressRefCount(const AnfNodePtr &kernel) {
196   if (!dynamic_malloc_) {
197     return;
198   }
199   MS_EXCEPTION_IF_NULL(kernel);
200   size_t input_num = common::AnfAlgo::GetInputTensorNum(kernel);
201   for (size_t i = 0; i < input_num; ++i) {
202     auto address = AnfAlgo::GetPrevNodeMutableOutputAddr(kernel, i);
203     MS_EXCEPTION_IF_NULL(address);
204     address->DecreaseRefCount();
205     if (address->ref_count() == 0 && address->GetDevicePtr() != nullptr) {
206       MemFree(address->GetDevicePtr());
207       address->SetDevicePtr(nullptr);
208     }
209   }
210   auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
211   MS_EXCEPTION_IF_NULL(kernel_mod);
212   for (size_t i = 0; i < kernel_mod->GetWorkspaceSizeList().size(); ++i) {
213     auto address = AnfAlgo::GetWorkspaceAddr(kernel, i);
214     MS_EXCEPTION_IF_NULL(address);
215     address->DecreaseRefCount();
216     if (address->ref_count() == 0 && address->GetDevicePtr() != nullptr) {
217       MemFree(address->GetDevicePtr());
218       address->SetDevicePtr(nullptr);
219     }
220   }
221 }
222 }  // namespace cpu
223 }  // namespace device
224 }  // namespace mindspore
225