| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
|
| 6 |
#ifndef DUNE_ISTL_TEST_MULTIRHSTEST_HH |
| 7 |
#define DUNE_ISTL_TEST_MULTIRHSTEST_HH |
| 8 |
|
| 9 |
#define DISABLE_AMG_DIRECTSOLVER 1 |
| 10 |
|
| 11 |
#include <iostream> // for input/output to shell |
| 12 |
#include <fstream> // for input/output to files |
| 13 |
#include <vector> // STL vector class |
| 14 |
#include <complex> |
| 15 |
|
| 16 |
#include <cmath> // Yes, we do some math here |
| 17 |
|
| 18 |
#include <dune/common/alignedallocator.hh> |
| 19 |
#include <dune/common/classname.hh> |
| 20 |
#include <dune/common/debugalign.hh> |
| 21 |
#include <dune/common/fvector.hh> |
| 22 |
#include <dune/common/fmatrix.hh> |
| 23 |
#if HAVE_VC |
| 24 |
#include <dune/common/simd/vc.hh> |
| 25 |
#endif |
| 26 |
#include <dune/common/timer.hh> |
| 27 |
#include <dune/istl/istlexception.hh> |
| 28 |
#include <dune/istl/basearray.hh> |
| 29 |
#include <dune/istl/bvector.hh> |
| 30 |
#include <dune/istl/bcrsmatrix.hh> |
| 31 |
#include <dune/istl/operators.hh> |
| 32 |
#include <dune/istl/solvers.hh> |
| 33 |
#include <dune/istl/preconditioners.hh> |
| 34 |
#include <dune/istl/paamg/amg.hh> |
| 35 |
#include <dune/istl/paamg/pinfo.hh> |
| 36 |
|
| 37 |
#include <dune/istl/test/laplacian.hh> |
| 38 |
|
| 39 |
template<typename T> |
| 40 |
struct Random { |
| 41 |
static T gen() |
| 42 |
{ |
| 43 |
return T(std::rand())/RAND_MAX; |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
#if HAVE_VC |
| 48 |
template<typename T, typename A> |
| 49 |
struct Random<Vc::Vector<T,A>> { |
| 50 |
static Vc::Vector<T,A> gen() |
| 51 |
{ |
| 52 |
return Vc::Vector<T,A>::Random(); |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
template<typename T, std::size_t N, typename V, std::size_t M> |
| 57 |
struct Random<Vc::SimdArray<T,N,V,M>> { |
| 58 |
static Vc::SimdArray<T,N,V,M> gen() |
| 59 |
{ |
| 60 |
return Vc::SimdArray<T,N,V,M>::Random(); |
| 61 |
} |
| 62 |
}; |
| 63 |
#endif |
| 64 |
|
| 65 |
template <typename V> |
| 66 |
V detectVectorType(Dune::LinearOperator<V,V> &); |
| 67 |
|
| 68 |
template<typename Operator, typename Solver> |
| 69 |
std::vector<double> run_test (std::string precName, std::string solverName, Operator & op, Solver & solver, unsigned int N, unsigned int Runs) |
| 70 |
{ |
| 71 |
using Vector = decltype(detectVectorType(op)); |
| 72 |
using FT = typename Vector::field_type; |
| 73 |
|
| 74 |
Dune::Timer t(false); |
| 75 |
std::vector<double> timestamps; |
| 76 |
|
| 77 |
std::cout << "Trying " << solverName << "(" << precName << ")" |
| 78 |
<< " with " << Dune::className<FT>() << std::endl; |
| 79 |
for (unsigned int run = 0; run < Runs; run++) { |
| 80 |
// set up system |
| 81 |
Vector x(N),b(N); |
| 82 |
for (unsigned int i=0; i<N; i++) |
| 83 |
x[i] = Random<FT>::gen(); |
| 84 |
b=0; op.apply(x,b); // set right hand side accordingly |
| 85 |
x=1; // initial guess |
| 86 |
|
| 87 |
// call the solver |
| 88 |
Dune::InverseOperatorResult r; |
| 89 |
t.start(); |
| 90 |
solver.apply(x,b,r); |
| 91 |
t.stop(); |
| 92 |
double time = t.lastElapsed(); |
| 93 |
timestamps.push_back(time); |
| 94 |
} |
| 95 |
|
| 96 |
double measuredTime = 0.0; |
| 97 |
for(auto d : timestamps) |
| 98 |
measuredTime += d; |
| 99 |
|
| 100 |
std::cout << Runs << " run(s) took " << measuredTime << std::endl; |
| 101 |
|
| 102 |
return timestamps; |
| 103 |
} |
| 104 |
|
| 105 |
template<typename Operator, typename Prec> |
| 106 |
void test_all_solvers(std::string precName, Operator & op, Prec & prec, unsigned int N, unsigned int Runs) |
| 107 |
{ |
| 108 |
using Vector = decltype(detectVectorType(op)); |
| 109 |
|
| 110 |
double reduction = 1e-1; |
| 111 |
int verb = 1; |
| 112 |
Dune::LoopSolver<Vector> loop(op,prec,reduction,18000,verb); |
| 113 |
Dune::CGSolver<Vector> cg(op,prec,reduction,8000,verb); |
| 114 |
Dune::BiCGSTABSolver<Vector> bcgs(op,prec,reduction,8000,verb); |
| 115 |
Dune::GradientSolver<Vector> grad(op,prec,reduction,18000,verb); |
| 116 |
Dune::RestartedGMResSolver<Vector> gmres(op,prec,reduction,40,8000,verb); |
| 117 |
Dune::MINRESSolver<Vector> minres(op,prec,reduction,8000,verb); |
| 118 |
Dune::GeneralizedPCGSolver<Vector> gpcg(op,prec,reduction,8000,verb); |
| 119 |
Dune::RestartedFCGSolver<Vector> rfcg(op,prec,reduction,8000,verb); |
| 120 |
Dune::CompleteFCGSolver<Vector> cfcg(op,prec,reduction,8000,verb); |
| 121 |
|
| 122 |
// run_test(precName, "Loop", op,loop,N,Runs); |
| 123 |
run_test(precName, "CG", op,cg,N,Runs); |
| 124 |
run_test(precName, "BiCGStab", op,bcgs,N,Runs); |
| 125 |
run_test(precName, "Gradient", op,grad,N,Runs); |
| 126 |
run_test(precName, "RestartedGMRes", op,gmres,N,Runs); |
| 127 |
run_test(precName, "MINRes", op,minres,N,Runs); |
| 128 |
run_test(precName, "GeneralizedPCG", op,gpcg,N,Runs); |
| 129 |
run_test(precName, "RestartedFCG", op,rfcg,N,Runs); |
| 130 |
run_test(precName, "CompleteFCG", op,cfcg,N,Runs); |
| 131 |
} |
| 132 |
|
| 133 |
template<typename FT> |
| 134 |
void test_all(unsigned int Runs = 1) |
| 135 |
{ |
| 136 |
// define Types |
| 137 |
typedef typename Dune::Simd::Scalar<FT> MT; |
| 138 |
typedef Dune::FieldVector<FT,1> VB; |
| 139 |
typedef Dune::FieldMatrix<MT,1,1> MB; |
| 140 |
typedef Dune::AlignedAllocator<VB> AllocV; |
| 141 |
typedef Dune::BlockVector<VB,AllocV> Vector; |
| 142 |
typedef Dune::BCRSMatrix<MB> Matrix; |
| 143 |
|
| 144 |
// size |
| 145 |
unsigned int size = 100; |
| 146 |
unsigned int N = size*size; |
| 147 |
|
| 148 |
// make a compressed row matrix with five point stencil |
| 149 |
Matrix A; |
| 150 |
setupLaplacian(A,size); |
| 151 |
typedef Dune::MatrixAdapter<Matrix,Vector,Vector> Operator; |
| 152 |
Operator op(A); // make linear operator from A |
| 153 |
|
| 154 |
// create all preconditioners |
| 155 |
Dune::SeqJac<Matrix,Vector,Vector> jac(A,1,0.1); // Jacobi preconditioner |
| 156 |
Dune::SeqGS<Matrix,Vector,Vector> gs(A,1,0.1); // GS preconditioner |
| 157 |
Dune::SeqSOR<Matrix,Vector,Vector> sor(A,1,0.1); // SOR preconditioner |
| 158 |
Dune::SeqSSOR<Matrix,Vector,Vector> ssor(A,1,0.1); // SSOR preconditioner |
| 159 |
Dune::SeqILU<Matrix,Vector,Vector> ilu_0(A,0.1); // preconditioner object |
| 160 |
Dune::SeqILU<Matrix,Vector,Vector> ilu_1(A,1,0.1); // preconditioner object |
| 161 |
|
| 162 |
// AMG |
| 163 |
typedef Dune::Amg::RowSum Norm; |
| 164 |
typedef Dune::Amg::CoarsenCriterion<Dune::Amg::UnSymmetricCriterion<Matrix,Norm> > |
| 165 |
Criterion; |
| 166 |
typedef Dune::SeqSSOR<Matrix,Vector,Vector> Smoother; |
| 167 |
typedef typename Dune::Amg::SmootherTraits<Smoother>::Arguments SmootherArgs; |
| 168 |
SmootherArgs smootherArgs; |
| 169 |
smootherArgs.iterations = 1; |
| 170 |
smootherArgs.relaxationFactor = 1; |
| 171 |
unsigned int coarsenTarget = 1000; |
| 172 |
unsigned int maxLevel = 10; |
| 173 |
Criterion criterion(15,coarsenTarget); |
| 174 |
criterion.setDefaultValuesIsotropic(2); |
| 175 |
criterion.setAlpha(.67); |
| 176 |
criterion.setBeta(1.0e-4); |
| 177 |
criterion.setMaxLevel(maxLevel); |
| 178 |
criterion.setSkipIsolated(false); |
| 179 |
criterion.setNoPreSmoothSteps(1); |
| 180 |
criterion.setNoPostSmoothSteps(1); |
| 181 |
Dune::SeqScalarProduct<Vector> sp; |
| 182 |
typedef Dune::Amg::AMG<Operator,Vector,Smoother,Dune::Amg::SequentialInformation> AMG; |
| 183 |
Smoother smoother(A,1,1); |
| 184 |
AMG amg(op, criterion, smootherArgs); |
| 185 |
|
| 186 |
// run the sub-tests |
| 187 |
test_all_solvers("Jacobi", op,jac,N,Runs); |
| 188 |
test_all_solvers("GaussSeidel", op,gs,N,Runs); |
| 189 |
test_all_solvers("SOR", op,sor,N,Runs); |
| 190 |
test_all_solvers("SSOR", op,ssor,N,Runs); |
| 191 |
test_all_solvers("ILU(0)", op,ilu_0,N,Runs); |
| 192 |
test_all_solvers("ILU(1)", op,ilu_1,N,Runs); |
| 193 |
test_all_solvers("AMG", op,amg,N,Runs); |
| 194 |
} |
| 195 |
|
| 196 |
#endif // DUNE_ISTL_TEST_MULTIRHSTEST_HH |