Open3D (C++ API)  0.16.0
Helper.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
29
30#pragma once
31
32#ifdef BUILD_CUDA_MODULE
33
34#include <cuda.h>
35#include <cuda_runtime.h>
36
37// TODO: Disable fmt() macro defined in fmt<7.0.0.
38// TODO: Remove this line once Open3D upgrades its fmt dependency.
39#define FMT_STRING_ALIAS 0
40
43
44#endif // #ifdef BUILD_CUDA_MODULE
45
46namespace open3d {
47namespace ml {
48
49#ifdef BUILD_CUDA_MODULE
50
51#define OPEN3D_ML_CUDA_DRIVER_CHECK(err) \
52 __OPEN3D_ML_CUDA_DRIVER_CHECK(err, __FILE__, __LINE__)
53
54inline void __OPEN3D_ML_CUDA_DRIVER_CHECK(CUresult err,
55 const char *file,
56 const int line,
57 bool abort = true) {
58 if (err != CUDA_SUCCESS) {
59 const char *error_string;
60 CUresult err_get_string = cuGetErrorString(err, &error_string);
61
62 if (err_get_string == CUDA_SUCCESS) {
63 utility::LogError("{}:{} CUDA driver error: {}", file, line,
64 error_string);
65 } else {
66 utility::LogError("{}:{} CUDA driver error: UNKNOWN", file, line);
67 }
68 }
69}
70
71inline cudaStream_t GetDefaultStream() { (cudaStream_t)0; }
72
73inline int GetDevice(cudaStream_t stream) {
74 if (stream == GetDefaultStream()) {
75 // Default device.
76 return 0;
77 }
78
79 // Remember current context.
80 CUcontext current_context;
81 OPEN3D_ML_CUDA_DRIVER_CHECK(cuCtxGetCurrent(&current_context));
82
83 // Switch to context of provided stream.
84 CUcontext context;
85 OPEN3D_ML_CUDA_DRIVER_CHECK(cuStreamGetCtx(stream, &context));
86 OPEN3D_ML_CUDA_DRIVER_CHECK(cuCtxSetCurrent(context));
87
88 // Query device of current context.
89 // This is the device of the provided stream.
90 CUdevice device;
91 OPEN3D_ML_CUDA_DRIVER_CHECK(cuCtxGetDevice(&device));
92
93 // Restore previous context.
94 OPEN3D_ML_CUDA_DRIVER_CHECK(cuCtxSetCurrent(current_context));
95
96 // CUdevice is a typedef to int.
97 return device;
98}
99
100class CUDAScopedDeviceStream {
101public:
102 explicit CUDAScopedDeviceStream(cudaStream_t stream)
103 : scoped_device_(GetDevice(stream)), scoped_stream_(stream) {}
104
105 CUDAScopedDeviceStream(CUDAScopedDeviceStream const &) = delete;
106 void operator=(CUDAScopedDeviceStream const &) = delete;
107
108private:
109 core::CUDAScopedDevice scoped_device_;
110 core::CUDAScopedStream scoped_stream_;
111};
112#endif
113
114} // namespace ml
115} // namespace open3d
Common CUDA utilities.
#define LogError(...)
Definition: Logging.h:67
ImGuiContext * context
Definition: Window.cpp:95
Definition: PinholeCameraIntrinsic.cpp:35