-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGeneral.cu
174 lines (123 loc) · 3.75 KB
/
General.cu
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
#ifndef CUDA_GENERAL_CU
#define CUDA_GENERAL_CU
/** A file of common CUDA includes and simple functions **/
/** to be directly #included by other cuda files. **/
#include <cublas.h>
#include "General.h"
// Define to check for errors after each cuda call
//#define DEBUG_CUDA
#ifdef DEBUG_CUDA
# define CHECKCUDA(s) {cudaError_t err = cudaThreadSynchronize(); \
if( err != cudaSuccess ) { \
cerr << "CUDA " << s \
<< ": " << cudaGetErrorString(err) << endl; \
exit(1); \
}}
#else
# define CHECKCUDA(s)
#endif
#define SAFECUDA(s) s;CHECKCUDA(#s)
// A quick class to time gpu kernels using device events
struct StopWatch_GPU
{
cudaEvent_t startTime, stopTime;
StopWatch_GPU() { cudaEventCreate(&startTime); cudaEventCreate(&stopTime); }
~StopWatch_GPU() { cudaEventDestroy(startTime); cudaEventDestroy(stopTime); }
inline void start() { cudaEventRecord(startTime,0); }
inline double stop() { return elapsed(); }
inline double elapsed()
{
cudaEventRecord(stopTime,0);
cudaEventSynchronize(stopTime);
float result;
cudaEventElapsedTime(&result, startTime, stopTime);
return result/1000.0; // 1000 mSec per Sec
}
};
inline int cudaMaxSMEM()
{
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, 0);
return deviceProp.sharedMemPerBlock;
}
template <class T>
inline void cudaMemcpyD2D( T* d_a, const T* d_b, int N )
{
DEBUG_TOTAL(StopWatch_GPU timer; timer.start());
SAFECUDA(cudaMemcpy( d_a, d_b, N*sizeof(T), cudaMemcpyDeviceToDevice ))
//CHECKCUDA("MemcpyD2D Error");
INCR_TOTAL(Transfer,timer.stop());
//cout << "D2D: " << N << endl;
}
template <class T>
inline void cudaMemcpyH2D( T* d_a, const T* h_a, int N )
{
DEBUG_TOTAL(StopWatch_GPU timer; timer.start());
cudaMemcpy( d_a, h_a, N*sizeof(T), cudaMemcpyHostToDevice );
CHECKCUDA("MemcpyH2D Error");
INCR_TOTAL(Transfer,timer.stop());
//cout << "H2D: " << N << endl;
}
template <class T>
inline void cudaMemcpyD2H( T* h_a, const T* d_a, int N )
{
DEBUG_TOTAL(StopWatch_GPU timer; timer.start());
cudaMemcpy( h_a, d_a, N*sizeof(T), cudaMemcpyDeviceToHost );
CHECKCUDA("MemcpyD2H Error");
INCR_TOTAL(Transfer,timer.stop());
//cout << "D2H: " << N << endl;
}
template <class T>
inline void cudaMemcpyH2D( T* d_a, const vector<T>& h_a )
{
cudaMemcpyH2D( d_a, &h_a[0], h_a.size() );
}
template <class T>
inline void cudaMemcpyD2H( vector<T>& h_a, const T* d_a )
{
cudaMemcpyD2H( &h_a[0], d_a, h_a.size() );
}
inline void cudaDelete( void* d_a )
{
cudaFree( d_a );
CHECKCUDA("cudaFree Error");
}
template <class T>
inline T* cudaNew( int N, const T* h_a = NULL )
{
T* d_a = NULL;
cudaMalloc( (void**)&d_a, N*sizeof(T) );
CHECKCUDA("Malloc Error");
if( h_a != NULL )
cudaMemcpyH2D( d_a, h_a, N );
return d_a;
}
template <class T>
inline T* cudaNew( const vector<T>& h_a )
{
return cudaNew( h_a.size(), &h_a[0] );
}
inline void cudaInit(int device = 0)
{
StopWatch initTimer;
int deviceCount;
cudaGetDeviceCount(&deviceCount);
if( deviceCount == 0 ) {
cout << "Error: No devices supporting CUDA" << endl;
exit(1);
}
if( device < 0 ) device = 0;
if( device > deviceCount - 1 ) device = deviceCount - 1;
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, device);
if (deviceProp.major < 1) {
cerr << "Error: " << deviceProp.name << " does not support CUDA." << endl;
exit(1);
}
cudaSetDevice(device);
cerr << "Initializing " << deviceProp.name << "... ";
int* temp = cudaNew<int>(1);
cudaDelete( temp );
cerr << initTimer.stop() << "s" << endl << endl;
}
#endif