• 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 #ifndef MINDSPORE_CCSRC_RUNTIME_DEVICE_CPU_CPU_MEMORY_MANAGER_H_
18 #define MINDSPORE_CCSRC_RUNTIME_DEVICE_CPU_CPU_MEMORY_MANAGER_H_
19 #include <vector>
20 #include <map>
21 #include <memory>
22 #include "include/backend/kernel_graph.h"
23 #include "backend/common/session/session_basic.h"
24 #include "include/backend/device_address.h"
25 #include "runtime/device/memory_manager.h"
26 #include "plugin/device/cpu/hal/device/cpu_simple_mem_plan.h"
27 #include "plugin/device/cpu/hal/hardware/cpu_memory_pool.h"
28 
29 namespace mindspore {
30 namespace device {
31 namespace cpu {
32 class BACKEND_EXPORT CPUMemoryManager : public MemoryManager {
33  public:
34   CPUMemoryManager() = default;
35   virtual ~CPUMemoryManager();
36 
Initialize()37   void Initialize() override { memory_pool_ = &(CPUMemoryPool::GetInstance()); }
Finalize()38   void Finalize() override { CPUMemoryPool::GetInstance().ReleaseDeviceRes(); }
39   void ResetDynamicMemory() override;
40 
41   void AssignMemory(const session::KernelGraph *graph);
42   void IncreaseAddressRefCount(const session::KernelGraph *graph) const;
43   void DecreaseAddressRefCount(const AnfNodePtr &kernel);
44   void *StaticMemMalloc(size_t mem_size);
45   void MemFree(void *ptr);
46   void IncreaseSummaryRefCount(const session::NamedSummaryOutputs &summary_outputs) const;
47   void DecreaseSummaryRefCount(const session::NamedSummaryOutputs &summary_outputs);
48 
49   void *MallocMemFromMemPool(size_t size, bool from_persistent_mem, bool need_recycle = false,
50                              uint32_t stream_id = kDefaultStreamIndex) override {
51     return CPUMemoryPool::GetInstance().AllocTensorMem(size, from_persistent_mem, false, stream_id);
52   }
FreeMemFromMemPool(void * device_ptr)53   void FreeMemFromMemPool(void *device_ptr) override { CPUMemoryPool::GetInstance().FreeTensorMem(device_ptr); }
54   std::vector<void *> MallocContinuousMemFromMemPool(const std::vector<size_t> &size_list,
55                                                      uint32_t stream_id = kDefaultStreamIndex) override {
56     return CPUMemoryPool::GetInstance().AllocContinuousTensorMem(size_list, stream_id);
57   }
58 
59  protected:
60   uint8_t *MallocStaticMem(size_t size, bool communication_mem, uint32_t graph_id) override;
61   uint8_t *MallocDynamicMem(size_t size, bool communication_mem) override;
62 
63  private:
64   uint8_t *MemMalloc(size_t size);
65   void MemFree() noexcept;
66   CPUSimpleMemPlan mem_plan_;
67 
68   size_t mem_size_{0};
69   uint8_t *mem_ptr_{nullptr};
70   bool dynamic_malloc_{false};
71   std::map<void *, size_t> dynamic_mem_;
72   std::map<void *, size_t> static_mem_;
73   std::map<void *, size_t> cached_mem_;
74   std::map<void *, std::shared_ptr<std::vector<uint8_t>>> mem_block_map_;
75 };
76 }  // namespace cpu
77 }  // namespace device
78 }  // namespace mindspore
79 #endif  // MINDSPORE_CCSRC_RUNTIME_DEVICE_CPU_CPU_MEMORY_MANAGER_H_
80