Open3D (C++ API)  0.11.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 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 
43 public:
44  static void* Malloc(size_t byte_size, const Device& device);
45  static void Free(void* ptr, const Device& device);
46  static void Memcpy(void* dst_ptr,
47  const Device& dst_device,
48  const void* src_ptr,
49  const Device& src_device,
50  size_t num_bytes);
52  static void MemcpyFromHost(void* dst_ptr,
53  const Device& dst_device,
54  const void* host_ptr,
55  size_t num_bytes);
57  static void MemcpyToHost(void* host_ptr,
58  const void* src_ptr,
59  const Device& src_device,
60  size_t num_bytes);
61 
62 protected:
63  static std::shared_ptr<DeviceMemoryManager> GetDeviceMemoryManager(
64  const Device& device);
65 };
66 
68 public:
69  virtual void* Malloc(size_t byte_size, const Device& device) = 0;
70  virtual void Free(void* ptr, const Device& device) = 0;
71  virtual void Memcpy(void* dst_ptr,
72  const Device& dst_device,
73  const void* src_ptr,
74  const Device& src_device,
75  size_t num_bytes) = 0;
76  virtual ~DeviceMemoryManager() {}
77 };
78 
80 public:
82  void* Malloc(size_t byte_size, const Device& device) override;
83  void Free(void* ptr, const Device& device) override;
84  void Memcpy(void* dst_ptr,
85  const Device& dst_device,
86  const void* src_ptr,
87  const Device& src_device,
88  size_t num_bytes) override;
89 };
90 
91 #ifdef BUILD_CUDA_MODULE
92 class CUDASimpleMemoryManager : public DeviceMemoryManager {
93 public:
94  CUDASimpleMemoryManager();
95  void* Malloc(size_t byte_size, const Device& device) override;
96  void Free(void* ptr, const Device& device) override;
97  void Memcpy(void* dst_ptr,
98  const Device& dst_device,
99  const void* src_ptr,
100  const Device& src_device,
101  size_t num_bytes) override;
102 
103 public:
104  static void ReleaseCache(){};
105 
106 protected:
107  bool IsCUDAPointer(const void* ptr);
108 };
109 
110 class CUDACachedMemoryManager : public DeviceMemoryManager {
111 public:
112  CUDACachedMemoryManager();
113  void* Malloc(size_t byte_size, const Device& device) override;
114  void Free(void* ptr, const Device& device) override;
115  void Memcpy(void* dst_ptr,
116  const Device& dst_device,
117  const void* src_ptr,
118  const Device& src_device,
119  size_t num_bytes) override;
120 
121 public:
122  static void ReleaseCache();
123 
124 protected:
125  bool IsCUDAPointer(const void* ptr);
126 };
127 #endif
128 
129 } // namespace core
130 } // 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:80
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:48
void ReleaseCache()
Definition: CUDAUtils.cpp:55
static void Free(void *ptr, const Device &device)
Definition: MemoryManager.cpp:44
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:88
static void * Malloc(size_t byte_size, const Device &device)
Definition: MemoryManager.cpp:40
virtual ~DeviceMemoryManager()
Definition: MemoryManager.h:76
Definition: Device.h:39
Definition: MemoryManager.h:42
Definition: PinholeCameraIntrinsic.cpp:35
static std::shared_ptr< DeviceMemoryManager > GetDeviceMemoryManager(const Device &device)
Definition: MemoryManager.cpp:96
Definition: MemoryManager.h:67
Definition: MemoryManager.h:79