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 <string> 22 #include "utils/hash_map.h" 23 #include "include/backend/mem_reuse/mem_dynamic_allocator.h" 24 25 namespace mindspore { 26 namespace device { 27 namespace ascend { 28 class AscendMemoryPool : public DynamicMemPoolBestFit { 29 public: 30 ~AscendMemoryPool() override = default; 31 AscendMemoryPool(const AscendMemoryPool &) = delete; 32 AscendMemoryPool &operator=(const AscendMemoryPool &) = delete; 33 34 size_t AllocDeviceMem(size_t size, DeviceMemPtr *addr) override; 35 bool FreeDeviceMem(const DeviceMemPtr &addr) override; 36 size_t MmapDeviceMem(const size_t size, const DeviceMemPtr addr) override; 37 size_t GetMaxUsedMemSize() const override; 38 size_t free_mem_size() override; 39 uint64_t total_mem_size() const override; GetMemoryPoolType()40 std::string GetMemoryPoolType() const override { return "Ascend"; } 41 // Set mem pool block size 42 void SetMemPoolBlockSize(size_t available_device_mem_size) override; 43 44 void ResetIdleMemBuf() const; 45 46 // The main program entry of memory alloc. 47 DeviceMemPtr AllocOverflowTensorMem(size_t size, bool from_persistent_mem = false); 48 GetInstance()49 static AscendMemoryPool &GetInstance() { 50 static AscendMemoryPool instance; 51 return instance; 52 } 53 54 protected: 55 // Calculate memory block required alloc size when adding the memory block. 56 size_t CalMemBlockAllocSize(size_t size, bool from_persistent_mem, bool need_recycle) override; 57 58 // The related interface of device memory eager free. 59 const bool IsEnableEagerFree() const override; 60 const bool SyncAllStreams() override; 61 size_t AllocDeviceMemByEagerFree(size_t size, DeviceMemPtr *addr) override; 62 size_t FreeDeviceMemByEagerFree(const DeviceMemPtr addr, const size_t size) override; 63 64 private: 65 AscendMemoryPool(); 66 std::mutex mutex_; 67 // overflow memory info, key is kernel, val is memory ptr 68 mindspore::HashMap<std::string, void *> overflow_memory_info_map_; 69 }; 70 } // namespace ascend 71 } // namespace device 72 } // namespace mindspore 73 74 #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_ASCEND_ASCEND_MEMORY_POOL_H_ 75