-
Notifications
You must be signed in to change notification settings - Fork 0
/
cublas_kernel.cu
244 lines (199 loc) · 7.08 KB
/
cublas_kernel.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <cstdio>
#include <ctime>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <complex>
#include <algorithm>
#include <sys/time.h>
#include <cuda_runtime.h>
#include <cusolverDn.h>
#define DEBUG
#define nstream 2
#ifdef DEBUG
#define CUSOLVER_CHECK(err) (HandlecusolverError(err, __FILE__, __LINE__))
#define CUDA_CHECK(err) (HandleError(err, __FILE__, __LINE__))
#define CUBLAS_CHECK(err) (HandleBlasError(err, __FILE__, __LINE__))
#else
#define CUSOLVER_CHECK(err) (err)
#define CUDA_CHECK(err) (err)
#define CUBLAS_CHECK(err) (err)
#endif
static void HandleBlasError(cublasStatus_t err, const char *file, int line)
{
if (err != CUBLAS_STATUS_SUCCESS)
{
fprintf(stderr, "ERROR: %s in %s at line %d (error-code %d)\n",
cublasGetStatusString(err), file, line, err);
fflush(stdout);
exit(-1);
}
}
static void HandlecusolverError(cusolverStatus_t err, const char *file, int line )
{
if (err != CUSOLVER_STATUS_SUCCESS)
{
fprintf(stderr, "ERROR: %d in %s at line %d, (error-code %d)\n",
err, file, line, err);
fflush(stdout);
exit(-1);
}
}
static void HandleError(cudaError_t err, const char *file, int line)
{
if (err != cudaSuccess)
{
fprintf(stderr, "ERROR: %s in %s at line %d (error-code %d)\n",
cudaGetErrorString(err), file, line, err);
fflush(stdout);
exit(-1);
}
}
template <typename T> void print_matrix(const int &m, const int &n, const T *A, const int &lda);
template <> void print_matrix(const int &m, const int &n, const float *A, const int &lda) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
std::printf("%0.2f ", A[j * lda + i]);
}
std::printf("\n");
}
}
template <> void print_matrix(const int &m, const int &n, const double *A, const int &lda) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
std::printf("%0.2f ", A[j * lda + i]);
}
std::printf("\n");
}
}
template <> void print_matrix(const int &m, const int &n, const cuComplex *A, const int &lda) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
std::printf("%0.2f + %0.2fj ", A[j * lda + i].x, A[j * lda + i].y);
}
std::printf("\n");
}
}
template <>
void print_matrix(const int &m, const int &n, const cuDoubleComplex *A, const int &lda) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
std::printf("%0.2f + %0.2fj ", A[j * lda + i].x, A[j * lda + i].y);
}
std::printf("\n");
}
}
int matmul_c_stream(const int m, cuDoubleComplex *A_, cuDoubleComplex *B_, const int nmat_ ) {
// properties of matrix
const int lda = m;
int nmat = 20;
int nnn = 100;
nmat = nnn;
cublasOperation_t transa=CUBLAS_OP_N;
cublasOperation_t transb=CUBLAS_OP_N;
// cublas setting variebles
cublasHandle_t cublasH;
cudaStream_t stream[nstream];
// printf("solving %d %dx%d matrices multiply with %d streams.\n",nmat,m,m, nstream);
// eigen storage and workspace
cuDoubleComplex *A; // matrix should be stored in pinned memory
cuDoubleComplex *B; // matrix should be stored in pinned memory
CUDA_CHECK(cudaMallocHost((void **)&A,sizeof(cuDoubleComplex)*lda * m * nmat));
CUDA_CHECK(cudaMallocHost((void **)&B,sizeof(cuDoubleComplex)*lda * m * nmat));
// copy to pinned memory
printf("Copy matrix to pinned memory.\n");
for (int i=0;i<nmat;i++) {
std::copy(A_,A_+lda*m,A+i*lda*m);
std::copy(B_,B_+lda*m,B+i*lda*m);
}
// // std::cout << A[m*m-1].x << std::endl;
// // std::cout << B[m*m-1].x << std::endl;
// printf("?\n");
cuDoubleComplex *d_A;
cuDoubleComplex *d_B;
cuDoubleComplex *d_C;
cuDoubleComplex alpha;
cuDoubleComplex beta;
alpha = {1.0,0.0};
beta = {0.0,0.0};
// // step 0: allocate device memory
// printf("allocate device memory.\n");
CUBLAS_CHECK(cublasCreate(&cublasH));
CUDA_CHECK(cudaMalloc(reinterpret_cast<void **>(&d_A), sizeof(cuDoubleComplex) * lda * m * nmat));
CUDA_CHECK(cudaMalloc(reinterpret_cast<void **>(&d_B), sizeof(cuDoubleComplex) * lda * m * nmat));
CUDA_CHECK(cudaMalloc(reinterpret_cast<void **>(&d_C), sizeof(cuDoubleComplex) * lda * m * nmat));
for (int i=0; i < nstream; i++ ) {
// CUDA_CHECK(cudaStreamCreate(&stream[i]));
CUDA_CHECK(cudaStreamCreateWithFlags(&stream[i], cudaStreamNonBlocking));
}
// /* step 3: Copy Host To Device */
CUDA_CHECK(
cudaMemcpy(d_A, A, sizeof(cuDoubleComplex) * lda * m * nmat, cudaMemcpyHostToDevice ));
CUDA_CHECK(
cudaMemcpy(d_B, B, sizeof(cuDoubleComplex) * lda * m * nmat, cudaMemcpyHostToDevice ));
// CUDA timer
cudaEvent_t start[nstream], stop[nstream];
for (int i = 0 ; i < nstream; i++) {
CUDA_CHECK(cudaEventCreate(&start[i]));
CUDA_CHECK(cudaEventCreate(&stop[i]));
}
for (int i=0; i < nstream; i++ ) {
CUDA_CHECK(cudaEventRecord(start[i],stream[i]));
}
// C timer
std::clock_t c_start = std::clock();
struct timeval begin, end;
gettimeofday(&begin, 0);
// Main part
for (int i=0; i < nnn; i++ ) {
printf("begin inner loop %d in %d \n",i,nnn);
int ist = i%nstream;
printf("begin cublasCreate\n");
CUBLAS_CHECK(cublasSetStream(cublasH, stream[ist]));
/* step 5: compute matmul */
printf("begin cublasZgemm\n");
CUBLAS_CHECK(cublasZgemm(cublasH, transa, transb, m, m, m,
&alpha, &d_A[i*m*lda], lda, &d_B[i*m*lda], lda, &beta, &d_C[i*m*lda], lda));
}
CUBLAS_CHECK(cublasDestroy(cublasH));
for (int i = 0; i< nstream; i++){
CUDA_CHECK(cudaEventRecord(stop[i],stream[i]));
}
CUDA_CHECK(cudaDeviceSynchronize());
CUDA_CHECK(cudaFreeHost(A));
CUDA_CHECK(cudaFreeHost(B));
printf("cudaFree\n");
CUDA_CHECK(cudaFree(d_A));
CUDA_CHECK(cudaFree(d_B));
CUDA_CHECK(cudaFree(d_C));
// // // C timer: CPU time
std::clock_t c_end = std::clock();
long double time_elapsed_ms = 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC;
std::cout << "CPU time used in cusolver: " << time_elapsed_ms << " ms\n";
// C timer: WALL time
gettimeofday(&end, 0);
long seconds = end.tv_sec - begin.tv_sec;
long microseconds = end.tv_usec - begin.tv_usec;
double elapsed = seconds + microseconds*1e-6;
printf("Wall Time measured: %.3f seconds.\n", elapsed);
// CUDA timer
for (int i=0; i< nstream; i++) {
CUDA_CHECK(cudaEventSynchronize(stop[i]));
float elapsed_time;
CUDA_CHECK(cudaEventElapsedTime(&elapsed_time, start[i], stop[i]));
float t_sum = 0;
t_sum += elapsed_time;
printf("The %d stream CUDA event time: %gs\n",i,t_sum/1000.0);
}
for (int i=0; i< nstream; i++) {
CUDA_CHECK(cudaEventDestroy(start[i]));
CUDA_CHECK(cudaEventDestroy(stop[i]));
}
// destroy streams
for (int i=0; i < nstream; i++ ) {
CUDA_CHECK(cudaStreamDestroy(stream[i]));
}
// reset device
CUDA_CHECK(cudaDeviceReset());
return EXIT_SUCCESS;
}