-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcuda_solver.cu
179 lines (141 loc) · 5.06 KB
/
cuda_solver.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
#include "cuda_solver.cuh"
cudaSolver::~cudaSolver() {
// Destroy cuSolver Handle
if (handle) {
cusolverStatus_t cu_err = cusolverDnDestroy(handle);
if (cu_err != CUSOLVER_STATUS_SUCCESS) {
printf("cudaSolver::~cudaSolver Failed to destroy cuSolver handle!\n");
exit(EXIT_FAILURE);
}
}
// Free cuSolver buffer
if (buffer) {
cudaFree(buffer);
}
if (info) {
cudaFree(info);
}
}
errorEnum cudaSolver::setCuSolver(int iSector, int numberOfParameters_,
cudaStream_t correlationStream) {
numberOfParameters = numberOfParameters_;
d_mat = cudaPyramidManager.getGlobalABChi(iSector);
d_vec = d_mat + numberOfParameters * numberOfParameters;
// cuSolver synchronizes with the correlation Stream
cusolverStatus_t cu_err = cusolverDnSetStream(handle, correlationStream);
if (cu_err != CUSOLVER_STATUS_SUCCESS) {
printf("Failed to set cuSolver stream!\n");
exit(EXIT_FAILURE);
}
// Allocate cuSolver buffer in GPU[ 0 ]
cudaError err = cudaSetDevice(0);
if (err != cudaSuccess) {
printf("Failed to set device to 0 (error code %s)!\n",
cudaGetErrorString(err));
return error_cuSolver;
}
int allocatedCusolverBufferSize = bufferSize;
cu_err = cusolverDnSpotrf_bufferSize(handle, CUBLAS_FILL_MODE_LOWER,
numberOfParameters, d_mat,
numberOfParameters, &bufferSize);
if (cu_err != CUSOLVER_STATUS_SUCCESS) {
printf("Error: Cholesky factorization buffer allocation failed\n");
return error_cuSolver;
}
#if DEBUG_SOLVER_CUDA
printf("cudaSolver::setCuSolver Computed cuSolver bufferSize = %d\n",
bufferSize);
#endif
if (bufferSize > allocatedCusolverBufferSize) {
cudaFree(buffer);
err = cudaMalloc(&buffer, sizeof(float) * bufferSize);
if (err != cudaSuccess) {
printf("Failed to allocate cusolver buffer (error code %s)!\n",
cudaGetErrorString(err));
return error_cuSolver;
}
#if DEBUG_SOLVER_CUDA
printf("cudaSolver::setCuSolver allocating cuSolver bufferSize = %d\n",
bufferSize);
#endif
}
return error_none;
}
errorEnum cudaSolver::callCusolver(int iSector, float *chi) {
/** The dense matrices are assumed to be stored in column-major order in
* memory by cuSolver.*/
cusolverStatus_t status;
d_mat = cudaPyramidManager.getGlobalABChi(iSector);
d_vec = d_mat + numberOfParameters * numberOfParameters;
// Run cuSolver from GPU#0
cudaError err = cudaSetDevice(0);
if (err != cudaSuccess) {
printf("Failed to set device (error code %s)!\n", cudaGetErrorString(err));
return error_cuSolver;
}
#if DEBUG_SOLVER_CUDA
printf("cudaSolver::callCusolver Matrix A and vector b to be solved:\n");
float *A = new float[numberOfParameters * numberOfParameters];
float *b = new float[numberOfParameters];
cudaMemcpy(A, d_mat, numberOfParameters * numberOfParameters * sizeof(float),
cudaMemcpyDeviceToHost);
cudaMemcpy(b, d_vec, numberOfParameters * sizeof(float),
cudaMemcpyDeviceToHost);
for (int j = 0; j < numberOfParameters; ++j) {
for (int i = 0; i < numberOfParameters; ++i) {
printf("%14.4e", A[i * numberOfParameters + j]);
}
printf(" %14.4e\n", b[j]);
}
delete[] A;
delete[] b;
printf("cudaSolver::callCusolver bufferSize = %d\n", bufferSize);
#endif
// Factorization A = L * L H
status =
cusolverDnSpotrf(handle, CUBLAS_FILL_MODE_LOWER, numberOfParameters,
d_mat, numberOfParameters, buffer, bufferSize, info);
if (status != CUSOLVER_STATUS_SUCCESS) {
printf("Error: Cholesky factorization failed\n");
assert(false);
}
#if DEBUG_SOLVER_CUDA
printf(
"cudaSolver::callCusolver Factorized L ( as in A = L * LH) Matrix :\n");
float *L = new float[numberOfParameters * numberOfParameters];
cudaMemcpy(L, d_mat, numberOfParameters * numberOfParameters * sizeof(float),
cudaMemcpyDeviceToHost);
for (int j = 0; j < numberOfParameters; ++j) {
for (int i = 0; i < numberOfParameters; ++i) {
printf("%14.4e", L[i * numberOfParameters + j]);
}
printf("\n");
}
delete[] L;
#endif
// Solver
status = cusolverDnSpotrs(handle, CUBLAS_FILL_MODE_LOWER, numberOfParameters,
1, d_mat, numberOfParameters, d_vec,
numberOfParameters, info);
// cudaDeviceSynchronize();
if (status != CUSOLVER_STATUS_SUCCESS) {
printf("Error: Cholesky solver failed\n");
assert(false);
}
// Save the chi
cudaMemcpyAsync(chi, &d_mat[numberOfParameters * (numberOfParameters + 1)],
sizeof(float), cudaMemcpyDeviceToHost, stream);
#if DEBUG_SOLVER_CUDA
printf("cudaSolver::callCusolver Update:\n");
float *temp = new float[numberOfParameters];
cudaMemcpy(temp, d_vec, numberOfParameters * sizeof(float),
cudaMemcpyDeviceToHost);
for (int i = 0; i < numberOfParameters; ++i) {
printf("%14.4e", temp[i]);
}
printf("\n");
fflush(stdout);
delete[] temp;
#endif
return error_none;
}