Open3D (C++ API)
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::__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 
64 #ifdef BUILD_CUDA_MODULE
65 inline void __OPEN3D_CUDA_CHECK(cudaError_t err,
66  const char* file,
67  const int line) {
68  if (err != cudaSuccess) {
69  utility::LogError("{}:{} CUDA runtime error: {}", file, line,
70  cudaGetErrorString(err));
71  }
72 }
73 
74 inline void __OPEN3D_GET_LAST_CUDA_ERROR(const char* message,
75  const char* file,
76  const int line) {
77  cudaError_t err = cudaGetLastError();
78  if (err != cudaSuccess) {
79  utility::LogError("{}:{} {}: OPEN3D_GET_LAST_CUDA_ERROR(): {}", file,
80  line, message, cudaGetErrorString(err));
81  }
82 }
83 #endif
84 
85 namespace cuda {
86 
87 int DeviceCount();
88 bool IsAvailable();
89 
90 } // namespace cuda
91 } // namespace open3d
int DeviceCount()
Definition: CUDAUtils.cpp:36
void LogError(const char *format, const Args &... args)
Definition: Console.h:174
Definition: Open3DViewer.h:29
bool IsAvailable()
Definition: CUDAUtils.cpp:45