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_ASCEND_ASCEND_MEMORY_POOL_H_ 18 #define MINDSPORE_CCSRC_RUNTIME_DEVICE_ASCEND_ASCEND_MEMORY_POOL_H_ 19 20 #include <memory> 21 #include "backend/optimizer/mem_reuse/mem_dynamic_allocator.h" 22 23 namespace mindspore { 24 namespace device { 25 namespace ascend { 26 class AscendMemoryPool : public DynamicMemPoolBestFit { 27 public: 28 ~AscendMemoryPool() override = default; 29 AscendMemoryPool(const AscendMemoryPool &) = delete; 30 AscendMemoryPool &operator=(const AscendMemoryPool &) = delete; 31 32 void Init(uint8_t *device_mem_base, uint64_t device_mem_size, uint64_t dynamic_mem_offset); 33 size_t AllocDeviceMem(size_t size, DeviceMemPtr *addr) override; 34 bool FreeDeviceMem(const DeviceMemPtr &addr) override; 35 void ResetIdleMemBuf(); 36 void set_device_mem_size(uint64_t device_mem_size); 37 void set_device_mem_pool_base(uint8_t *device_mem_pool_base); 38 void set_device_mem_pool_offset(uint64_t device_mem_pool_offset); 39 void set_graph_dynamic_mem_offset(uint64_t graph_dynamic_mem_offset); 40 41 uint64_t device_mem_pool_offset() const; 42 size_t free_mem_size() override; 43 size_t total_mem_size() override; 44 GetInstance()45 static AscendMemoryPool &GetInstance() { 46 static AscendMemoryPool instance; 47 return instance; 48 } 49 50 protected: 51 // The real size by memory alloc aligned. 52 size_t AlignMemorySize(size_t size) const override; 53 // Calculate memory block required alloc size when adding the memory block. 54 size_t CalMemBlockAllocSize(size_t size) override; 55 56 private: 57 AscendMemoryPool() = default; 58 uint8_t *device_mem_pool_base_{nullptr}; 59 uint64_t device_mem_size_{0}; 60 uint64_t device_mem_pool_offset_{0}; 61 uint64_t graph_dynamic_mem_offset_{0}; 62 }; 63 } // namespace ascend 64 } // namespace device 65 } // namespace mindspore 66 67 #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_ASCEND_ASCEND_MEMORY_POOL_H_ 68