FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
amavanka_base.hpp
1
2// FEAT3: Finite Element Analysis Toolbox, Version 3
3// Copyright (C) 2010 by Stefan Turek & the FEAT group
4// FEAT3 is released under the GNU General Public License version 3,
5// see the file 'copyright.txt' in the top level directory for details.
6
7#pragma once
8
9#include <kernel/lafem/null_matrix.hpp>
10#include <kernel/lafem/sparse_matrix_bcsr.hpp>
11#include <kernel/lafem/sparse_matrix_csr.hpp>
12#include <kernel/lafem/saddle_point_matrix.hpp>
13#include <kernel/lafem/tuple_matrix.hpp>
14
15#ifdef __CUDACC__
16#include <cuda/std/utility>
17#endif
18
19namespace FEAT
20{
21 namespace Solver
22 {
24 namespace Intern
25 {
36 template<typename SystemMatrix_>
37#ifndef DOXYGEN
38 struct AmaVankaMatrixHelper;
39#else // DOXYGEN
40 struct AmaVankaMatrixHelper
41 {
43 typedef ... VankaMatrix;
45 static constexpr int num_blocks = 0;
46
67 template<int n_>
68 static void fill_ptr_wrapper(
69 CSRTupleMatrixWrapper<typename SystemMatrix_::DataType, typename SystemMatrix_::IndexType, n_>& wrap,
70 const SystemMatrix_& matrix, Memory::Location mem_loc, int row, int col);
71
92 template<int n_>
93 static void fill_column_info(
94 CSRTupleMatrixWrapper<typename SystemMatrix_::DataType, typename SystemMatrix_::IndexType, n_>& wrap,
95 const SystemMatrix_& matrix, int row, int col);
96
108 static bool compare(const SystemMatrix_& matrix_a, const SystemMatrix_& matrix_b);
109 };
110#endif // DOXYGEN
111
120 template<typename DT_, typename IT_, int n_>
121 struct CSRTupleMatrixWrapper
122 {
123 typedef DT_ DataType;
124 typedef IT_ IndexType;
125 static constexpr int n = n_;
126 Memory::TypedView<DataType> data_views[n_*n_];
127 Memory::TypedView<IndexType> col_views[n_*n_];
128 Memory::TypedView<IndexType> row_views[n_*n_];
129
130 // the views are non-copyable, so we keep the raw pointers in a sub struct here,
131 // because they need to be copied for the cuda kernel launches
132 struct RawData
133 {
134 //arrays hold pointer in an row major order, i.e. (1,1), (1,2) ...
135 DataType* data_arrays[n_*n_];
136 const IndexType* col_arrays[n_*n_];
137 const IndexType* row_arrays[n_*n_];
138 IndexType used_elements[n_*n_];
139 //since the layout is symmetric, we only need to save the row and colum counts of the first
140 //meta row, which is entirely determined by the rows of the first entry and the columns
141 //of the first meta row
142 //required??
143 IndexType tensor_counts[n_+1];
144 //since the overall layout has to be symmteric and quadratic, we only have to save the
145 //the block dimension of the first column
146 int blocksizes[n_+1];
147
148
149 // the mapping from the actual quadratic indices to the n_+1 sized row is simply
150 // (i,j) -> (i+min(i,1), j+1)
151 CUDA_HOST std::pair<IndexType, IndexType> get_tensor_size(Index i, Index j) const
152 {
153 return std::make_pair(tensor_counts[i+std::min(i,Index(1))], tensor_counts[j+1]);
154 }
155
156 CUDA_HOST std::pair<IndexType, IndexType> get_block_sizes(Index i, Index j) const
157 {
158 return std::make_pair(blocksizes[i+std::min(i,Index(1))], blocksizes[j+1]);
159 }
160
161 CUDA_HOST Index get_all_rows_size() const
162 {
163 Index val = tensor_counts[0];
164 for(int i = 1; i < n_; ++i)
165 val += tensor_counts[i+1];
166 return val;
167 }
168 } raw_data;
169
170 CSRTupleMatrixWrapper() = default;
171
172 CSRTupleMatrixWrapper(CSRTupleMatrixWrapper&& other)
173 {
174 for(int i = 0; i < n_*n_; ++i)
175 {
176 this->data_views[i] = std::forward<Memory::TypedView<DataType>>(other.data_views[i]);
177 this->col_views[i] = std::forward<Memory::TypedView<IndexType>>(other.col_views[i]);
178 this->row_views[i] = std::forward<Memory::TypedView<IndexType>>(other.row_views[i]);
179 this->raw_data.data_arrays[i] = other.raw_data.data_arrays[i];
180 this->raw_data.row_arrays[i] = other.raw_data.row_arrays[i];
181 this->raw_data.col_arrays[i] = other.raw_data.col_arrays[i];
182 this->raw_data.used_elements[i] = other.raw_data.used_elements[i];
183 }
184 for(int i = 0; i <= n_; ++i)
185 {
186 this->raw_data.tensor_counts[i] = other.raw_data.tensor_counts[i];
187 this->raw_data.blocksizes[i] = other.raw_data.blocksizes[i];
188 }
189 }
190
191 CUDA_HOST FEAT::String print()
192 {
193 FEAT::String str("--------------------------\n\nMatrixWrapper: \n");
194 for(int i = 0; i < n_; ++i)
195 {
196 for(int j = 0; j < n_; ++j)
197 {
198 str += FEAT::String("Block (") + stringify(i) + ", " + stringify(j) + ")\n";
199 str += FEAT::String("Tensor size (") + stringify(raw_data.tensor_counts[Index(i)+std::min(Index(i),Index(1))])
200 + ", " + stringify(raw_data.tensor_counts[j+1]) + "), Blocksize ("
201 + stringify(raw_data.blocksizes[Index(i)+std::min(Index(i),Index(1))]) + ", " + stringify(raw_data.blocksizes[j+1])
202 + ")\n";
203 }
204 }
205 str += String("\n--------------------------\n");
206 return str;
207 }
208 };
209
210 template<typename DT_, typename IT_, int bh_, int bw_>
211 struct AmaVankaMatrixHelper<LAFEM::NullMatrix<DT_, IT_, bh_, bw_>>
212 {
213 typedef LAFEM::SparseMatrixBCSR<DT_, IT_, bh_, bw_> VankaMatrix;
214 static constexpr int num_blocks = 1;
215
216 template<int n_>
217 static void fill_ptr_wrapper(CSRTupleMatrixWrapper<DT_, IT_, n_>& wrap, const LAFEM::NullMatrix<DT_, IT_, bh_, bw_>&, Memory::Location, int row, int col)
218 {
219 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
220 wrap.data_views[n_*row + col].release();
221 wrap.col_views[n_*row + col].release();
222 wrap.row_views[n_*row + col].release();
223 wrap.raw_data.data_arrays[n_*row + col] = nullptr;
224 wrap.raw_data.col_arrays[n_*row + col] = nullptr;
225 wrap.raw_data.row_arrays[n_*row + col] = nullptr;
226 wrap.raw_data.used_elements[n_*row + col] = IT_(0);
227 }
228
229 template<int n_>
230 static void fill_column_info(CSRTupleMatrixWrapper<DT_, IT_, n_>& wrap, const LAFEM::NullMatrix<DT_, IT_, bh_, bw_>& matrix, int row, int col)
231 {
232 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
233 ASSERTM(row == 0, "Row counter has to be zero");
234 #ifndef DEBUG
235 (void)row;
236 #endif
237 if(col == 0)
238 {
239 wrap.raw_data.tensor_counts[0] = matrix.num_rows();
240 wrap.raw_data.blocksizes[0] = bh_;
241 }
242 wrap.raw_data.tensor_counts[col+1] = matrix.num_cols();
243 wrap.raw_data.blocksizes[col+1] = bw_;
244 }
245
246 static void fill_row_helper(const LAFEM::NullMatrix<DT_, IT_, bh_, bw_>& matrix, Index* accum_row_idx, Index* accum_row_ctr, int row_block, int curr_row)
247 {
248 // fill with list from 0 to num_rows in raw perspective
249 std::generate(accum_row_idx+curr_row, accum_row_idx+curr_row + matrix.num_rows_raw(), [n=0]() mutable{return n++;});
250 // fill with constant value
251 std::fill(accum_row_ctr+curr_row, accum_row_ctr+curr_row + matrix.num_rows_raw(), Index(row_block));
252 }
253
254 /*static bool compare(const LAFEM::NullMatrix<DT_, IT_, bh_, bw_>& matrix_a, const LAFEM::NullMatrix<DT_, IT_, bh_, bw_>& matrix_b)
255 {
256 return (matrix_a.num_rows() == matrix_b.num_rows()) && (matrix_b.num_cols() == matrix_b.num_cols());
257 }*/
258 }; // struct AmaVankaMatrixHelper<LAFEM::NullMatrix<...>>
259
260 template<typename DT_, typename IT_>
261 struct AmaVankaMatrixHelper<LAFEM::SparseMatrixCSR<DT_, IT_>>
262 {
263 typedef LAFEM::SparseMatrixCSR<DT_, IT_> VankaMatrix;
264 static constexpr int num_blocks = 1;
265
266 template<int n_>
267 static void fill_ptr_wrapper(CSRTupleMatrixWrapper<DT_, IT_, n_>& wrap, const LAFEM::SparseMatrixCSR<DT_, IT_>& matrix, Memory::Location mem_loc, int row, int col)
268 {
269 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
270 wrap.data_views[n_*row + col] = const_cast<LAFEM::SparseMatrixCSR<DT_, IT_>&>(matrix).val_view_rw(mem_loc);
271 wrap.col_views[n_*row + col] = matrix.col_idx_view_r(mem_loc);
272 wrap.row_views[n_*row + col] = matrix.row_ptr_view_r(mem_loc);
273 wrap.raw_data.data_arrays[n_*row + col] = wrap.data_views[n_*row + col].get_w();
274 wrap.raw_data.col_arrays[n_*row + col] = wrap.col_views[n_*row + col].get_r();
275 wrap.raw_data.row_arrays[n_*row + col] = wrap.row_views[n_*row + col].get_r();
276 wrap.raw_data.used_elements[n_*row + col] = IT_(matrix.num_nzes());
277 }
278
279 template<int n_>
280 static void fill_column_info(CSRTupleMatrixWrapper<DT_, IT_, n_>& wrap, const LAFEM::SparseMatrixCSR<DT_, IT_>& matrix, int row, int col)
281 {
282 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
283 ASSERTM(row == 0, "Row counter has to be zero");
284 #ifndef DEBUG
285 (void)row;
286 #endif
287 if(col == 0)
288 {
289 wrap.raw_data.tensor_counts[0] = IT_(matrix.num_rows());
290 wrap.raw_data.blocksizes[0] = 1;
291 }
292 wrap.raw_data.tensor_counts[col+1] = IT_(matrix.num_cols());
293 wrap.raw_data.blocksizes[col+1] = 1;
294 }
295
296 static void fill_row_helper(const LAFEM::SparseMatrixCSR<DT_, IT_>& matrix, Index* accum_row_idx, Index* accum_row_ctr, int row_block, int curr_row)
297 {
298 ASSERTM((accum_row_idx+curr_row + matrix.num_rows()) == std::find(accum_row_idx+curr_row, accum_row_idx+curr_row + matrix.num_rows(), ~Index(0)), "Row array was already written to");
299 ASSERTM((accum_row_ctr+curr_row + matrix.num_rows()) == std::find(accum_row_ctr+curr_row, accum_row_ctr+curr_row + matrix.num_rows(), ~Index(0)), "Row array was already written to");
300 // fill with list from 0 to num_rows in raw perspective
301 std::generate(accum_row_idx+curr_row, accum_row_idx+curr_row + matrix.num_rows(), [n=0]() mutable{return n++;});
302 // fill with constant value
303 std::fill(accum_row_ctr+curr_row, accum_row_ctr+curr_row + matrix.num_rows(), Index(row_block));
304 }
305
306 /*static bool compare(const LAFEM::SparseMatrixCSR<DT_, IT_>& matrix_a, const LAFEM::SparseMatrixCSR<DT_, IT_>& matrix_b)
307 {
308 auto tmp = matrix_a.clone(LAFEM::CloneMode::Weak);
309 tmp.axpy(matrix_b, DT_(-1));
310 const DT_ tol = Math::pow(Math::eps<DT_>(), DT_(0.7));
311 return (tmp.norm_frobenius()/(Math::sqrt(DT_(tmp.size()))) <= tol);
312 }*/
313 }; // struct AmaVankaMatrixHelper<LAFEM::SparseMatrixCSR<...>>
314
315 template<typename DT_, typename IT_, int bh_, int bw_>
316 struct AmaVankaMatrixHelper<LAFEM::SparseMatrixBCSR<DT_, IT_, bh_, bw_>>
317 {
318 typedef LAFEM::SparseMatrixBCSR<DT_, IT_, bh_, bw_> VankaMatrix;
319 static constexpr int num_blocks = 1;
320
321 template<int n_>
322 static void fill_ptr_wrapper(CSRTupleMatrixWrapper<DT_, IT_, n_>& wrap, const LAFEM::SparseMatrixBCSR<DT_, IT_, bh_, bw_>& matrix, Memory::Location mem_loc, int row, int col)
323 {
324 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
325 wrap.data_views[n_*row + col] = const_cast<LAFEM::SparseMatrixBCSR<DT_, IT_, bh_, bw_>&>(matrix).val_view_raw_rw(mem_loc);
326 wrap.col_views[n_*row + col] = matrix.col_idx_view_r(mem_loc);
327 wrap.row_views[n_*row + col] = matrix.row_ptr_view_r(mem_loc);
328 wrap.raw_data.data_arrays[n_*row + col] = wrap.data_views[n_*row + col].get_w();
329 wrap.raw_data.col_arrays[n_*row + col] = wrap.col_views[n_*row + col].get_r();
330 wrap.raw_data.row_arrays[n_*row + col] = wrap.row_views[n_*row + col].get_r();
331 wrap.raw_data.used_elements[n_*row + col] = IT_(matrix.num_nzes());
332 }
333
334 template<int n_>
335 static void fill_column_info(CSRTupleMatrixWrapper<DT_, IT_, n_>& wrap, const LAFEM::SparseMatrixBCSR<DT_, IT_, bh_, bw_>& matrix, int row, int col)
336 {
337 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
338 ASSERTM(row == 0, "Row counter has to be zero");
339 #ifndef DEBUG
340 (void)row;
341 #endif
342 if(col == 0)
343 {
344 wrap.raw_data.tensor_counts[0] = IT_(matrix.num_rows());
345 wrap.raw_data.blocksizes[0] = bh_;
346 }
347 wrap.raw_data.tensor_counts[col+1] = IT_(matrix.num_cols());
348 wrap.raw_data.blocksizes[col+1] = bw_;
349 }
350
351 static void fill_row_helper(const LAFEM::SparseMatrixBCSR<DT_, IT_, bh_, bw_>& matrix, Index* accum_row_idx, Index* accum_row_ctr, int row_block, int curr_row)
352 {
353 ASSERTM((accum_row_idx+curr_row + matrix.num_rows_raw()) == std::find(accum_row_idx+curr_row, accum_row_idx+curr_row + matrix.num_rows_raw(), ~Index(0)), "Row array was already written to");
354 ASSERTM((accum_row_ctr+curr_row + matrix.num_rows_raw()) == std::find(accum_row_ctr+curr_row, accum_row_ctr+curr_row + matrix.num_rows_raw(), ~Index(0)), "Row array was already written to");
355 // fill with list from 0 to num_rows in raw perspective
356 std::generate(accum_row_idx+curr_row, accum_row_idx+curr_row + matrix.num_rows_raw(), [n=0]() mutable{return n++;});
357 // fill with constant value
358 std::fill(accum_row_ctr+curr_row, accum_row_ctr+curr_row + matrix.num_rows_raw(), Index(row_block));
359 }
360
361 /*static bool compare(const LAFEM::SparseMatrixBCSR<DT_, IT_, bh_, bw_>& matrix_a, const LAFEM::SparseMatrixBCSR<DT_, IT_, bh_, bw_>& matrix_b)
362 {
363 auto tmp = matrix_a.clone(LAFEM::CloneMode::Weak);
364 tmp.axpy(matrix_b, DT_(-1));
365 const DT_ tol = Math::pow(Math::eps<DT_>(), DT_(0.7));
366 return (tmp.norm_frobenius()/(Math::sqrt(DT_(tmp.size()))) <= tol);
367 }*/
368 }; // struct AmaVankaMatrixHelper<LAFEM::SparseMatrixBCSR<...>>
369
370 template<typename First_, typename... Rest_>
371 struct AmaVankaMatrixHelper<LAFEM::TupleMatrixRow<First_, Rest_...>>
372 {
373 typedef LAFEM::TupleMatrixRow<
374 typename AmaVankaMatrixHelper<First_>::VankaMatrix,
375 typename AmaVankaMatrixHelper<Rest_>::VankaMatrix...> VankaMatrix;
376
377 typedef typename First_::DataType DataType;
378 typedef typename First_::IndexType IndexType;
379 template<int n_>
380 static void fill_ptr_wrapper(CSRTupleMatrixWrapper<DataType, IndexType, n_>& wrap, const LAFEM::TupleMatrixRow<First_, Rest_...>& matrix, Memory::Location mem_loc, int row, int col)
381 {
382 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
383 AmaVankaMatrixHelper<First_>::fill_ptr_wrapper(wrap, matrix.first(), mem_loc, row, col);
384 AmaVankaMatrixHelper<LAFEM::TupleMatrixRow<Rest_...>>::fill_ptr_wrapper(wrap, matrix.rest(), mem_loc, row, col+1);
385 }
386
387 template<int n_>
388 static void fill_column_info(CSRTupleMatrixWrapper<DataType, IndexType, n_>& wrap, const LAFEM::TupleMatrixRow<First_, Rest_...>& matrix, int row, int col)
389 {
390 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
391 ASSERTM(row == 0, "Row counter has to be zero");
392 AmaVankaMatrixHelper<First_>::fill_column_info(wrap, matrix.first(), row, col);
393 AmaVankaMatrixHelper<LAFEM::TupleMatrixRow<Rest_...>>::fill_column_info(wrap, matrix.rest(), row, col+1);
394 }
395
396 static void fill_row_helper(const LAFEM::TupleMatrixRow<First_, Rest_...>& matrix, Index* accum_row_idx, Index* accum_row_ctr, int row_block, int curr_row)
397 {
398 AmaVankaMatrixHelper<First_>::fill_row_helper(matrix.first(), accum_row_idx, accum_row_ctr, row_block, curr_row);
399 }
400
401 /*static bool compare(const LAFEM::TupleMatrixRow<First_, Rest_...>& matrix_a, const LAFEM::TupleMatrixRow<First_, Rest_...>& matrix_b)
402 {
403 bool compare = AmaVankaMatrixHelper<First_>::compare(matrix_a.first(), matrix_b.first());
404 compare &= AmaVankaMatrixHelper<LAFEM::TupleMatrixRow<Rest_...>>::compare(matrix_a.rest(), matrix_b.rest());
405 return compare;
406 }*/
407 }; // struct AmaVankaMatrixHelper<LAFEM::TupleMatrixRow<...>>
408
409 template<typename First_>
410 struct AmaVankaMatrixHelper<LAFEM::TupleMatrixRow<First_>>
411 {
412 public:
413 typedef LAFEM::TupleMatrixRow<typename AmaVankaMatrixHelper<First_>::VankaMatrix> VankaMatrix;
414
415 typedef typename First_::DataType DataType;
416 typedef typename First_::IndexType IndexType;
417 template<int n_>
418 static void fill_ptr_wrapper(CSRTupleMatrixWrapper<DataType, IndexType, n_>& wrap, const LAFEM::TupleMatrixRow<First_>& matrix, Memory::Location mem_loc, int row, int col)
419 {
420 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
421 AmaVankaMatrixHelper<First_>::fill_ptr_wrapper(wrap, matrix.first(), mem_loc, row, col);
422 }
423
424 template<int n_>
425 static void fill_column_info(CSRTupleMatrixWrapper<DataType, IndexType, n_>& wrap, const LAFEM::TupleMatrixRow<First_>& matrix, int row, int col)
426 {
427 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
428 ASSERTM(row == 0, "Row counter has to be zero");
429 AmaVankaMatrixHelper<First_>::fill_column_info(wrap, matrix.first(), row, col);
430 }
431
432 static void fill_row_helper(const LAFEM::TupleMatrixRow<First_>& matrix, Index* accum_row_idx, Index* accum_row_ctr, int row_block, int curr_row)
433 {
434 AmaVankaMatrixHelper<First_>::fill_row_helper(matrix.first(), accum_row_idx, accum_row_ctr, row_block, curr_row);
435 }
436
437 /*static bool compare(const LAFEM::TupleMatrixRow<First_>& matrix_a, const LAFEM::TupleMatrixRow<First_>& matrix_b)
438 {
439 bool compare = AmaVankaMatrixHelper<First_>::compare(matrix_a.first(), matrix_b.first());
440 return compare;
441 }*/
442 }; // struct AmaVankaMatrixHelper<LAFEM::TupleMatrixRow<First_>>
443
444 template<typename FirstRow_, typename... RestRows_>
445 struct AmaVankaMatrixHelper<LAFEM::TupleMatrix<FirstRow_, RestRows_...>>
446 {
447 typedef LAFEM::TupleMatrix<
448 typename AmaVankaMatrixHelper<FirstRow_>::VankaMatrix,
449 typename AmaVankaMatrixHelper<RestRows_>::VankaMatrix...> VankaMatrix;
450 static constexpr int num_blocks = VankaMatrix::num_col_blocks;
451
452 typedef typename FirstRow_::DataType DataType;
453 typedef typename FirstRow_::IndexType IndexType;
454 template<int n_>
455 static void fill_ptr_wrapper(CSRTupleMatrixWrapper<DataType, IndexType, n_>& wrap, const LAFEM::TupleMatrix<FirstRow_, RestRows_...>& matrix, Memory::Location mem_loc, int row, int col)
456 {
457 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
458 AmaVankaMatrixHelper<FirstRow_>::fill_ptr_wrapper(wrap, matrix.first(), mem_loc, row, col);
459 AmaVankaMatrixHelper<LAFEM::TupleMatrix<RestRows_...>>::fill_ptr_wrapper(wrap, matrix.rest(), mem_loc, row+1, col);
460 }
461
462 template<int n_>
463 static void fill_column_info(CSRTupleMatrixWrapper<DataType, IndexType, n_>& wrap, const LAFEM::TupleMatrix<FirstRow_, RestRows_...>& matrix, int row, int col)
464 {
465 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
466 ASSERTM(row == 0, "Row counter has to be zero");
467 AmaVankaMatrixHelper<FirstRow_>::fill_column_info(wrap, matrix.first(), row, col);
468 }
469
470 static void fill_row_helper(const LAFEM::TupleMatrix<FirstRow_, RestRows_...>& matrix, Index* accum_row_idx, Index* accum_row_ctr, int row_block, int curr_row)
471 {
472 AmaVankaMatrixHelper<FirstRow_>::fill_row_helper(matrix.first(), accum_row_idx, accum_row_ctr, row_block, curr_row);
473 AmaVankaMatrixHelper<LAFEM::TupleMatrix<RestRows_...>>::fill_row_helper(matrix.rest(), accum_row_idx, accum_row_ctr, row_block+1, curr_row+matrix.first().num_rows_raw());
474 }
475
476 /*static bool compare(const LAFEM::TupleMatrix<FirstRow_, RestRows_...>& matrix_a, const LAFEM::TupleMatrix<FirstRow_, RestRows_...>& matrix_b)
477 {
478 bool compare = AmaVankaMatrixHelper<FirstRow_>::compare(matrix_a.first(), matrix_b.first());
479 compare &= AmaVankaMatrixHelper<LAFEM::TupleMatrix<RestRows_...>>::compare(matrix_a.rest(), matrix_b.rest());
480 return compare;
481 }*/
482 }; // struct AmaVankaMatrixHelper<LAFEM::TupleMatrix<...>>
483
484 template<typename FirstRow_>
485 struct AmaVankaMatrixHelper<LAFEM::TupleMatrix<FirstRow_>>
486 {
487 typedef LAFEM::TupleMatrix<FirstRow_> MatrixType;
488 typedef LAFEM::TupleMatrix<typename AmaVankaMatrixHelper<FirstRow_>::VankaMatrix> VankaMatrix;
489 static constexpr int num_blocks = VankaMatrix::num_col_blocks;
490
491 typedef typename FirstRow_::DataType DataType;
492 typedef typename FirstRow_::IndexType IndexType;
493 template<int n_>
494 static void fill_ptr_wrapper(CSRTupleMatrixWrapper<DataType, IndexType, n_>& wrap, const LAFEM::TupleMatrix<FirstRow_>& matrix, Memory::Location mem_loc, int row, int col)
495 {
496 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
497 AmaVankaMatrixHelper<FirstRow_>::fill_ptr_wrapper(wrap, matrix.first(), mem_loc, row, col);
498 }
499
500 template<int n_>
501 static void fill_column_info(CSRTupleMatrixWrapper<DataType, IndexType, n_>& wrap, const LAFEM::TupleMatrix<FirstRow_>& matrix, int row, int col)
502 {
503 ASSERTM((n_ > row) && (n_ > col), "MatrixWrapper is too small");
504 ASSERTM(row == 0, "Row counter has to be zero");
505 AmaVankaMatrixHelper<FirstRow_>::fill_column_info(wrap, matrix.first(), row, col);
506 }
507
508 static void fill_row_helper(const LAFEM::TupleMatrix<FirstRow_>& matrix, Index* accum_row_idx, Index* accum_row_ctr, int row_block, int curr_row)
509 {
510 AmaVankaMatrixHelper<FirstRow_>::fill_row_helper(matrix.first(), accum_row_idx, accum_row_ctr, row_block, curr_row);
511 }
512
513 /*static bool compare(const LAFEM::TupleMatrix<FirstRow_>& matrix_a, const LAFEM::TupleMatrix<FirstRow_>& matrix_b)
514 {
515 bool compare = AmaVankaMatrixHelper<FirstRow_>::compare(matrix_a.first(), matrix_b.first());
516 return compare;
517 }*/
518 }; // struct AmaVankaMatrixHelper<LAFEM::TupleMatrix<FirstRow_>>
519
520 template<typename MatrixA_, typename MatrixB_, typename MatrixD_>
521 struct AmaVankaMatrixHelper<LAFEM::SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>>
522 {
523 typedef typename MatrixA_::DataType DataType;
524 typedef typename MatrixA_::IndexType IndexType;
525 static constexpr int block_size = MatrixA_::block_width;
526 static constexpr int num_blocks = 2;
527
528 typedef LAFEM::TupleMatrix<
529 LAFEM::TupleMatrixRow<
530 LAFEM::SparseMatrixBCSR<DataType, IndexType, block_size, block_size>,
531 LAFEM::SparseMatrixBCSR<DataType, IndexType, block_size, 1>>,
532 LAFEM::TupleMatrixRow<
533 LAFEM::SparseMatrixBCSR<DataType, IndexType, 1, block_size>,
534 LAFEM::SparseMatrixBCSR<DataType, IndexType, 1, 1>>
535 > VankaMatrix;
536
537 template<int n_>
538 static void fill_ptr_wrapper(CSRTupleMatrixWrapper<DataType, IndexType, n_>& wrap, const LAFEM::SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>& matrix, Memory::Location mem_loc, int row, int col)
539 {
540 ASSERTM((n_+1 > row) && (n_+1 > col), "MatrixWrapper is too small");
541 AmaVankaMatrixHelper<MatrixA_>::fill_ptr_wrapper(wrap, matrix.template at<0,0>(), mem_loc, row, col);
542 AmaVankaMatrixHelper<MatrixB_>::fill_ptr_wrapper(wrap, matrix.template at<0,1>(), mem_loc, row, col+1);
543 AmaVankaMatrixHelper<MatrixD_>::fill_ptr_wrapper(wrap, matrix.template at<1,0>(), mem_loc, row+1, col);
544 AmaVankaMatrixHelper<LAFEM::NullMatrix<DataType, IndexType>>::fill_ptr_wrapper(wrap, LAFEM::NullMatrix<DataType, IndexType>(matrix.template at<1,0>().num_rows(), matrix.template at<0,1>().num_cols()), mem_loc, row+1, col+1);
545 }
546
547 template<int n_>
548 static void fill_column_info(CSRTupleMatrixWrapper<DataType, IndexType, n_>& wrap, const LAFEM::SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>& matrix, int row, int col)
549 {
550 ASSERTM((n_+1 > row) && (n_+1 > col), "MatrixWrapper is too small");
551 ASSERTM(row == 0, "Row counter has to be zero");
552 AmaVankaMatrixHelper<MatrixA_>::fill_column_info(wrap, matrix.template at<0,0>(), row, col);
553 AmaVankaMatrixHelper<MatrixB_>::fill_column_info(wrap, matrix.template at<0,1>(), row, col+1);
554 }
555
556 static void fill_row_helper(const LAFEM::SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>& matrix, Index* accum_row_idx, Index* accum_row_ctr, int row_block, int curr_row)
557 {
558 AmaVankaMatrixHelper<MatrixA_>::fill_row_helper(matrix.template at<0,0>(), accum_row_idx, accum_row_ctr, row_block, curr_row);
559 AmaVankaMatrixHelper<MatrixD_>::fill_row_helper(matrix.template at<1,0>(), accum_row_idx, accum_row_ctr, row_block+1, curr_row + matrix.template at<0,0>().num_rows_raw());
560 }
561
562 /*static bool compare(const LAFEM::SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>& matrix_a, const LAFEM::SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>& matrix_b)
563 {
564 bool compare = true;
565 compare &= AmaVankaMatrixHelper<MatrixA_>::compare(matrix_a.template at<0,0>(), matrix_b.template at<0,0>());
566 compare &= AmaVankaMatrixHelper<MatrixB_>::compare(matrix_a.template at<0,1>(), matrix_b.template at<0,1>());
567 compare &= AmaVankaMatrixHelper<MatrixD_>::compare(matrix_a.template at<1,0>(), matrix_b.template at<1,0>());
568 return compare;
569 }*/
570
571 }; // struct AmaVankaMatrixHelper<LAFEM::SaddlePointMatrix<...>>
572
573 template<typename Matrix_>
574 CSRTupleMatrixWrapper<typename Matrix_::DataType, typename Matrix_::IndexType, Intern::AmaVankaMatrixHelper<Matrix_>::num_blocks>
575 get_meta_matrix_wrapper(const Matrix_& matrix, Memory::Location mem_loc)
576 {
577 CSRTupleMatrixWrapper<typename Matrix_::DataType, typename Matrix_::IndexType, Intern::AmaVankaMatrixHelper<Matrix_>::num_blocks>
578 mat_wrapper;
579 AmaVankaMatrixHelper<Matrix_>::fill_ptr_wrapper(mat_wrapper, matrix, mem_loc, 0, 0);
580 AmaVankaMatrixHelper<Matrix_>::fill_column_info(mat_wrapper, matrix, 0, 0);
581 return mat_wrapper;
582 }
583
584 /* *********************************************************************************************************** */
585 /* *********************************************************************************************************** */
586 /* *********************************************************************************************************** */
587
596 class AmaVankaCore
597 {
598 public:
599#ifdef DOXYGEN
613 template<typename Matrix_>
614 static void block_sizes(const Matrix_& matrix, std::vector<Index>& sizes)
615 {
616 }
617#endif // DOXYGEN
618
620 template<typename DT_, typename IT_>
621 static void block_sizes(const LAFEM::SparseMatrixCSR<DT_, IT_>&, std::vector<Index>& sizes)
622 {
623 sizes.push_back(Index(1));
624 }
625
627 template<typename DT_, typename IT_, int bs_>
628 static void block_sizes(const LAFEM::SparseMatrixBCSR<DT_, IT_, bs_, bs_>&, std::vector<Index>& sizes)
629 {
630 sizes.push_back(Index(bs_));
631 }
632
634 template<typename... Rows_>
635 static void block_sizes(const LAFEM::TupleMatrix<Rows_...>& matrix, std::vector<Index>& sizes)
636 {
637 block_sizes_tpl<0>(matrix, sizes);
638 }
639
641 template<int col_, typename FirstRow_, typename SecondRow_, typename... RestRows_>
642 static void block_sizes_tpl(const LAFEM::TupleMatrix<FirstRow_, SecondRow_, RestRows_...>& matrix, std::vector<Index>& sizes)
643 {
644 block_sizes(matrix.first().template at<col_>(), sizes);
645 block_sizes_tpl<col_+1>(matrix.rest(), sizes);
646 }
647
649 template<int col_, typename FirstRow_>
650 static void block_sizes_tpl(const LAFEM::TupleMatrix<FirstRow_>& matrix, std::vector<Index>& sizes)
651 {
652 block_sizes(matrix.first().template at<col_>(), sizes);
653 }
654
655 /* ********************************************************************************************************* */
656
666 template<typename Matrix_>
667 static bool deduct_macro_dofs(const Matrix_& DOXY(matrix), std::vector<Adjacency::Graph>& DOXY(macro_dofs))
668 {
669 return false;
670 }
671
686 //template<typename MatrixA_, typename MatrixB_, typename MatrixD_>
687 //static bool deduct_macro_dofs(const LAFEM::SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>& matrix,
688 template<typename DT_, typename IT_, int bs_>
689 static bool deduct_macro_dofs(
690 const LAFEM::SaddlePointMatrix<
691 LAFEM::SparseMatrixBCSR<DT_, IT_, bs_, bs_>,
692 LAFEM::SparseMatrixBCSR<DT_, IT_, bs_, 1>,
693 LAFEM::SparseMatrixBCSR<DT_, IT_, 1, bs_>>& matrix,
694 std::vector<Adjacency::Graph>& macro_dofs)
695 {
696 typedef IT_ IndexType;
697
698 // fetch matrix dimensions
699 const Index num_dofs_p = Index(matrix.block_d().num_rows());
700
701 // fetch matrix array
702 const Memory::TypedView<IndexType> row_ptr_b = matrix.block_b().row_ptr_view_r();
703 const Memory::TypedView<IndexType> col_idx_b = matrix.block_b().col_idx_view_r();
704 const Memory::TypedView<IndexType> row_ptr_d = matrix.block_d().row_ptr_view_r();
705 const Memory::TypedView<IndexType> col_idx_d = matrix.block_d().col_idx_view_r();
706
707 // PHASE 1: determine pressure macros
708 std::map<IndexType, int> map_s;
709 std::vector<int> mask(std::size_t(num_dofs_p), 0);
710 std::vector<Index> p_ptr, p_idx;
711 p_ptr.push_back(Index(0));
712
713 // loop over all pressure nodes
714 for(Index i(0); i < num_dofs_p; ++i)
715 {
716 // is this node already processed?
717 if(mask[i] != 0)
718 continue;
719
720 // clear the local map
721 map_s.clear();
722
723 // okay, loop over all entries of D
724 for(IndexType kd = row_ptr_d[i]; kd < row_ptr_d[i+1]; ++kd)
725 {
726 // fetch the column index of D
727 const IndexType col_d = col_idx_d[kd];
728
729 // loop over the row of B
730 for(IndexType kb = row_ptr_b[col_d]; kb < row_ptr_b[col_d+1]; ++kb)
731 {
732 // fetch the column index of B
733 const IndexType col_b = col_idx_b[kb];
734
735 // insert into map
736 auto ib = map_s.emplace(col_b, 1);
737 if(!ib.second)
738 {
739 // node already exists, increment counter then
740 ++(ib.first->second);
741 }
742 }
743 }
744
745 // compute the map degree
746 int ideg = 0;
747 for(auto it = map_s.begin(); it != map_s.end(); ++it)
748 ideg = Math::max(ideg, it->second);
749
750 // loop over all map entries with maximum degree
751 for(auto it = map_s.begin(); it != map_s.end(); ++it)
752 {
753 if(ideg == it->second)
754 {
755 // insert into map
756 p_idx.push_back(Index(it->first));
757 mask[it->first] = 1;
758 }
759 }
760
761 // push row
762 p_ptr.push_back(Index(p_idx.size()));
763 }
764
765 // PHASE 2: determine velocity macros
766
767 // fetch number of pressure macros
768 const Index num_macros = Index(p_ptr.size()-1u);
769
770 // clear block arrays
771 std::set<IndexType> set_v;
772 std::vector<Index> v_ptr, v_idx;
773 v_ptr.reserve(num_macros+1u);
774 v_ptr.push_back(Index(0));
775
776 // loop over all pressure blocks
777 for(IndexType i(0); i < num_macros; ++i)
778 {
779 // loop over all pressure dofs in the current block
780 for(Index j = p_ptr[i]; j < p_ptr[i+1]; ++j)
781 {
782 // get the pressure dof index
783 const Index pix = p_idx[j];
784
785 // now loop over the corresponding row of D
786 for(IndexType k = row_ptr_d[pix]; k < row_ptr_d[pix+1]; ++k)
787 {
788 // insert velocity dof into block set
789 set_v.insert(col_idx_d[k]);
790 }
791 }
792
793 // push velocity block
794 for(auto it = set_v.begin(); it != set_v.end(); ++it)
795 {
796 v_idx.push_back(Index(*it));
797 }
798
799 // update pointer
800 v_ptr.push_back(Index(v_idx.size()));
801
802 // clear local set
803 set_v.clear();
804 }
805
806 // create graphs
807 macro_dofs.resize(std::size_t(2));
808 macro_dofs.front() = Adjacency::Graph(num_macros, matrix.block_b().num_rows(), Index(v_idx.size()), v_ptr.data(), v_idx.data());
809 macro_dofs.back() = Adjacency::Graph(num_macros, matrix.block_d().num_rows(), Index(p_idx.size()), p_ptr.data(), p_idx.data());
810 return true;
811 }
812
813 /* ********************************************************************************************************* */
814
815#ifdef DOXYGEN
853 template<typename Matrix_, typename DT_>
854 static std::pair<Index,Index> gather(const Matrix_& matrix,
855 DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
856 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
857 {
858 }
859#endif // DOXYGEN
860
862 template<typename DT_, typename IT_, int bh_, int bw_>
863 static std::pair<Index,Index> gather(const LAFEM::NullMatrix<DT_, IT_, bh_, bw_>&,
864 DT_*, const Index, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
865 const Index, const Index row_block, const Index, const Index col_block)
866 {
867 // get graph domain pointer arrays
868 const Index* row_dom_ptr = macro_dofs.at(row_block).get_domain_ptr();
869 const Index* col_dom_ptr = macro_dofs.at(col_block).get_domain_ptr();
870
871 // get the number of rows/cols to gather
872 const Index num_rows = row_dom_ptr[macro+1] - row_dom_ptr[macro];
873 const Index num_cols = col_dom_ptr[macro+1] - col_dom_ptr[macro];
874
875 // return the number of gathered rows
876 return std::make_pair(num_rows * Index(bh_), num_cols * Index(bw_));
877 }
878
880 template<typename DT_, typename IT_>
881 static std::pair<Index,Index> gather(const LAFEM::SparseMatrixCSR<DT_, IT_>& matrix,
882 DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
883 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
884 {
885 // get matrix arrays
886 const Memory::TypedView<DT_> vals = matrix.val_view_r();
887 const Memory::TypedView<IT_> row_ptr = matrix.row_ptr_view_r();
888 const Memory::TypedView<IT_> col_idx = matrix.col_idx_view_r();
889
890 // get the dofs for our row/col blocks
891 const Adjacency::Graph& row_dofs = macro_dofs.at(row_block);
892 const Adjacency::Graph& col_dofs = macro_dofs.at(col_block);
893
894 // get graph domain pointer arrays
895 const Index* row_dom_ptr = row_dofs.get_domain_ptr();
896 const Index* col_dom_ptr = col_dofs.get_domain_ptr();
897
898 // get the number of rows/cols to gather
899 const Index num_rows = row_dom_ptr[macro+1] - row_dom_ptr[macro];
900 const Index num_cols = col_dom_ptr[macro+1] - col_dom_ptr[macro];
901
902 // get graph image index arrays for our macro
903 const Index* row_img_idx = &(row_dofs.get_image_idx()[row_dom_ptr[macro]]);
904 const Index* col_img_idx = &(col_dofs.get_image_idx()[col_dom_ptr[macro]]);
905
906 // loop over all rows
907 for(Index i(0); i < num_rows; ++i)
908 {
909 // get the index of this row in the big matrix
910 const Index irow = row_img_idx[i];
911
912 // initialize loop variable for local columns
913 Index j = Index(0);
914
915 // row over the row
916 for(IT_ ai = row_ptr[irow]; ai < row_ptr[irow + Index(1)]; ++ai)
917 {
918 // get the column index
919 const Index icol = Index(col_idx[ai]);
920
921 // now search its position in our local matrix
922 while((j < num_cols) && (col_img_idx[j] < icol))
923 {
924 ++j;
925 }
926
927 // did we find our local entry?
928 if((j < num_cols) && (col_img_idx[j] == icol))
929 {
930 // copy macro row
931 local[(row_off + i) * stride + col_off + j] = vals[ai];
932 }
933 }
934 }
935
936 // return the number of gathered rows
937 return std::make_pair(num_rows, num_cols);
938 }
939
941 template<typename DT_, typename IT_, int bh_, int bw_>
942 static std::pair<Index,Index> gather(const LAFEM::SparseMatrixBCSR<DT_, IT_, bh_, bw_>& matrix,
943 DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
944 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
945 {
946 // get matrix arrays
947 const Memory::TypedView<Tiny::Matrix<DT_, bh_, bw_>> vals = matrix.val_view_r();
948 const Memory::TypedView<IT_> row_ptr = matrix.row_ptr_view_r();
949 const Memory::TypedView<IT_> col_idx = matrix.col_idx_view_r();
950
951 // get the dofs for our row/col blocks
952 const Adjacency::Graph& row_dofs = macro_dofs.at(row_block);
953 const Adjacency::Graph& col_dofs = macro_dofs.at(col_block);
954
955 // get graph domain pointer arrays
956 const Index* row_dom_ptr = row_dofs.get_domain_ptr();
957 const Index* col_dom_ptr = col_dofs.get_domain_ptr();
958
959 // get the number of rows/cols to gather
960 const Index num_rows = row_dom_ptr[macro+1] - row_dom_ptr[macro];
961 const Index num_cols = col_dom_ptr[macro+1] - col_dom_ptr[macro];
962
963 // get graph image index arrays for our macro
964 const Index* row_img_idx = &(row_dofs.get_image_idx()[row_dom_ptr[macro]]);
965 const Index* col_img_idx = &(col_dofs.get_image_idx()[col_dom_ptr[macro]]);
966
967 // loop over all rows
968 for(Index i(0); i < num_rows; ++i)
969 {
970 // get the index of this row in the big matrix
971 const Index irow = row_img_idx[i];
972
973 // initialize loop variable for local columns
974 Index j = Index(0);
975
976 // row over the row
977 for(IT_ ai = row_ptr[irow]; ai < row_ptr[irow + Index(1)]; ++ai)
978 {
979 // get the column index
980 const Index icol = Index(col_idx[ai]);
981
982 // now search its position in our local matrix
983 while((j < num_cols) && (col_img_idx[j] < icol))
984 {
985 ++j;
986 }
987
988 // did we find our local entry?
989 if((j < num_cols) && (col_img_idx[j] == icol))
990 {
991 // found it, so store it in our local matrix
992 const Tiny::Matrix<DT_, bh_, bw_>& mv = vals[ai];
993
994 // copy matrix macro
995 for(int ii(0); ii < bh_; ++ii)
996 {
997 // compute offset within local matrix
998 const Index lro = (row_off + i * Index(bh_) + Index(ii)) * stride + col_off + j * Index(bw_);
999
1000 // copy macro row
1001 for(int jj(0); jj < bw_; ++jj)
1002 {
1003 local[lro + IT_(jj)] = mv[ii][jj];
1004 }
1005 }
1006 }
1007 }
1008 }
1009
1010 // return the number of gathered rows
1011 return std::make_pair(num_rows * Index(bh_), num_cols * Index(bw_));
1012 }
1013
1015 template<typename DT_, typename First_, typename Second_, typename... Rest_>
1016 static std::pair<Index,Index> gather(const LAFEM::TupleMatrixRow<First_, Second_, Rest_...>& matrix,
1017 DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
1018 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1019 {
1020 const std::pair<Index,Index> nrc_f = AmaVankaCore::gather(matrix.first(), local, stride, macro,
1021 macro_dofs, row_off, row_block, col_off, col_block);
1022
1023 const std::pair<Index,Index> nrc_r = AmaVankaCore::gather(matrix.rest(), local, stride, macro,
1024 macro_dofs, row_off, row_block, col_off + nrc_f.second, col_block + Index(1));
1025
1026 XASSERTM(nrc_f.first == nrc_r.first, "block row count mismatch");
1027
1028 return std::make_pair(nrc_f.first, nrc_f.second + nrc_r.second);
1029 }
1030
1032 template<typename DT_, typename First_>
1033 static std::pair<Index,Index> gather(const LAFEM::TupleMatrixRow<First_>& matrix,
1034 DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
1035 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1036 {
1037 return AmaVankaCore::gather(matrix.first(), local, stride, macro,
1038 macro_dofs, row_off, row_block, col_off, col_block);
1039 }
1040
1042 template<typename DT_, typename FirstRow_, typename SecondRow_, typename... RestRows_>
1043 static std::pair<Index,Index> gather(const LAFEM::TupleMatrix<FirstRow_, SecondRow_, RestRows_...>& matrix,
1044 DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
1045 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1046 {
1047 const std::pair<Index,Index> nrc_f = AmaVankaCore::gather(matrix.first(), local, stride, macro,
1048 macro_dofs, row_off, row_block, col_off, col_block);
1049
1050 const std::pair<Index,Index> nrc_r = AmaVankaCore::gather(matrix.rest(), local, stride, macro,
1051 macro_dofs, row_off + nrc_f.first, row_block + Index(1), col_off, col_block);
1052
1053 XASSERTM(nrc_f.second == nrc_r.second, "block column count mismatch");
1054
1055 return std::make_pair(nrc_f.first + nrc_r.first, nrc_f.second);
1056 }
1057
1059 template<typename DT_, typename FirstRow_>
1060 static std::pair<Index,Index> gather(const LAFEM::TupleMatrix<FirstRow_>& matrix,
1061 DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
1062 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1063 {
1064 return AmaVankaCore::gather(matrix.first(), local, stride, macro,
1065 macro_dofs, row_off, row_block, col_off, col_block);
1066 }
1067
1069 template<typename DT_, typename MatrixA_, typename MatrixB_, typename MatrixD_>
1070 static std::pair<Index,Index> gather(const LAFEM::SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>& matrix,
1071 DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
1072 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1073 {
1074 // gather matrix A
1075 const std::pair<Index,Index> nrc_a = AmaVankaCore::gather(matrix.block_a(), local, stride,
1076 macro, macro_dofs, row_off, row_block, col_off, col_block);
1077
1078 // gather matrix B
1079 const std::pair<Index,Index> nrc_b = AmaVankaCore::gather(matrix.block_b(), local, stride,
1080 macro, macro_dofs, row_off, row_block, col_off + nrc_a.second, col_block + Index(1));
1081
1082 // gather matrix D
1083 const std::pair<Index,Index> nrc_d = AmaVankaCore::gather(matrix.block_d(), local, stride,
1084 macro, macro_dofs, row_off + nrc_a.first, row_block + Index(1), col_off, col_block);
1085
1086 XASSERTM(nrc_a.first == nrc_b.first, "block row count mismatch");
1087 XASSERTM(nrc_a.second == nrc_d.second, "block column count mismatch");
1088
1089 return std::make_pair(nrc_a.first + nrc_d.first, nrc_a.second + nrc_b.second);
1090 }
1091
1092 /* ********************************************************************************************************* */
1093
1094#ifdef DOXYGEN
1117 template<typename Matrix_>
1118 static void alloc(Matrix_& matrix,
1119 const std::vector<Adjacency::Graph>& dof_macros, const std::vector<Adjacency::Graph>& macro_dofs,
1120 const Index row_block, const Index col_block)
1121 {
1122 }
1123#endif // DOXYGEN
1124
1126 template<typename DT_, typename IT_>
1127 static void alloc(LAFEM::SparseMatrixCSR<DT_, IT_>& matrix,
1128 const std::vector<Adjacency::Graph>& dof_macros, const std::vector<Adjacency::Graph>& macro_dofs,
1129 const Index row_block, const Index col_block)
1130 {
1131 XASSERT(row_block < Index(dof_macros.size()));
1132 XASSERT(col_block < Index(macro_dofs.size()));
1133 Adjacency::Graph graph(Adjacency::RenderType::injectify_sorted, dof_macros.at(row_block), macro_dofs.at(col_block));
1134 LAFEM::SparseMatrixCSR<DT_, IT_> matrix_main(graph);
1135 matrix.convert(matrix_main);
1136 }
1137
1139 template<typename DT_, typename IT_, int bh_, int bw_>
1140 static void alloc(LAFEM::SparseMatrixBCSR<DT_, IT_, bh_, bw_>& matrix,
1141 const std::vector<Adjacency::Graph>& dof_macros, const std::vector<Adjacency::Graph>& macro_dofs,
1142 const Index row_block, const Index col_block)
1143 {
1144 XASSERT(row_block < Index(dof_macros.size()));
1145 XASSERT(col_block < Index(macro_dofs.size()));
1146 Adjacency::Graph graph(Adjacency::RenderType::injectify_sorted, dof_macros.at(row_block), macro_dofs.at(col_block));
1147 matrix.convert(graph);
1148 }
1149
1151 template<typename First_, typename Second_, typename... Rest_>
1152 static void alloc(LAFEM::TupleMatrixRow<First_, Second_, Rest_...>& matrix,
1153 const std::vector<Adjacency::Graph>& dof_macros, const std::vector<Adjacency::Graph>& macro_dofs,
1154 const Index row_block, const Index col_block)
1155 {
1156 AmaVankaCore::alloc(matrix.first(), dof_macros, macro_dofs, row_block, col_block);
1157 AmaVankaCore::alloc(matrix.rest(), dof_macros, macro_dofs, row_block, col_block + Index(1));
1158 }
1159
1161 template<typename First_>
1162 static void alloc(LAFEM::TupleMatrixRow<First_>& matrix,
1163 const std::vector<Adjacency::Graph>& dof_macros, const std::vector<Adjacency::Graph>& macro_dofs,
1164 const Index row_block, const Index col_block)
1165 {
1166 AmaVankaCore::alloc(matrix.first(), dof_macros, macro_dofs, row_block, col_block);
1167 }
1168
1170 template<typename FirstRow_, typename SecondRow_, typename... RestRows_>
1171 static void alloc(LAFEM::TupleMatrix<FirstRow_, SecondRow_, RestRows_...>& matrix,
1172 const std::vector<Adjacency::Graph>& dof_macros, const std::vector<Adjacency::Graph>& macro_dofs,
1173 const Index row_block, const Index col_block)
1174 {
1175 AmaVankaCore::alloc(matrix.first(), dof_macros, macro_dofs, row_block, col_block);
1176 AmaVankaCore::alloc(matrix.rest(), dof_macros, macro_dofs, row_block + Index(1), col_block);
1177 }
1178
1180 template<typename FirstRow_>
1181 static void alloc(LAFEM::TupleMatrix<FirstRow_>& matrix,
1182 const std::vector<Adjacency::Graph>& dof_macros, const std::vector<Adjacency::Graph>& macro_dofs,
1183 const Index row_block, const Index col_block)
1184 {
1185 AmaVankaCore::alloc(matrix.first(), dof_macros, macro_dofs, row_block, col_block);
1186 }
1187
1188 /* ********************************************************************************************************* */
1189
1190#ifdef DOXYGEN
1231 template<typename DT_, typename Matrix_>
1232 static std::pair<Index,Index> scatter_add(Matrix_& matrix,
1233 const DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
1234 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1235 {
1236 }
1237#endif // DOXYGEN
1238
1240 template<typename DT_, typename IT_>
1241 static std::pair<Index,Index> scatter_add(LAFEM::SparseMatrixCSR<DT_, IT_>& matrix,
1242 const DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
1243 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1244 {
1245 // get matrix arrays
1246 Memory::TypedView<DT_> vals = matrix.val_view_rw();
1247 const Memory::TypedView<IT_> row_ptr = matrix.row_ptr_view_r();
1248 const Memory::TypedView<IT_> col_idx = matrix.col_idx_view_r();
1249
1250 // get the dofs for our row/col blocks
1251 const Adjacency::Graph& row_dofs = macro_dofs.at(row_block);
1252 const Adjacency::Graph& col_dofs = macro_dofs.at(col_block);
1253
1254 // get graph domain pointer arrays
1255 const Index* row_dom_ptr = row_dofs.get_domain_ptr();
1256 const Index* col_dom_ptr = col_dofs.get_domain_ptr();
1257
1258 // get the number of rows/cols to gather
1259 const Index num_rows = row_dom_ptr[macro+1] - row_dom_ptr[macro];
1260 const Index num_cols = col_dom_ptr[macro+1] - col_dom_ptr[macro];
1261
1262 // get graph image index arrays for our macro
1263 const Index* row_img_idx = &(row_dofs.get_image_idx()[row_dom_ptr[macro]]);
1264 const Index* col_img_idx = &(col_dofs.get_image_idx()[col_dom_ptr[macro]]);
1265
1266 // loop over all rows
1267 for(Index i(0); i < num_rows; ++i)
1268 {
1269 // get the index of this row in the big matrix
1270 const Index irow = row_img_idx[i];
1271
1272 // initialize loop variable for local columns
1273 Index j = Index(0);
1274
1275 // row over the row
1276 for(IT_ ai = row_ptr[irow]; ai < row_ptr[irow + Index(1)]; ++ai)
1277 {
1278 // get the column index
1279 const Index icol = Index(col_idx[ai]);
1280
1281 // now search its position in our local matrix
1282 while((j < num_cols) && (col_img_idx[j] < icol))
1283 {
1284 ++j;
1285 }
1286
1287 // did we find our local entry?
1288 if((j < num_cols) && (col_img_idx[j] == icol))
1289 {
1290 vals[ai] += local[(row_off + i) * stride + col_off + j];
1291 }
1292 }
1293 }
1294
1295 // return the number of scattered rows
1296 return std::make_pair(num_rows, num_cols);
1297 }
1298
1300 template<typename DT_, typename IT_, int bh_, int bw_>
1301 static std::pair<Index,Index> scatter_add(LAFEM::SparseMatrixBCSR<DT_, IT_, bh_, bw_>& matrix,
1302 const DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
1303 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1304 {
1305 // get matrix arrays
1306 Memory::TypedView<Tiny::Matrix<DT_, bh_, bw_>> vals = matrix.val_view_rw();
1307 const Memory::TypedView<IT_> row_ptr = matrix.row_ptr_view_r();
1308 const Memory::TypedView<IT_> col_idx = matrix.col_idx_view_r();
1309
1310 // get the dofs for our row/col blocks
1311 const Adjacency::Graph& row_dofs = macro_dofs.at(row_block);
1312 const Adjacency::Graph& col_dofs = macro_dofs.at(col_block);
1313
1314 // get graph domain pointer arrays
1315 const Index* row_dom_ptr = row_dofs.get_domain_ptr();
1316 const Index* col_dom_ptr = col_dofs.get_domain_ptr();
1317
1318 // get the number of rows/cols to gather
1319 const Index num_rows = row_dom_ptr[macro+1] - row_dom_ptr[macro];
1320 const Index num_cols = col_dom_ptr[macro+1] - col_dom_ptr[macro];
1321
1322 // get graph image index arrays for our macro
1323 const Index* row_img_idx = &(row_dofs.get_image_idx()[row_dom_ptr[macro]]);
1324 const Index* col_img_idx = &(col_dofs.get_image_idx()[col_dom_ptr[macro]]);
1325
1326 // loop over all rows
1327 for(Index i(0); i < num_rows; ++i)
1328 {
1329 // get the index of this row in the big matrix
1330 const Index irow = row_img_idx[i];
1331
1332 // initialize loop variable for local columns
1333 Index j = Index(0);
1334
1335 // row over the row
1336 for(IT_ ai = row_ptr[irow]; ai < row_ptr[irow + Index(1)]; ++ai)
1337 {
1338 // get the column index
1339 const Index icol = Index(col_idx[ai]);
1340
1341 // now search its position in our local matrix
1342 while((j < num_cols) && (col_img_idx[j] < icol))
1343 {
1344 ++j;
1345 }
1346
1347 // did we find our local entry?
1348 if((j < num_cols) && (col_img_idx[j] == icol))
1349 {
1350 // found it, so store it in our local matrix
1351 Tiny::Matrix<DT_, bh_, bw_>& mv = vals[ai];
1352
1353 // copy matrix macro
1354 for(int ii(0); ii < bh_; ++ii)
1355 {
1356 // compute offset within local matrix
1357 const Index lro = (row_off + i * Index(bh_) + Index(ii)) * stride + col_off + j * Index(bw_);
1358
1359 // copy macro row
1360 for(int jj(0); jj < bw_; ++jj)
1361 {
1362 mv[ii][jj] += local[lro + IT_(jj)];
1363 }
1364 }
1365 }
1366 }
1367 }
1368
1369 // return the number of scattered rows
1370 return std::make_pair(num_rows * Index(bh_), num_cols * Index(bw_));
1371 }
1372
1374 template<typename DT_, typename First_, typename Second_, typename... Rest_>
1375 static std::pair<Index,Index> scatter_add(LAFEM::TupleMatrixRow<First_, Second_, Rest_...>& matrix,
1376 const DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
1377 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1378 {
1379 const std::pair<Index,Index> nrc_f = AmaVankaCore::scatter_add(matrix.first(), local, stride, macro,
1380 macro_dofs, row_off, row_block, col_off, col_block);
1381
1382 const std::pair<Index,Index> nrc_r = AmaVankaCore::scatter_add(matrix.rest(), local, stride, macro,
1383 macro_dofs, row_off, row_block, col_off + nrc_f.second, col_block + Index(1));
1384
1385 XASSERTM(nrc_f.first == nrc_r.first, "block row count mismatch");
1386
1387 return std::make_pair(nrc_f.first, nrc_f.second + nrc_r.second);
1388 }
1389
1391 template<typename DT_, typename First_>
1392 static std::pair<Index,Index> scatter_add(LAFEM::TupleMatrixRow<First_>& matrix,
1393 const DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
1394 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1395 {
1396 return AmaVankaCore::scatter_add(matrix.first(), local, stride, macro,
1397 macro_dofs, row_off, row_block, col_off, col_block);
1398 }
1399
1401 template<typename DT_, typename FirstRow_, typename SecondRow_, typename... RestRows_>
1402 static std::pair<Index,Index> scatter_add(LAFEM::TupleMatrix<FirstRow_, SecondRow_, RestRows_...>& matrix,
1403 const DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
1404 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1405 {
1406 const std::pair<Index,Index> nrc_f = AmaVankaCore::scatter_add(matrix.first(), local, stride,
1407 macro, macro_dofs, row_off, row_block, col_off, col_block);
1408
1409 const std::pair<Index,Index> nrc_r = AmaVankaCore::scatter_add(matrix.rest(), local, stride,
1410 macro, macro_dofs, row_off + nrc_f.first, row_block + Index(1), col_off, col_block);
1411
1412 XASSERTM(nrc_f.second == nrc_r.second, "block column count mismatch");
1413
1414 return std::make_pair(nrc_f.first + nrc_r.first, nrc_f.second);
1415 }
1416
1418 template<typename DT_, typename FirstRow_>
1419 static std::pair<Index,Index> scatter_add(LAFEM::TupleMatrix<FirstRow_>& matrix,
1420 const DT_* local, const Index stride, const Index macro, const std::vector<Adjacency::Graph>& macro_dofs,
1421 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1422 {
1423 return AmaVankaCore::scatter_add(matrix.first(), local, stride,
1424 macro, macro_dofs, row_off, row_block, col_off, col_block);
1425 }
1426
1427 /* ********************************************************************************************************* */
1428
1429#ifdef DOXYGEN
1452 template<typename Matrix_>
1453 static void scale_rows(Matrix_& matrix, const DT_ omega,
1454 const std::vector<Adjacency::Graph>& dof_macros, const Index row_block, const Index col_block)
1455 {
1456 }
1457#endif // DOXYGEN
1458
1460 template<typename DT_, typename IT_>
1461 static void scale_rows(LAFEM::SparseMatrixCSR<DT_, IT_>& matrix, const DT_ omega,
1462 const std::vector<Adjacency::Graph>& dof_macros, const std::vector<int>& macro_mask, const Index row_block, const Index col_block)
1463 {
1464 // get matrix arrays
1465 Memory::TypedView<DT_> vals = matrix.val_view_rw();
1466 const Memory::TypedView<IT_> row_ptr = matrix.row_ptr_view_r();
1467 const Memory::TypedView<IT_> col_idx = matrix.col_idx_view_r();
1468
1469 // get the dofs for our row blocks
1470 const Adjacency::Graph& row_blocks = dof_macros.at(row_block);
1471 const Index num_rows = row_blocks.get_num_nodes_domain();
1472 XASSERT(matrix.num_rows() == num_rows);
1473
1474 // get graph domain pointer arrays
1475 const Index* row_dom_ptr = row_blocks.get_domain_ptr();
1476 const Index* row_img_idx = row_blocks.get_image_idx();
1477
1478 // loop over all rows/dofs
1479 for(Index i(0); i < num_rows; ++i)
1480 {
1481 XASSERT(row_dom_ptr[i] < row_dom_ptr[i+1]);
1482
1483 // get number of active macros for this DOF
1484 Index n(0);
1485 if(macro_mask.empty())
1486 n = row_dom_ptr[i+1] - row_dom_ptr[i];
1487 else
1488 {
1489 for(Index j(row_dom_ptr[i]); j < row_dom_ptr[i+1]; ++j)
1490 n += Index(macro_mask[row_img_idx[j]]);
1491 }
1492
1493 if(n > 0u)
1494 {
1495 const DT_ sc = omega / DT_(Math::max(n,Index(1)));
1496 for(IT_ j(row_ptr[i]); j < row_ptr[i+1]; ++j)
1497 vals[j] *= sc;
1498 }
1499 else if(row_block == col_block) // null row: replace by unit row
1500 {
1501 for(IT_ j(row_ptr[i]); j < row_ptr[i+1]; ++j)
1502 {
1503 vals[j] = DT_(col_idx[j] == i ? 1 : 0);
1504 }
1505 }
1506 }
1507 }
1508
1510 template<typename DT_, typename IT_, int bh_, int bw_>
1511 static void scale_rows(LAFEM::SparseMatrixBCSR<DT_, IT_, bh_, bw_>& matrix, const DT_ omega,
1512 const std::vector<Adjacency::Graph>& dof_macros, const std::vector<int>& macro_mask, const Index row_block, const Index col_block)
1513 {
1514 // get matrix arrays
1515 Memory::TypedView<Tiny::Matrix<DT_, bh_, bw_>> vals = matrix.val_view_rw();
1516 const Memory::TypedView<IT_> row_ptr = matrix.row_ptr_view_r();
1517 const Memory::TypedView<IT_> col_idx = matrix.col_idx_view_r();
1518
1519 // get the dofs for our row blocks
1520 const Adjacency::Graph& row_blocks = dof_macros.at(row_block);
1521 const Index num_rows = row_blocks.get_num_nodes_domain();
1522 XASSERT(matrix.num_rows() == num_rows);
1523
1524 // get graph domain pointer arrays
1525 const Index* row_dom_ptr = row_blocks.get_domain_ptr();
1526 const Index* row_img_idx = row_blocks.get_image_idx();
1527
1528 // loop over all rows/dofs
1529 for(Index i(0); i < num_rows; ++i)
1530 {
1531 XASSERT(row_dom_ptr[i] < row_dom_ptr[i+1]);
1532
1533 // get number of active macros for this DOF
1534 Index n(0);
1535 if(macro_mask.empty())
1536 n = row_dom_ptr[i+1] - row_dom_ptr[i];
1537 else
1538 {
1539 for(Index j(row_dom_ptr[i]); j < row_dom_ptr[i+1]; ++j)
1540 n += Index(macro_mask[row_img_idx[j]]);
1541 }
1542
1543 if(n > 0u)
1544 {
1545 const DT_ sc = omega / DT_(n);
1546 for(IT_ j(row_ptr[i]); j < row_ptr[i+1]; ++j)
1547 vals[j] *= sc;
1548 }
1549 else if(row_block == col_block) // null row: replace by unit row
1550 {
1551 for(IT_ j(row_ptr[i]); j < row_ptr[i+1]; ++j)
1552 {
1553 if(col_idx[j] == i)
1554 vals[j].set_identity();
1555 }
1556 }
1557 }
1558 }
1559
1561 template<typename DT_, typename First_, typename Second_, typename... Rest_>
1562 static void scale_rows(LAFEM::TupleMatrixRow<First_, Second_, Rest_...>& matrix, const DT_ omega,
1563 const std::vector<Adjacency::Graph>& dof_macros, const std::vector<int>& macro_mask, const Index row_block, const Index col_block)
1564 {
1565 AmaVankaCore::scale_rows(matrix.first(), omega, dof_macros, macro_mask, row_block, col_block);
1566 AmaVankaCore::scale_rows(matrix.rest(), omega, dof_macros, macro_mask, row_block, col_block + Index(1));
1567 }
1568
1570 template<typename DT_, typename First_>
1571 static void scale_rows(LAFEM::TupleMatrixRow<First_>& matrix, const DT_ omega,
1572 const std::vector<Adjacency::Graph>& dof_macros, const std::vector<int>& macro_mask, const Index row_block, const Index col_block)
1573 {
1574 AmaVankaCore::scale_rows(matrix.first(), omega, dof_macros, macro_mask, row_block, col_block);
1575 }
1576
1578 template<typename DT_, typename FirstRow_, typename SecondRow_, typename... RestRows_>
1579 static void scale_rows(LAFEM::TupleMatrix<FirstRow_, SecondRow_, RestRows_...>& matrix, const DT_ omega,
1580 const std::vector<Adjacency::Graph>& dof_macros, const std::vector<int>& macro_mask, const Index row_block, const Index col_block)
1581 {
1582 AmaVankaCore::scale_rows(matrix.first(), omega, dof_macros, macro_mask, row_block, col_block);
1583 AmaVankaCore::scale_rows(matrix.rest(), omega, dof_macros, macro_mask, row_block + Index(1), col_block);
1584 }
1585
1587 template<typename DT_, typename FirstRow_>
1588 static void scale_rows(LAFEM::TupleMatrix<FirstRow_>& matrix, const DT_ omega,
1589 const std::vector<Adjacency::Graph>& dof_macros, const std::vector<int>& macro_mask, const Index row_block, const Index col_block)
1590 {
1591 AmaVankaCore::scale_rows(matrix.first(), omega, dof_macros, macro_mask, row_block, col_block);
1592 }
1593
1594 /* ********************************************************************************************************* */
1595
1608 template<typename Matrix_>
1609 static Index calc_stride(const Matrix_& matrix, const std::vector<Adjacency::Graph>& macro_dofs)
1610 {
1611 const std::size_t num_graphs = macro_dofs.size();
1612 const Index num_macros = macro_dofs.front().get_num_nodes_domain();
1613
1614 // compute macro sizes
1615 std::vector<Index> block_sizes;
1616 AmaVankaCore::block_sizes(matrix, block_sizes);
1617
1618 XASSERT(num_graphs == block_sizes.size());
1619
1620 // loop over all macros
1621 Index rtn = Index(0);
1622 for(Index imacro(0); imacro < num_macros; ++imacro)
1623 {
1624 Index count = Index(0);
1625
1626 // loop over all graphs and summarize degrees
1627 for(std::size_t igraph(0); igraph < num_graphs; ++igraph)
1628 {
1629 count += macro_dofs.at(igraph).degree(imacro) * block_sizes.at(igraph);
1630 }
1631
1632 rtn = Math::max(rtn, count);
1633 }
1634
1635 return rtn;
1636 }
1637
1638 }; // struct AmaVankaCore
1639
1648 struct VoxelAmaVankaCore
1649 {
1650 template<typename DT_, typename IT_>
1651 CUDA_HOST_DEVICE static void gather_loc_cell(DT_* local, const DT_* vals, const IT_* row_ptr, const IT_* col_idx,
1652 const Index* row_img_idx, const Index* col_img_idx, const int bh_, const int bw_, const Index num_rows, const Index num_cols,
1653 const Index stride, const Index row_off, const Index col_off)
1654 {
1655 if(vals == nullptr)
1656 return;
1657 // loop over all rows
1658 for(Index i(0); i < num_rows; ++i)
1659 {
1660 // get the index of this row in the big matrix
1661 const Index irow = row_img_idx[i];
1662
1663 // initialize loop variable for local columns
1664 Index j = Index(0);
1665
1666 // row over the row
1667 for(IT_ ai = row_ptr[irow]; ai < row_ptr[irow + Index(1)]; ++ai)
1668 {
1669 // get the column index
1670 const Index icol = Index(col_idx[ai]);
1671
1672 // now search its position in our local matrix
1673 while((j < num_cols) && (col_img_idx[j] < icol))
1674 {
1675 ++j;
1676 }
1677
1678 // did we find our local entry?
1679 if((j < num_cols) && (col_img_idx[j] == icol))
1680 {
1681 // offset by ai times bw*bh size matrices
1682 const DT_* mv = vals + ai*IT_(bw_)*IT_(bh_);
1683 // copy matrix macro
1684 for(int ii(0); ii < bh_; ++ii)
1685 {
1686 // compute offset within local matrix
1687 const Index lro = (row_off + i * Index(bh_) + Index(ii)) * stride + col_off + j * Index(bw_);
1688
1689 // copy macro row
1690 for(int jj(0); jj < bw_; ++jj)
1691 {
1692 local[lro + IT_(jj)] = mv[ii*bw_+jj];
1693 }
1694 }
1695 }
1696 }
1697 }
1698 }
1699
1700 // ##################### HOST VARIANT ################################
1701 template<typename DT_, typename IT_, int n_>
1702 CUDA_HOST static std::pair<Index,Index> gather_loc(const typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData& mat_wrap,
1703 DT_* local, const Index stride, const Index macro, const Adjacency::Graph** macro_dofs,
1704 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1705 {
1706 //gather arrays
1707 const Index cur_ind = Index(n_)*row_block + col_block;
1708 const DT_* vals = mat_wrap.data_arrays[cur_ind];
1709 const IT_* row_ptr = mat_wrap.row_arrays[cur_ind];
1710 const IT_* col_idx = mat_wrap.col_arrays[cur_ind];
1711 const Adjacency::Graph& row_dofs = *macro_dofs[row_block];
1712 const Adjacency::Graph& col_dofs = *macro_dofs[col_block];
1713
1714 // get our internal blocksize
1715 const int bh_ = int(mat_wrap.get_block_sizes(row_block, col_block).first);
1716 const int bw_ = int(mat_wrap.get_block_sizes(row_block, col_block).second);
1717
1718 // get graph domain pointer arrays
1719 const Index* row_dom_ptr = row_dofs.get_domain_ptr();
1720 const Index* col_dom_ptr = col_dofs.get_domain_ptr();
1721
1722 // get the number of rows/cols to gather
1723 const Index num_rows = row_dom_ptr[macro+1] - row_dom_ptr[macro];
1724 const Index num_cols = col_dom_ptr[macro+1] - col_dom_ptr[macro];
1725
1726 // get graph image index arrays for our macro
1727 const Index* row_img_idx = &(row_dofs.get_image_idx()[row_dom_ptr[macro]]);
1728 const Index* col_img_idx = &(col_dofs.get_image_idx()[col_dom_ptr[macro]]);
1729
1730 gather_loc_cell(local, vals, row_ptr, col_idx, row_img_idx, col_img_idx, bh_, bw_, num_rows, num_cols,
1731 stride, row_off, col_off);
1732
1733 // return the number of gathered rows
1734 return std::make_pair(num_rows * Index(bh_), num_cols * Index(bw_));
1735 }
1736
1737 template<typename DT_, typename IT_, int n_>
1738 CUDA_HOST static std::pair<Index,Index> gather_row(const typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData& mat_wrap,
1739 DT_* local, const Index stride, const Index macro, const Adjacency::Graph** macro_dofs,
1740 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1741 {
1742 std::pair<Index, Index> nrc_r{0,0};
1743 //loop over cols
1744 for(int l = 0; l < n_; ++l)
1745 {
1746 std::pair<Index, Index> nrc_l = gather_loc<DT_,IT_,n_>(mat_wrap, local, stride, macro, macro_dofs, row_off, row_block, col_off+nrc_r.second, col_block+Index(l));
1747 nrc_r.first = nrc_l.first;
1748 nrc_r.second += nrc_l.second;
1749 }
1750
1751 return nrc_r;
1752 }
1753
1754 template<typename DT_, typename IT_, int n_>
1755 CUDA_HOST static std::pair<Index,Index> gather(const typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData& mat_wrap,
1756 DT_* local, const Index stride, const Index macro, const Adjacency::Graph** macro_dofs,
1757 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1758 {
1759 std::pair<Index, Index> nrc_f{0,0};
1760 //loop over rows
1761 for(int l = 0; l < n_; ++l)
1762 {
1763 std::pair<Index, Index> nrc_r = gather_row<DT_,IT_,n_>(mat_wrap, local, stride, macro, macro_dofs, row_off+nrc_f.first, row_block+Index(l), col_off, col_block);
1764 nrc_f.first += nrc_r.first;
1765 nrc_f.second = nrc_r.second;
1766 }
1767
1768 return nrc_f;
1769 }
1770
1771 #ifdef __CUDACC__
1772 // ##################### DEVICE VARIANT ################################
1773 template<typename DT_, typename IT_, int n_, bool uniform_macros_>
1774 CUDA_HOST_DEVICE static cuda::std::pair<Index,Index> gather_loc(const typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData& mat_wrap,
1775 DT_* local, const Index stride, const Index macro, const Index** macro_dofs, Index* max_degrees_dofs,
1776 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1777 {
1778 //gather arrays
1779 const Index cur_ind = Index(n_)*row_block + col_block;
1780 const DT_* vals = mat_wrap.data_arrays[cur_ind];
1781 const IT_* row_ptr = mat_wrap.row_arrays[cur_ind];
1782 const IT_* col_idx = mat_wrap.col_arrays[cur_ind];
1783 const Index* row_dofs = macro_dofs[row_block];
1784 const Index* col_dofs = macro_dofs[col_block];
1785 const Index degree_row = max_degrees_dofs[row_block] + Index(!uniform_macros_);
1786 const Index degree_col = max_degrees_dofs[col_block] + Index(!uniform_macros_);
1787
1788 // get our internal blocksize
1789 const int bh_ = int(mat_wrap.blocksizes[row_block+CudaMath::cuda_min(row_block,Index(1))]);
1790 const int bw_ = int(mat_wrap.blocksizes[col_block+Index(1)]);
1791
1792 // get graph domain pointer arrays
1793 const Index* row_dom_ptr = row_dofs + degree_row*macro;
1794 const Index* col_dom_ptr = col_dofs + degree_col*macro;
1795
1796 // get the number of rows/cols to gather
1797 const Index num_rows = uniform_macros_ ? degree_row : row_dom_ptr[0];
1798 const Index num_cols = uniform_macros_ ? degree_col : col_dom_ptr[0];
1799
1800 // // get graph image index arrays for our macro
1801 // const Index* row_img_idx = &(row_dofs.get_image_idx()[row_dom_ptr[macro]]);
1802 // const Index* col_img_idx = &(col_dofs.get_image_idx()[col_dom_ptr[macro]]);
1803
1804 gather_loc_cell(local, vals, row_ptr, col_idx, row_dom_ptr + Index(!uniform_macros_), col_dom_ptr + Index(!uniform_macros_), bh_, bw_, num_rows, num_cols,
1805 stride, row_off, col_off);
1806
1807 // return the number of gathered rows
1808 return cuda::std::make_pair(num_rows * Index(bh_), num_cols * Index(bw_));
1809 }
1810
1811 template<typename DT_, typename IT_, int n_, bool uniform_macros_>
1812 CUDA_HOST_DEVICE static cuda::std::pair<Index,Index> gather_row(const typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData& mat_wrap,
1813 DT_* local, const Index stride, const Index macro, const Index** macro_dofs, Index* max_degrees_dofs,
1814 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1815 {
1816 cuda::std::pair<Index, Index> nrc_r{0,0};
1817 //loop over cols
1818 for(int l = 0; l < n_; ++l)
1819 {
1820 cuda::std::pair<Index, Index> nrc_l = gather_loc<DT_, IT_, n_, uniform_macros_>(mat_wrap, local, stride, macro, macro_dofs, max_degrees_dofs, row_off, row_block, col_off+nrc_r.second, col_block+Index(l));
1821 nrc_r.first = nrc_l.first;
1822 nrc_r.second += nrc_l.second;
1823 }
1824
1825 return nrc_r;
1826 }
1827
1828 template<typename DT_, typename IT_, int n_, bool uniform_macros_>
1829 CUDA_HOST_DEVICE static cuda::std::pair<Index,Index> gather(const typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData& mat_wrap,
1830 DT_* local, const Index stride, const Index macro, const Index** macro_dofs, Index* max_degrees_dofs,
1831 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1832 {
1833 cuda::std::pair<Index, Index> nrc_f{0,0};
1834 //loop over rows
1835 for(int l = 0; l < n_; ++l)
1836 {
1837 cuda::std::pair<Index, Index> nrc_r = gather_row<DT_, IT_, n_, uniform_macros_>(mat_wrap, local, stride, macro, macro_dofs, max_degrees_dofs, row_off+nrc_f.first, row_block+Index(l), col_off, col_block);
1838 nrc_f.first += nrc_r.first;
1839 nrc_f.second = nrc_r.second;
1840 }
1841
1842 return nrc_f;
1843 }
1844 #endif
1845
1846 template<typename DT_, typename IT_>
1847 CUDA_HOST_DEVICE static void scatter_add_loc_cell(const DT_* local, DT_* vals, const IT_* row_ptr, const IT_* col_idx,
1848 const Index* row_img_idx, const Index* col_img_idx, const int bh_, const int bw_, const Index num_rows, const Index num_cols,
1849 const Index stride, const Index row_off, const Index col_off)
1850 {
1851 ASSERT(vals != nullptr);
1852 // loop over all rows
1853 for(Index i(0); i < num_rows; ++i)
1854 {
1855 // get the index of this row in the big matrix
1856 const Index irow = row_img_idx[i];
1857
1858 // initialize loop variable for local columns
1859 Index j = Index(0);
1860
1861 // row over the row
1862 for(IT_ ai = row_ptr[irow]; ai < row_ptr[irow + Index(1)]; ++ai)
1863 {
1864 // get the column index
1865 const Index icol = Index(col_idx[ai]);
1866
1867 // now search its position in our local matrix
1868 while((j < num_cols) && (col_img_idx[j] < icol))
1869 {
1870 ++j;
1871 }
1872
1873 // did we find our local entry?
1874 if((j < num_cols) && (col_img_idx[j] == icol))
1875 {
1876 // offset by ai times bh*bw site matrices
1877 DT_* mv = vals + ai*IT_(bh_)*IT_(bw_);
1878 // copy matrix macro
1879 for(int ii(0); ii < bh_; ++ii)
1880 {
1881 // compute offset within local matrix
1882 const Index lro = (row_off + i * Index(bh_) + Index(ii)) * stride + col_off + j * Index(bw_);
1883 // copy macro row
1884 for(int jj(0); jj < bw_; ++jj)
1885 {
1886 mv[ii*bw_+jj] += local[lro + IT_(jj)];
1887 }
1888 }
1889 }
1890 }
1891 }
1892 }
1893
1894 //################## Host Version#####################
1895 template<typename DT_, typename IT_, int n_>
1896 CUDA_HOST static std::pair<Index,Index> scatter_add_loc(typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData& mat_wrap,
1897 const DT_* local, const Index stride, const Index macro, const Adjacency::Graph** macro_dofs,
1898 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1899 {
1900 //gather arrays
1901 const Index cur_ind = Index(n_)*row_block + col_block;
1902 DT_* vals = const_cast<DT_*>(mat_wrap.data_arrays[cur_ind]);
1903 const IT_* row_ptr = mat_wrap.row_arrays[cur_ind];
1904 const IT_* col_idx = mat_wrap.col_arrays[cur_ind];
1905 const Adjacency::Graph& row_dofs = *macro_dofs[row_block];
1906 const Adjacency::Graph& col_dofs = *macro_dofs[col_block];
1907
1908 // get our internal blocksize
1909 const int bh_ = int(mat_wrap.get_block_sizes(row_block, col_block).first);
1910 const int bw_ = int(mat_wrap.get_block_sizes(row_block, col_block).second);
1911
1912 // get graph domain pointer arrays
1913 const Index* row_dom_ptr = row_dofs.get_domain_ptr();
1914 const Index* col_dom_ptr = col_dofs.get_domain_ptr();
1915
1916 // get the number of rows/cols to gather
1917 const Index num_rows = row_dom_ptr[macro+1] - row_dom_ptr[macro];
1918 const Index num_cols = col_dom_ptr[macro+1] - col_dom_ptr[macro];
1919
1920 // get graph image index arrays for our macro
1921 const Index* row_img_idx = &(row_dofs.get_image_idx()[row_dom_ptr[macro]]);
1922 const Index* col_img_idx = &(col_dofs.get_image_idx()[col_dom_ptr[macro]]);
1923
1924 scatter_add_loc_cell(local, vals, row_ptr, col_idx, row_img_idx, col_img_idx, bh_, bw_, num_rows, num_cols,
1925 stride, row_off, col_off);
1926
1927 // return the number of gathered rows
1928 return std::make_pair(num_rows * Index(bh_), num_cols * Index(bw_));
1929 }
1930
1931 template<typename DT_, typename IT_, int n_>
1932 CUDA_HOST static std::pair<Index,Index> scatter_add_row(typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData& mat_wrap,
1933 const DT_* local, const Index stride, const Index macro, const Adjacency::Graph** macro_dofs,
1934 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1935 {
1936 std::pair<Index, Index> nrc_r{0,0};
1937 //loop over cols
1938 for(int l = 0; l < n_; ++l)
1939 {
1940 std::pair<Index, Index> nrc_l = scatter_add_loc<DT_,IT_,n_>(mat_wrap, local, stride, macro, macro_dofs, row_off, row_block, col_off+nrc_r.second, col_block+Index(l));
1941 nrc_r.first = nrc_l.first;
1942 nrc_r.second += nrc_l.second;
1943 }
1944
1945 return nrc_r;
1946 }
1947
1948 template<typename DT_, typename IT_, int n_>
1949 CUDA_HOST static std::pair<Index,Index> scatter_add(typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData& mat_wrap,
1950 const DT_* local, const Index stride, const Index macro, const Adjacency::Graph** macro_dofs,
1951 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1952 {
1953 std::pair<Index, Index> nrc_f{0,0};
1954 //loop over rows
1955 for(int l = 0; l < n_; ++l)
1956 {
1957 std::pair<Index, Index> nrc_r = scatter_add_row<DT_,IT_,n_>(mat_wrap, local, stride, macro, macro_dofs, row_off+nrc_f.first, row_block+Index(l), col_off, col_block);
1958 nrc_f.first += nrc_r.first;
1959 nrc_f.second = nrc_f.second;
1960 }
1961
1962 return nrc_f;
1963 }
1964
1965 #ifdef __CUDACC__
1966 //################# Device version #######################
1967 template<typename DT_, typename IT_, int n_, bool uniform_macros_>
1968 CUDA_HOST_DEVICE static cuda::std::pair<Index,Index> scatter_add_loc(typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData& mat_wrap,
1969 const DT_* local, const Index stride, const Index macro, const Index** macro_dofs, Index* max_degrees_dofs,
1970 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
1971 {
1972 //gather arrays
1973 const Index cur_ind = Index(n_)*row_block + col_block;
1974 DT_* vals = mat_wrap.data_arrays[cur_ind];
1975 const IT_* row_ptr = mat_wrap.row_arrays[cur_ind];
1976 const IT_* col_idx = mat_wrap.col_arrays[cur_ind];
1977 const Index* row_dofs = macro_dofs[row_block];
1978 const Index* col_dofs = macro_dofs[col_block];
1979 const Index degree_row = max_degrees_dofs[row_block] + Index(!uniform_macros_);
1980 const Index degree_col = max_degrees_dofs[col_block] + Index(!uniform_macros_);
1981
1982 // get our internal blocksize
1983 const int bh_ = int(mat_wrap.blocksizes[row_block+CudaMath::cuda_min(row_block,Index(1))]);
1984 const int bw_ = int(mat_wrap.blocksizes[col_block+Index(1)]);
1985
1986 // get graph domain pointer arrays
1987 const Index* row_dom_ptr = row_dofs + degree_row*macro;
1988 const Index* col_dom_ptr = col_dofs + degree_col*macro;
1989
1990 // get the number of rows/cols to gather
1991 const Index num_rows = uniform_macros_ ? degree_row : row_dom_ptr[0];
1992 const Index num_cols = uniform_macros_ ? degree_col : col_dom_ptr[0];
1993
1994 // // get graph image index arrays for our macro
1995 // const Index* row_img_idx = &(row_dofs.get_image_idx()[row_dom_ptr[macro]]);
1996 // const Index* col_img_idx = &(col_dofs.get_image_idx()[col_dom_ptr[macro]]);
1997
1998 scatter_add_loc_cell(local, vals, row_ptr, col_idx, row_dom_ptr + Index(!uniform_macros_), col_dom_ptr + Index(!uniform_macros_), bh_, bw_, num_rows, num_cols,
1999 stride, row_off, col_off);
2000
2001 // return the number of gathered rows
2002 return cuda::std::make_pair(num_rows * Index(bh_), num_cols * Index(bw_));
2003 }
2004
2005 template<typename DT_, typename IT_, int n_, bool uniform_macros_>
2006 CUDA_HOST_DEVICE static cuda::std::pair<Index,Index> scatter_add_row(typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData& mat_wrap,
2007 const DT_* local, const Index stride, const Index macro, const Index** macro_dofs, Index* max_degrees_dofs,
2008 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
2009 {
2010 cuda::std::pair<Index, Index> nrc_r{0,0};
2011 //loop over cols
2012 for(int l = 0; l < n_; ++l)
2013 {
2014 cuda::std::pair<Index, Index> nrc_l = scatter_add_loc<DT_, IT_, n_, uniform_macros_>(mat_wrap, local, stride, macro, macro_dofs, max_degrees_dofs, row_off, row_block, col_off+nrc_r.second, col_block+Index(l));
2015 nrc_r.first = nrc_l.first;
2016 nrc_r.second += nrc_l.second;
2017 }
2018
2019 return nrc_r;
2020 }
2021
2022 template<typename DT_, typename IT_, int n_, bool uniform_macros_>
2023 CUDA_HOST_DEVICE static cuda::std::pair<Index,Index> scatter_add(typename Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>::RawData& mat_wrap,
2024 const DT_* local, const Index stride, const Index macro, const Index** macro_dofs, Index* max_degrees_dofs,
2025 const Index row_off, const Index row_block, const Index col_off, const Index col_block)
2026 {
2027 cuda::std::pair<Index, Index> nrc_f{0,0};
2028 //loop over rows
2029 for(int l = 0; l < n_; ++l)
2030 {
2031 cuda::std::pair<Index, Index> nrc_r = scatter_add_row<DT_, IT_, n_, uniform_macros_>(mat_wrap, local, stride, macro, macro_dofs, max_degrees_dofs, row_off+nrc_f.first, row_block+Index(l), col_off, col_block);
2032 nrc_f.first += nrc_r.first;
2033 nrc_f.second = nrc_f.second;
2034 }
2035
2036 return nrc_f;
2037 }
2038 #endif
2039
2040 template<typename DT_, typename IT_, bool skip_singular_>
2041 CUDA_HOST static void scale_row(DT_* vals, DT_ omega, const IT_* row_ptr, const IT_* col_idx, const Index* row_dom_ptr, const Index* row_img_idx,
2042 int hw, int hb, Index cur_row, const int* m_mask, const bool meta_diag)
2043 {
2044 ASSERT(row_dom_ptr[cur_row] < row_dom_ptr[cur_row+1]);
2045 //get number of active macros for this DOF
2046 Index n(0);
2047 if constexpr(skip_singular_)
2048 {
2049 for(Index j = row_dom_ptr[cur_row]; j < row_dom_ptr[cur_row+1]; ++j)
2050 {
2051 n += Index(m_mask[row_img_idx[j]]);
2052 }
2053 }
2054 else
2055 {
2056 n = row_dom_ptr[cur_row+1] - row_dom_ptr[cur_row];
2057 }
2058
2059 if(n > 0)
2060 {
2061 const DT_ sc = omega/ DT_(Math::max(n, Index(1)));
2062 for(IT_ j = row_ptr[cur_row]; j < row_ptr[cur_row+1]; ++j)
2063 {
2064 DT_* loc_val = vals + j*IT_(hw)*IT_(hb);
2065 for(int ii = 0; ii < hw; ++ii)
2066 {
2067 for(int jj = 0; jj < hb; ++jj)
2068 {
2069 loc_val[ii*hb+jj] *= sc;
2070 }
2071 }
2072 }
2073 }
2074 else if(meta_diag) // null row replace by unit row
2075 {
2076 for(IT_ j = row_ptr[cur_row]; j < row_ptr[cur_row+1]; ++j)
2077 {
2078 DT_* loc_val = vals + j*IT_(hw)*IT_(hb);
2079 const DT_ diag_val = DT_(col_idx[j] == cur_row ? 1 : 0);
2080 for(int ii = 0; ii < hw; ++ii)
2081 {
2082 for(int jj = 0; jj < hb; ++jj)
2083 {
2084 loc_val[ii*hb+jj] = DT_(0);
2085 }
2086 }
2087 //handle the diagonal entries
2088 for(int ii = 0; ii < Math::min(hw, hb); ++ii)
2089 {
2090 loc_val[ii*hb + ii] = diag_val;
2091 }
2092 }
2093 }
2094
2095 }
2096
2097 #ifdef __CUDACC__
2098 template<typename DT_, typename IT_, bool skip_singular_>
2099 CUDA_HOST_DEVICE static void scale_row(DT_* vals, DT_ omega, const IT_* row_ptr, const IT_* col_idx, const Index* row_dom_ptr, const Index row_number,
2100 int hw, int hb, Index cur_row, const int* m_mask, bool meta_diag)
2101 {
2102 //get number of active macros for this DOF
2103 Index n(0);
2104 if constexpr(skip_singular_)
2105 {
2106 for(Index j = 0; j < row_number; ++j)
2107 {
2108 n += Index(m_mask[row_dom_ptr[j]]);
2109 }
2110 }
2111 else
2112 {
2113 n = row_number;
2114 }
2115
2116 if(n > 0)
2117 {
2118 const DT_ sc = omega/ DT_(CudaMath::cuda_max(n, Index(1)));
2119 for(IT_ j = row_ptr[cur_row]; j < row_ptr[cur_row+1]; ++j)
2120 {
2121 DT_* loc_val = vals + j*hw*hb;
2122 for(int ii = 0; ii < hw; ++ii)
2123 {
2124 for(int jj = 0; jj < hb; ++jj)
2125 {
2126 loc_val[ii*hb+jj] *= sc;
2127 }
2128 }
2129 }
2130 }
2131 else if(meta_diag) // null row replace by unit row
2132 {
2133 for(IT_ j = row_ptr[cur_row]; j < row_ptr[cur_row+1]; ++j)
2134 {
2135 DT_* loc_val = vals + j*hw*hb;
2136 const DT_ diag_val = DT_(col_idx[j] == cur_row ? 1 : 0);
2137 for(int ii = 0; ii < hw; ++ii)
2138 {
2139 for(int jj = 0; jj < hb; ++jj)
2140 {
2141 loc_val[ii*hb+jj] = DT_(0);
2142 }
2143 }
2144 //handle the diagonal entries
2145 for(int ii = 0; ii < CudaMath::cuda_min(hw, hb); ++ii)
2146 {
2147 loc_val[ii*hb + ii] = diag_val;
2148 }
2149 }
2150 }
2151
2152 }
2153 #endif
2154 };
2155
2156 } // namespace Intern
2158 }
2159}
#define ASSERT(expr)
Debug-Assertion macro definition.
Definition: assertion.hpp:229
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
Definition: assertion.hpp:230
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
String class implementation.
Definition: string.hpp:47
@ other
generic/other permutation strategy
Location
Memory location enumeration.
FEAT namespace.
Definition: adjactor.hpp:12
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:993
std::uint64_t Index
Index data type.