-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxeigenvaluepower.cu
354 lines (293 loc) · 13.1 KB
/
maxeigenvaluepower.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
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
// Damodar Rajbhandari (2023-Feb-16)
// Code compilation: make mainpower
// Usage: ./maxeigenvaluepower <path-to-matrix-market-file>
// C++ DEPENDENCIES
#include <iostream>
#include <fstream>
#include <sstream>
// CUDA TOOLKIT DEPENDENCIES
#include <cuda_runtime_api.h>
#include <cusparse.h>
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/inner_product.h>
#include <thrust/random.h>
// Terminal output color (just for cosmetic purpose)
#define RST "\x1B[37m" // Reset color to white
#define KGRN "\033[0;32m" // Define green color
#define RD "\x1B[31m" // Define red color
#define FGRN(x) KGRN x RST // Define compiler function for green color
#define FRD(x) RD x RST // Define compiler function for red color
// To check if CUDA API calls are successful
#define CHECK_CUDA(func) \
{ \
cudaError_t status = (func); \
if (status != cudaSuccess) { \
printf("CUDA API failed at line %d with error: %s (%d)\n", \
__LINE__, cudaGetErrorString(status), status); \
exit(EXIT_FAILURE); \
} \
}
// To check if cuSPARSE API calls are successful
#define CHECK_CUSPARSE(func) \
{ \
cusparseStatus_t status = (func); \
if (status != CUSPARSE_STATUS_SUCCESS) { \
printf("CUSPARSE API failed at line %d with error: %s (%d)\n", \
__LINE__, cusparseGetErrorString(status), status); \
exit(EXIT_FAILURE); \
} \
}
// To check if cuSOLVER API calls are successful
#define CHECK_CUSOLVER(func) \
{ \
cusolverStatus_t status = (func); \
if (status != CUSOLVER_STATUS_SUCCESS) { \
printf("CUSOLVER API failed at line %d with error: %s (%d)\n", \
__LINE__, status, status); \
exit(EXIT_FAILURE); \
} \
}
// Generate random number in the range [0, 1)
struct genRandomNumber {
__device__
float operator () (int idx) {
thrust::default_random_engine randGen;
thrust::uniform_real_distribution<float> uniDist;
randGen.discard(idx);
return uniDist(randGen);
}
};
struct CSThrust {
int m; // number of rows
int n; // number of columns
int nnz; // number of non-zero elements
std::string format; // Either "CSC" or "CSR"
thrust::device_vector<int> pointers; // can be column pointer (also called column offsets) or row pointer (also called row offsets)
thrust::device_vector<int> indices; // can be row indices or column indices
thrust::device_vector<float> values; // can be cscValues or csrValues
};
// Read a matrix market file and return a CSR matrix
// See https://math.nist.gov/MatrixMarket/formats.html for more details
void readMTXFile2CSR(const std::string& filepath, CSThrust& csr) {
std::ifstream mtxfile(filepath.c_str(), std::ios::in);
if (!mtxfile.is_open()) {
std::cout << FRD("[ERROR]: ") << "Error opening file: " << filepath << std::endl;
exit(EXIT_FAILURE);
}
// Reading header
std::string header;
std::getline(mtxfile, header);
assert(mtxfile.good());
std::stringstream formatheader(header);
std::string substr[5];
formatheader >> substr[0] >> substr[1] >> substr[2] >> substr[3] >> substr[4];
assert(substr[0] == "%%MatrixMarket");
assert(substr[1] == "matrix");
assert(substr[2] == "coordinate");
if (substr[3].compare("complex") == 0) {
std::cout << FRD("[ERROR]: ") << "Only real and integer valued matrices are supported" << std::endl;
exit(EXIT_FAILURE);
}
// Get symmetry (matrix needs to be square matrix)
bool is_symmetric = false;
if (substr[4] == "symmetric") {
is_symmetric = true;
} else if (substr[4] == "general") {
is_symmetric = false;
} else {
std::cout << FRD("[ERROR]: ") << "Only symmetric and general matrices are supported" << std::endl;
exit(EXIT_FAILURE);
}
// Ignore comments afterwards
while (mtxfile.peek() == '%') {
mtxfile.ignore(2048, '\n');
}
// Read nrows, ncols, nnz
int nrows, ncols, nnz;
mtxfile >> nrows >> ncols >> nnz;
// Set and resize CSR variables
csr.format = "CSR";
csr.m = nrows;
csr.n = ncols;
// Symmetric matrix is a square matrix
if (is_symmetric) {
assert(nrows == ncols);
csr.nnz = 2* nnz - nrows;
} else {
csr.nnz = nnz;
}
// Read the matrix from mtx file, if it's symmetric, then recreate upper triangular part.
// Store in row-major order
std::vector<float> values(nrows * ncols, 0.0);
int row, col;
float val;
int diag_count = 0;
for (int coeff = 0; coeff < nnz; coeff++) {
mtxfile >> row >> col >> val;
if (row == col) {
diag_count++;
values[(row - 1) * ncols + (col - 1)] = val;
if (is_symmetric) {
values[(col - 1) * ncols + (row - 1)] = val;
}
} else {
values[(row - 1) * ncols + (col - 1)] = val;
if (is_symmetric) {
values[(col - 1) * ncols + (row - 1)] = val;
}
}
}
assert(diag_count == nrows);
mtxfile.close();
// Convert to CSR
csr.pointers.resize(csr.m + 1);
csr.indices.resize(csr.nnz);
csr.values.resize(csr.nnz);
// Setting row offset start with 0 index
csr.pointers[0] = 0;
// Extract out the CSR variables
int nnz_idx = 0;
for (int row = 0; row < csr.m; row++) {
for (int col = 0; col < csr.n; col++) {
if (values[row * csr.n + col] != 0.0) {
csr.indices[nnz_idx] = col;
csr.values[nnz_idx] = values[row * csr.n + col];
nnz_idx++;
}
}
csr.pointers[row + 1] = nnz_idx;
}
}
float computeMaxEigenvaluePowerMethod(CSThrust& M, int max_iter) {
assert(M.format == "CSR"); // We only use CSR format
assert(M.m == M.n);
// Initialize x_i to [1 1 ... 1]^T
thrust::device_vector<float> x_i(M.m, 1.0f), x_k(M.m, 0.0f);
float max_eigenvalue;
// CUSPARSE APIs
cusparseHandle_t handle = NULL;
cusparseSpMatDescr_t matM;
cusparseDnVecDescr_t xi, xk;
void *dBuffer = NULL;
size_t bufferSize = 0;
float alpha = 1.0f;
float beta = 0.0f;
CHECK_CUSPARSE( cusparseCreate(&handle) )
CHECK_CUSPARSE( cusparseCreateCsr(&matM, M.m, M.n, M.nnz,
thrust::raw_pointer_cast(M.pointers.data()),
thrust::raw_pointer_cast(M.indices.data()),
thrust::raw_pointer_cast(M.values.data()),
CUSPARSE_INDEX_32I, CUSPARSE_INDEX_32I,
CUSPARSE_INDEX_BASE_ZERO, CUDA_R_32F) )
CHECK_CUSPARSE( cusparseCreateDnVec(&xi, M.m, thrust::raw_pointer_cast(x_i.data()), CUDA_R_32F) )
CHECK_CUSPARSE( cusparseCreateDnVec(&xk, M.m, thrust::raw_pointer_cast(x_k.data()), CUDA_R_32F) )
CHECK_CUSPARSE( cusparseSpMV_bufferSize(handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
&alpha, matM, xi, &beta, xk, CUDA_R_32F,
CUSPARSE_MV_ALG_DEFAULT, &bufferSize) )
CHECK_CUDA( cudaMalloc(&dBuffer, bufferSize) )
// Power iteration method
for (int i = 0; i < max_iter; i++) {
// Compute x_k = A * x_i; generates Krylov subspace
CHECK_CUSPARSE( cusparseSpMV(handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
&alpha, matM, xi, &beta, xk, CUDA_R_32F,
CUSPARSE_MV_ALG_DEFAULT, dBuffer) )
// Compute the L2 norm of x_k
float norm = std::sqrt(thrust::inner_product(x_k.begin(), x_k.end(), x_k.begin(), 0.0f));
// Normalize x_k and update x_i
// thrust::transform(x_k.begin(), x_k.end(), x_i.begin(), x_i.begin(), thrust::placeholders::_1 / norm); // redundant
thrust::transform(x_k.begin(), x_k.end(), x_i.begin(), thrust::placeholders::_1 / norm);
}
// Compute the maximum eigenvalue
CHECK_CUSPARSE( cusparseSpMV(handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
&alpha, matM, xi, &beta, xk, CUDA_R_32F,
CUSPARSE_MV_ALG_DEFAULT, dBuffer) )
max_eigenvalue = thrust::inner_product(x_i.begin(), x_i.end(), x_k.begin(), 0.0f);
// Destroy the handle and descriptors
CHECK_CUSPARSE( cusparseDestroySpMat(matM) )
CHECK_CUSPARSE( cusparseDestroyDnVec(xi) )
CHECK_CUSPARSE( cusparseDestroyDnVec(xk) )
CHECK_CUSPARSE( cusparseDestroy(handle) )
CHECK_CUDA( cudaFree(dBuffer) )
return max_eigenvalue;
}
float computeMaxEigenvaluePowerMethodOptimized(CSThrust& M, int max_iter) {
assert(M.format == "CSR"); // We only use CSR format
assert(M.m == M.n);
// Initialize two vectors x_i and x_k
thrust::device_vector<float> x_i(M.m), x_k(M.m, 0.0f);
// Set x_i := the random vector
thrust::transform(thrust::make_counting_iterator(0),
thrust::make_counting_iterator(M.m),
x_i.begin(),
genRandomNumber());
// CUSPARSE APIs
cusparseHandle_t handle = NULL;
cusparseSpMatDescr_t matM;
cusparseDnVecDescr_t xi, xk;
void *dBuffer = NULL;
size_t bufferSize = 0;
float alpha = 1.0f;
float beta = 0.0f;
CHECK_CUSPARSE( cusparseCreate(&handle) )
CHECK_CUSPARSE( cusparseCreateCsr(&matM, M.m, M.n, M.nnz,
thrust::raw_pointer_cast(M.pointers.data()),
thrust::raw_pointer_cast(M.indices.data()),
thrust::raw_pointer_cast(M.values.data()),
CUSPARSE_INDEX_32I, CUSPARSE_INDEX_32I,
CUSPARSE_INDEX_BASE_ZERO, CUDA_R_32F) )
CHECK_CUSPARSE( cusparseCreateDnVec(&xi, M.m, thrust::raw_pointer_cast(x_i.data()), CUDA_R_32F) )
CHECK_CUSPARSE( cusparseCreateDnVec(&xk, M.m, thrust::raw_pointer_cast(x_k.data()), CUDA_R_32F) )
CHECK_CUSPARSE( cusparseSpMV_bufferSize(handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
&alpha, matM, xi, &beta, xk, CUDA_R_32F,
CUSPARSE_MV_ALG_DEFAULT, &bufferSize) )
CHECK_CUDA( cudaMalloc(&dBuffer, bufferSize) )
float max_eigenvalue(0.0f), max_eigenvalue_prev(0.0f);
float tol = 1e-6; // tolerance for convergence
int itr = 0;
// Power iteration method
while (itr < max_iter) {
// Compute x_k = A * x_i; generates Krylov subspace
CHECK_CUSPARSE( cusparseSpMV(handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
&alpha, matM, xi, &beta, xk, CUDA_R_32F,
CUSPARSE_MV_ALG_DEFAULT, dBuffer) )
// Compute the L2 norm of x_k
float norm = std::sqrt(thrust::inner_product(x_k.begin(), x_k.end(), x_k.begin(), 0.0f));
// Normalize x_k and update x_i
thrust::transform(x_k.begin(), x_k.end(), x_i.begin(), thrust::placeholders::_1 / norm);
// Compute the maximum eigenvalue
CHECK_CUSPARSE( cusparseSpMV(handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
&alpha, matM, xi, &beta, xk, CUDA_R_32F,
CUSPARSE_MV_ALG_DEFAULT, dBuffer) )
max_eigenvalue = thrust::inner_product(x_i.begin(), x_i.end(), x_k.begin(), 0.0f);
if (std::abs(max_eigenvalue - max_eigenvalue_prev) < tol) {
std::cout << FGRN("[NOTE]: ") << "Converged at iterations: " << itr << std::endl;
return max_eigenvalue;
}
max_eigenvalue_prev = max_eigenvalue;
itr++;
}
// Destroy the handle and descriptors
CHECK_CUSPARSE( cusparseDestroySpMat(matM) )
CHECK_CUSPARSE( cusparseDestroyDnVec(xi) )
CHECK_CUSPARSE( cusparseDestroyDnVec(xk) )
CHECK_CUSPARSE( cusparseDestroy(handle) )
CHECK_CUDA( cudaFree(dBuffer) )
std::cout << FRD("[NOTE]: ") << "Maximum number of iterations reached." << std::endl; // no convergence
return max_eigenvalue;
}
int main(int argc, char** argv) {
std::string mtx_filepath;
if (argc > 1) {
mtx_filepath = argv[1];
} else {
std::cout << FRD("[ERROR]: ") << "Please provide a path to a matrix market file." << std::endl;
return EXIT_FAILURE;
}
CSThrust M; // Create a sparse matrix instance
readMTXFile2CSR(mtx_filepath, M); // Read the matrix market file and convert it to csr
// float lambda_max = computeMaxEigenvaluePowerMethod(M, 1000); // Compute the largest eigenvalue
float lambda_max = computeMaxEigenvaluePowerMethodOptimized(M, 1e3); // Compute the largest eigenvalue
std::cout << FGRN("[SUCCESS]: ") << "Max eigenvalue: " << lambda_max << std::endl;
return EXIT_SUCCESS;
}