14 #include <unordered_map>
44 }
else if (src_ptr ==
nullptr || dst_ptr ==
nullptr) {
48 std::shared_ptr<MemoryManagerDevice> device_mm;
50 if (src_device.
IsCPU() && dst_device.
IsCPU()) {
54 else if (src_device.
IsCPU() && dst_device.
IsCUDA()) {
56 }
else if (src_device.
IsCUDA() && dst_device.
IsCPU()) {
62 else if (src_device.
IsCPU() && dst_device.
IsSYCL()) {
64 }
else if (src_device.
IsSYCL() && dst_device.
IsCPU()) {
75 device_mm->Memcpy(dst_ptr, dst_device, src_ptr, src_device, num_bytes);
83 Memcpy(dst_ptr, dst_device, host_ptr,
Device(
"CPU:0"), num_bytes);
91 Memcpy(host_ptr,
Device(
"CPU:0"), src_ptr, src_device, num_bytes);
97 std::shared_ptr<MemoryManagerDevice>,
99 map_device_type_to_memory_manager = {
101 std::make_shared<MemoryManagerCPU>()},
102 #ifdef BUILD_CUDA_MODULE
103 #ifdef ENABLE_CACHED_CUDA_MANAGER
105 std::make_shared<MemoryManagerCached>(
106 std::make_shared<MemoryManagerCUDA>())},
109 std::make_shared<MemoryManagerCUDA>()},
112 #ifdef BUILD_SYCL_MODULE
114 std::make_shared<MemoryManagerSYCL>()},
118 if (map_device_type_to_memory_manager.find(device.
GetType()) ==
119 map_device_type_to_memory_manager.end()) {
121 "Unsupported device \"{}\". Set BUILD_CUDA_MODULE=ON to "
122 "compile for CUDA support and BUILD_SYCL_MODULE=ON to compile "
126 return map_device_type_to_memory_manager.at(device.
GetType());
bool IsCUDA() const
Returns true iff device type is CUDA.
DeviceType GetType() const
Returns type of the device, e.g. DeviceType::CPU, DeviceType::CUDA.
bool IsCPU() const
Returns true iff device type is CPU.
DeviceType
Type for device.
std::string ToString() const
Returns string representation of device, e.g. "CPU:0", "CUDA:0".
bool IsSYCL() const
Returns true iff device type is SYCL GPU.
void CountFree(void *ptr, const Device &device)
static MemoryManagerStatistic & GetInstance()
void CountMalloc(void *ptr, size_t byte_size, const Device &device)
Adds the given allocation to the statistics.
static void MemcpyFromHost(void *dst_ptr, const Device &dst_device, const void *host_ptr, size_t num_bytes)
Same as Memcpy, but with host (CPU:0) as default src_device.
static std::shared_ptr< MemoryManagerDevice > GetMemoryManagerDevice(const Device &device)
Internally dispatches the appropriate MemoryManagerDevice instance.
static void MemcpyToHost(void *host_ptr, const void *src_ptr, const Device &src_device, size_t num_bytes)
Same as Memcpy, but with host (CPU:0) as default dst_device.
static void * Malloc(size_t byte_size, const Device &device)
static void Free(void *ptr, const Device &device)
Frees previously allocated memory at address ptr on device device.
static void Memcpy(void *dst_ptr, const Device &dst_device, const void *src_ptr, const Device &src_device, size_t num_bytes)
Helper functions for the ml ops.
Generic file read and write utility for python interface.