Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
37 namespace open3d {
38 namespace core {
39 
40 class DeviceMemoryManager;
41 
54 public:
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 
82 protected:
84  static std::shared_ptr<DeviceMemoryManager> GetDeviceMemoryManager(
85  const Device& device);
86 };
87 
90 public:
91  virtual ~DeviceMemoryManager() = 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 
125 public:
128  explicit CachedMemoryManager(
129  const std::shared_ptr<DeviceMemoryManager>& 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 
146 public:
148  static void ReleaseCache(const Device& device);
149 
152  static void ReleaseCache();
153 
154 protected:
155  std::shared_ptr<DeviceMemoryManager> device_mm_;
156 };
157 
161 public:
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
179 class CUDAMemoryManager : public DeviceMemoryManager {
182 public:
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 
198 protected:
199  bool IsCUDAPointer(const void* ptr, const Device& device);
200 };
201 #endif
202 
203 } // namespace core
204 } // namespace open3d
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:86
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
void ReleaseCache()
Releases CUDA memory manager cache. This is typically used for debugging.
Definition: CUDAUtils.cpp:60
static void Free(void *ptr, const Device &device)
Frees previously allocated memory at address ptr on device device.
Definition: MemoryManager.cpp:47
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:94
static void * Malloc(size_t byte_size, const Device &device)
Definition: MemoryManager.cpp:41
Definition: Device.h:39
Definition: MemoryManager.h:53
std::shared_ptr< DeviceMemoryManager > device_mm_
Definition: MemoryManager.h:155
Definition: PinholeCameraIntrinsic.cpp:35
Definition: MemoryManager.h:124
static std::shared_ptr< DeviceMemoryManager > GetDeviceMemoryManager(const Device &device)
Internally dispatches the appropriate DeviceMemoryManager instance.
Definition: MemoryManager.cpp:102
Interface for all concrete memory manager classses.
Definition: MemoryManager.h:89
Definition: MemoryManager.h:160