FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
voxel_amavanka.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
10#include <kernel/backend.hpp>
11#include <kernel/adjacency/coloring_data_handler.hpp>
12#include <kernel/lafem/null_matrix.hpp>
13#include <kernel/lafem/sparse_matrix_bcsr.hpp>
14#include <kernel/lafem/sparse_matrix_csr.hpp>
15#include <kernel/lafem/saddle_point_matrix.hpp>
16#include <kernel/lafem/tuple_matrix.hpp>
17#include <kernel/global/matrix.hpp>
18#include <kernel/solver/base.hpp>
19#include <kernel/util/stop_watch.hpp>
20#include <kernel/solver/amavanka.hpp>
21
22#ifdef FEAT_HAVE_CUDA
23#include <kernel/util/cuda_util.hpp>
24#endif
25
26namespace FEAT
27{
28 namespace Intern
29 {
35 enum VankaAssemblyPolicy
36 {
38 oneThreadperBlock = 0,
40 batchedAssembly = 1
41 };
42
51 enum VankaMacroPolicy
52 {
54 anisotropicMacros = 0,
56 uniformMacros = 1
57 };
58 }
59 namespace Solver
60 {
61 namespace Arch
62 {
105 template<typename DT_, typename IT_, int n_>
106 void assemble_vanka_host(const Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>& mat_wrap,
107 Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>& vanka_wrap, const std::vector<Adjacency::Graph>& macro_dofs,
108 const std::vector<Adjacency::Graph>& dof_macros, std::vector<int>& macro_mask, const Adjacency::ColoringDataHandler& coloring_data,
109 Index stride, DT_ omega, DT_ eps, bool skip_singular);
110
184 template<typename DT_, typename IT_, int n_>
185 void assemble_vanka_device(const Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>& mat_wrap,
186 Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>& vanka_wrap, const std::vector<Memory::Arbiter>& d_macro_dofs,
187 const std::vector<Memory::Arbiter>& d_dof_macros, Memory::Arbiter& d_macro_mask, const std::vector<Index>& max_degree_dofs,
188 const std::vector<Index>& max_degree_macros, const Adjacency::ColoringDataHandler& coloring_data,
189 Index num_macros, Index stride, DT_ omega, DT_ eps, bool skip_singular, bool uniform_macros);
190
260 template<typename DT_, typename IT_, int n_>
261 void assemble_vanka_device_batched(const Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>& mat_wrap,
262 Intern::CSRTupleMatrixWrapper<DT_, IT_, n_>& vanka_wrap, const std::vector<Memory::Arbiter>& d_macro_dofs,
263 const std::vector<Memory::Arbiter>& d_dof_macros, Memory::Arbiter& d_macro_mask, const std::vector<Index>& max_degree_dofs,
264 const std::vector<Index>& max_degree_macros, const Adjacency::ColoringDataHandler& coloring_data,
265 Index num_macros, Index stride, Index actual_matrix_size, DT_ omega, bool skip_singular, bool synchronize);
266 }
267
268 #ifndef __CUDACC__
299 template<typename Matrix_,
300 typename Filter_,
301 FEAT::Intern::VankaAssemblyPolicy pol_threading_ = FEAT::Intern::VankaAssemblyPolicy::batchedAssembly,
302 FEAT::Intern::VankaMacroPolicy macro_type_ = FEAT::Intern::VankaMacroPolicy::uniformMacros>
304 public Solver::AmaVanka<Matrix_, Filter_>
305 {
306 public:
309
311 typedef typename Matrix_::DataType DataType;
313 typedef typename Matrix_::IndexType IndexType;
315 typedef typename Matrix_::VectorTypeL VectorType;
316
317 protected:
319 typedef typename Intern::AmaVankaMatrixHelper<Matrix_>::VankaMatrix VankaMatrixType;
321 typedef Intern::CSRTupleMatrixWrapper<DataType, IndexType, Intern::AmaVankaMatrixHelper<Matrix_>::num_blocks> MatrixWrapper;
323 typedef Intern::CSRTupleMatrixWrapper<DataType, IndexType, Intern::AmaVankaMatrixHelper<VankaMatrixType>::num_blocks> VankaWrapper;
327 std::vector<Memory::Arbiter> _d_macro_dofs, _d_dof_macros; // of Index
329 std::vector<Index> _max_degree_dofs, _max_degree_macros;
333 bool _allocate_device = false;
337
340 {
341 _max_degree_dofs.resize(this->_macro_dofs.size());
342 _max_degree_macros.resize(this->_macro_dofs.size());
343 for(std::size_t i = 0; i < _max_degree_dofs.size(); ++i)
344 {
345 if constexpr(macro_type_ == FEAT::Intern::VankaMacroPolicy::uniformMacros)
346 _max_degree_dofs[i] = this->_macro_dofs[i].degree(Index(0));
347 else
348 _max_degree_dofs[i] = this->_macro_dofs[i].degree();
349 _max_degree_macros[i] = this->_dof_macros[i].degree();
350 }
351 }
352
355 {
356 XASSERTM(_max_degree_dofs.size() == this->_macro_dofs.size(), "call _alloc_max_degrees beforehand");
358 return;
359 #ifdef FEAT_HAVE_CUDA
360 _d_macro_dofs.resize(this->_macro_dofs.size());
361 _d_dof_macros.resize(this->_dof_macros.size());
362 for(std::size_t i = 0; i < this->_macro_dofs.size(); ++i)
363 {
364 Index malloc_size;
365 if constexpr(macro_type_ == FEAT::Intern::VankaMacroPolicy::uniformMacros)
366 malloc_size = this->_macro_dofs[i].get_num_nodes_domain() * _max_degree_dofs[i] * sizeof(Index);
367 else
368 malloc_size = this->_macro_dofs[i].get_num_nodes_domain() * (_max_degree_dofs[i]+1) * sizeof(Index);
369 _d_macro_dofs[i] = Memory::Arbiter(malloc_size);
370 // prepare tmp array
371 {
373 Index* tmp_alias = dmd_view.get_w();
374 const Index* dom_ptr = this->_macro_dofs[i].get_domain_ptr();
375 const Index* img_ptr = this->_macro_dofs[i].get_image_idx();
376 for(Index k = 0; k < this->_macro_dofs[i].get_num_nodes_domain(); ++k)
377 {
378 if constexpr(macro_type_ == FEAT::Intern::VankaMacroPolicy::uniformMacros)
379 {
380 Memory::memcopy_main(tmp_alias + k*_max_degree_dofs[i], img_ptr + dom_ptr[k], _max_degree_dofs[i]*sizeof(Index));
381 }
382 else
383 {
384 const Index loc_size = dom_ptr[k+1] - dom_ptr[k];
385 tmp_alias[k*(_max_degree_dofs[i]+1)] = loc_size;
386 Memory::memcopy_main(tmp_alias + k*(_max_degree_dofs[i]+1) + 1, img_ptr + dom_ptr[k], loc_size*sizeof(Index));
387 Memory::memset_main(tmp_alias + k*(_max_degree_dofs[i]+1) + loc_size + 1, ~int(0), (_max_degree_dofs[i] - loc_size)*sizeof(Index));
388 }
389 }
390 }
391 malloc_size = this->_dof_macros[i].get_num_nodes_domain()*(_max_degree_macros[i]+1)*sizeof(Index);
392 _d_dof_macros[i] = Memory::Arbiter(malloc_size);
393 {
395 Index* tmp_alias = ddm_view.get_w();
396 const Index* dom_ptr = this->_dof_macros[i].get_domain_ptr();
397 const Index* img_ptr = this->_dof_macros[i].get_image_idx();
398 for(Index k = 0; k < this->_dof_macros[i].get_num_nodes_domain(); ++k)
399 {
400 const Index loc_size = dom_ptr[k+1] - dom_ptr[k];
401 tmp_alias[k*(_max_degree_macros[i]+1)] = loc_size;
402 Memory::memcopy_main(tmp_alias + k*(_max_degree_macros[i]+1) + 1, img_ptr + dom_ptr[k], loc_size*sizeof(Index));
403 Memory::memset_main(tmp_alias + k*(_max_degree_macros[i]+1) + loc_size + 1, ~int(0), (_max_degree_macros[i] - loc_size)*sizeof(Index));
404 }
405 }
406 }
407 {
408 if(this->_skip_singular)
409 {
410 _d_macro_mask = Memory::Arbiter(this->_macro_mask.size()*sizeof(int),
412 }
413 else
414 {
415 // we need to create an empty arbiter so that we can create a view
416 // to it in the kernel wrapper functions; otherwise we would need to
417 // perform a lot more if-else there...
418 _d_macro_mask = Memory::Arbiter(0u);
419 }
420 }
421 #endif
422 }
423
426 {
428 _d_dof_macros.clear();
429 _d_macro_dofs.clear();
430 }
431
450 void _init_numeric_generic(const MatrixWrapper& mat_wrap, VankaWrapper& vanka_wrap, Index DOXY(num_macros), Index stride, DataType eps)
451 {
452 //call backend
453 Arch::assemble_vanka_host(mat_wrap, vanka_wrap, this->_macro_dofs, this->_dof_macros, this->_macro_mask, _coloring_data,
454 stride, this->_omega, eps, this->_skip_singular);
455 }
456
457 #if defined(FEAT_HAVE_CUDA) || defined(DOXYGEN)
476 void _init_numeric_cuda(const MatrixWrapper& mat_wrap, VankaWrapper& vanka_wrap, Index num_macros, Index stride, DataType eps)
477 {
478 XASSERTM(_allocate_device, "Allocate device disabled!");
479 bool uniform_macros = (macro_type_ == FEAT::Intern::VankaMacroPolicy::uniformMacros);
480 //call backend
481 if constexpr(pol_threading_ == FEAT::Intern::VankaAssemblyPolicy::oneThreadperBlock)
482 {
483 Arch::assemble_vanka_device(mat_wrap, vanka_wrap, _d_macro_dofs, _d_dof_macros, _d_macro_mask, _max_degree_dofs, _max_degree_macros, _coloring_data, num_macros, stride, this->_omega, eps, this->_skip_singular, uniform_macros);
484 }
485 else if (pol_threading_ == FEAT::Intern::VankaAssemblyPolicy::batchedAssembly)
486 {
487 XASSERTM(uniform_macros, "Batched assembly only works with uniform macros!");
488 // TODO: stride always actual local matrix size?
489 Arch::assemble_vanka_device_batched(mat_wrap, vanka_wrap, _d_macro_dofs, _d_dof_macros, _d_macro_mask, _max_degree_dofs, _max_degree_macros, _coloring_data, num_macros, stride, stride, this->_omega, this->_skip_singular, _sync_after_assembly);
490 }
491 }
492 #endif
493
494
495 public:
517 template<typename ColoringType_>
518 explicit VoxelAmaVanka(const Matrix_& matrix, const Filter_& filter,
519 const ColoringType_& coloring,
520 const DataType omega = DataType(1), const Index num_steps = Index(1)) :
521 BaseClass(matrix, filter, omega, num_steps),
524 _d_dof_macros(),
526 {
527 _coloring_data.fill_color(coloring);
528 #ifdef FEAT_HAVE_CUDA
529 _allocate_device = Util::cuda_get_device_count() > 0;
530 #endif
531 }
532
545 {
547 }
548
549 // rule of 5
550 VoxelAmaVanka(const VoxelAmaVanka&) = delete;
551
552 VoxelAmaVanka& operator=(const VoxelAmaVanka&) = delete;
553
554 // VoxelAmaVanka(VoxelAmaVanka&& other) noexcept :
555 // BaseClass(std::move(other)),
556 // _coloring_data(std::move(other._coloring_data)),
557 // _d_macro_dofs(std::move(other._d_macro_dofs)),
558 // _d_dof_macros(std::move(other._d_dof_macros)),
559 // _max_degree_dofs(std::move(other._max_degree_dofs)),
560 // _max_degree_macros(std::move(other._max_degree_macros)),
561 // _d_macro_mask(other._d_macro_mask),
562 // _allocate_device(other._allocate_device)
563 // {
564 // other._d_macro_dofs.clear();
565 // other._d_dof_macros.clear();
566 // other._max_degree_dofs.clear();
567 // other._max_degree_macros.clear();
568 // other._d_macro_mask = nullptr;
569 // }
570
571 // VoxelAmaVanka& operator=(VoxelAmaVanka&& other) noexcept
572 // {
573 // if(this == &other)
574 // return *this;
575 // this->_free_device();
576 // BaseClass::operator=(std::move(other));
577 // _coloring_data = std::move(other._coloring_data);
578 // _d_macro_dofs = std::move(other._d_macro_dofs);
579 // _d_dof_macros = std::move(other._d_dof_macros);
580 // _max_degree_dofs = std::move(other._max_degree_dofs);
581 // _max_degree_macros = std::move(other._max_degree_macros);
582 // _d_macro_mask = other._d_macro_mask;
583 // _allocate_device = other._allocate_device;
584 // other._d_macro_dofs.clear();
585 // other._d_dof_macros.clear();
586 // other._max_degree_dofs.clear();
587 // other._max_degree_macros.clear();
588 // other._d_macro_mask = nullptr;
589 // }
590
591 VoxelAmaVanka(VoxelAmaVanka&&) noexcept = delete;
592
593
594 VoxelAmaVanka& operator=(VoxelAmaVanka&&) noexcept = delete;
595
596 // /**
597 // * \brief Sets whether device ptr should be allocated.
598 // *
599 // * \param[in] allocate Should device ptr be allocated?
600 // *
601 // * \warning Setting this to false while having PreferedBackend set to cuda during the assembly
602 // * will lead to an error.
603 // */
604 // void set_allocate_device(bool allocate)
605 // {
606 // _allocate_device = allocate;
607 // }
608
618 template<typename ColoringType_>
619 void fill_color(const ColoringType_& color, int hint = -1)
620 {
621 XASSERTM(color.size() == this->_macro_dofs.front().get_num_nodes_domain(), "Coloring does not fit macro dofs");
622 XASSERTM(_coloring_data.initialized(), "Coloring data already initialized");
623 _coloring_data.fill_color(color, hint);
624 }
625
627 virtual String name() const override
628 {
629 return "VoxelAmaVanka";
630 }
631
633 virtual void init_symbolic() override
634 {
636 this->watch_init_symbolic.start();
638 // _alloc_row_helper();
640 this->watch_init_symbolic.stop();
641 }
642
644 virtual void done_symbolic() override
645 {
646 _free_device();
647 // _accum_row_ctr.clear();
648 // _accum_row_index.clear();
649 _max_degree_dofs.clear();
650 _max_degree_macros.clear();
652 }
653
654
655 virtual void init_numeric() override
656 {
657 const DataType eps = Math::eps<DataType>();
658 //call numeric init of BaseSolver, but not of Vanka
659 this->watch_init_numeric.start();
661
662 // get maximum macro size
663 const Index num_macros = Index(this->_macro_dofs.front().get_num_nodes_domain());
664 const Index stride = Intern::AmaVankaCore::calc_stride(this->_vanka, this->_macro_dofs);
665 this->_vanka.format();
666
667#if defined(FEAT_HAVE_CUDA) || defined(DOXYGEN)
669 {
670 auto matrix_wrapper = Solver::Intern::get_meta_matrix_wrapper(this->_matrix, Memory::Location::cuda);
671 auto vanka_wrapper = Solver::Intern::get_meta_matrix_wrapper(this->_vanka, Memory::Location::cuda);
672 _init_numeric_cuda(matrix_wrapper, vanka_wrapper, num_macros, stride, eps);
673 }
674 else
675#endif
676 {
677 auto matrix_wrapper = Solver::Intern::get_meta_matrix_wrapper(this->_matrix, Memory::Location::main);
678 auto vanka_wrapper = Solver::Intern::get_meta_matrix_wrapper(this->_vanka, Memory::Location::main);
679 _init_numeric_generic(matrix_wrapper, vanka_wrapper, num_macros, stride, eps);
680 }
681 this->watch_init_numeric.stop();
682 }
683
684
685 }; // VoxelAmaVanka
686
687
709 template<typename Matrix_, typename Filter_, typename ColoringType_,
710 FEAT::Intern::VankaAssemblyPolicy pol_threading_ = FEAT::Intern::VankaAssemblyPolicy::batchedAssembly,
711 FEAT::Intern::VankaMacroPolicy macro_type_ = FEAT::Intern::VankaMacroPolicy::uniformMacros>
712 std::shared_ptr<VoxelAmaVanka<Matrix_, Filter_, pol_threading_, macro_type_>> new_voxel_amavanka(const Matrix_& matrix, const Filter_& filter, const ColoringType_& coloring,
713 typename Matrix_::DataType omega = typename Matrix_::DataType(1), Index num_steps = Index(1))
714 {
715 return std::make_shared<VoxelAmaVanka<Matrix_, Filter_, pol_threading_, macro_type_>>(matrix, filter,coloring, omega, num_steps);
716 }
717 #endif //__CUDACC__
718
719 }
720}
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
Datahandler for inverse coloring data.
void fill_color(const std::vector< int > &coloring, int hint=-1)
Fill in the coloring array.
static PreferredBackend get_preferred_backend()
get current preferred backend
Definition: backend.cpp:32
Memory arbiter class.
void release()
Releases this arbiter from its associated memory block and frees it if necessary.
Typed memory view class.
T_ * get_w()
Returns a typed pointer to the array that grants write (and read) access.
Additive Macro-wise Matrix-based Vanka preconditioner/smoother.
Definition: amavanka.hpp:65
const Matrix_ & _matrix
the system matrix
Definition: amavanka.hpp:81
std::vector< int > _macro_mask
the macro mask
Definition: amavanka.hpp:93
DataType _omega
damping parameter
Definition: amavanka.hpp:97
bool _skip_singular
skip singular macros?
Definition: amavanka.hpp:89
virtual void init_symbolic() override
Performs symbolic factorization.
Definition: amavanka.hpp:285
VankaMatrixType _vanka
the Vanka preconditioner matrix
Definition: amavanka.hpp:85
virtual void done_symbolic() override
Releases the symbolic factorization data.
Definition: amavanka.hpp:332
std::vector< Adjacency::Graph > _macro_dofs
the DOF-macro graphs
Definition: amavanka.hpp:91
virtual void init_numeric()
Numeric initialization method.
Definition: base.hpp:237
Additive Macro-wise Matrix-based Vanka preconditioner/smoother.
void set_sync_after_assembly(bool sync)
Controls whether the batched cuda assembly synchronizes at the end.
bool _allocate_device
flag whether we should allocate additional device pointer
void _init_numeric_generic(const MatrixWrapper &mat_wrap, VankaWrapper &vanka_wrap, Index num_macros, Index stride, DataType eps)
Calls generic numeric kernel.
std::vector< Index > _max_degree_dofs
size data
Memory::Arbiter _d_macro_mask
array of macro mask
virtual void done_symbolic() override
Frees symbolic values and device pointers.
Matrix_::DataType DataType
our data type
Intern::CSRTupleMatrixWrapper< DataType, IndexType, Intern::AmaVankaMatrixHelper< Matrix_ >::num_blocks > MatrixWrapper
our matrix data wrapper
void _free_device()
Frees device pointers.
void _init_numeric_cuda(const MatrixWrapper &mat_wrap, VankaWrapper &vanka_wrap, Index num_macros, Index stride, DataType eps)
Calls cuda numeric kernel.
Intern::AmaVankaMatrixHelper< Matrix_ >::VankaMatrix VankaMatrixType
the type of our Vanka matrix
Matrix_::VectorTypeL VectorType
our vector type
Intern::CSRTupleMatrixWrapper< DataType, IndexType, Intern::AmaVankaMatrixHelper< VankaMatrixType >::num_blocks > VankaWrapper
our vanka data wrapper
std::vector< Memory::Arbiter > _d_macro_dofs
vector of graph arrays
Solver::AmaVanka< Matrix_, Filter_ > BaseClass
our base-class
virtual void init_symbolic() override
Initializes symbolic values and device pointers.
Adjacency::ColoringDataHandler _coloring_data
coloring
Matrix_::IndexType IndexType
our index type
VoxelAmaVanka(const Matrix_ &matrix, const Filter_ &filter, const ColoringType_ &coloring, const DataType omega=DataType(1), const Index num_steps=Index(1))
Constructor.
void fill_color(const ColoringType_ &color, int hint=-1)
Fills the coloring data.
virtual String name() const override
Returns the name of the solver.
void _alloc_device()
Allocates device pointers, if required.
virtual void init_numeric() override
Performs numeric factorization.
void _alloc_max_degrees()
Calculate the max degree of our graphs.
void start()
Starts the stop-watch.
Definition: stop_watch.hpp:43
void stop()
Stops the stop-watch and increments elapsed time.
Definition: stop_watch.hpp:51
String class implementation.
Definition: string.hpp:47
void memcopy_main(void *dst, const void *src, std::size_t bytes)
Copies a region of a main memory block into another main memory block.
@ write
write-only access (see attention note above)
void memset_main(void *ptr, int value, std::size_t bytes)
Formats all bytes of a memory block in main memory to a given value.
@ format_to_zero
allocate and format all bits to zero
std::shared_ptr< VoxelAmaVanka< Matrix_, Filter_, pol_threading_, macro_type_ > > new_voxel_amavanka(const Matrix_ &matrix, const Filter_ &filter, const ColoringType_ &coloring, typename Matrix_::DataType omega=typename Matrix_::DataType(1), Index num_steps=Index(1))
Creates a new VoxelAmaVanka smoother object.
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.