NVIDIA DeepStream SDK API Reference

9.2 Release
sources/libs/nvmultiobjecttracker/include/modules/utility/cuda_common.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef NVTRACKER2LIB_CUDA_COMMON_HPP_
19 #define NVTRACKER2LIB_CUDA_COMMON_HPP_
20 
21 #include <cstdlib>
22 #include <iostream>
23 #include <string>
24 #include <cassert>
25 
26 #include <cuda_runtime_api.h>
27 #include <cuda_runtime.h>
28 #include "cuda.h"
29 #include "cufft.h"
30 #include "cuComplex.h"
31 #include "cublas_v2.h"
32 
33 #ifndef _WIN32
34 #include <vpi/Status.h>
35 #endif
36 
41 static const char *_cufftGetErrorEnum(cufftResult error)
42 {
43  switch (error)
44  {
45  case CUFFT_SUCCESS: // 0, The cuFFT operation was successful
46  return "CUFFT_SUCCESS";
47 
48  case CUFFT_INVALID_PLAN: // 1, cuFFT was passed an invalid plan handle
49  return "CUFFT_INVALID_PLAN";
50 
51  case CUFFT_ALLOC_FAILED: // 2, cuFFT failed to allocate GPU or CPU memory
52  return "CUFFT_ALLOC_FAILED";
53 
54  case CUFFT_INVALID_TYPE: // 3, No longer used
55  return "CUFFT_INVALID_TYPE";
56 
57  case CUFFT_INVALID_VALUE: // 4, User specified an invalid pointer or parameter
58  return "CUFFT_INVALID_VALUE";
59 
60  case CUFFT_INTERNAL_ERROR: // 5, Driver or internal cuFFT library error
61  return "CUFFT_INTERNAL_ERROR";
62 
63  case CUFFT_EXEC_FAILED: // 6, Failed to execute an FFT on the GPU
64  return "CUFFT_EXEC_FAILED";
65 
66  case CUFFT_SETUP_FAILED: // 7, The cuFFT library failed to initialize
67  return "CUFFT_SETUP_FAILED";
68 
69  case CUFFT_INVALID_SIZE: // 8, User specified an invalid transform size
70  return "CUFFT_INVALID_SIZE";
71 
72  case CUFFT_UNALIGNED_DATA: // 9, No longer used
73  return "CUFFT_UNALIGNED_DATA";
74 
75  case CUFFT_INVALID_DEVICE: // 11, Execution of a plan was on different GPU than plan creation
76  return "CUFFT_INVALID_DEVICE";
77 
78  case CUFFT_NO_WORKSPACE: // 13, No workspace has been provided prior to plan execution
79  return "CUFFT_NO_WORKSPACE";
80 
81  case CUFFT_NOT_IMPLEMENTED: // 14, Function does not implement functionality for parameters given.
82  return "CUFFT_NOT_IMPLEMENTED";
83 
84  case CUFFT_NOT_SUPPORTED: // 16, Operation is not supported for parameters given.
85  return "CUFFT_NOT_SUPPORTED";
86 
87  default:
88  return "Unknown CUFFT error";
89  }
90 }
91 
96 static const char *_cublasGetErrorEnum(cublasStatus_t error)
97 {
98  switch (error)
99  {
100  case CUBLAS_STATUS_SUCCESS: // 0
101  return "CUBLAS_STATUS_SUCCESS";
102 
103  case CUBLAS_STATUS_NOT_INITIALIZED: // 1
104  return "CUBLAS_STATUS_NOT_INITIALIZED";
105 
106  case CUBLAS_STATUS_ALLOC_FAILED: // 3
107  return "CUBLAS_STATUS_ALLOC_FAILED";
108 
109  case CUBLAS_STATUS_INVALID_VALUE: // 7
110  return "CUBLAS_STATUS_INVALID_VALUE";
111 
112  case CUBLAS_STATUS_ARCH_MISMATCH: // 8
113  return "CUBLAS_STATUS_ARCH_MISMATCH";
114 
115  case CUBLAS_STATUS_MAPPING_ERROR: // 11
116  return "CUBLAS_STATUS_MAPPING_ERROR";
117 
118  case CUBLAS_STATUS_EXECUTION_FAILED: // 13
119  return "CUBLAS_STATUS_EXECUTION_FAILED";
120 
121  case CUBLAS_STATUS_INTERNAL_ERROR: // 14
122  return "CUBLAS_STATUS_INTERNAL_ERROR";
123 
124  case CUBLAS_STATUS_NOT_SUPPORTED: // 15
125  return "CUBLAS_STATUS_NOT_SUPPORTED";
126 
127  case CUBLAS_STATUS_LICENSE_ERROR: // 16
128  return "CUBLAS_STATUS_LICENSE_ERROR";
129 
130  default:
131  return "Unknown CUBLAS error";
132  }
133 }
134 
142 inline void gpuAssert(CUresult code, const char *file, int line, bool abort=true)
143 {
144  if (code != CUDA_SUCCESS)
145  {
146  const char* errstr = nullptr;
147  cuGetErrorString(code, &errstr);
148  fprintf(stderr,"GPUassert: %s %s %d\n", errstr, file, line);
149  if (abort) { throw std::runtime_error("GPUassert failed"); }
150  }
151 }
152 
160 inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
161 {
162  if (code != cudaSuccess)
163  {
164  fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
165  if (abort) { throw std::runtime_error("GPUassert failed"); }
166  }
167 }
168 
176 inline void gpuAssert_cublas(cublasStatus_t code, const char *file, int line, bool abort=true)
177 {
178  if (code != CUBLAS_STATUS_SUCCESS)
179  {
180  fprintf(stderr,"GPUassert_CUBLAS: %s %s %d\n", _cublasGetErrorEnum(code), file, line);
181  if (abort) { throw std::runtime_error("GPUassert_CUBLAS failed"); }
182  }
183 }
184 
192 inline void gpuAssert_cufft(cufftResult code, const char *file, int line, bool abort=true)
193 {
194  if (code != CUFFT_SUCCESS)
195  {
196  fprintf(stderr,"GPUassert_CUFFT: %s %s %d\n", _cufftGetErrorEnum(code), file, line);
197  if (abort) { throw std::runtime_error("GPUassert_CUFFT failed"); }
198  }
199 }
200 
201 #ifndef _WIN32
202 
209 inline void gpuAssert_vpi(VPIStatus code, const char *file, int line, bool abort=true)
210 {
211  if (code != VPI_SUCCESS)
212  {
213  char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH] = {};
214  vpiGetLastStatusMessage(buffer, sizeof(buffer));
215  fprintf(stderr,"GPUassert_VPI: %s %s %s %d\n", vpiStatusGetName(code), buffer, file, line);
216  if (abort) { throw std::runtime_error("GPUassert_VPI failed"); }
217  }
218 }
219 
220 #endif
221 
222 // Check a CUDA, cuBLAS, cuFFT, or VPI status.
223 #define CUDA_CHECK(condition) \
224  gpuAssert(condition, __FILE__, __LINE__);
225 
226 #define CUBLAS_CHECK(condition) \
227  gpuAssert_cublas(condition, __FILE__, __LINE__);
228 
229 #define CUFFT_CHECK(condition) \
230  gpuAssert_cufft(condition, __FILE__, __LINE__);
231 
232 #ifndef _WIN32
233 #define VPI_CHECK(STMT) \
234  gpuAssert_vpi((STMT), __FILE__, __LINE__); \
235 
236 #endif
237 
238 #endif // NVTRACKER2LIB_CUDA_COMMON_HPP_
gpuAssert_cublas
void gpuAssert_cublas(cublasStatus_t code, const char *file, int line, bool abort=true)
Report a cuBLAS error and optionally throw an exception.
Definition: sources/libs/nvmultiobjecttracker/include/modules/utility/cuda_common.hpp:176
_cufftGetErrorEnum
static const char * _cufftGetErrorEnum(cufftResult error)
Return the symbolic name of a cuFFT status.
Definition: sources/libs/nvmultiobjecttracker/include/modules/utility/cuda_common.hpp:41
_cublasGetErrorEnum
static const char * _cublasGetErrorEnum(cublasStatus_t error)
Return the symbolic name of a cuBLAS status.
Definition: sources/libs/nvmultiobjecttracker/include/modules/utility/cuda_common.hpp:96
gpuAssert
void gpuAssert(CUresult code, const char *file, int line, bool abort=true)
Report a CUDA driver error and optionally throw an exception.
Definition: sources/libs/nvmultiobjecttracker/include/modules/utility/cuda_common.hpp:142
gpuAssert_vpi
void gpuAssert_vpi(VPIStatus code, const char *file, int line, bool abort=true)
Report a VPI error and optionally throw an exception.
Definition: sources/libs/nvmultiobjecttracker/include/modules/utility/cuda_common.hpp:209
gpuAssert_cufft
void gpuAssert_cufft(cufftResult code, const char *file, int line, bool abort=true)
Report a cuFFT error and optionally throw an exception.
Definition: sources/libs/nvmultiobjecttracker/include/modules/utility/cuda_common.hpp:192