forked from dusty-nv/jetson-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcudaUtility.h
152 lines (126 loc) · 4.15 KB
/
cudaUtility.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef __CUDA_UTILITY_H_
#define __CUDA_UTILITY_H_
#include <cuda_runtime.h>
#include <cuda.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "logging.h"
/**
* Execute a CUDA call and print out any errors
* @return the original cudaError_t result
* @ingroup cudaError
*/
#define CUDA(x) cudaCheckError((x), #x, __FILE__, __LINE__)
/**
* Evaluates to true on success
* @ingroup cudaError
*/
#define CUDA_SUCCESS(x) (CUDA(x) == cudaSuccess)
/**
* Evaluates to true on failure
* @ingroup cudaError
*/
#define CUDA_FAILED(x) (CUDA(x) != cudaSuccess)
/**
* Return from the boolean function if CUDA call fails
* @ingroup cudaError
*/
#define CUDA_VERIFY(x) if(CUDA_FAILED(x)) return false;
/**
* Return on CUDA errors, continue on cudaSuccess.
* @ingroup cudaError
*/
#define CUDA_ASSERT(x) { const cudaError_t _retval = CUDA(x); if(_retval != cudaSuccess) return _retval; }
/**
* LOG_CUDA string.
* @ingroup cudaError
*/
#define LOG_CUDA "[cuda] "
/*
* define this if you want all cuda calls to be printed
* @ingroup cudaError
*/
//#define CUDA_TRACE
/**
* cudaCheckError
* @ingroup cudaError
*/
inline cudaError_t cudaCheckError(cudaError_t retval, const char* txt, const char* file, int line )
{
if( retval == cudaSuccess )
{
#if !defined(CUDA_TRACE)
return cudaSuccess;
#else
LogDebug(LOG_CUDA "%s\n", txt);
#endif
}
else
{
LogError(LOG_CUDA "%s\n", txt);
LogError(LOG_CUDA " %s (error %u) (hex 0x%02X)\n", cudaGetErrorString(retval), retval, retval);
LogError(LOG_CUDA " %s:%i\n", file, line);
}
return retval;
}
/**
* Check for non-NULL pointer before freeing it, and then set the pointer to NULL.
* @ingroup cudaError
*/
#define CUDA_FREE(x) if(x != NULL) { cudaFree(x); x = NULL; }
/**
* Check for non-NULL pointer before freeing it, and then set the pointer to NULL.
* @ingroup cudaError
*/
#define CUDA_FREE_HOST(x) if(x != NULL) { cudaFreeHost(x); x = NULL; }
/**
* Check for non-NULL pointer before deleting it, and then set the pointer to NULL.
* @ingroup util
*/
#define SAFE_DELETE(x) if(x != NULL) { delete x; x = NULL; }
/**
* Check for non-NULL pointer before freeing it, and then set the pointer to NULL.
* @ingroup util
*/
#define SAFE_FREE(x) if(x != NULL) { free(x); x = NULL; }
/**
* If a / b has a remainder, round up. This function is commonly using when launching
* CUDA kernels, to compute a grid size inclusive of the entire dataset if it's dimensions
* aren't evenly divisible by the block size.
*
* For example:
*
* const dim3 blockDim(8,8);
* const dim3 gridDim(iDivUp(imgWidth,blockDim.x), iDivUp(imgHeight,blockDim.y));
*
* Then inside the CUDA kernel, there is typically a check that thread index is in-bounds.
*
* Without the use of iDivUp(), if the data dimensions weren't evenly divisible by the
* block size, parts of the data wouldn't be covered by the grid and not processed.
*
* @ingroup cuda
*/
inline __device__ __host__ int iDivUp( int a, int b ) { return (a % b != 0) ? (a / b + 1) : (a / b); }
#endif