Open3D (C++ API)
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 
39 class DeviceMemoryManager;
40 
42 public:
43  static void* Malloc(size_t byte_size, const Device& device);
44  static void Free(void* ptr, const Device& device);
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);
51  static void MemcpyFromHost(void* dst_ptr,
52  const Device& dst_device,
53  const void* host_ptr,
54  size_t num_bytes);
56  static void MemcpyToHost(void* host_ptr,
57  const void* src_ptr,
58  const Device& src_device,
59  size_t num_bytes);
60 
61 protected:
62  static std::shared_ptr<DeviceMemoryManager> GetDeviceMemoryManager(
63  const Device& device);
64 };
65 
67 public:
68  virtual void* Malloc(size_t byte_size, const Device& device) = 0;
69  virtual void Free(void* ptr, const Device& device) = 0;
70  virtual void Memcpy(void* dst_ptr,
71  const Device& dst_device,
72  const void* src_ptr,
73  const Device& src_device,
74  size_t num_bytes) = 0;
75 };
76 
78 public:
80  void* Malloc(size_t byte_size, const Device& device) override;
81  void Free(void* ptr, const Device& device) override;
82  void Memcpy(void* dst_ptr,
83  const Device& dst_device,
84  const void* src_ptr,
85  const Device& src_device,
86  size_t num_bytes) override;
87 };
88 
89 #ifdef BUILD_CUDA_MODULE
90 class CUDAMemoryManager : public DeviceMemoryManager {
91 public:
92  CUDAMemoryManager();
93  void* Malloc(size_t byte_size, const Device& device) override;
94  void Free(void* ptr, const Device& device) override;
95  void Memcpy(void* dst_ptr,
96  const Device& dst_device,
97  const void* src_ptr,
98  const Device& src_device,
99  size_t num_bytes) override;
100 
101 protected:
102  bool IsCUDAPointer(const void* ptr);
103 };
104 #endif
105 
106 } // namespace open3d
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:87
static std::shared_ptr< DeviceMemoryManager > GetDeviceMemoryManager(const Device &device)
Definition: MemoryManager.cpp:95
Definition: MemoryManager.h:41
Definition: MemoryManager.h:66
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:79
Definition: MemoryManager.h:77
Definition: Open3DViewer.h:29
static void * Malloc(size_t byte_size, const Device &device)
Definition: MemoryManager.cpp:39
static void Free(void *ptr, const Device &device)
Definition: MemoryManager.cpp:43
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:47
Definition: Device.h:38