This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
132 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,93 @@ | ||
/*! | ||
* Copyright (c) 2015 by Contributors | ||
*/ | ||
#include "mxnet/storage.h" | ||
#include <mshadow/tensor.h> | ||
#include <mxnet/storage.h> | ||
#include <dmlc/logging.h> | ||
#include "./storage_manager.h" | ||
#include "./naive_storage_manager.h" | ||
#include "./pooled_storage_manager.h" | ||
#include "./cpu_storage.h" | ||
#include "./gpu_storage.h" | ||
#include "mxnet/cuda_utils.h" | ||
|
||
namespace mxnet { | ||
|
||
// class NaiveStorageManager : public StorageManager { | ||
// public: | ||
// virtual Handle Alloc(size_t size, Context ctx); | ||
// virtual void Free(Handle handle); | ||
// }; | ||
struct Storage::Impl { | ||
static constexpr size_t kPoolThreshold = 4096 * 1024 * 1024ul; | ||
|
||
template <class DeviceStorage> | ||
using CurrentStorageManager = storage::PooledStorageManager<DeviceStorage, kPoolThreshold>; | ||
|
||
static void ActivateDevice(Context ctx) { | ||
switch (ctx.dev_mask) { | ||
case cpu::kDevMask: | ||
break; | ||
case gpu::kDevMask: | ||
#if MXNET_USE_CUDA | ||
CUDA_CALL(cudaSetDevice(ctx.dev_id)); | ||
#else // MXNET_USE_CUDA | ||
LOG(FATAL) << "Please compile with CUDA enabled"; | ||
#endif // MXNET_USE_CUDA | ||
break; | ||
default: | ||
LOG(FATAL) << "Unimplemented device"; | ||
} | ||
} | ||
|
||
std::unordered_map< | ||
int, std::unordered_map<int, std::unique_ptr<storage::StorageManager>>> | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
hotpxl
Author
Contributor
|
||
storage_managers; | ||
}; // struct Storage::Impl | ||
|
||
StorageManager::Handle StorageManager::Alloc(size_t size, Context ctx) { | ||
Storage::Handle Storage::Alloc(size_t size, Context ctx) { | ||
Handle hd; | ||
hd.ctx = ctx; | ||
if (ctx.dev_mask == cpu::kDevMask) { | ||
hd.dptr = calloc(size, sizeof(real_t)); | ||
} else { | ||
#if MXNET_USE_CUDA | ||
cudaMalloc(&hd.dptr, size); | ||
#endif | ||
auto&& device = impl_->storage_managers[ctx.dev_mask]; | ||
auto&& device_id_it = device.find(ctx.dev_id); | ||
// Allocate device if necessary. | ||
if (device_id_it == device.end()) { | ||
switch (ctx.dev_mask) { | ||
case cpu::kDevMask: | ||
device_id_it = | ||
device.emplace(std::make_pair( | ||
ctx.dev_id, | ||
std::unique_ptr<storage::StorageManager>{ | ||
new Storage::Impl::CurrentStorageManager< | ||
storage::CpuStorage>{}})).first; | ||
break; | ||
case gpu::kDevMask: | ||
device_id_it = | ||
device.emplace(std::make_pair( | ||
ctx.dev_id, | ||
std::unique_ptr<storage::StorageManager>{ | ||
new Storage::Impl::CurrentStorageManager< | ||
storage::GpuStorage>{}})).first; | ||
break; | ||
default: | ||
LOG(FATAL) << "Unimplemented device"; | ||
} | ||
} | ||
Impl::ActivateDevice(ctx); | ||
hd.dptr = device_id_it->second->Alloc(size); | ||
hd.size = size; | ||
return hd; | ||
} | ||
|
||
void StorageManager::Free(StorageManager::Handle handle) { | ||
if (handle.ctx.dev_mask == cpu::kDevMask) { | ||
free(handle.dptr); | ||
handle.dptr = NULL; | ||
// cudaFreeHost(handle.dptr); | ||
} else { | ||
#if MXNET_USE_CUDA | ||
cudaFree(handle.dptr); | ||
#endif | ||
} | ||
void Storage::Free(Storage::Handle handle) { | ||
Impl::ActivateDevice(handle.ctx); | ||
impl_->storage_managers.at(handle.ctx.dev_mask) | ||
.at(handle.ctx.dev_id) | ||
->Free(handle.dptr, handle.size); | ||
} | ||
|
||
StorageManager* StorageManager::Get() { | ||
static StorageManager inst; | ||
Storage::~Storage() = default; | ||
|
||
Storage* Storage::Get() { | ||
static Storage inst; | ||
return &inst; | ||
} | ||
|
||
Storage::Storage() : impl_{new Impl{}} {} | ||
|
||
} // namespace mxnet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
consider switch two unordered_map to vector. Since the device id is likely to be consecutive and regular, having two hash maps here may not be ideal for quick lookups.