1#ifndef DUNE_FEM_SOLVER_VIENNACL_HH
2#define DUNE_FEM_SOLVER_VIENNACL_HH
7#define VIENNACL_WITH_EIGEN
16#define VIENNACL_WITH_OPENMP
19#include <viennacl/linalg/bicgstab.hpp>
20#include <viennacl/linalg/cg.hpp>
21#include <viennacl/linalg/gmres.hpp>
22#include <viennacl/linalg/ilu.hpp>
23#include <viennacl/compressed_matrix.hpp>
24#include <viennacl/vector.hpp>
26#include <dune/fem/solver/inverseoperatorinterface.hh>
27#include <dune/fem/operator/linear/spoperator.hh>
28#include <dune/fem/operator/linear/eigenoperator.hh>
30#include <dune/fem/solver/parameter.hh>
38 template<
class DiscreteFunction,
int method = -1 >
39 class ViennaCLInverseOperator;
41 template<
class DiscreteFunction,
int method >
42 struct ViennaCLInverseOperatorTraits
45 typedef AdaptiveDiscreteFunction< typename DiscreteFunction::DiscreteFunctionSpaceType > SolverDiscreteFunctionType;
48 typedef OperatorType PreconditionerType;
50 typedef Fem::SparseRowLinearOperator< DiscreteFunction, DiscreteFunction > AssembledOperatorType;
53 typedef SolverParameter SolverParameterType;
60 template<
class DiscreteFunction,
int method >
61 class ViennaCLInverseOperator
62 :
public InverseOperatorInterface< ViennaCLInverseOperatorTraits< DiscreteFunction, method > >
64 typedef InverseOperatorInterface< ViennaCLInverseOperatorTraits< DiscreteFunction, method > > BaseType;
67 typedef typename BaseType::DomainFunctionType DomainFunction;
68 typedef typename BaseType::RangeFunctionType RangeFunction;
70 typedef typename DomainFunction :: DiscreteFunctionSpaceType DomainSpaceType;
71 typedef typename RangeFunction :: DiscreteFunctionSpaceType RangeSpaceType;
74 typedef Fem::EigenLinearOperator< RangeFunction, DomainFunction > EigenOperatorType;
77 typedef Fem::SparseRowLinearOperator< RangeFunction, DomainFunction > OperatorType;
79 typedef typename BaseType :: SolverDiscreteFunctionType SolverDiscreteFunctionType;
82 typedef typename OperatorType::MatrixType Matrix;
87 typedef viennacl::compressed_matrix< Field > ViennaCLMatrix;
88 typedef viennacl::vector< Field > ViennaCLVector;
91 typedef viennacl::linalg::no_precond Preconditioner;
93 ViennaCLInverseOperator (
const OperatorType &op,
double redEps,
double absLimit,
unsigned int maxIter,
bool verbose )
94 : ViennaCLInverseOperator( redEps, absLimit, maxIter, verbose )
100 : ViennaCLInverseOperator( redEps, absLimit, maxIter, false )
106 const bool verbose =
false,
107 const SolverParameter& parameter = SolverParameter( Parameter::container() ) )
108 : BaseType( parameter ),
109 absLimit_( absLimit ),
110 method_( method < 0 ? parameter.solverMethod({ SolverParameter::gmres, SolverParameter::cg, SolverParameter::bicgstab }) : method )
114 ViennaCLInverseOperator (
const SolverParameter& parameter = SolverParameter( Parameter::container() ) )
115 : ViennaCLInverseOperator( -1,
116 parameter.tolerance(), parameter.maxIterations(),
117 parameter.verbose(), parameter )
119 assert( parameter.errorMeasure() == 0 );
122 void bind(
const OperatorType& op )
124 BaseType::bind( op );
126 eigenMatrix_ =
dynamic_cast<const EigenOperatorType*
> (&op);
129 const auto& matrix = eigenMatrix_->matrix();
130 gupMatrix_.resize( matrix.rows(), matrix.cols() );
131 viennacl::copy( matrix.data(), gpuMatrix_ );
135 if( assembledOperator_ )
137 const auto& matrix = assembledOperator_->matrix();
138 gpuMatrix_.resize( matrix.rows(), matrix.cols() );
139 std::vector< std::map< size_type, Field > > cpuMatrix;
140 matrix.fillCSRStorage( cpuMatrix );
141 viennacl::copy( cpuMatrix, gpuMatrix_ );
148 gpuMatrix_ = ViennaCLMatrix();
150 eigenOperator_ =
nullptr;
154 int apply(
const SolverDiscreteFunctionType& u, SolverDiscreteFunctionType& w )
const
156 viennacl::vector< Field > vclU( u.size() ), vclW( w.size() );
157 viennacl::copy( u.dbegin(), u.dend(), vclU.begin() );
159 int maxIterations = parameter().maxIterations();
164 if( method_ == SolverParameter::cg )
167 viennacl::linalg::cg_tag tag( absLimit_, maxIterations );
168 vclW = viennacl::linalg::solve( gpuMatrix_, vclU, tag, ilu0 );
169 iterations = tag.iters();
171 else if ( method_ == SolverParameter::bicgstab )
188 viennacl::linalg::bicgstab_tag tag( absLimit_, maxIterations );
189 vclW = viennacl::linalg::solve( gpuMatrix_, vclU, tag, ilu0 );
190 iterations = tag.iters();
192 else if ( method_ == SolverParameter::gmres )
195 viennacl::linalg::gmres_tag tag( absLimit_, maxIterations );
196 vclW = viennacl::linalg::solve( gpuMatrix_, vclU, tag, ilu0 );
197 iterations = tag.iters();
201 DUNE_THROW(NotImplemented,
"ViennaCL does not support this solver");
204 viennacl::copy( vclW.begin(), vclW.end(), w.dbegin() );
209 ViennaCLMatrix gpuMatrix_;
211 using BaseType :: assembledOperator_;
212 using BaseType :: iterations_;
213 using BaseType :: parameter;
215 const EigenOperatorType* eigenOperator_ =
nullptr;
228 template<
class DiscreteFunction >
229 using ViennaCLCGInverseOperator = ViennaCLInverseOperator< DiscreteFunction, SolverParameter :: cg >;
235 template<
class DiscreteFunction >
236 using ViennaCLBiCGStabInverseOperator = ViennaCLInverseOperator< DiscreteFunction, SolverParameter :: bicgstab >;
242 template<
class DiscreteFunction >
243 using ViennaCLGMResInverseOperator = ViennaCLInverseOperator< DiscreteFunction, SolverParameter :: gmres >;
Inverse operator base on CG method. Uses a runtime parameter fem.preconditioning which enables diagon...
Definition: cginverseoperator.hh:408
forward declaration
Definition: discretefunction.hh:51
A::size_type size_type
Type for indices and sizes.
Definition: matrix.hh:577
typename Imp::BlockTraits< T >::field_type field_type
Export the type representing the underlying field.
Definition: matrix.hh:565
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
Dune namespace.
Definition: alignedallocator.hh:13