FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
voxel_amavanka.cu
1#include <kernel/base_header.hpp>
2#include <kernel/backend.hpp>
3#include <kernel/solver/amavanka_base.hpp>
4#include <kernel/solver/voxel_amavanka.hpp>
5
6#include "cusparse_v2.h"
7#include <cuda/std/utility>
8
9namespace FEAT
10{
11 namespace Solver
12 {
13 namespace Intern
14 {
15 // Wrapper arrays
16 template<int n_>
17 struct MacroDofs
18 {
19 const Index* macro_dofs[n_];
20 };
21
22 template<int n_>
23 struct MDegreeDofs
24 {
25 Index max_degree_dofs[n_];
26 };
27
28 template<int n_>
29 struct DofMacros
30 {
31 const Index* dof_macros[n_];
32 };
33
34 template<int n_>
35 struct MDegreeMacros
36 {
37 Index max_degree_macros[n_];
38 };
39 }
40
41 namespace
42 {
43 /// Allocates device memory, using stream-ordered allocation (cudaMallocAsync) when async is
44 /// requested and available, else a plain synchronous cudaMalloc.
45 inline void* vanka_malloc(std::size_t bytes, bool async, cudaStream_t stream)
46 {
47 void* ptr = nullptr;
48 ptr = async ? Memory::alloc_cuda_async(bytes, Util::StreamWrapper{stream, 0u}) : Memory::alloc_cuda(bytes);
49
50 // cudaError_t st;
51 // #if defined(CUDART_VERSION) && (CUDART_VERSION >= 11020)
52 // st = async ? cudaMallocAsync(&ptr, bytes, stream) : cudaMalloc(&ptr, bytes);
53 // #else
54 // (void)async; (void)stream;
55 // st = cudaMalloc(&ptr, bytes);
56 // #endif
57 // if(st != cudaSuccess)
58 // throw InternalError(__func__, __FILE__, __LINE__, "vanka_malloc failed: " + stringify(cudaGetErrorString(st)));
59 return ptr;
60 }
61
62 /// Frees device memory allocated by vanka_malloc, matching the allocation mode.
63 inline void vanka_free(void* ptr, bool async, cudaStream_t stream)
64 {
65 if(async)
66 {
67 Memory::free_cuda_async(ptr, Util::StreamWrapper{stream, 0u});
68 }
69 else
70 {
71 Memory::free_cuda_async(ptr);
72 }
73 // cudaError_t st;
74 // #if defined(CUDART_VERSION) && (CUDART_VERSION >= 11020)
75 // st = async ? cudaFreeAsync(ptr, stream) : cudaFree(ptr);
76 // #else
77 // (void)async; (void)stream;
78 // st = cudaFree(ptr);
79 // #endif
80 // if(st != cudaSuccess)
81 // throw InternalError(__func__, __FILE__, __LINE__, "vanka_free failed: " + stringify(cudaGetErrorString(st)));
82 }
83 }
84
85 namespace Kernel
86 {
87 template<typename DT_, typename IT_, int n_>
88 __global__ void gather_local_matrices(const typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData mat_wrap,
89 Intern::MacroDofs<n_> macro_dofs_wrapper, Intern::MDegreeDofs<n_> max_degree_wrapper,
90 Index num_macros, Index stride, DT_* __restrict__ _local)
91 {
92 Index idx = threadIdx.x + blockDim.x * blockIdx.x;
93 if(idx >= num_macros)
94 return;
95 typedef DT_ DataType;
96 DT_* local = _local + idx*stride*stride;
97 // note: actually, blas routines work in column-major storage, but with quadratic matrices and
98 // as long as gather and scatter both work on the same storage format (i.e. row-major or column-major), we can simply gather in row-major format
99 Intern::VoxelAmaVankaCore::template gather<DT_, IT_, n_, true>(mat_wrap, local, stride, idx, macro_dofs_wrapper.macro_dofs,
100 max_degree_wrapper.max_degree_dofs, Index(0), Index(0),
101 Index(0), Index(0));
102 }
103
104 template<typename DT_, typename IT_, int n_, bool skip_singular_>
105 __global__ void scatter_local_matrices(typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData vanka_wrap,
106 Intern::MacroDofs<n_> macro_dofs_wrapper, Intern::MDegreeDofs<n_> max_degree_wrapper,
107 Index stride, const DT_* __restrict__ _local, int* __restrict__ macro_mask,
108 const int* __restrict__ coloring_map, Index color_size, int* info_array)
109 {
110 Index idx = threadIdx.x + blockDim.x * blockIdx.x;
111 if(idx >= color_size)
112 return;
113 typedef DT_ DataType;
114 const int imacro = coloring_map[idx];
115 const DT_* local = _local + imacro*stride*stride;
116 // typedef IT_ IndexType;
117 if constexpr(skip_singular_)
118 {
119 const bool singular = info_array[imacro] > 0;
120 // set macro regularity mask
121 macro_mask[imacro] = (singular ? 0 : 1);
122
123 // scatter local matrix
124 if(!singular)
125 {
126 Intern::VoxelAmaVankaCore::template scatter_add<DT_, IT_, n_, true>(vanka_wrap, local, stride, imacro, macro_dofs_wrapper.macro_dofs,
127 max_degree_wrapper.max_degree_dofs, Index(0), Index(0), Index(0), Index(0));
128 }
129 }
130 else
131 {
132 Intern::VoxelAmaVankaCore::template scatter_add<DT_, IT_, n_, true>(vanka_wrap, local, stride, imacro, macro_dofs_wrapper.macro_dofs,
133 max_degree_wrapper.max_degree_dofs, Index(0), Index(0), Index(0), Index(0));
134 }
135 }
136
137 template<typename DT_>
138 __global__ void set_batched_matrix_ptrs(DT_** local_ptrs, DT_* local, Index num_macros, Index stride)
139 {
140 Index idx = threadIdx.x + blockDim.x * blockIdx.x;
141 if(idx >= num_macros)
142 return;
143 local_ptrs[idx] = local + idx*stride*stride;
144 }
145
146 template<typename DT_>
147 void cublas_getrfBatched(DT_** __restrict__ a_array, int* __restrict__ pivot, int* __restrict__ info_array, Index batch_size, Index leading_dimension, Index n);
148
149 template<>
150 void cublas_getrfBatched(double** __restrict__ a_array, int* __restrict__ pivot, int* __restrict__ info_array, Index batch_size, Index leading_dimension, Index n)
151 {
152 cublasStatus_t status = cublasDgetrfBatched(Util::Intern::cublas_handle, int(n), a_array, int(leading_dimension), pivot, info_array, int(batch_size));
153 if(status != CUBLAS_STATUS_SUCCESS)
154 throw InternalError(__func__, __FILE__, __LINE__, "cublasDgetrfBatched failed with status code: " + stringify(status));
155 }
156
157 template<>
158 void cublas_getrfBatched(float** __restrict__ a_array, int* __restrict__ pivot, int* __restrict__ info_array, Index batch_size, Index leading_dimension, Index n)
159 {
160 cublasStatus_t status = cublasSgetrfBatched(Util::Intern::cublas_handle, int(n), a_array, int(leading_dimension), pivot, info_array, int(batch_size));
161 if(status != CUBLAS_STATUS_SUCCESS)
162 throw InternalError(__func__, __FILE__, __LINE__, "cublasSgetrfBatched failed with status code: " + stringify(status));
163 }
164
165 template<typename DT_>
166 void cublas_getriBatched(DT_** __restrict__ a_array, DT_** __restrict__ c_array, int* __restrict__ pivot, int* __restrict__ info_array, Index batch_size, Index leading_dimension_a, Index n, Index leading_dimension_c);
167
168 template<>
169 void cublas_getriBatched(double** __restrict__ a_array, double** __restrict__ c_array, int* __restrict__ pivot, int* __restrict__ info_array, Index batch_size, Index leading_dimension_a, Index n, Index leading_dimension_c)
170 {
171 cublasStatus_t status = cublasDgetriBatched(Util::Intern::cublas_handle, int(n), a_array, int(leading_dimension_a), pivot, c_array, int(leading_dimension_c), info_array, int(batch_size));
172 if(status != CUBLAS_STATUS_SUCCESS)
173 throw InternalError(__func__, __FILE__, __LINE__, "cublasDgetriBatched failed with status code: " + stringify(status));
174 }
175
176 template<>
177 void cublas_getriBatched(float** __restrict__ a_array, float** __restrict__ c_array, int* __restrict__ pivot, int* __restrict__ info_array, Index batch_size, Index leading_dimension_a, Index n, Index leading_dimension_c)
178 {
179 cublasStatus_t status = cublasSgetriBatched(Util::Intern::cublas_handle, int(n), a_array, int(leading_dimension_a), pivot, c_array, int(leading_dimension_c), info_array, int(batch_size));
180 if(status != CUBLAS_STATUS_SUCCESS)
181 throw InternalError(__func__, __FILE__, __LINE__, "cublasSgetriBatched failed with status code: " + stringify(status));
182 }
183
184 template<typename DT_>
185 void batch_invert_matrices(dim3 grid, dim3 block, Index num_macros, Index stride, Index uniform_mat_size, DT_* __restrict__ local, DT_* __restrict__ local_t, int* __restrict__ pivot, int* __restrict__ info_array, bool async, cudaStream_t stream, bool do_sync)
186 {
187 // when running stream-ordered, allocate the pointer-array scratch async as well, so the
188 // whole batched path stays free of device-wide syncs; otherwise reuse the static workspace
189 void* loc_ptr_wrapper = async ? vanka_malloc(2u*sizeof(DT_*)*num_macros, async, stream)
190 : Util::cuda_get_static_memory(2u*sizeof(DT_*)*num_macros);
191 DT_** local_ptrs = static_cast<DT_**>(loc_ptr_wrapper);
192 DT_** local_t_ptrs = local_ptrs + num_macros;
193
194
195 //set matrix ptr arrays
196 Kernel::template set_batched_matrix_ptrs<DT_><<<grid, block>>>(local_ptrs, local, num_macros, stride);
197 Kernel::template set_batched_matrix_ptrs<DT_><<<grid, block>>>(local_t_ptrs, local_t, num_macros, stride);
198
199 if(do_sync)
200 Util::cuda_check_last_error();
201
202 //and now call cublas kernels to invert our matrices
203 cublas_getrfBatched(local_ptrs, pivot, info_array, num_macros, uniform_mat_size, uniform_mat_size);
204 cublas_getriBatched(local_ptrs, local_t_ptrs, pivot, info_array, num_macros, uniform_mat_size, uniform_mat_size, uniform_mat_size);
205
206 if(async)
207 vanka_free(loc_ptr_wrapper, async, stream);
208 }
209
210
211 template<typename DT_, typename IT_, int n_, bool skip_singular_, bool uniform_macros_>
212 __global__ void assemble_unscaled_vanka_device(const typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData mat_wrap,
213 typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData vanka_wrap,
214 Intern::MacroDofs<n_> macro_dofs_wrapper, int* __restrict__ macro_mask,
215 Intern::MDegreeDofs<n_> max_degree_wrapper, const int* __restrict__ coloring_map, Index color_size,
216 Index num_macros, Index stride, DT_ eps, DT_* __restrict__ _local, DT_* __restrict__ _local_t, Index* __restrict__ _pivot)
217 {
218 Index idx = threadIdx.x + blockDim.x * blockIdx.x;
219 if(idx >= color_size)
220 return;
221 typedef DT_ DataType;
222 // typedef IT_ IndexType;
223 if constexpr(skip_singular_)
224 {
225 const int imacro = coloring_map[idx];
226 DT_* local = _local + idx*stride*stride;
227 DT_* local_t = _local_t + idx*stride*stride;
228 Index* pivot = _pivot + idx*stride;
229 const Index** ptr_dofs = macro_dofs_wrapper.macro_dofs;
230 Index* ptr_deg = max_degree_wrapper.max_degree_dofs;
231 const cuda::std::pair<Index, Index> nrc = Intern::VoxelAmaVankaCore::template gather<DT_, IT_, n_, uniform_macros_>(mat_wrap, local, stride, Index(imacro), ptr_dofs,
232 ptr_deg, Index(0), Index(0),
233 Index(0), Index(0));
234 // the approach used for checking the regularity of the local matrix is to check whether
235 //
236 // || I - A*A^{-1} ||_F^2 < eps
237 //
238 // we could try to analyse the pivots returned by invert_matrix function instead, but
239 // unfortunately this approach sometimes leads to false positives
240
241 // make a backup if checking for singularity
242 for(Index i(0); i < nrc.first; ++i)
243 for(Index j(0); j < nrc.second; ++j)
244 local_t[i*stride+j] = local[i*stride+j];
245
246 // invert matrix
247 CudaMath::cuda_invert_matrix(nrc.first, stride, local, pivot);
248
249 // compute (squared) Frobenius norm of (I - A*A^{-1})
250 DataType norm = DataType(0);
251 for(Index i(0); i < nrc.first; ++i)
252 {
253 for(Index j(0); j < nrc.first; ++j)
254 {
255 DataType xij = DataType(i == j ? 1 : 0);
256 for(Index l(0); l < nrc.first; ++l)
257 xij -= local_t[i*stride+l] * local[l*stride+j]; // A_il * (A^{-1})_lj
258 norm += xij * xij;
259 }
260 }
261
262 // is the matrix block singular?
263 // Note: we check for !(norm < eps) instead of (norm >= eps),
264 // because the latter one evaluates to false if norm is NaN,
265 // which would result in a false negative
266 const bool singular = !(norm < eps);
267
268 // set macro regularity mask
269 macro_mask[imacro] = (singular ? 0 : 1);
270
271 // scatter local matrix
272 if(!singular)
273 {
274 Intern::VoxelAmaVankaCore::template scatter_add<DT_, IT_, n_, uniform_macros_>(vanka_wrap, local, stride, imacro, macro_dofs_wrapper.macro_dofs,
275 max_degree_wrapper.max_degree_dofs, Index(0), Index(0), Index(0), Index(0));
276 }
277
278 // reformat local matrix
279 for(Index i(0); i < nrc.first; ++i)
280 for(Index j(0); j < nrc.second; ++j)
281 local[i*stride+j] = DataType(0);
282 }
283 else
284 {
285 const int imacro = coloring_map[idx];
286 DT_* local = _local + idx*stride*stride;
287 Index* pivot = _pivot + idx*stride;
288 const cuda::std::pair<Index, Index> nrc = Intern::VoxelAmaVankaCore::template gather<DT_, IT_, n_, uniform_macros_>(mat_wrap, local, stride, imacro, macro_dofs_wrapper.macro_dofs,
289 max_degree_wrapper.max_degree_dofs, Index(0), Index(0),
290 Index(0), Index(0));
291 // invert matrix
292 CudaMath::cuda_invert_matrix(nrc.first, stride, local, pivot);
293
294 Intern::VoxelAmaVankaCore::template scatter_add<DT_, IT_, n_, uniform_macros_>(vanka_wrap, local, stride, imacro, macro_dofs_wrapper.macro_dofs,
295 max_degree_wrapper.max_degree_dofs, Index(0), Index(0),
296 Index(0), Index(0));
297
298 // reformat local matrix
299 for(Index i(0); i < nrc.first; ++i)
300 for(Index j(0); j < nrc.second; ++j)
301 local[i*stride+j] = DataType(0);
302 }
303 }
304
305 template<typename DT_, typename IT_, int n_, bool skip_singular_>
306 __global__ void scale_vanka_rows_device(typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData vanka_wrap, const DT_ omega,
307 Intern::DofMacros<n_> wrapper_dof_macros, Intern::MDegreeMacros<n_> wrapper_max_degree_macros, const int* __restrict__ m_mask, const Index all_row_size)
308 {
309 Index idx = threadIdx.x + blockDim.x * blockIdx.x;
310 if(idx >= all_row_size * n_)
311 return;
312
313 // find meta row and actual row index for the current row
314 Index xrow = idx % all_row_size;
315 Index meta_col = idx / all_row_size;
316 Index rowc = 0;
317 Index next_row = vanka_wrap.tensor_counts[0];
318 while(next_row <= xrow)
319 {
320 xrow -= next_row;
321 ++rowc;
322 next_row = vanka_wrap.tensor_counts[1+rowc];
323 }
324 const Index max_row_degree_size = wrapper_max_degree_macros.max_degree_macros[rowc]+1;
325 const Index* row_img_idx = wrapper_dof_macros.dof_macros[rowc] + xrow*max_row_degree_size;
326 const Index real_degree = *row_img_idx;
327 const int hw = vanka_wrap.blocksizes[Index(1) + CudaMath::cuda_min(Index(rowc),Index(1))];
328 // const int num_rows = int(next_row);
329 DT_* vals = vanka_wrap.data_arrays[rowc*n_ + meta_col];
330 const IT_* row_ptr = vanka_wrap.row_arrays[rowc*n_+meta_col];
331 const IT_* col_idx = vanka_wrap.col_arrays[rowc*n_+meta_col];
332 const int hb = vanka_wrap.blocksizes[meta_col +1];
333 const bool meta_diag = (rowc == meta_col);
334 // careful, num rows is counted against native elements, not raw elements
335 Intern::VoxelAmaVankaCore::scale_row<DT_, IT_, skip_singular_>(vals, omega, row_ptr, col_idx, row_img_idx+1, real_degree, hw, hb, xrow, m_mask, meta_diag);
336
337 }
338 }
339
340
341
342
343
344
345
346
347
348
349
350
351 namespace Arch
352 {
353 template<typename DT_, typename IT_, int n_>
354 void assemble_vanka_device(const Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>& mat_wrap,
355 Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>& vanka_wrap, const std::vector<Memory::Arbiter>& d_macro_dofs,
356 const std::vector<Memory::Arbiter>& d_dof_macros, Memory::Arbiter& d_macro_mask, const std::vector<Index>& max_degree_dofs,
357 const std::vector<Index>& max_degree_macros, const Adjacency::ColoringDataHandler& coloring_data,
358 Index num_macros, Index stride, DT_ omega, DT_ eps, bool skip_singular, bool uniform_macros)
359 {
360 typedef DT_ DataType;
361 const Index blocksize = Util::cuda_blocksize_vanka_assembly;
362 dim3 grid;
363 dim3 block;
364 block.x = (unsigned int)blocksize;
365 //this is probably to much data
366 grid.x = (unsigned int)(ceil(double(coloring_data.get_max_size())/double(block.x)));
367 //further extract internal arrays
368 Intern::DofMacros<n_> graphs_dof_macros;
369 Intern::MacroDofs<n_> graphs_macro_dofs;
370 Intern::MDegreeDofs<n_> degree_dofs;
371 Intern::MDegreeMacros<n_> degree_macros;
372 std::vector<Memory::TypedView<Index>> dmd_views(d_macro_dofs.size());
373 std::vector<Memory::TypedView<Index>> ddm_views(d_dof_macros.size());
374 for(int i = 0; i < n_; ++i)
375 {
376 dmd_views[i] = d_macro_dofs[i].view(Memory::Location::cuda, Memory::Access::read);
377 ddm_views[i] = d_dof_macros[i].view(Memory::Location::cuda, Memory::Access::read);
378 graphs_dof_macros.dof_macros[i] = ddm_views[i].get_r();
379 graphs_macro_dofs.macro_dofs[i] = dmd_views[i].get_r();
380 degree_dofs.max_degree_dofs[i] = max_degree_dofs.at(i);
381 degree_macros.max_degree_macros[i] = max_degree_macros.at(i);
382 }
383 Memory::TypedView<int> dmm_view(d_macro_mask.view(Memory::Location::cuda, Memory::Access::read_write));
384 //auto& coloring_map = coloring_data.get_coloring_maps();
385 const Index max_color_size = coloring_data.get_max_size();
386 // allocate arrays for local matrix
387 DataType* local = (DataType*)Memory::alloc_cuda(max_color_size*stride*stride*sizeof(DataType));
388 Memory::format_cuda(local, DataType(0), max_color_size*stride*stride);
389 DataType* local_t = nullptr;
390 if(skip_singular)
391 {
392 local_t = (DataType*)Memory::alloc_cuda_async(max_color_size*stride*stride*sizeof(DataType));
393 Memory::format_cuda(local_t, DataType(0), max_color_size*stride*stride);
394 }
395 Index* pivot = (Index*)Memory::alloc_cuda_async(max_color_size*stride*sizeof(Index));
396 Memory::format_cuda(pivot, Index(0), max_color_size*stride);
397
398 const Memory::TypedView<int> color_view = coloring_data.color_map_view(Memory::Location::cuda, Memory::Access::read);
399 for(Index k = 0; k < coloring_data.get_num_colors(); ++k)
400 {
401 if(uniform_macros)
402 {
403 if(skip_singular)
404 Solver::Kernel::template assemble_unscaled_vanka_device<DT_, IT_, n_, true, true><<<grid, block>>>
405 (mat_wrap.raw_data, vanka_wrap.raw_data, graphs_macro_dofs,
406 dmm_view.get_w(), degree_dofs,
407 &color_view.get_r()[coloring_data.get_color_offset(k)], coloring_data.get_color_size(k),
408 num_macros, stride, eps,
409 local, local_t, pivot);
410 else
411 Solver::Kernel::template assemble_unscaled_vanka_device<DT_, IT_, n_, false, true><<<grid, block>>>
412 (mat_wrap.raw_data, vanka_wrap.raw_data, graphs_macro_dofs,
413 dmm_view.get_w(), degree_dofs,
414 &color_view.get_r()[coloring_data.get_color_offset(k)], coloring_data.get_color_size(k),
415 num_macros, stride, eps,
416 local, local_t, pivot);
417 }
418 else
419 {
420 if(skip_singular)
421 Solver::Kernel::template assemble_unscaled_vanka_device<DT_, IT_, n_, true, false><<<grid, block>>>
422 (mat_wrap.raw_data, vanka_wrap.raw_data, graphs_macro_dofs,
423 dmm_view.get_w(), degree_dofs,
424 &color_view.get_r()[coloring_data.get_color_offset(k)], coloring_data.get_color_size(k),
425 num_macros, stride, eps,
426 local, local_t, pivot);
427 else
428 Solver::Kernel::template assemble_unscaled_vanka_device<DT_, IT_, n_, false, false><<<grid, block>>>
429 (mat_wrap.raw_data, vanka_wrap.raw_data, graphs_macro_dofs,
430 dmm_view.get_w(), degree_dofs,
431 &color_view.get_r()[coloring_data.get_color_offset(k)], coloring_data.get_color_size(k),
432 num_macros, stride, eps,
433 local, local_t, pivot);
434 }
435 }
436 //check for cuda error in our kernel
437 Util::cuda_check_last_error();
438 color_view.release();
439 Memory::free_cuda_async((void*)pivot);
440 if(local_t)
441 Memory::free_cuda_async((void*)local_t);
442 Memory::free_cuda_async((void*)local);
443 // get max row_size
444 const Index all_row_size = vanka_wrap.raw_data.get_all_rows_size();
445 block.x = (unsigned int)Util::cuda_blocksize_spmv;
446 grid.x = (unsigned int)(ceil(double(all_row_size*n_)/double(block.x)));
447 if(skip_singular)
448 Solver::Kernel::template scale_vanka_rows_device<DT_, IT_, n_, true><<<grid, block>>>(vanka_wrap.raw_data, omega, graphs_dof_macros, degree_macros, dmm_view.get_r(), all_row_size);
449 else
450 Solver::Kernel::template scale_vanka_rows_device<DT_, IT_, n_, false><<<grid, block>>>(vanka_wrap.raw_data, omega, graphs_dof_macros, degree_macros, dmm_view.get_r(), all_row_size);
451
452 //check for cuda error in our kernel
453 Util::cuda_check_last_error();
454 }
455
456 // only works with uniform macros
457 template<typename DT_, typename IT_, int n_>
458 void assemble_vanka_device_batched(const Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>& mat_wrap,
459 Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>& vanka_wrap, const std::vector<Memory::Arbiter>& d_macro_dofs,
460 const std::vector<Memory::Arbiter>& d_dof_macros, Memory::Arbiter& d_macro_mask, const std::vector<Index>& max_degree_dofs,
461 const std::vector<Index>& max_degree_macros, const Adjacency::ColoringDataHandler& coloring_data,
462 Index num_macros, Index stride, Index actual_matrix_size, DT_ omega, bool skip_singular, bool synchronize)
463 {
464 typedef DT_ DataType;
465 // stream-ordered (async) allocation requires memory-pool support; if unavailable we fall
466 // back to the synchronous path and always synchronize at the end
467 const bool stream_ordered = Util::cuda_get_stream_ordered_alloc_supported();
468 const cudaStream_t stream = 0; // default stream, as everywhere else in this file
469 const bool async = stream_ordered; // use cudaMallocAsync only when pools are supported
470 const bool do_sync = synchronize || !stream_ordered; // force the error-check/sync when we cannot be async
471 const Index blocksize = Util::cuda_blocksize_vanka_assembly;
472 dim3 grid;
473 dim3 block;
474 //further extract internal arrays
475 Intern::DofMacros<n_> graphs_dof_macros;
476 Intern::MacroDofs<n_> graphs_macro_dofs;
477 Intern::MDegreeDofs<n_> degree_dofs;
478 Intern::MDegreeMacros<n_> degree_macros;
479 std::vector<Memory::TypedView<Index>> dmd_views(d_macro_dofs.size());
480 std::vector<Memory::TypedView<Index>> ddm_views(d_dof_macros.size());
481 for(int i = 0; i < n_; ++i)
482 {
483 dmd_views[i] = d_macro_dofs[i].view(Memory::Location::cuda, Memory::Access::read);
484 ddm_views[i] = d_dof_macros[i].view(Memory::Location::cuda, Memory::Access::read);
485 graphs_dof_macros.dof_macros[i] = ddm_views[i].get_r();
486 graphs_macro_dofs.macro_dofs[i] = dmd_views[i].get_r();
487 degree_dofs.max_degree_dofs[i] = max_degree_dofs.at(i);
488 degree_macros.max_degree_macros[i] = max_degree_macros.at(i);
489 }
490 Memory::TypedView<int> dmm_view(d_macro_mask.view(Memory::Location::cuda, Memory::Access::read_write));
491 // allocate arrays for local matrix (stream-ordered when async, else synchronous cudaMalloc)
492 const std::size_t loc_bytes = std::size_t(num_macros)*stride*stride*sizeof(DataType);
493 const std::size_t pivot_bytes = std::size_t(num_macros)*actual_matrix_size*sizeof(int);
494 const std::size_t info_bytes = std::size_t(num_macros)*sizeof(int);
495 DataType* local = (DataType*)vanka_malloc(loc_bytes, async, stream);
496 cudaMemsetAsync(local, 0, loc_bytes, stream);
497 DataType* local_t = (DataType*)vanka_malloc(loc_bytes, async, stream);
498 cudaMemsetAsync(local_t, 0, loc_bytes, stream);
499 int* pivot = (int*)vanka_malloc(pivot_bytes, async, stream);
500 cudaMemsetAsync(pivot, 0, pivot_bytes, stream);
501 // do NOT use static memory here, since the batch invert kernel also uses it (in the sync path)
502 int* info_array = (int*)vanka_malloc(info_bytes, async, stream);
503 cudaMemsetAsync(info_array, 0, info_bytes, stream);
504 // gather and invert
505 block.x = (unsigned int)blocksize;
506 //this is probably to much data
507 grid.x = (unsigned int)(ceil(double(num_macros)/double(block.x)));
508 Solver::Kernel::template gather_local_matrices<DT_, IT_, n_><<<grid, block>>>(mat_wrap.raw_data, graphs_macro_dofs, degree_dofs, num_macros, stride, local);
509 Solver::Kernel::batch_invert_matrices(grid, block, num_macros, stride, actual_matrix_size, local, local_t, pivot, info_array, async, stream, do_sync);
510 // now scatter local matrices... this requires coloring
511 block.x = (unsigned int)blocksize;
512 //this is probably to much data
513 grid.x = (unsigned int)(ceil(double(coloring_data.get_max_size())/double(block.x)));
514 const Memory::TypedView<int> color_view = coloring_data.color_map_view(Memory::Location::cuda, Memory::Access::read);
515 for(Index k = 0; k < coloring_data.get_num_colors(); ++k)
516 {
517 if(skip_singular)
518 Solver::Kernel::template scatter_local_matrices<DT_, IT_, n_, true><<<grid, block>>>
519 (vanka_wrap.raw_data, graphs_macro_dofs,
520 degree_dofs, stride, local_t, dmm_view.get_w(),
521 &color_view.get_r()[coloring_data.get_color_offset(k)],
522 coloring_data.get_color_size(k), info_array);
523 else
524 Solver::Kernel::template scatter_local_matrices<DT_, IT_, n_, false><<<grid, block>>>
525 (vanka_wrap.raw_data, graphs_macro_dofs,
526 degree_dofs, stride, local_t, dmm_view.get_w(),
527 &color_view.get_r()[coloring_data.get_color_offset(k)],
528 coloring_data.get_color_size(k), info_array);
529 }
530 //check for cuda error in our kernel
531 if(do_sync)
532 Util::cuda_check_last_error();
533 color_view.release();
534 vanka_free((void*)info_array, async, stream);
535 vanka_free((void*)pivot, async, stream);
536 vanka_free((void*)local_t, async, stream);
537 vanka_free((void*)local, async, stream);
538 // get max row_size
539 const Index all_row_size = vanka_wrap.raw_data.get_all_rows_size();
540 block.x = (unsigned int)Util::cuda_blocksize_spmv;
541 grid.x = (unsigned int)(ceil(double(all_row_size*n_)/double(block.x)));
542 if(skip_singular)
543 Solver::Kernel::template scale_vanka_rows_device<DT_, IT_, n_, true><<<grid, block>>>(vanka_wrap.raw_data, omega, graphs_dof_macros, degree_macros, dmm_view.get_r(), all_row_size);
544 else
545 Solver::Kernel::template scale_vanka_rows_device<DT_, IT_, n_, false><<<grid, block>>>(vanka_wrap.raw_data, omega, graphs_dof_macros, degree_macros, dmm_view.get_r(), all_row_size);
546
547 //check for cuda error in our kernel
548 if(do_sync)
549 Util::cuda_check_last_error();
550
551
552
553 }
554 }
555 }
556}
557
558
559using namespace FEAT;
560using namespace FEAT::Solver;
561
562//########################## oneThreadperMacro kernel ################################################
563//#########################1x1 kernels###############################################################
564template void Arch::assemble_vanka_device(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint32_t, 1>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint32_t, 1>&, const std::vector<Memory::Arbiter>&,
565 const std::vector<Memory::Arbiter>&, Memory::Arbiter&,const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, double, double, bool, bool);
566template void Arch::assemble_vanka_device(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint32_t, 1>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint32_t, 1>&, const std::vector<Memory::Arbiter>&,
567 const std::vector<Memory::Arbiter>&, Memory::Arbiter&,const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, float, float, bool, bool);
568template void Arch::assemble_vanka_device(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint64_t, 1>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint64_t, 1>&, const std::vector<Memory::Arbiter>&,
569 const std::vector<Memory::Arbiter>&, Memory::Arbiter&,const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, double, double, bool, bool);
570template void Arch::assemble_vanka_device(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint64_t, 1>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint64_t, 1>&, const std::vector<Memory::Arbiter>&,
571 const std::vector<Memory::Arbiter>&, Memory::Arbiter&,const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, float, float, bool, bool);
572//#########################2x2 kernels###############################################################
573template void Arch::assemble_vanka_device(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint32_t, 2>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint32_t, 2>&, const std::vector<Memory::Arbiter>&,
574 const std::vector<Memory::Arbiter>&, Memory::Arbiter&,const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, double, double, bool, bool);
575template void Arch::assemble_vanka_device(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint32_t, 2>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint32_t, 2>&, const std::vector<Memory::Arbiter>&,
576 const std::vector<Memory::Arbiter>&, Memory::Arbiter&,const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, float, float, bool, bool);
577template void Arch::assemble_vanka_device(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint64_t, 2>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint64_t, 2>&, const std::vector<Memory::Arbiter>&,
578 const std::vector<Memory::Arbiter>&, Memory::Arbiter&,const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, double, double, bool, bool);
579template void Arch::assemble_vanka_device(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint64_t, 2>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint64_t, 2>&, const std::vector<Memory::Arbiter>&,
580 const std::vector<Memory::Arbiter>&, Memory::Arbiter&,const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, float, float, bool, bool);
581
582
583/** For copy pasting new nxn kernels...
584template void Arch::assemble_vanka_device(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint32_t, n_>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint32_t, n_>&, const std::vector<Memory::Arbiter>&,
585 const std::vector<Memory::Arbiter>&, Memory::Arbiter&,const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, double, double, bool, bool);
586template void Arch::assemble_vanka_device(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint32_t, n_>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint32_t, n_>&, const std::vector<Memory::Arbiter>&,
587 const std::vector<Memory::Arbiter>&, Memory::Arbiter&,const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, float, float, bool, bool);
588template void Arch::assemble_vanka_device(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint64_t, n_>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint64_t, n_>&, const std::vector<Memory::Arbiter>&,
589 const std::vector<Memory::Arbiter>&, Memory::Arbiter&,const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, double, double, bool, bool);
590template void Arch::assemble_vanka_device(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint64_t, n_>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint64_t, n_>&, const std::vector<Memory::Arbiter>&,
591 const std::vector<Memory::Arbiter>&, Memory::Arbiter&,const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, float, float, bool, bool);
592//*/
593
594//######################### cuBlasbasedKernels ##############################################################
595//#########################1x1 kernels###############################################################
596template void Arch::assemble_vanka_device_batched(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint64_t, 1>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint64_t, 1>&, const std::vector<Memory::Arbiter>&, const std::vector<Memory::Arbiter>&,
597 Memory::Arbiter&, const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, Index, double, bool, bool);
598template void Arch::assemble_vanka_device_batched(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint64_t, 1>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint64_t, 1>&, const std::vector<Memory::Arbiter>&, const std::vector<Memory::Arbiter>&,
599 Memory::Arbiter&, const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, Index, float, bool, bool);
600template void Arch::assemble_vanka_device_batched(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint32_t, 1>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint32_t, 1>&, const std::vector<Memory::Arbiter>&, const std::vector<Memory::Arbiter>&,
601 Memory::Arbiter&, const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, Index, double, bool, bool);
602template void Arch::assemble_vanka_device_batched(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint32_t, 1>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint32_t, 1>&, const std::vector<Memory::Arbiter>&, const std::vector<Memory::Arbiter>&,
603 Memory::Arbiter&, const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, Index, float, bool, bool);
604
605
606//#########################2x2 kernels###############################################################
607template void Arch::assemble_vanka_device_batched(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint64_t, 2>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint64_t, 2>&, const std::vector<Memory::Arbiter>&, const std::vector<Memory::Arbiter>&,
608 Memory::Arbiter&, const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, Index, double, bool, bool);
609template void Arch::assemble_vanka_device_batched(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint64_t, 2>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint64_t, 2>&, const std::vector<Memory::Arbiter>&, const std::vector<Memory::Arbiter>&,
610 Memory::Arbiter&, const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, Index, float, bool, bool);
611template void Arch::assemble_vanka_device_batched(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint32_t, 2>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint32_t, 2>&, const std::vector<Memory::Arbiter>&, const std::vector<Memory::Arbiter>&,
612 Memory::Arbiter&, const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, Index, double, bool, bool);
613template void Arch::assemble_vanka_device_batched(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint32_t, 2>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint32_t, 2>&, const std::vector<Memory::Arbiter>&, const std::vector<Memory::Arbiter>&,
614 Memory::Arbiter&, const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, Index, float, bool, bool);
615
616
617
618
619/** For copy pasting new nxn kernels...
620template void Arch::assemble_vanka_device_batched(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint64_t, n_>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint64_t, n_>&, const std::vector<Memory::Arbiter>&, const std::vector<Memory::Arbiter>&,
621 Memory::Arbiter&, const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, Index, double, bool, bool);
622template void Arch::assemble_vanka_device_batched(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint64_t, n_>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint64_t, n_>&, const std::vector<Memory::Arbiter>&, const std::vector<Memory::Arbiter>&,
623 Memory::Arbiter&, const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, Index, float, bool, bool);
624template void Arch::assemble_vanka_device_batched(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint32_t, n_>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<double, std::uint32_t, n_>&, const std::vector<Memory::Arbiter>&, const std::vector<Memory::Arbiter>&,
625 Memory::Arbiter&, const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, Index, double, bool, bool);
626template void Arch::assemble_vanka_device_batched(const FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint32_t, n_>&, FEAT::Solver::Intern::CSRTupleMatrixWrapper<float, std::uint32_t, n_>&, const std::vector<Memory::Arbiter>&, const std::vector<Memory::Arbiter>&,
627 Memory::Arbiter&, const std::vector<Index>&, const std::vector<Index>&, const Adjacency::ColoringDataHandler&, Index, Index, Index, float, bool, bool);
628//*/