Open3D (C++ API)  0.16.0
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"
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
73namespace open3d {
74namespace core {
75
76#ifdef BUILD_CUDA_MODULE
77
101class CUDAScopedDevice {
102public:
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
112private:
113 int prev_device_id_;
114};
115
155class CUDAScopedStream {
156private:
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
164public:
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
176private:
177 cudaStream_t prev_stream_;
178 cudaStream_t new_stream_;
179 bool owns_new_stream_ = false;
180};
181
195class CUDAState {
196public:
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
214private:
215 CUDAState();
216
217 std::vector<std::vector<bool>> p2p_enabled_;
218};
219
221int GetCUDACurrentWarpSize();
222
224int GetCUDACurrentDeviceTextureAlignment();
225
227size_t GetCUDACurrentTotalMemSize();
228#endif
229
230namespace cuda {
231
234int DeviceCount();
235
238bool IsAvailable();
239
241void ReleaseCache();
242
245void Synchronize();
246
251void Synchronize(const Device& device);
252
256void AssertCUDADeviceAvailable(int device_id);
257
261void AssertCUDADeviceAvailable(const Device& device);
262
263#ifdef BUILD_CUDA_MODULE
264
265int GetDevice();
266cudaStream_t GetStream();
267cudaStream_t GetDefaultStream();
268
269#endif
270
271} // namespace cuda
272} // namespace core
273} // namespace open3d
274
275// Exposed as implementation detail of macros at the end of the file.
276#ifdef BUILD_CUDA_MODULE
277
278namespace open3d {
279namespace core {
280
281void __OPEN3D_CUDA_CHECK(cudaError_t err, const char* file, const int line);
282
283void __OPEN3D_GET_LAST_CUDA_ERROR(const char* message,
284 const char* file,
285 const int line);
286
287} // namespace core
288} // namespace open3d
289
290#endif
void ReleaseCache()
Releases CUDA memory manager cache. This is typically used for debugging.
Definition: CUDAUtils.cpp:59
bool IsAvailable()
Definition: CUDAUtils.cpp:57
int DeviceCount()
Definition: CUDAUtils.cpp:40
void Synchronize()
Definition: CUDAUtils.cpp:77
void AssertCUDADeviceAvailable(int device_id)
Definition: CUDAUtils.cpp:94
Definition: PinholeCameraIntrinsic.cpp:35