Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
skip memory allocation when handle size is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxihu committed Mar 20, 2019
1 parent 00d59af commit 02a0fec
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/storage/cpu_device_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ class CPUDeviceStorage {
}; // class CPUDeviceStorage

inline void* CPUDeviceStorage::Alloc(Storage::Handle* handle) {
void* ptr = nullptr;
const size_t size = handle->size;
void* ptr;
if (size == 0) return ptr;

#if _MSC_VER
ptr = _aligned_malloc(size, alignment_);
if (ptr == NULL) LOG(FATAL) << "Failed to allocate CPU Memory";
if (ptr == nullptr) LOG(FATAL) << "Failed to allocate CPU Memory";
#else
int ret = posix_memalign(&ptr, alignment_, size);
if (ret != 0) LOG(FATAL) << "Failed to allocate CPU Memory";
Expand All @@ -77,7 +79,7 @@ inline void* CPUDeviceStorage::Alloc(Storage::Handle* handle) {
}

inline void CPUDeviceStorage::Free(Storage::Handle handle) {
void * ptr = handle.dptr;
void* ptr = handle.dptr;
#if _MSC_VER
_aligned_free(ptr);
#else
Expand Down
7 changes: 6 additions & 1 deletion src/storage/cpu_shared_storage_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,18 @@ class CPUSharedStorageManager final : public StorageManager {
}; // class CPUSharedStorageManager

void CPUSharedStorageManager::Alloc(Storage::Handle* handle) {
if (handle->size == 0) {
handle->dptr = nullptr;
return;
}

std::lock_guard<std::recursive_mutex> lock(mutex_);
std::uniform_int_distribution<> dis(0, std::numeric_limits<int>::max());
int fid = -1;
std::string filename;
bool is_new = false;
size_t size = handle->size + alignment_;
void *ptr = nullptr;
void* ptr = nullptr;
#ifdef _WIN32
CheckAndRealFree();
HANDLE map_handle = nullptr;
Expand Down
6 changes: 4 additions & 2 deletions src/storage/gpu_device_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ class GPUDeviceStorage {
}; // class GPUDeviceStorage

inline void* GPUDeviceStorage::Alloc(Storage::Handle* handle) {
const size_t size = handle->size;
void* ret = nullptr;
const size_t size = handle->size;
if (size == 0) return ret;

#if MXNET_USE_CUDA
mxnet::common::cuda::DeviceStore device_store(handle->ctx.real_dev_id(), true);
#if MXNET_USE_NCCL
Expand All @@ -73,7 +75,7 @@ inline void* GPUDeviceStorage::Alloc(Storage::Handle* handle) {

inline void GPUDeviceStorage::Free(Storage::Handle handle) {
#if MXNET_USE_CUDA
void * ptr = handle.dptr;
void* ptr = handle.dptr;
mxnet::common::cuda::DeviceStore device_store(handle.ctx.real_dev_id(), true);
#if MXNET_USE_NCCL
std::lock_guard<std::mutex> l(Storage::Get()->GetMutex(Context::kGPU));
Expand Down
4 changes: 3 additions & 1 deletion src/storage/pinned_memory_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class PinnedMemoryStorage {
inline void* PinnedMemoryStorage::Alloc(Storage::Handle* handle) {
void* ret = nullptr;
const size_t size = handle->size;
if (size == 0) return ret;

#if MXNET_USE_NCCL
std::lock_guard<std::mutex> lock(Storage::Get()->GetMutex(Context::kGPU));
#endif
Expand All @@ -63,7 +65,7 @@ inline void* PinnedMemoryStorage::Alloc(Storage::Handle* handle) {
}

inline void PinnedMemoryStorage::Free(Storage::Handle handle) {
void * ptr = handle.dptr;
void* ptr = handle.dptr;
#if MXNET_USE_NCCL
std::lock_guard<std::mutex> lock(Storage::Get()->GetMutex(Context::kGPU));
#endif
Expand Down
12 changes: 12 additions & 0 deletions src/storage/pooled_storage_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ class GPUPooledStorageManager final : public StorageManager {
}; // class GPUPooledStorageManager

void GPUPooledStorageManager::Alloc(Storage::Handle* handle) {
// Set dptr to nullptr when handle size is 0.
if (handle->size == 0) {
handle->dptr = nullptr;
return;
}

std::lock_guard<std::mutex> lock(Storage::Get()->GetMutex(Context::kGPU));
size_t size = RoundAllocSize(handle->size);
auto&& reuse_it = memory_pool_.find(size);
Expand Down Expand Up @@ -290,6 +296,12 @@ class GPUPooledRoundedStorageManager final : public StorageManager {
}; // class GPUPooledRoundedStorageManager

void GPUPooledRoundedStorageManager::Alloc(Storage::Handle* handle) {
// Set dptr to nullptr when handle size is 0.
if (handle->size == 0) {
handle->dptr = nullptr;
return;
}

std::lock_guard<std::mutex> lock(Storage::Get()->GetMutex(Context::kGPU));
int bucket = get_bucket(handle->size);
size_t size = get_size(bucket);
Expand Down

0 comments on commit 02a0fec

Please sign in to comment.