FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
sor_precond.cu
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6// includes, FEAT
7#include <kernel/base_header.hpp>
8#include <kernel/util/exception.hpp>
9#include <kernel/util/memory_aux.hpp>
10#include <kernel/util/cuda_util.hpp>
11
12#include <vector>
13#include <algorithm>
14
15using namespace FEAT;
16
17
18#include "cusparse_v2.h"
19
20namespace FEAT
21{
22 namespace Solver
23 {
24 namespace Intern
25 {
26 __global__ void cuda_sor_apply_kernel(int m, double * y, const double * x,
27 const double * csrVal, const int * csrRowPtr, const int * csrColInd, double omega, int * inverseRowPtr)
28 {
29 Index idx = threadIdx.x + blockDim.x * blockIdx.x;
30 if (idx >= m)
31 return;
32
33 int row = inverseRowPtr[idx];
34 double d(0.);
35 int col;
36 for (col = csrRowPtr[idx*2] ; csrColInd[col] < row ; ++col)
37 {
38 d += csrVal[col] * y[csrColInd[col]];
39 }
40 y[row] = omega * (x[row] - d) / csrVal[col];
41 }
42
43 template<int n_>
44 struct InverseHelper;
45
46 template<>
47 struct InverseHelper<2>
48 {
49 __device__ static void compute(double (&inv)[2][2], const double * a)
50 {
51 double d = double(1) / (a[0]*a[3] - a[1]*a[2]);
52 inv[0][0] = d*a[3];
53 inv[0][1] = -d*a[1];
54 inv[1][0] = -d*a[2];
55 inv[1][1] = d*a[0];
56 }
57 };
58
59 template<>
60 struct InverseHelper<3>
61 {
62 __device__ static void compute(double (&inv)[3][3], const double * a)
63 {
64 inv[0][0] = a[4]*a[8] - a[5]*a[7];
65 inv[1][0] = a[5]*a[6] - a[3]*a[8];
66 inv[2][0] = a[3]*a[7] - a[4]*a[6];
67 double d = double(1) / (a[0]*inv[0][0] + a[1]*inv[1][0] + a[2]*inv[2][0]);
68 inv[0][0] *= d;
69 inv[1][0] *= d;
70 inv[2][0] *= d;
71 inv[0][1] = d*(a[2]*a[7] - a[1]*a[8]);
72 inv[1][1] = d*(a[0]*a[8] - a[2]*a[6]);
73 inv[2][1] = d*(a[1]*a[6] - a[0]*a[7]);
74 inv[0][2] = d*(a[1]*a[5] - a[2]*a[4]);
75 inv[1][2] = d*(a[2]*a[3] - a[0]*a[5]);
76 inv[2][2] = d*(a[0]*a[4] - a[1]*a[3]);
77 }
78 };
79
80 template <int BlockSize_>
81 __global__ void cuda_sor_bcsr_apply_kernel(int m, double * y, const double * x,
82 const double * csrVal, const int * csrRowPtr, const int * csrColInd, double omega, int * inverseRowPtr)
83 {
84 Index idx = threadIdx.x + blockDim.x * blockIdx.x;
85 if (idx >= m)
86 return;
87
88 int row = inverseRowPtr[idx];
89 double d[BlockSize_];
90 for (int j(0) ; j < BlockSize_ ; ++j)
91 {
92 d[j] = double(0);
93 }
94 int col;
95 for (col = csrRowPtr[idx*2] ; csrColInd[col] < row ; ++col)
96 {
97 for (int h(0) ; h < BlockSize_ ; ++h)
98 {
99 for (int w(0) ; w < BlockSize_ ; ++w)
100 {
101 d[h] += csrVal[col * BlockSize_ * BlockSize_ + h * BlockSize_ + w] * y[csrColInd[col] * BlockSize_ + w];
102 }
103 }
104 }
105
106 double inv[BlockSize_][BlockSize_];
107 InverseHelper<BlockSize_>::compute(inv, csrVal + col * BlockSize_ * BlockSize_);
108
109 //y[row * BlockSize_ + j] = omega * (x[row * BlockSize_ + j] - d[j]) / csrVal[col];
110 double temp[BlockSize_];
111 for (int j(0) ; j < BlockSize_ ; ++j)
112 {
113 temp[j] = x[row * BlockSize_ + j] - d[j];
114 }
115
116 for (int h(0) ; h < BlockSize_ ; ++h)
117 {
118 double r(0);
119 for (int w(0) ; w < BlockSize_ ; ++w)
120 {
121 r += inv[h][w] * temp[w];
122 }
123 y[row * BlockSize_ + h] = omega * r;
124 }
125 }
126
127 void cuda_sor_init_symbolic(int m, int nnz, const double * csrVal, const int * csrRowPtr, const int * csrColInd, int & ncolors, int* & colored_row_ptr, int* & rows_per_color, int* & inverse_row_ptr)
128 {
129 cusparseColorInfo_t cinfo;
130 cusparseStatus_t status = cusparseCreateColorInfo(&cinfo);
131 if (status != CUSPARSE_STATUS_SUCCESS)
132 throw InternalError(__func__, __FILE__, __LINE__, "cusparseCreateColorInfo failed with status code: " + stringify(status));
133
134 int * d_coloring = (int*)Memory::alloc_cuda(m * sizeof(int));
135 double one(1.0);
136
137 cusparseMatDescr_t descr_M = 0;
138 cusparseCreateMatDescr(&descr_M);
139 cusparseSetMatIndexBase(descr_M, CUSPARSE_INDEX_BASE_ZERO);
140 cusparseSetMatType(descr_M, CUSPARSE_MATRIX_TYPE_GENERAL);
141
142 status = cusparseDcsrcolor(Util::Intern::cusparse_handle, m, nnz, descr_M,
143 csrVal, csrRowPtr, csrColInd, &one, &ncolors, d_coloring, NULL, cinfo);
144 if (status != CUSPARSE_STATUS_SUCCESS)
145 throw InternalError(__func__, __FILE__, __LINE__, "cusparseDcsrcolor failed with status code: " + stringify(status));
146
147 status = cusparseDestroyColorInfo(cinfo);
148 if (status != CUSPARSE_STATUS_SUCCESS)
149 throw InternalError(__func__, __FILE__, __LINE__, "cusparseDestroyColorInfo failed with status code: " + stringify(status));
150
151 //std::cout<<"pre colors: "<<ncolors<<" rows: "<<m<<std::endl;
152 int * coloring = new int[m];
153 Memory::memcopy_cuda_to_main(coloring, d_coloring, m * sizeof(int));
154 Memory::free_cuda(d_coloring);
155
156 //decrement from non existing colors
157 for (int color(0) ; color < ncolors ; ++color)
158 {
159 //search for each color
160 bool found(false);
161 for (int i(0) ; i < m ; ++i)
162 {
163 if (coloring[i] == color)
164 {
165 found = true;
166 break;
167 }
168 }
169 // if color was not found, decrement all remaining colors and retry search with the same color again
170 if (!found)
171 {
172 for (int i(0) ; i < m ; ++i)
173 {
174 if (coloring[i] > color)
175 {
176 --coloring[i];
177 }
178 }
179 --ncolors;
180 --color;
181 }
182 }
183
184 /*std::cout<<"row to color:"<<std::endl;
185 for (int i(0) ; i < m ; ++i)
186 {
187 std::cout<<coloring[i]<<" ";
188 }
189 std::cout<<std::endl;*/
190 //std::cout<<"colors: "<<ncolors<<" rows: "<<m<<std::endl;
191
192 // count rows per color
193 rows_per_color = new int[ncolors];
194 for (int i(0) ; i < ncolors ; ++i)
195 {
196 rows_per_color[i] = 0;
197 }
198 for (int i(0) ; i < m ; ++i)
199 {
200 rows_per_color[coloring[i]] += 1;
201 }
202
203 int* colors_ascending = new int[ncolors];
204 //vector of pair<rows, color>
205 std::vector<std::pair<int, int>> temp;
206 for (int i(0) ; i < ncolors ; ++i)
207 {
208 temp.push_back(std::make_pair(rows_per_color[i], i));
209 }
210 std::sort(temp.begin(), temp.end());
211
212 for (int i(0) ; i < ncolors ; ++i)
213 {
214 colors_ascending[i] = temp[i].second;
215 }
216
217 //resort rows per color by ascending row count
218 for (int i(0) ; i < ncolors ; ++i)
219 {
220 rows_per_color[i] = temp[i].first;
221 }
222 /*std::cout<<"colors ascending: "<<std::endl;
223 for (int i(0) ; i < ncolors ; ++i)
224 std::cout<<i<<" "<<colors_ascending[i]<<std::endl;
225 std::cout<<std::endl;
226
227 std::cout<<"rows per color:"<<std::endl;
228 for (int i(0) ; i < ncolors ; ++i)
229 {
230 std::cout<<i<<" "<<rows_per_color[i]<<std::endl;
231 }
232 std::cout<<std::endl;*/
233
234 int * host_irp = new int[m];
235 int * host_crp = new int[2*m];
236 int * host_row_ptr = new int[m+1];
237 Memory::memcopy_cuda_to_main(host_row_ptr, csrRowPtr, (m+1) * sizeof(int));
238
239 //iterate over all colors, by ascending row count
240 int crp_i(0); //index into host_crp
241 for (int color_i(0) ; color_i < ncolors ; ++color_i)
242 {
243 int color = colors_ascending[color_i];
244 //search all rows with matching color
245 for (int row(0) ; row < m ; ++row)
246 {
247 if (coloring[row] == color)
248 {
249 host_crp[crp_i*2] = host_row_ptr[row];
250 host_crp[crp_i*2+1] = host_row_ptr[row+1];
251 host_irp[crp_i] = row;
252 ++crp_i;
253 }
254 }
255 }
256
257 delete [] host_row_ptr;
258
259 inverse_row_ptr = (int*)Memory::alloc_cuda(m * sizeof(int));
260 Memory::memcopy_main_to_cuda(inverse_row_ptr, host_irp, m * sizeof(int));
261
262 colored_row_ptr = (int*)Memory::alloc_cuda(2 * m * sizeof(int));
263 Memory::memcopy_main_to_cuda(colored_row_ptr, host_crp, 2 * m * sizeof(int));
264
265 delete[] coloring;
266 delete [] colors_ascending;
267 delete [] host_irp;
268 delete [] host_crp;
269
270 cudaDeviceSynchronize();
271#ifdef DEBUG
272 cudaError_t last_error(cudaGetLastError());
273 if (cudaSuccess != last_error)
274 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
275#endif
276 }
277
278 void cuda_sor_done_symbolic(int * colored_row_ptr, int * rows_per_color, int * inverse_row_ptr)
279 {
280 Memory::free_cuda(colored_row_ptr);
281 Memory::free_cuda(inverse_row_ptr);
282 delete [] rows_per_color;
283
284 cudaDeviceSynchronize();
285#ifdef DEBUG
286 cudaError_t last_error(cudaGetLastError());
287 if (cudaSuccess != last_error)
288 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
289#endif
290 }
291
292 int cuda_sor_apply(int m, double * y, const double * x, const double * csrVal, const int * csrColInd, int ncolors, double omega,
293 int * colored_row_ptr, int * rows_per_color, int * inverse_row_ptr)
294 {
295 cudaMemset(y, 0, m * sizeof(double));
296
297 int row_offset(0);
298 for (int i(0) ; i < ncolors ; ++i)
299 {
300 Index blocksize = Util::cuda_blocksize_spmv;
301 dim3 grid;
302 dim3 block;
303 block.x = (unsigned)blocksize;
304 grid.x = (unsigned)ceil((rows_per_color[i])/(double)(block.x));
305
306 cuda_sor_apply_kernel<<<grid, block>>>(rows_per_color[i], y, x, csrVal, colored_row_ptr + row_offset * 2, csrColInd, omega, inverse_row_ptr + row_offset);
307 row_offset += rows_per_color[i];
308 }
309
310 cudaDeviceSynchronize();
311#ifdef DEBUG
312 cudaError_t last_error(cudaGetLastError());
313 if (cudaSuccess != last_error)
314 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
315#endif
316
317 return 0;
318 }
319
320 template<int BlockSize_>
321 int cuda_sor_bcsr_apply(int m, double * y, const double * x, const double * csrVal, const int * csrColInd, int ncolors, double omega,
322 int * colored_row_ptr, int * rows_per_color, int * inverse_row_ptr)
323 {
324 cudaMemset(y, 0, m * sizeof(double));
325
326 int row_offset(0);
327 for (int i(0) ; i < ncolors ; ++i)
328 {
329 Index blocksize = Util::cuda_blocksize_spmv;
330 dim3 grid;
331 dim3 block;
332 block.x = (unsigned)blocksize;
333 grid.x = (unsigned)ceil((rows_per_color[i])/(double)(block.x));
334
335 cuda_sor_bcsr_apply_kernel<BlockSize_><<<grid, block>>>(rows_per_color[i], y, x, csrVal, colored_row_ptr + row_offset * 2, csrColInd, omega, inverse_row_ptr + row_offset);
336 row_offset += rows_per_color[i];
337 }
338
339 cudaDeviceSynchronize();
340#ifdef DEBUG
341 cudaError_t last_error(cudaGetLastError());
342 if (cudaSuccess != last_error)
343 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
344#endif
345
346 return 0;
347 }
348 // cuda_sor_bcsr_apply_kernel is hardcoded for BS_ == 2 && BS_ == 3
349 template int cuda_sor_bcsr_apply<2>(int m, double * y, const double * x, const double * csrVal, const int * csrColInd, int ncolors, double omega,
350 int * colored_row_ptr, int * rows_per_color, int * inverse_row_ptr);
351 template int cuda_sor_bcsr_apply<3>(int m, double * y, const double * x, const double * csrVal, const int * csrColInd, int ncolors, double omega,
352 int * colored_row_ptr, int * rows_per_color, int * inverse_row_ptr);
353 }
354 }
355}