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 17 #ifndef MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_GPU_MEMORY_ALLOCATOR_H_ 18 #define MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_GPU_MEMORY_ALLOCATOR_H_ 19 20 #include <memory> 21 #include "runtime/device/gpu/cuda_driver.h" 22 #include "backend/optimizer/mem_reuse/mem_dynamic_allocator.h" 23 24 namespace mindspore { 25 namespace device { 26 namespace gpu { 27 class GPUMemoryAllocator : public DynamicMemPoolBestFit { 28 public: 29 ~GPUMemoryAllocator() override = default; 30 bool Init(); 31 void CheckMaxDeviceMemory() const; 32 bool Finalize(); 33 bool AllocBufferQueueMem(size_t size, DeviceMemPtr *addr); 34 35 size_t AllocDeviceMem(size_t size, DeviceMemPtr *addr) override; 36 bool FreeDeviceMem(const DeviceMemPtr &addr) override; 37 size_t free_mem_size() override; 38 size_t total_mem_size() override; 39 GetInstance()40 static GPUMemoryAllocator &GetInstance() { 41 static GPUMemoryAllocator instance; 42 return instance; 43 } 44 45 private: 46 GPUMemoryAllocator() = default; 47 GPUMemoryAllocator(const GPUMemoryAllocator &) = delete; 48 GPUMemoryAllocator &operator=(const GPUMemoryAllocator &) = delete; 49 50 // Used to track address of data buffer queue. 51 DeviceMemPtr buffer_q_addr_{nullptr}; 52 53 float limited_device_memory_{0.0}; 54 size_t total_used_device_memory_{0}; 55 size_t available_device_memory_{0}; 56 }; 57 } // namespace gpu 58 } // namespace device 59 } // namespace mindspore 60 61 #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_GPU_MEMORY_ALLOCATOR_H_ 62