-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheigh.cpp
408 lines (335 loc) · 12.2 KB
/
eigh.cpp
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <random>
#include <sstream>
#include <stdexcept>
#include <vector>
#ifdef MAGMA
#include "magma_v2.h"
#endif
#if defined(CUDA)
#include <cuda_runtime.h>
#include <cusolverDn.h>
#elif defined(HIP)
#include <hip/hip_runtime.h>
#include "rocblas/rocblas.h"
#include "rocsolver/rocsolver.h"
#define cudaMalloc hipMalloc
#define cudaFree hipFree
#define cudaDeviceReset hipDeviceReset
#define cudaDeviceSynchronize hipDeviceSynchronize
#define cudaMemcpyAsync hipMemcpyAsync
#define cudaMemcpy hipMemcpy
#define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
#define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
#define cudaMemcpyHostToDevice hipMemcpyHostToDevice
#define cudaStream_t hipStream_t
#define cudaStreamDestroy hipStreamDestroy
#define cudaStreamSynchronize hipStreamSynchronize
#define cudaStreamNonBlocking hipStreamNonBlocking
#define cudaStreamCreateWithFlags hipStreamCreateWithFlags
#else
#error "Define CUDA or HIP"
#endif
#if defined(MAGMA)
#define uplo_t magma_uplo_t
#define UPLO_LOWER MagmaLower
#define UPLO_UPPER MagmaUpper
#define vec_mode_t magma_vec_t
#define VEC_MODE_NO MagmaNoVec
#define VEC_MODE_YES MagmaVec
template <typename T>
magma_int_t (*magma_syevd_gpu)(magma_vec_t, magma_uplo_t, magma_int_t, T*, magma_int_t, T*, T*, magma_int_t, T*, magma_int_t, magma_int_t*, magma_int_t, magma_int_t*);
template<>
magma_int_t (*magma_syevd_gpu<float>)(magma_vec_t, magma_uplo_t, magma_int_t, float*, magma_int_t, float*, float*, magma_int_t, float*, magma_int_t, magma_int_t*, magma_int_t, magma_int_t*) = &magma_ssyevd_gpu;
template<>
magma_int_t (*magma_syevd_gpu<double>)(magma_vec_t, magma_uplo_t, magma_int_t, double*, magma_int_t, double*, double*, magma_int_t, double*, magma_int_t, magma_int_t*, magma_int_t, magma_int_t*) = &magma_dsyevd_gpu;
#elif defined(CUDA)
#define uplo_t cublasFillMode_t
#define UPLO_LOWER CUBLAS_FILL_MODE_LOWER
#define UPLO_UPPER CUBLAS_FILL_MODE_UPPER
#define vec_mode_t cusolverEigMode_t
#define VEC_MODE_NO CUSOLVER_EIG_MODE_NOVECTOR
#define VEC_MODE_YES CUSOLVER_EIG_MODE_VECTOR
template <typename T>
cudaDataType cusolver_dtype;
template<>
cudaDataType cusolver_dtype<float> = CUDA_R_32F;
template<>
cudaDataType cusolver_dtype<double> = CUDA_R_64F;
#else
#define uplo_t rocblas_fill
#define UPLO_LOWER rocblas_fill_lower
#define UPLO_UPPER rocblas_fill_upper
#define vec_mode_t rocblas_evect
#define VEC_MODE_NO rocblas_evect_none
#define VEC_MODE_YES rocblas_evect_original
template <typename T>
rocblas_status (*rocsolver_syevd)(rocblas_handle, const rocblas_evect, const rocblas_fill, const rocblas_int, T*, const rocblas_int, T*, T*, rocblas_int*);
template<>
rocblas_status (*rocsolver_syevd<float>)(rocblas_handle, const rocblas_evect, const rocblas_fill, const rocblas_int, float*, const rocblas_int, float*, float*, rocblas_int*) = &rocsolver_ssyevd;
template<>
rocblas_status (*rocsolver_syevd<double>)(rocblas_handle, const rocblas_evect, const rocblas_fill, const rocblas_int, double*, const rocblas_int, double*, double*, rocblas_int*) = &rocsolver_dsyevd;
#endif
constexpr int N_MAX_PRINT = 3;
template <typename T>
void print_matrix(const int &n, const std::vector<T> &A) {
// Print transpose
for (int i = 0; i < n; i++) {
if (N_MAX_PRINT < i && i < n - N_MAX_PRINT - 1) {
if (i == N_MAX_PRINT + 1) {
for (int j = 0; j < (N_MAX_PRINT + 1) * 2 + 1; j++) {
std::printf(" %14s", "...");
}
std::cout << "\n";
}
continue;
}
for (int j = 0; j < n; j++) {
if (N_MAX_PRINT < j && j < n - N_MAX_PRINT - 1) {
if (j == N_MAX_PRINT + 1) {
std::printf(" %14s", "...");
}
continue;
}
std::printf(" %14.6e", A[j * n + i]);
}
std::cout << "\n";
}
std::cout << std::flush;
}
template<typename T>
struct Calculator {
cudaStream_t stream;
int h_info;
int n;
int lda;
uplo_t uplo;
vec_mode_t vec;
#if defined(MAGMA)
magma_queue_t queue;
T *h_wA;
T *h_work;
magma_int_t lwork;
magma_int_t *h_iwork;
magma_int_t liwork;
#elif defined(CUDA)
cusolverDnHandle_t handle;
cusolverDnParams_t params;
int *d_info;
void *d_work;
size_t d_work_size;
void *h_work;
size_t h_work_size;
#else
rocblas_handle handle;
int *d_info;
T *d_work;
#endif
Calculator(int n, uplo_t uplo, vec_mode_t vec) : n{n}, lda{n}, uplo{uplo}, vec{vec} {
#if defined(MAGMA)
// Initialize
magma_init();
magma_queue_create(0, &queue);
#if defined(CUDA)
stream = magma_queue_get_cuda_stream(queue);
#elif defined(HIP)
stream = magma_queue_get_hip_stream(queue);
#endif
// Query work sizes
T lwork_opt;
magma_int_t liwork_opt;
magma_syevd_gpu<T>(vec, uplo, n, nullptr, lda, nullptr, nullptr, lda, &lwork_opt, -1, &liwork_opt, -1, &h_info);
lwork = static_cast<magma_int_t>(lwork_opt);
liwork = liwork_opt;
// Allocate work arrays
h_wA = reinterpret_cast<T*>(malloc(sizeof(T) * lda*n));
h_work = reinterpret_cast<T*>(malloc(sizeof(T) * lwork));
h_iwork = reinterpret_cast<magma_int_t*>(malloc(sizeof(magma_int_t) * liwork));
#elif defined(CUDA)
// Initialize
cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking);
cusolverDnCreate(&handle);
cusolverDnSetStream(handle, stream);
cusolverDnCreateParams(¶ms);
// Query work sizes
cusolverDnXsyevd_bufferSize(
handle, params, vec, uplo, n, cusolver_dtype<T>, nullptr, lda,
cusolver_dtype<T>, nullptr, cusolver_dtype<T>, &d_work_size,
&h_work_size);
// Allocate work arrays
cudaMalloc(reinterpret_cast<void **>(&d_work), d_work_size);
if (0 < h_work_size) {
h_work = reinterpret_cast<void *>(malloc(h_work_size));
}
cudaMalloc(reinterpret_cast<void **>(&d_info), sizeof(int));
#elif defined(HIP)
// Initialize
cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking);
rocblas_create_handle(&handle);
rocblas_set_stream(handle, stream);
// Allocate work arrays
cudaMalloc(reinterpret_cast<void **>(&d_work), sizeof(T) * n);
cudaMalloc(reinterpret_cast<void **>(&d_info), sizeof(int));
#endif
}
~Calculator() {
#if defined(MAGMA)
free(h_iwork);
free(h_wA);
free(h_work);
magma_queue_destroy(queue);
magma_finalize();
#elif defined(CUDA)
cudaFree(d_work);
if (0 < h_work_size) {
free(h_work);
}
cudaFree(d_info);
cusolverDnDestroy(handle);
cudaStreamDestroy(stream);
#elif defined(HIP)
cudaFree(d_work);
cudaFree(d_info);
rocblas_destroy_handle(handle);
cudaStreamDestroy(stream);
#endif
}
void calculate(const T* d_A_input, T* d_W, T* h_W, T* h_V = nullptr) {
// The input array gets overwritten so we work on a copy
T *d_A;
cudaMalloc(reinterpret_cast<void **>(&d_A), sizeof(T) * lda*n);
cudaMemcpyAsync(d_A, d_A_input, sizeof(T) * lda*n, cudaMemcpyDeviceToDevice, stream);
#if defined(MAGMA)
magma_syevd_gpu<T>(vec, uplo, n, d_A, lda, h_W, h_wA, lda, h_work, lwork, h_iwork, liwork, &h_info);
// Copy eigenvectors to GPU
cudaMemcpyAsync(d_W, h_W, sizeof(T) * n, cudaMemcpyHostToDevice, stream);
#elif defined(CUDA)
cusolverDnXsyevd(
handle, params, vec, uplo, n, cusolver_dtype<T>, d_A, lda,
cusolver_dtype<T>, d_W, cusolver_dtype<T>, d_work, d_work_size,
h_work, h_work_size, d_info);
cudaMemcpyAsync(&h_info, d_info, sizeof(int), cudaMemcpyDeviceToHost, stream);
cudaStreamSynchronize(stream);
#elif defined(HIP)
rocsolver_syevd<T>(handle, vec, uplo, n, d_A, lda, d_W, d_work, d_info);
cudaMemcpyAsync(&h_info, d_info, sizeof(int), cudaMemcpyDeviceToHost, stream);
cudaStreamSynchronize(stream);
#endif
if (0 > h_info) {
std::stringstream ss;
ss << "info int: " << -h_info;
throw std::runtime_error(ss.str());
}
// Copy to host
#if !defined(MAGMA)
if (h_W) {
cudaMemcpyAsync(h_W, d_W, sizeof(T) * n, cudaMemcpyDeviceToHost, stream);
cudaStreamSynchronize(stream);
}
#endif
if (h_V) {
cudaMemcpyAsync(h_V, d_A, sizeof(T) * lda*n, cudaMemcpyDeviceToHost, stream);
cudaStreamSynchronize(stream);
}
cudaFree(d_A);
}
};
template <typename T>
void run(int n, int repeat) {
std::cout << "RUN"
<< " n: " << n
<< " repeat: " << repeat
<< " dtype: " << typeid(T).name()
<< std::endl;
const int lda = n;
std::vector<T> h_A(lda * n, 0);
std::vector<T> h_V(lda * n, 0);
std::vector<T> h_W(n, 0);
std::mt19937 gen(n);
std::uniform_real_distribution<T> dis(0.0, 1.0);
// Build a symmetric matrix
for (int i = 0; i < n; i++) {
// Set off-diagonals to a value < 1
for (int j = 0; j < n; j++) {
T val = dis(gen);
h_A[i * n + j] = val;
h_A[j * n + i] = val;
}
// Set diagonal
h_A[i * n + i] += i + 1;
}
std::cout << "Input matrix" << std::endl;
print_matrix(n, h_A);
T *d_A = nullptr;
T *d_W = nullptr;
cudaMalloc(reinterpret_cast<void **>(&d_A), sizeof(T) * h_A.size());
cudaMalloc(reinterpret_cast<void **>(&d_W), sizeof(T) * h_W.size());
cudaMemcpy(d_A, h_A.data(), sizeof(T) * h_A.size(), cudaMemcpyHostToDevice);
cudaDeviceSynchronize();
uplo_t uplo = UPLO_LOWER;
vec_mode_t vec = VEC_MODE_YES;
{
Calculator<T> calc(n, uplo, vec);
// Warm up
calc.calculate(d_A, d_W, h_W.data(), h_V.data());
std::cout << "Output matrix" << std::endl;
print_matrix(n, h_V);
// Run timing
auto t0 = std::chrono::high_resolution_clock::now();
for (int iter = 0; iter < repeat; iter++) {
calc.calculate(d_A, d_W, h_W.data());
}
auto t1 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> time = t1 - t0;
std::cout << "average time " << time.count()*1e-3 / repeat << " s" << std::endl;
}
{
// Run timing recreating handles etc every time
auto t0 = std::chrono::high_resolution_clock::now();
for (int iter = 0; iter < repeat; iter++) {
Calculator<T> calc(n, uplo, vec);
calc.calculate(d_A, d_W, h_W.data());
}
auto t1 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> time = t1 - t0;
std::cout << "average time " << time.count()*1e-3 / repeat << " s (including handle creation)" << std::endl;
}
cudaFree(d_A);
cudaFree(d_W);
}
int main(int argc, char *argv[]) {
// Default values
std::list<int> matrix_sizes = {10};
int repeat = 10;
bool do_double = true;
// Parse args
if (argc > 1) {
matrix_sizes.clear();
char *token = strtok(argv[1], ",");
while (token != NULL) {
matrix_sizes.push_back(std::stoi(token));
token = strtok(NULL, ",");
}
}
if (argc > 2) {
repeat = std::stoi(argv[2]);
}
if (argc > 3) {
if (std::string(argv[3]) != "double") {
do_double = false;
}
}
// Calculate
for (auto n: matrix_sizes) {
if (do_double)
run<double>(n, repeat);
else
run<float>(n, repeat);
}
cudaDeviceReset();
return EXIT_SUCCESS;
}