Open3D (C++ API)  0.16.0
MemoryManager.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// The MIT License (MIT)
5//
6// Copyright (c) 2018-2021 www.open3d.org
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in
16// all copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24// IN THE SOFTWARE.
25// ----------------------------------------------------------------------------
26
27#pragma once
28
29#include <cstring>
30#include <memory>
31#include <stdexcept>
32#include <string>
33#include <unordered_map>
34
35#include "open3d/core/Device.h"
36
37namespace open3d {
38namespace core {
39
40class MemoryManagerDevice;
41
54public:
57 static void* Malloc(size_t byte_size, const Device& device);
58
60 static void Free(void* ptr, const Device& device);
61
64 static void Memcpy(void* dst_ptr,
65 const Device& dst_device,
66 const void* src_ptr,
67 const Device& src_device,
68 size_t num_bytes);
69
71 static void MemcpyFromHost(void* dst_ptr,
72 const Device& dst_device,
73 const void* host_ptr,
74 size_t num_bytes);
75
77 static void MemcpyToHost(void* host_ptr,
78 const void* src_ptr,
79 const Device& src_device,
80 size_t num_bytes);
81
82protected:
84 static std::shared_ptr<MemoryManagerDevice> GetMemoryManagerDevice(
85 const Device& device);
86};
87
90public:
91 virtual ~MemoryManagerDevice() = default;
92
95 virtual void* Malloc(size_t byte_size, const Device& device) = 0;
96
98 virtual void Free(void* ptr, const Device& device) = 0;
99
102 virtual void Memcpy(void* dst_ptr,
103 const Device& dst_device,
104 const void* src_ptr,
105 const Device& src_device,
106 size_t num_bytes) = 0;
107};
108
125public:
128 explicit MemoryManagerCached(
129 const std::shared_ptr<MemoryManagerDevice>& device_mm);
130
133 void* Malloc(size_t byte_size, const Device& device) override;
134
136 void Free(void* ptr, const Device& device) override;
137
140 void Memcpy(void* dst_ptr,
141 const Device& dst_device,
142 const void* src_ptr,
143 const Device& src_device,
144 size_t num_bytes) override;
145
146public:
148 static void ReleaseCache(const Device& device);
149
152 static void ReleaseCache();
153
154protected:
155 std::shared_ptr<MemoryManagerDevice> device_mm_;
156};
157
161public:
164 void* Malloc(size_t byte_size, const Device& device) override;
165
167 void Free(void* ptr, const Device& device) override;
168
171 void Memcpy(void* dst_ptr,
172 const Device& dst_device,
173 const void* src_ptr,
174 const Device& src_device,
175 size_t num_bytes) override;
176};
177
178#ifdef BUILD_CUDA_MODULE
181class MemoryManagerCUDA : public MemoryManagerDevice {
182public:
185 void* Malloc(size_t byte_size, const Device& device) override;
186
188 void Free(void* ptr, const Device& device) override;
189
192 void Memcpy(void* dst_ptr,
193 const Device& dst_device,
194 const void* src_ptr,
195 const Device& src_device,
196 size_t num_bytes) override;
197
198protected:
199 bool IsCUDAPointer(const void* ptr, const Device& device);
200};
201#endif
202
203#ifdef BUILD_SYCL_MODULE
211class MemoryManagerSYCL : public MemoryManagerDevice {
212public:
215 void* Malloc(size_t byte_size, const Device& device) override;
216
218 void Free(void* ptr, const Device& device) override;
219
222 void Memcpy(void* dst_ptr,
223 const Device& dst_device,
224 const void* src_ptr,
225 const Device& src_device,
226 size_t num_bytes) override;
227};
228#endif
229
230} // namespace core
231} // namespace open3d
Definition: Device.h:37
Definition: MemoryManager.h:160
void * Malloc(size_t byte_size, const Device &device) override
Definition: MemoryManagerCPU.cpp:35
void Free(void *ptr, const Device &device) override
Frees previously allocated memory at address ptr on device device.
Definition: MemoryManagerCPU.cpp:44
void Memcpy(void *dst_ptr, const Device &dst_device, const void *src_ptr, const Device &src_device, size_t num_bytes) override
Definition: MemoryManagerCPU.cpp:50
Definition: MemoryManager.h:124
std::shared_ptr< MemoryManagerDevice > device_mm_
Definition: MemoryManager.h:155
void Memcpy(void *dst_ptr, const Device &dst_device, const void *src_ptr, const Device &src_device, size_t num_bytes) override
Definition: MemoryManagerCached.cpp:518
static void ReleaseCache()
Definition: MemoryManagerCached.cpp:530
MemoryManagerCached(const std::shared_ptr< MemoryManagerDevice > &device_mm)
Definition: MemoryManagerCached.cpp:492
void Free(void *ptr, const Device &device) override
Frees previously allocated memory at address ptr on device device.
Definition: MemoryManagerCached.cpp:510
void * Malloc(size_t byte_size, const Device &device) override
Definition: MemoryManagerCached.cpp:502
Interface for all concrete memory manager classes.
Definition: MemoryManager.h:89
virtual void Free(void *ptr, const Device &device)=0
Frees previously allocated memory at address ptr on device device.
virtual void * Malloc(size_t byte_size, const Device &device)=0
virtual ~MemoryManagerDevice()=default
virtual void Memcpy(void *dst_ptr, const Device &dst_device, const void *src_ptr, const Device &src_device, size_t num_bytes)=0
Definition: MemoryManager.h:53
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.
Definition: MemoryManager.cpp:104
static std::shared_ptr< MemoryManagerDevice > GetMemoryManagerDevice(const Device &device)
Internally dispatches the appropriate MemoryManagerDevice instance.
Definition: MemoryManager.cpp:112
static void Memcpy(void *dst_ptr, const Device &dst_device, const void *src_ptr, const Device &src_device, size_t num_bytes)
Definition: MemoryManager.cpp:54
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.
Definition: MemoryManager.cpp:96
static void * Malloc(size_t byte_size, const Device &device)
Definition: MemoryManager.cpp:41
static void Free(void *ptr, const Device &device)
Frees previously allocated memory at address ptr on device device.
Definition: MemoryManager.cpp:47
Definition: PinholeCameraIntrinsic.cpp:35