Open3D (C++ API)  0.18.0+5c982c7
Helper.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
10 
11 #pragma once
12 
13 #ifdef BUILD_CUDA_MODULE
14 
15 #include <cuda.h>
16 #include <cuda_runtime.h>
17 
18 // TODO: Disable fmt() macro defined in fmt<7.0.0.
19 // TODO: Remove this line once Open3D upgrades its fmt dependency.
20 #define FMT_STRING_ALIAS 0
21 
22 #include "open3d/core/CUDAUtils.h"
23 #include "open3d/utility/Logging.h"
24 
25 #endif // #ifdef BUILD_CUDA_MODULE
26 
27 namespace open3d {
28 namespace ml {
29 
30 #ifdef BUILD_CUDA_MODULE
31 
32 #define OPEN3D_ML_CUDA_DRIVER_CHECK(err) \
33  __OPEN3D_ML_CUDA_DRIVER_CHECK(err, __FILE__, __LINE__)
34 
35 inline void __OPEN3D_ML_CUDA_DRIVER_CHECK(CUresult err,
36  const char *file,
37  const int line,
38  bool abort = true) {
39  if (err != CUDA_SUCCESS) {
40  const char *error_string;
41  CUresult err_get_string = cuGetErrorString(err, &error_string);
42 
43  if (err_get_string == CUDA_SUCCESS) {
44  utility::LogError("{}:{} CUDA driver error: {}", file, line,
45  error_string);
46  } else {
47  utility::LogError("{}:{} CUDA driver error: UNKNOWN", file, line);
48  }
49  }
50 }
51 
52 inline cudaStream_t GetDefaultStream() { (cudaStream_t)0; }
53 
54 inline int GetDevice(cudaStream_t stream) {
55  if (stream == GetDefaultStream()) {
56  // Default device.
57  return 0;
58  }
59 
60  // Remember current context.
61  CUcontext current_context;
62  OPEN3D_ML_CUDA_DRIVER_CHECK(cuCtxGetCurrent(&current_context));
63 
64  // Switch to context of provided stream.
65  CUcontext context;
66  OPEN3D_ML_CUDA_DRIVER_CHECK(cuStreamGetCtx(stream, &context));
67  OPEN3D_ML_CUDA_DRIVER_CHECK(cuCtxSetCurrent(context));
68 
69  // Query device of current context.
70  // This is the device of the provided stream.
71  CUdevice device;
72  OPEN3D_ML_CUDA_DRIVER_CHECK(cuCtxGetDevice(&device));
73 
74  // Restore previous context.
75  OPEN3D_ML_CUDA_DRIVER_CHECK(cuCtxSetCurrent(current_context));
76 
77  // CUdevice is a typedef to int.
78  return device;
79 }
80 
81 class CUDAScopedDeviceStream {
82 public:
83  explicit CUDAScopedDeviceStream(cudaStream_t stream)
84  : scoped_device_(GetDevice(stream)), scoped_stream_(stream) {}
85 
86  CUDAScopedDeviceStream(CUDAScopedDeviceStream const &) = delete;
87  void operator=(CUDAScopedDeviceStream const &) = delete;
88 
89 private:
90  core::CUDAScopedDevice scoped_device_;
91  core::CUDAScopedStream scoped_stream_;
92 };
93 #endif
94 
95 } // namespace ml
96 } // namespace open3d
Common CUDA utilities.
#define LogError(...)
Definition: Logging.h:48
ImGuiContext * context
Definition: Window.cpp:76
Definition: PinholeCameraIntrinsic.cpp:16