Open3D (C++ API)  0.18.0+252c867
MemoryManager.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <cstring>
11 #include <memory>
12 #include <stdexcept>
13 #include <string>
14 #include <unordered_map>
15 
16 #include "open3d/core/Device.h"
17 
18 namespace open3d {
19 namespace core {
20 
21 class MemoryManagerDevice;
22 
35 public:
38  static void* Malloc(size_t byte_size, const Device& device);
39 
41  static void Free(void* ptr, const Device& device);
42 
45  static void Memcpy(void* dst_ptr,
46  const Device& dst_device,
47  const void* src_ptr,
48  const Device& src_device,
49  size_t num_bytes);
50 
52  static void MemcpyFromHost(void* dst_ptr,
53  const Device& dst_device,
54  const void* host_ptr,
55  size_t num_bytes);
56 
58  static void MemcpyToHost(void* host_ptr,
59  const void* src_ptr,
60  const Device& src_device,
61  size_t num_bytes);
62 
63 protected:
65  static std::shared_ptr<MemoryManagerDevice> GetMemoryManagerDevice(
66  const Device& device);
67 };
68 
71 public:
72  virtual ~MemoryManagerDevice() = default;
73 
76  virtual void* Malloc(size_t byte_size, const Device& device) = 0;
77 
79  virtual void Free(void* ptr, const Device& device) = 0;
80 
83  virtual void Memcpy(void* dst_ptr,
84  const Device& dst_device,
85  const void* src_ptr,
86  const Device& src_device,
87  size_t num_bytes) = 0;
88 };
89 
106 public:
109  explicit MemoryManagerCached(
110  const std::shared_ptr<MemoryManagerDevice>& device_mm);
111 
114  void* Malloc(size_t byte_size, const Device& device) override;
115 
117  void Free(void* ptr, const Device& device) override;
118 
121  void Memcpy(void* dst_ptr,
122  const Device& dst_device,
123  const void* src_ptr,
124  const Device& src_device,
125  size_t num_bytes) override;
126 
127 public:
129  static void ReleaseCache(const Device& device);
130 
133  static void ReleaseCache();
134 
135 protected:
136  std::shared_ptr<MemoryManagerDevice> device_mm_;
137 };
138 
142 public:
145  void* Malloc(size_t byte_size, const Device& device) override;
146 
148  void Free(void* ptr, const Device& device) override;
149 
152  void Memcpy(void* dst_ptr,
153  const Device& dst_device,
154  const void* src_ptr,
155  const Device& src_device,
156  size_t num_bytes) override;
157 };
158 
159 #ifdef BUILD_CUDA_MODULE
162 class MemoryManagerCUDA : public MemoryManagerDevice {
163 public:
166  void* Malloc(size_t byte_size, const Device& device) override;
167 
169  void Free(void* ptr, const Device& device) override;
170 
173  void Memcpy(void* dst_ptr,
174  const Device& dst_device,
175  const void* src_ptr,
176  const Device& src_device,
177  size_t num_bytes) override;
178 
179 protected:
180  bool IsCUDAPointer(const void* ptr, const Device& device);
181 };
182 #endif
183 
184 #ifdef BUILD_SYCL_MODULE
192 class MemoryManagerSYCL : public MemoryManagerDevice {
193 public:
196  void* Malloc(size_t byte_size, const Device& device) override;
197 
199  void Free(void* ptr, const Device& device) override;
200 
203  void Memcpy(void* dst_ptr,
204  const Device& dst_device,
205  const void* src_ptr,
206  const Device& src_device,
207  size_t num_bytes) override;
208 };
209 #endif
210 
211 } // namespace core
212 } // namespace open3d
Definition: Device.h:18
Definition: MemoryManager.h:141
void * Malloc(size_t byte_size, const Device &device) override
Definition: MemoryManagerCPU.cpp:16
void Free(void *ptr, const Device &device) override
Frees previously allocated memory at address ptr on device device.
Definition: MemoryManagerCPU.cpp:25
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:31
Definition: MemoryManager.h:105
std::shared_ptr< MemoryManagerDevice > device_mm_
Definition: MemoryManager.h:136
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:499
static void ReleaseCache()
Definition: MemoryManagerCached.cpp:511
MemoryManagerCached(const std::shared_ptr< MemoryManagerDevice > &device_mm)
Definition: MemoryManagerCached.cpp:473
void Free(void *ptr, const Device &device) override
Frees previously allocated memory at address ptr on device device.
Definition: MemoryManagerCached.cpp:491
void * Malloc(size_t byte_size, const Device &device) override
Definition: MemoryManagerCached.cpp:483
Interface for all concrete memory manager classes.
Definition: MemoryManager.h:70
virtual void * Malloc(size_t byte_size, const Device &device)=0
virtual void Free(void *ptr, const Device &device)=0
Frees previously allocated memory at address ptr on device device.
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:34
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:85
static std::shared_ptr< MemoryManagerDevice > GetMemoryManagerDevice(const Device &device)
Internally dispatches the appropriate MemoryManagerDevice instance.
Definition: MemoryManager.cpp:93
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:35
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:77
static void * Malloc(size_t byte_size, const Device &device)
Definition: MemoryManager.cpp:22
static void Free(void *ptr, const Device &device)
Frees previously allocated memory at address ptr on device device.
Definition: MemoryManager.cpp:28
Definition: PinholeCameraIntrinsic.cpp:16