From 3a0a4586a3129bdc1100276236bc1cc50227fa8f Mon Sep 17 00:00:00 2001 From: QI JUN Date: Thu, 7 Dec 2017 20:24:36 +0800 Subject: [PATCH] refine GPU memory allocation policy (#6373) * fix gpu memory allocation policy * refine codes * fix code style * follow comments --- paddle/memory/detail/system_allocator.cc | 2 +- paddle/memory/memory.cc | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/paddle/memory/detail/system_allocator.cc b/paddle/memory/detail/system_allocator.cc index 6b4e46f56a0..b543b767e80 100644 --- a/paddle/memory/detail/system_allocator.cc +++ b/paddle/memory/detail/system_allocator.cc @@ -83,7 +83,7 @@ void* GPUAllocator::Alloc(size_t& index, size_t size) { paddle::platform::GpuMemoryUsage(available, capacity); // Reserve memory for page tables, etc. - size_t reserving = capacity - paddle::platform::GpuMaxAllocSize(); + size_t reserving = 0.05 * capacity + paddle::platform::GpuMinChunkSize(); size_t usable = available > reserving ? available - reserving : 0; // If remaining size no less than expected size, using general diff --git a/paddle/memory/memory.cc b/paddle/memory/memory.cc index 95cfe2525e3..9cafdfda75d 100644 --- a/paddle/memory/memory.cc +++ b/paddle/memory/memory.cc @@ -64,19 +64,21 @@ BuddyAllocator* GetGPUBuddyAllocator(int gpu_id) { int gpu_num = platform::GetCUDADeviceCount(); as = new BuddyAllocator*[gpu_num]; for (int gpu = 0; gpu < gpu_num; gpu++) { - platform::SetDeviceId(gpu); - as[gpu] = new BuddyAllocator(new detail::GPUAllocator, - platform::GpuMinChunkSize(), - platform::GpuMaxChunkSize()); + as[gpu] = nullptr; } + } + platform::SetDeviceId(gpu_id); + if (!as[gpu_id]) { + as[gpu_id] = new BuddyAllocator(new detail::GPUAllocator, + platform::GpuMinChunkSize(), + platform::GpuMaxChunkSize()); VLOG(10) << "\n\nNOTE: each GPU device use " << FLAGS_fraction_of_gpu_memory_to_use * 100 << "% of GPU memory.\n" - << "You can set environment variable '" - << platform::kEnvFractionGpuMemoryToUse + << "You can set GFlags environment variable '" + << "FLAGS_fraction_of_gpu_memory_to_use" << "' to change the fraction of GPU usage.\n\n"; } - platform::SetDeviceId(gpu_id); return as[gpu_id]; } -- GitLab