// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: #ifndef DUNE_ISTL_TEST_MULTIRHSTEST_HH #define DUNE_ISTL_TEST_MULTIRHSTEST_HH #define DISABLE_AMG_DIRECTSOLVER 1 #include // for input/output to shell #include // for input/output to files #include // STL vector class #include #include // Yes, we do some math here #include #include #include #include #include #if HAVE_VC #include #endif #include #include #include #include #include #include #include #include #include #include #include template struct Random { static T gen() { return T(std::rand())/RAND_MAX; } }; #if HAVE_VC template struct Random> { static Vc::Vector gen() { return Vc::Vector::Random(); } }; template struct Random> { static Vc::SimdArray gen() { return Vc::SimdArray::Random(); } }; #endif template V detectVectorType(Dune::LinearOperator &); template std::vector run_test (std::string precName, std::string solverName, Operator & op, Solver & solver, unsigned int N, unsigned int Runs) { using Vector = decltype(detectVectorType(op)); using FT = typename Vector::field_type; Dune::Timer t(false); std::vector timestamps; std::cout << "Trying " << solverName << "(" << precName << ")" << " with " << Dune::className() << std::endl; for (unsigned int run = 0; run < Runs; run++) { // set up system Vector x(N),b(N); for (unsigned int i=0; i::gen(); b=0; op.apply(x,b); // set right hand side accordingly x=1; // initial guess // call the solver Dune::InverseOperatorResult r; t.start(); solver.apply(x,b,r); t.stop(); double time = t.lastElapsed(); timestamps.push_back(time); } double measuredTime = 0.0; for(auto d : timestamps) measuredTime += d; std::cout << Runs << " run(s) took " << measuredTime << std::endl; return timestamps; } template void test_all_solvers(std::string precName, Operator & op, Prec & prec, unsigned int N, unsigned int Runs) { using Vector = decltype(detectVectorType(op)); double reduction = 1e-1; int verb = 1; Dune::LoopSolver loop(op,prec,reduction,18000,verb); Dune::CGSolver cg(op,prec,reduction,8000,verb); Dune::BiCGSTABSolver bcgs(op,prec,reduction,8000,verb); Dune::GradientSolver grad(op,prec,reduction,18000,verb); Dune::RestartedGMResSolver gmres(op,prec,reduction,40,8000,verb); Dune::MINRESSolver minres(op,prec,reduction,8000,verb); Dune::GeneralizedPCGSolver gpcg(op,prec,reduction,8000,verb); Dune::RestartedFCGSolver rfcg(op,prec,reduction,8000,verb); Dune::CompleteFCGSolver cfcg(op,prec,reduction,8000,verb); // run_test(precName, "Loop", op,loop,N,Runs); run_test(precName, "CG", op,cg,N,Runs); run_test(precName, "BiCGStab", op,bcgs,N,Runs); run_test(precName, "Gradient", op,grad,N,Runs); run_test(precName, "RestartedGMRes", op,gmres,N,Runs); run_test(precName, "MINRes", op,minres,N,Runs); run_test(precName, "GeneralizedPCG", op,gpcg,N,Runs); run_test(precName, "RestartedFCG", op,rfcg,N,Runs); run_test(precName, "CompleteFCG", op,cfcg,N,Runs); } template void test_all(unsigned int Runs = 1) { // define Types typedef typename Dune::Simd::Scalar MT; typedef Dune::FieldVector VB; typedef Dune::FieldMatrix MB; typedef Dune::AlignedAllocator AllocV; typedef Dune::BlockVector Vector; typedef Dune::BCRSMatrix Matrix; // size unsigned int size = 100; unsigned int N = size*size; // make a compressed row matrix with five point stencil Matrix A; setupLaplacian(A,size); typedef Dune::MatrixAdapter Operator; Operator op(A); // make linear operator from A // create all preconditioners Dune::SeqJac jac(A,1,0.1); // Jacobi preconditioner Dune::SeqGS gs(A,1,0.1); // GS preconditioner Dune::SeqSOR sor(A,1,0.1); // SOR preconditioner Dune::SeqSSOR ssor(A,1,0.1); // SSOR preconditioner Dune::SeqILU ilu_0(A,0.1); // preconditioner object Dune::SeqILU ilu_1(A,1,0.1); // preconditioner object // AMG typedef Dune::Amg::RowSum Norm; typedef Dune::Amg::CoarsenCriterion > Criterion; typedef Dune::SeqSSOR Smoother; typedef typename Dune::Amg::SmootherTraits::Arguments SmootherArgs; SmootherArgs smootherArgs; smootherArgs.iterations = 1; smootherArgs.relaxationFactor = 1; unsigned int coarsenTarget = 1000; unsigned int maxLevel = 10; Criterion criterion(15,coarsenTarget); criterion.setDefaultValuesIsotropic(2); criterion.setAlpha(.67); criterion.setBeta(1.0e-4); criterion.setMaxLevel(maxLevel); criterion.setSkipIsolated(false); criterion.setNoPreSmoothSteps(1); criterion.setNoPostSmoothSteps(1); Dune::SeqScalarProduct sp; typedef Dune::Amg::AMG AMG; Smoother smoother(A,1,1); AMG amg(op, criterion, smootherArgs); // run the sub-tests test_all_solvers("Jacobi", op,jac,N,Runs); test_all_solvers("GaussSeidel", op,gs,N,Runs); test_all_solvers("SOR", op,sor,N,Runs); test_all_solvers("SSOR", op,ssor,N,Runs); test_all_solvers("ILU(0)", op,ilu_0,N,Runs); test_all_solvers("ILU(1)", op,ilu_1,N,Runs); test_all_solvers("AMG", op,amg,N,Runs); } #endif // DUNE_ISTL_TEST_MULTIRHSTEST_HH