Jetson Inference
DNN Vision Library
cudaUtility.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __CUDA_UTILITY_H_
24 #define __CUDA_UTILITY_H_
25 
26 
27 #include <cuda_runtime.h>
28 #include <cuda.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdint.h>
32 
33 #include "logging.h"
34 
35 
41 #define CUDA(x) cudaCheckError((x), #x, __FILE__, __LINE__)
42 
47 #define CUDA_SUCCESS(x) (CUDA(x) == cudaSuccess)
48 
53 #define CUDA_FAILED(x) (CUDA(x) != cudaSuccess)
54 
59 #define CUDA_VERIFY(x) if(CUDA_FAILED(x)) return false;
60 
65 #define LOG_CUDA "[cuda] "
66 
67 /*
68  * define this if you want all cuda calls to be printed
69  * @ingroup cudaError
70  */
71 //#define CUDA_TRACE
72 
73 
74 
79 inline cudaError_t cudaCheckError(cudaError_t retval, const char* txt, const char* file, int line )
80 {
81 #if !defined(CUDA_TRACE)
82  if( retval == cudaSuccess)
83  return cudaSuccess;
84 #endif
85 
86  //int activeDevice = -1;
87  //cudaGetDevice(&activeDevice);
88 
89  //Log("[cuda] device %i - %s\n", activeDevice, txt);
90 
91  if( retval == cudaSuccess )
92  LogDebug(LOG_CUDA "%s\n", txt);
93  else
94  LogError(LOG_CUDA "%s\n", txt);
95 
96  if( retval != cudaSuccess )
97  {
98  LogError(LOG_CUDA " %s (error %u) (hex 0x%02X)\n", cudaGetErrorString(retval), retval, retval);
99  LogError(LOG_CUDA " %s:%i\n", file, line);
100  }
101 
102  return retval;
103 }
104 
105 
110 #define CUDA_FREE(x) if(x != NULL) { cudaFree(x); x = NULL; }
111 
116 #define CUDA_FREE_HOST(x) if(x != NULL) { cudaFreeHost(x); x = NULL; }
117 
122 #define SAFE_DELETE(x) if(x != NULL) { delete x; x = NULL; }
123 
128 #define SAFE_FREE(x) if(x != NULL) { free(x); x = NULL; }
129 
130 
148 inline __device__ __host__ int iDivUp( int a, int b ) { return (a % b != 0) ? (a / b + 1) : (a / b); }
149 
150 
151 #endif
152 
cudaError_t cudaCheckError(cudaError_t retval, const char *txt, const char *file, int line)
cudaCheckError
Definition: cudaUtility.h:79
#define LogDebug(format, args...)
Log a printf-style debug message (Log::DEBUG)
Definition: logging.h:180
#define LogError(format, args...)
Log a printf-style error message (Log::ERROR)
Definition: logging.h:150
__device__ __host__ int iDivUp(int a, int b)
If a / b has a remainder, round up.
Definition: cudaUtility.h:148
#define LOG_CUDA
LOG_CUDA string.
Definition: cudaUtility.h:65