Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CUDAUtils.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 
32 
33 #pragma once
34 
35 #include "open3d/core/Device.h"
36 #include "open3d/utility/Logging.h"
37 
38 #ifdef BUILD_CUDA_MODULE
39 
40 #include <cuda.h>
41 #include <cuda_runtime.h>
42 
43 #include <memory>
44 #include <vector>
45 
47 
48 #define OPEN3D_FORCE_INLINE __forceinline__
49 #define OPEN3D_HOST_DEVICE __host__ __device__
50 #define OPEN3D_DEVICE __device__
51 #define OPEN3D_ASSERT_HOST_DEVICE_LAMBDA(type) \
52  static_assert(__nv_is_extended_host_device_lambda_closure_type(type), \
53  #type " must be a __host__ __device__ lambda")
54 #define OPEN3D_CUDA_CHECK(err) \
55  open3d::core::__OPEN3D_CUDA_CHECK(err, __FILE__, __LINE__)
56 #define OPEN3D_GET_LAST_CUDA_ERROR(message) \
57  __OPEN3D_GET_LAST_CUDA_ERROR(message, __FILE__, __LINE__)
58 #define CUDA_CALL(cuda_function, ...) cuda_function(__VA_ARGS__);
59 
60 #else // #ifdef BUILD_CUDA_MODULE
61 
62 #define OPEN3D_FORCE_INLINE inline
63 #define OPEN3D_HOST_DEVICE
64 #define OPEN3D_DEVICE
65 #define OPEN3D_ASSERT_HOST_DEVICE_LAMBDA(type)
66 #define OPEN3D_CUDA_CHECK(err)
67 #define OPEN3D_GET_LAST_CUDA_ERROR(message)
68 #define CUDA_CALL(cuda_function, ...) \
69  utility::LogError("Not built with CUDA, cannot call " #cuda_function);
70 
71 #endif // #ifdef BUILD_CUDA_MODULE
72 
73 namespace open3d {
74 namespace core {
75 
76 #ifdef BUILD_CUDA_MODULE
77 
101 class CUDAScopedDevice {
102 public:
103  explicit CUDAScopedDevice(int device_id);
104 
105  explicit CUDAScopedDevice(const Device& device);
106 
107  ~CUDAScopedDevice();
108 
109  CUDAScopedDevice(const CUDAScopedDevice&) = delete;
110  CUDAScopedDevice& operator=(const CUDAScopedDevice&) = delete;
111 
112 private:
113  int prev_device_id_;
114 };
115 
155 class CUDAScopedStream {
156 private:
157  struct CreateNewStreamTag {
158  CreateNewStreamTag(const CreateNewStreamTag&) = delete;
159  CreateNewStreamTag& operator=(const CreateNewStreamTag&) = delete;
160  CreateNewStreamTag(CreateNewStreamTag&&) = delete;
161  CreateNewStreamTag& operator=(CreateNewStreamTag&&) = delete;
162  };
163 
164 public:
165  constexpr static CreateNewStreamTag CreateNewStream = {};
166 
167  explicit CUDAScopedStream(const CreateNewStreamTag&);
168 
169  explicit CUDAScopedStream(cudaStream_t stream);
170 
171  ~CUDAScopedStream();
172 
173  CUDAScopedStream(const CUDAScopedStream&) = delete;
174  CUDAScopedStream& operator=(const CUDAScopedStream&) = delete;
175 
176 private:
177  cudaStream_t prev_stream_;
178  cudaStream_t new_stream_;
179  bool owns_new_stream_ = false;
180 };
181 
195 class CUDAState {
196 public:
197  static CUDAState& GetInstance();
198 
199  CUDAState(const CUDAState&) = delete;
200  CUDAState& operator=(const CUDAState&) = delete;
201 
204  bool IsP2PEnabled(int src_id, int tar_id) const;
205 
208  bool IsP2PEnabled(const Device& src, const Device& tar) const;
209 
212  void ForceDisableP2PForTesting();
213 
214 private:
215  CUDAState();
216 
217  std::vector<std::vector<bool>> p2p_enabled_;
218 };
219 
221 int GetCUDACurrentWarpSize();
222 
224 int GetCUDACurrentDeviceTextureAlignment();
225 
226 #endif
227 
228 namespace cuda {
229 
232 int DeviceCount();
233 
236 bool IsAvailable();
237 
239 void ReleaseCache();
240 
243 void Synchronize();
244 
249 void Synchronize(const Device& device);
250 
254 void AssertCUDADeviceAvailable(int device_id);
255 
259 void AssertCUDADeviceAvailable(const Device& device);
260 
261 #ifdef BUILD_CUDA_MODULE
262 
263 int GetDevice();
264 cudaStream_t GetStream();
265 cudaStream_t GetDefaultStream();
266 
267 #endif
268 
269 } // namespace cuda
270 } // namespace core
271 } // namespace open3d
272 
273 // Exposed as implementation detail of macros at the end of the file.
274 #ifdef BUILD_CUDA_MODULE
275 
276 namespace open3d {
277 namespace core {
278 
279 void __OPEN3D_CUDA_CHECK(cudaError_t err, const char* file, const int line);
280 
281 void __OPEN3D_GET_LAST_CUDA_ERROR(const char* message,
282  const char* file,
283  const int line);
284 
285 } // namespace core
286 } // namespace open3d
287 
288 #endif
void ReleaseCache()
Releases CUDA memory manager cache. This is typically used for debugging.
Definition: CUDAUtils.cpp:60
void Synchronize()
Definition: CUDAUtils.cpp:78
int DeviceCount()
Definition: CUDAUtils.cpp:40
bool IsAvailable()
Definition: CUDAUtils.cpp:58
void AssertCUDADeviceAvailable(int device_id)
Definition: CUDAUtils.cpp:95
Definition: PinholeCameraIntrinsic.cpp:35