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 
32 
38 #define CUDA(x) cudaCheckError((x), #x, __FILE__, __LINE__)
39 
44 #define CUDA_SUCCESS(x) (CUDA(x) == cudaSuccess)
45 
50 #define CUDA_FAILED(x) (CUDA(x) != cudaSuccess)
51 
56 #define CUDA_VERIFY(x) if(CUDA_FAILED(x)) return false;
57 
62 #define LOG_CUDA "[cuda] "
63 
64 /*
65  * define this if you want all cuda calls to be printed
66  */
67 //#define CUDA_TRACE
68 
69 
70 
75 inline cudaError_t cudaCheckError(cudaError_t retval, const char* txt, const char* file, int line )
76 {
77 #if !defined(CUDA_TRACE)
78  if( retval == cudaSuccess)
79  return cudaSuccess;
80 #endif
81 
82  //int activeDevice = -1;
83  //cudaGetDevice(&activeDevice);
84 
85  //Log("[cuda] device %i - %s\n", activeDevice, txt);
86 
87  printf(LOG_CUDA "%s\n", txt);
88 
89 
90  if( retval != cudaSuccess )
91  {
92  printf(LOG_CUDA " %s (error %u) (hex 0x%02X)\n", cudaGetErrorString(retval), retval, retval);
93  printf(LOG_CUDA " %s:%i\n", file, line);
94  }
95 
96  return retval;
97 }
98 
99 
105 #define SAFE_DELETE(x) if(x != NULL) { delete x; x = NULL; }
106 
107 
112 inline __device__ __host__ int iDivUp( int a, int b ) { return (a % b != 0) ? (a / b + 1) : (a / b); }
113 
114 
115 #endif
116 
cudaError_t cudaCheckError(cudaError_t retval, const char *txt, const char *file, int line)
cudaCheckError
Definition: cudaUtility.h:75
#define LOG_CUDA
LOG_CUDA string.
Definition: cudaUtility.h:62
__device__ __host__ int iDivUp(int a, int b)
If a / b has a remainder, round up.
Definition: cudaUtility.h:112