Open3D (C++ API)  0.11.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 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/utility/Console.h"
36 
37 #ifdef BUILD_CUDA_MODULE
38 
39 #include <cuda.h>
40 #include <cuda_runtime.h>
41 
42 #define OPEN3D_HOST_DEVICE __host__ __device__
43 #define OPEN3D_DEVICE __device__
44 #define OPEN3D_ASSERT_HOST_DEVICE_LAMBDA(type) \
45  static_assert(__nv_is_extended_host_device_lambda_closure_type(type), \
46  #type " must be a __host__ __device__ lambda")
47 #define OPEN3D_CUDA_CHECK(err) \
48  open3d::core::__OPEN3D_CUDA_CHECK(err, __FILE__, __LINE__)
49 #define OPEN3D_GET_LAST_CUDA_ERROR(message) \
50  __OPEN3D_GET_LAST_CUDA_ERROR(message, __FILE__, __LINE__)
51 
52 #else // #ifdef BUILD_CUDA_MODULE
53 
54 #define OPEN3D_HOST_DEVICE
55 #define OPEN3D_DEVICE
56 #define OPEN3D_ASSERT_HOST_DEVICE_LAMBDA(type)
57 #define OPEN3D_CUDA_CHECK(err)
58 #define OPEN3D_GET_LAST_CUDA_ERROR(message)
59 
60 #endif // #ifdef BUILD_CUDA_MODULE
61 
62 namespace open3d {
63 namespace core {
64 
65 #ifdef BUILD_CUDA_MODULE
66 inline void __OPEN3D_CUDA_CHECK(cudaError_t err,
67  const char* file,
68  const int line) {
69  if (err != cudaSuccess) {
70  utility::LogError("{}:{} CUDA runtime error: {}", file, line,
71  cudaGetErrorString(err));
72  }
73 }
74 
75 inline void __OPEN3D_GET_LAST_CUDA_ERROR(const char* message,
76  const char* file,
77  const int line) {
78  cudaError_t err = cudaGetLastError();
79  if (err != cudaSuccess) {
80  utility::LogError("{}:{} {}: OPEN3D_GET_LAST_CUDA_ERROR(): {}", file,
81  line, message, cudaGetErrorString(err));
82  }
83 }
84 
86 inline int GetCUDACurrentDeviceTextureAlignment() {
87  int device = 0;
88  cudaError_t err = cudaGetDevice(&device);
89  if (err != cudaSuccess) {
91  "GetCUDACurrentDeviceTextureAlignment(): cudaGetDevice failed "
92  "with {}",
93  cudaGetErrorString(err));
94  }
95 
96  int value = 0;
97  err = cudaDeviceGetAttribute(&value, cudaDevAttrTextureAlignment, device);
98  if (err != cudaSuccess) {
100  "GetCUDACurrentDeviceTextureAlignment(): "
101  "cudaDeviceGetAttribute failed with {}",
102  cudaGetErrorString(err));
103  }
104  return value;
105 }
106 #endif
107 
108 namespace cuda {
109 
110 int DeviceCount();
111 bool IsAvailable();
112 void ReleaseCache();
113 
114 } // namespace cuda
115 } // namespace core
116 } // namespace open3d
void ReleaseCache()
Definition: CUDAUtils.cpp:55
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
int DeviceCount()
Definition: CUDAUtils.cpp:40
bool IsAvailable()
Definition: CUDAUtils.cpp:53
Definition: PinholeCameraIntrinsic.cpp:35