-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsample9.cu
221 lines (175 loc) · 6.56 KB
/
sample9.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
/******************************************************
* C++ Library of the Linear Conjugate Gradient Methods (LibLCG)
*
* Copyright (C) 2022 Yi Zhang ([email protected])
*
* LibLCG is distributed under a dual licensing scheme. You can
* redistribute it and/or modify it under the terms of the GNU Lesser
* General Public License (LGPL) as published by the Free Software Foundation,
* either version 2 of the License, or (at your option) any later version.
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from
* using the LibLCG, please consider the option to obtain a commercial
* license for a fee. These licenses are offered by the LibLCG developing
* team. As a rule, licenses are provided "as-is", unlimited in time for
* a one time fee. Please send corresponding requests to: [email protected].
* Please do not forget to include some description of your company and the
* realm of its activities. Also add information on how to contact you by
* electronic and paper mail.
******************************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include "../lib/clcg_cuda.h"
void read(std::string filePath, int *pN, int *pnz, cuDoubleComplex **cooVal,
int **cooRowIdx, int **cooColIdx, cuDoubleComplex **b)
{
std::ifstream in(filePath, std::ios::binary);
in.read((char*)pN, sizeof(int));
in.read((char*)pnz, sizeof(int));
*cooVal = new cuDoubleComplex[*pnz]{};
*cooRowIdx = new int[*pnz]{};
*cooColIdx = new int[*pnz]{};
*b = new cuDoubleComplex[*pN]{};
for (int i = 0; i < *pnz; ++i)
{
in.read((char*)&(*cooRowIdx)[i], sizeof(int));
in.read((char*)&(*cooColIdx)[i], sizeof(int));
in.read((char*)&(*cooVal)[i], sizeof(cuDoubleComplex));
}
in.read((char*)(*b), sizeof(cuDoubleComplex)*(*pN));
return;
}
void readAnswer(std::string filePath, int *pN, cuDoubleComplex **x)
{
std::ifstream in(filePath, std::ios::binary);
in.read((char*)pN, sizeof(int));
*x = new cuDoubleComplex[*pN]{};
in.read((char*)(*x), sizeof(cuDoubleComplex)*(*pN));
return;
}
lcg_float avg_error(cuDoubleComplex *a, cuDoubleComplex *b, int n)
{
lcg_float avg = 0.0;
cuDoubleComplex tmp;
for (size_t i = 0; i < n; i++)
{
tmp = clcg_Zdiff(a[i], b[i]);
avg += (tmp.x*tmp.x + tmp.y*tmp.y);
}
return sqrt(avg)/n;
}
// Declare as global variables
cuDoubleComplex one, zero;
void *d_buf;
cusparseSpMatDescr_t smat_A;
int *d_rowIdxA; // COO
int *d_rowPtrA; // CSR
int *d_colIdxA;
cuDoubleComplex *d_A;
cuDoubleComplex *d_B;
void cudaAx(void* instance, cublasHandle_t cub_handle, cusparseHandle_t cus_handle,
cusparseDnVecDescr_t x, cusparseDnVecDescr_t prod_Ax, const int n_size, const int nz_size,
cusparseOperation_t oper_t)
{
one.x = 1.0; one.y = 0.0;
zero.x = 0.0; zero.y = 0.0;
// Calculate the product of A*x
cusparseSpMV(cus_handle, oper_t, &one, smat_A, x, &zero, prod_Ax, CUDA_C_64F, CUSPARSE_SPMV_ALG_DEFAULT, d_buf);
return;
}
int cudaProgress(void* instance, const cuDoubleComplex* m, const lcg_float converge,
const clcg_para* param, const int n_size, const int nz_size, const int k)
{
if (converge <= param->epsilon) {
std::clog << "Iteration-times: " << k << "\tconvergence: " << converge << std::endl;
}
return 0;
}
int main(int argc, char **argv)
{
std::string inputPath = "data/case_1K_cA";
std::string answerPath = "data/case_1K_cB";
int N, nz;
int *rowIdxA, *colIdxA;
cuDoubleComplex *A, *b;
read(inputPath, &N, &nz, &A, &rowIdxA, &colIdxA, &b);
cuDoubleComplex *ans_x;
readAnswer(answerPath, &N, &ans_x);
std::clog << "N = " << N << std::endl;
std::clog << "nz = " << nz << std::endl;
// Create handles
cublasHandle_t cubHandle;
cusparseHandle_t cusHandle;
cublasCreate(&cubHandle);
cusparseCreate(&cusHandle);
// Allocate GPU memory & copy matrix/vector to device
cudaMalloc(&d_A, nz * sizeof(cuDoubleComplex));
cudaMalloc(&d_rowIdxA, nz * sizeof(int));
cudaMalloc(&d_rowPtrA, (N + 1) * sizeof(int));
cudaMalloc(&d_colIdxA, nz * sizeof(int));
cudaMalloc(&d_B, N * sizeof(cuDoubleComplex));
cudaMemcpy(d_A, A, nz * sizeof(cuDoubleComplex), cudaMemcpyHostToDevice);
cudaMemcpy(d_rowIdxA, rowIdxA, nz * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_colIdxA, colIdxA, nz * sizeof(int), cudaMemcpyHostToDevice);
// Convert matrix A from COO format to CSR format
cusparseXcoo2csr(cusHandle, d_rowIdxA, nz, N, d_rowPtrA, CUSPARSE_INDEX_BASE_ZERO);
// Create sparse matrix
cusparseCreateCsr(&smat_A, N, N, nz, d_rowPtrA, d_colIdxA, d_A, CUSPARSE_INDEX_32I,
CUSPARSE_INDEX_32I, CUSPARSE_INDEX_BASE_ZERO, CUDA_C_64F);
// This is just used to get bufferSize;
cusparseDnVecDescr_t dvec_tmp;
cusparseCreateDnVec(&dvec_tmp, N, d_B, CUDA_C_64F);
size_t bufferSize_B, bufferSize_B2;
cusparseSpMV_bufferSize(cusHandle, CUSPARSE_OPERATION_NON_TRANSPOSE, &one, smat_A,
dvec_tmp, &zero, dvec_tmp, CUDA_C_64F, CUSPARSE_MV_ALG_DEFAULT, &bufferSize_B);
cusparseSpMV_bufferSize(cusHandle, CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE, &one, smat_A,
dvec_tmp, &zero, dvec_tmp, CUDA_C_64F, CUSPARSE_MV_ALG_DEFAULT, &bufferSize_B2);
if (bufferSize_B2 > bufferSize_B) bufferSize_B = bufferSize_B2;
cudaMalloc(&d_buf, bufferSize_B);
// Declare an initial solution
clcg_para self_para = clcg_default_parameters();
self_para.epsilon = 1e-6;
self_para.abs_diff = 0;
int ret;
cuDoubleComplex *host_m = new cuDoubleComplex[N];
// Solve with BICG
for (size_t i = 0; i < N; i++)
{
host_m[i].x = 0.0; host_m[i].y = 0.0;
}
ret = clcg_solver_cuda(cudaAx, cudaProgress, host_m, b, N, nz, &self_para, nullptr, cubHandle, cusHandle, CLCG_BICG);
lcg_error_str(ret);
std::clog << "Averaged error (compared with ans_x): " << avg_error(host_m, ans_x, N) << std::endl;
// Solve with BICG_SYM
for (size_t i = 0; i < N; i++)
{
host_m[i].x = 0.0; host_m[i].y = 0.0;
}
ret = clcg_solver_cuda(cudaAx, cudaProgress, host_m, b, N, nz, &self_para, nullptr, cubHandle, cusHandle, CLCG_BICG_SYM);
lcg_error_str(ret);
std::clog << "Averaged error (compared with ans_x): " << avg_error(host_m, ans_x, N) << std::endl;
// Free Host memory
delete[] A;
delete[] rowIdxA;
delete[] colIdxA;
delete[] b;
delete[] ans_x;
delete[] host_m;
// Free Device memory
cudaFree(d_A);
cudaFree(d_rowIdxA);
cudaFree(d_rowPtrA);
cudaFree(d_colIdxA);
cudaFree(d_B);
cusparseDestroyDnVec(dvec_tmp);
cusparseDestroySpMat(smat_A);
cudaFree(d_buf);
// Free handles
cublasDestroy(cubHandle);
cusparseDestroy(cusHandle);
return 0;
}