forked from NVIDIA/MinkowskiEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu.cuh
187 lines (161 loc) · 8.95 KB
/
gpu.cuh
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Copyright (c) Chris Choy (chrischoy@ai.stanford.edu).
*
* 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.
*
* Please cite "4D Spatio-Temporal ConvNets: Minkowski Convolutional Neural
* Networks", CVPR'19 (https://arxiv.org/abs/1904.08755) if you use any part
* of the code.
*/
#ifndef GPU_H_
#define GPU_H_
#include <cublas_v2.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <curand.h>
#include <cusparse.h>
#include <driver_types.h> // cuda driver types
#include <exception>
#include <iostream>
#include <vector>
#include "utils.hpp"
// AtomicAddition for double with cuda arch <= 600
#if !defined(__CUDA_ARCH__) || __CUDA_ARCH__ >= 600
#else
__device__ double atomicAdd(double *address, double val) {
unsigned long long int *address_as_ull = (unsigned long long int *)address;
unsigned long long int old = *address_as_ull, assumed;
do {
assumed = old;
old = atomicCAS(address_as_ull, assumed,
__double_as_longlong(val + __longlong_as_double(assumed)));
} while (assumed != old);
return __longlong_as_double(old);
}
#endif
namespace minkowski {
//
// CUDA macros
//
// CUDA: various checks for different function calls.
#define CUDA_CHECK(condition) \
/* Code block avoids redefinition of cudaError_t error */ \
{ \
cudaError_t error = condition; \
if (error != cudaSuccess) { \
throw std::runtime_error(Formatter() \
<< " " << cudaGetErrorString(error) << " at " \
<< __FILE__ << ":" << __LINE__); \
} \
}
// CUDA: various checks for different function calls.
#ifdef DEBUG
#define CUDA_CHECK_DEBUG(condition) \
/* Code block avoids redefinition of cudaError_t error */ \
{ \
cudaError_t error = condition; \
if (error != cudaSuccess) { \
throw std::runtime_error(Formatter() \
<< " " << cudaGetErrorString(error) << " at " \
<< __FILE__ << ":" << __LINE__); \
} \
}
#else
#define CUDA_CHECK_DEBUG(...) (void)0
#endif
#define CUDA_CHECK_ARGS(condition, ...) \
/* Code block avoids redefinition of cudaError_t error */ \
{ \
cudaError_t error = condition; \
if (error != cudaSuccess) { \
Formatter formatter; \
formatter << " " << cudaGetErrorString(error) << " at "; \
formatter << __FILE__ << ":" << __LINE__; \
formatter.append(__VA_ARGS__); \
throw std::runtime_error(formatter.str()); \
} \
}
#define CUBLAS_CHECK(condition) \
{ \
cublasStatus_t status = condition; \
if (status != CUBLAS_STATUS_SUCCESS) { \
throw std::runtime_error(Formatter() \
<< minkowski::cublasGetErrorString(status) \
<< " at " << __FILE__ << ":" << __LINE__); \
} \
}
#define CUSPARSE_CHECK(call) \
{ \
cusparseStatus_t err; \
if ((err = (call)) != CUSPARSE_STATUS_SUCCESS) { \
throw std::runtime_error(Formatter() \
<< minkowski::cusparseGetErrorString(err) \
<< " at " << __FILE__ << ":" << __LINE__); \
} \
}
#define CURAND_CHECK(condition) \
{ \
curandStatus_t status = condition; \
if (status != CURAND_STATUS_SUCCESS) { \
throw std::runtime_error(Formatter() \
<< curandGetErrorString(status) << " at " \
<< __FILE__ << ":" << __LINE__); \
} \
}
// CUDA: grid stride looping
#define CUDA_KERNEL_LOOP(i, n) \
for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < (n); \
i += blockDim.x * gridDim.x)
// CUDA: check for error after kernel execution and exit loudly if there is one.
#define CUDA_POST_KERNEL_CHECK \
{ \
cudaError_t status = cudaPeekAtLastError(); \
if (status != cudaSuccess) { \
throw std::runtime_error(Formatter() \
<< " " << cudaGetErrorString(status) << " at " \
<< __FILE__ << ":" << __LINE__); \
} \
}
#define THRUST_CHECK(condition) \
try { \
condition; \
} catch (thrust::system_error e) { \
throw std::runtime_error(Formatter() \
<< "Thrust error: " << e.what() << " at " \
<< __FILE__ << ":" << __LINE__); \
}
#define THRUST_CATCH \
catch (thrust::system_error e) { \
throw std::runtime_error(Formatter() \
<< "Thrust error: " << e.what() << " at " \
<< __FILE__ << ":" << __LINE__); \
}
// CUDA: library error reporting.
const char *cublasGetErrorString(cublasStatus_t error);
// CUSparse error reporting.
const char *cusparseGetErrorString(cusparseStatus_t error);
cusparseHandle_t getCurrentCUDASparseHandle();
constexpr uint32_t CUDA_NUM_THREADS = 128;
constexpr uint32_t SHARED_BLOCK_SIZE = 32;
constexpr uint32_t MAX_GRID = 65535;
inline int GET_BLOCKS(const uint32_t N, const uint32_t THREADS) {
return std::max((N + THREADS - 1) / THREADS, uint32_t(1));
}
std::pair<size_t, size_t> get_memory_info();
} // end namespace minkowski
#endif // GPU_H_