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.
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>
18#include "cusparse_v2.h"
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)
29 Index idx = threadIdx.x + blockDim.x * blockIdx.x;
33 int row = inverseRowPtr[idx];
36 for (col = csrRowPtr[idx*2] ; csrColInd[col] < row ; ++col)
38 d += csrVal[col] * y[csrColInd[col]];
40 y[row] = omega * (x[row] - d) / csrVal[col];
47 struct InverseHelper<2>
49 __device__ static void compute(double (&inv)[2][2], const double * a)
51 double d = double(1) / (a[0]*a[3] - a[1]*a[2]);
60 struct InverseHelper<3>
62 __device__ static void compute(double (&inv)[3][3], const double * a)
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]);
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]);
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)
84 Index idx = threadIdx.x + blockDim.x * blockIdx.x;
88 int row = inverseRowPtr[idx];
90 for (int j(0) ; j < BlockSize_ ; ++j)
95 for (col = csrRowPtr[idx*2] ; csrColInd[col] < row ; ++col)
97 for (int h(0) ; h < BlockSize_ ; ++h)
99 for (int w(0) ; w < BlockSize_ ; ++w)
101 d[h] += csrVal[col * BlockSize_ * BlockSize_ + h * BlockSize_ + w] * y[csrColInd[col] * BlockSize_ + w];
106 double inv[BlockSize_][BlockSize_];
107 InverseHelper<BlockSize_>::compute(inv, csrVal + col * BlockSize_ * BlockSize_);
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)
113 temp[j] = x[row * BlockSize_ + j] - d[j];
116 for (int h(0) ; h < BlockSize_ ; ++h)
119 for (int w(0) ; w < BlockSize_ ; ++w)
121 r += inv[h][w] * temp[w];
123 y[row * BlockSize_ + h] = omega * r;
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)
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));
134 int * d_coloring = (int*)Memory::alloc_cuda(m * sizeof(int));
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);
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));
147 status = cusparseDestroyColorInfo(cinfo);
148 if (status != CUSPARSE_STATUS_SUCCESS)
149 throw InternalError(__func__, __FILE__, __LINE__, "cusparseDestroyColorInfo failed with status code: " + stringify(status));
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);
156 //decrement from non existing colors
157 for (int color(0) ; color < ncolors ; ++color)
159 //search for each color
161 for (int i(0) ; i < m ; ++i)
163 if (coloring[i] == color)
169 // if color was not found, decrement all remaining colors and retry search with the same color again
172 for (int i(0) ; i < m ; ++i)
174 if (coloring[i] > color)
184 /*std::cout<<"row to color:"<<std::endl;
185 for (int i(0) ; i < m ; ++i)
187 std::cout<<coloring[i]<<" ";
189 std::cout<<std::endl;*/
190 //std::cout<<"colors: "<<ncolors<<" rows: "<<m<<std::endl;
192 // count rows per color
193 rows_per_color = new int[ncolors];
194 for (int i(0) ; i < ncolors ; ++i)
196 rows_per_color[i] = 0;
198 for (int i(0) ; i < m ; ++i)
200 rows_per_color[coloring[i]] += 1;
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)
208 temp.push_back(std::make_pair(rows_per_color[i], i));
210 std::sort(temp.begin(), temp.end());
212 for (int i(0) ; i < ncolors ; ++i)
214 colors_ascending[i] = temp[i].second;
217 //resort rows per color by ascending row count
218 for (int i(0) ; i < ncolors ; ++i)
220 rows_per_color[i] = temp[i].first;
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;
227 std::cout<<"rows per color:"<<std::endl;
228 for (int i(0) ; i < ncolors ; ++i)
230 std::cout<<i<<" "<<rows_per_color[i]<<std::endl;
232 std::cout<<std::endl;*/
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));
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)
243 int color = colors_ascending[color_i];
244 //search all rows with matching color
245 for (int row(0) ; row < m ; ++row)
247 if (coloring[row] == color)
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;
257 delete [] host_row_ptr;
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));
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));
266 delete [] colors_ascending;
270 cudaDeviceSynchronize();
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)));
278 void cuda_sor_done_symbolic(int * colored_row_ptr, int * rows_per_color, int * inverse_row_ptr)
280 Memory::free_cuda(colored_row_ptr);
281 Memory::free_cuda(inverse_row_ptr);
282 delete [] rows_per_color;
284 cudaDeviceSynchronize();
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)));
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)
295 cudaMemset(y, 0, m * sizeof(double));
298 for (int i(0) ; i < ncolors ; ++i)
300 Index blocksize = Util::cuda_blocksize_spmv;
303 block.x = (unsigned)blocksize;
304 grid.x = (unsigned)ceil((rows_per_color[i])/(double)(block.x));
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];
310 cudaDeviceSynchronize();
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)));
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)
324 cudaMemset(y, 0, m * sizeof(double));
327 for (int i(0) ; i < ncolors ; ++i)
329 Index blocksize = Util::cuda_blocksize_spmv;
332 block.x = (unsigned)blocksize;
333 grid.x = (unsigned)ceil((rows_per_color[i])/(double)(block.x));
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];
339 cudaDeviceSynchronize();
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)));
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);