|
|
|
@ -38,7 +38,8 @@ BuddyAllocator* GetCPUBuddyAllocator() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void* Alloc<platform::CPUPlace>(platform::CPUPlace place, size_t size) {
|
|
|
|
|
void* Alloc<platform::CPUPlace>(platform::CPUPlace place, size_t size,
|
|
|
|
|
bool use_pinned) {
|
|
|
|
|
VLOG(10) << "Allocate " << size << " bytes on " << platform::Place(place);
|
|
|
|
|
void* p = GetCPUBuddyAllocator()->Alloc(size);
|
|
|
|
|
VLOG(10) << " pointer=" << p;
|
|
|
|
@ -46,7 +47,8 @@ void* Alloc<platform::CPUPlace>(platform::CPUPlace place, size_t size) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void Free<platform::CPUPlace>(platform::CPUPlace place, void* p) {
|
|
|
|
|
void Free<platform::CPUPlace>(platform::CPUPlace place, void* p,
|
|
|
|
|
bool use_pinned) {
|
|
|
|
|
VLOG(10) << "Free pointer=" << p << " on " << platform::Place(place);
|
|
|
|
|
GetCPUBuddyAllocator()->Free(p);
|
|
|
|
|
}
|
|
|
|
@ -82,15 +84,47 @@ BuddyAllocator* GetGPUBuddyAllocator(int gpu_id) {
|
|
|
|
|
return as[gpu_id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BuddyAllocator* GetCUDAPinnedBuddyAllocator(int gpu_id) {
|
|
|
|
|
static BuddyAllocator** as = NULL;
|
|
|
|
|
if (as == NULL) {
|
|
|
|
|
int gpu_num = platform::GetCUDADeviceCount();
|
|
|
|
|
as = new BuddyAllocator*[gpu_num];
|
|
|
|
|
for (int gpu = 0; gpu < gpu_num; gpu++) {
|
|
|
|
|
as[gpu] = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
platform::SetDeviceId(gpu_id);
|
|
|
|
|
if (!as[gpu_id]) {
|
|
|
|
|
as[gpu_id] = new BuddyAllocator(new detail::CUDAPinnedAllocator,
|
|
|
|
|
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 GFlags environment variable '"
|
|
|
|
|
<< "FLAGS_fraction_of_gpu_memory_to_use"
|
|
|
|
|
<< "' to change the fraction of GPU usage.\n\n";
|
|
|
|
|
}
|
|
|
|
|
return as[gpu_id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
size_t Used<platform::CUDAPlace>(platform::CUDAPlace place) {
|
|
|
|
|
return GetGPUBuddyAllocator(place.device)->Used();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void* Alloc<platform::CUDAPlace>(platform::CUDAPlace place, size_t size) {
|
|
|
|
|
auto* buddy_allocator = GetGPUBuddyAllocator(place.device);
|
|
|
|
|
auto* ptr = buddy_allocator->Alloc(size);
|
|
|
|
|
void* Alloc<platform::CUDAPlace>(platform::CUDAPlace place, size_t size,
|
|
|
|
|
bool use_pinned) {
|
|
|
|
|
void* ptr;
|
|
|
|
|
if (use_pinned) {
|
|
|
|
|
auto* buddy_allocator = GetCUDAPinnedBuddyAllocator(place.device);
|
|
|
|
|
ptr = buddy_allocator->Alloc(size);
|
|
|
|
|
} else {
|
|
|
|
|
auto* buddy_allocator = GetGPUBuddyAllocator(place.device);
|
|
|
|
|
ptr = buddy_allocator->Alloc(size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ptr == nullptr) {
|
|
|
|
|
int cur_dev = platform::GetCurrentDeviceId();
|
|
|
|
|
platform::SetDeviceId(place.device);
|
|
|
|
@ -108,8 +142,13 @@ void* Alloc<platform::CUDAPlace>(platform::CUDAPlace place, size_t size) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void Free<platform::CUDAPlace>(platform::CUDAPlace place, void* p) {
|
|
|
|
|
GetGPUBuddyAllocator(place.device)->Free(p);
|
|
|
|
|
void Free<platform::CUDAPlace>(platform::CUDAPlace place, void* p,
|
|
|
|
|
bool use_pinned) {
|
|
|
|
|
if (use_pinned) {
|
|
|
|
|
GetCUDAPinnedBuddyAllocator(place.device)->Free(p);
|
|
|
|
|
} else {
|
|
|
|
|
GetGPUBuddyAllocator(place.device)->Free(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|