// @HEADER // ***************************************************************************** // Belos: Block Linear Solvers Package // // Copyright 2004-2016 NTESS and the Belos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER // // This driver reads a problem from a Harwell-Boeing (HB) file. // Multiple right-hand-sides are created randomly. // The initial guesses are all set to zero. // // This test is for testing the deflation in the pseudo-block Gmres solver. // One set of linear systems is solved and then augmented with additional // linear systems and resolved. The already solved linear systems should be // deflated immediately, leaving only the augmented systems to be solved. // // // Tpetra #include #include #include #include // Belos #include "BelosConfigDefs.hpp" #include "BelosLinearProblem.hpp" #include "BelosTpetraTestFramework.hpp" #include "BelosPseudoBlockGmresSolMgr.hpp" template int run(int argc, char *argv[]) { using ST = typename Tpetra::CrsMatrix::scalar_type; using LO = typename Tpetra::CrsMatrix<>::local_ordinal_type; using GO = typename Tpetra::CrsMatrix<>::global_ordinal_type; using NT = typename Tpetra::CrsMatrix<>::node_type; using OP = typename Tpetra::Operator; using MV = typename Tpetra::MultiVector; using MT = typename Teuchos::ScalarTraits::magnitudeType; using tmap_t = Tpetra::Map; using tcrsmatrix_t = Tpetra::CrsMatrix; using MVT = typename Belos::MultiVecTraits; using OPT = typename Belos::OperatorTraits; using Teuchos::RCP; using Teuchos::rcp; using Teuchos::ParameterList; Teuchos::GlobalMPISession session(&argc, &argv, nullptr); bool verbose = false; bool success = true; try { const auto comm = Tpetra::getDefaultComm(); const int myPID = comm->getRank(); bool procVerbose = false; int frequency = -1; // how often residuals are printed by solver int initNumRHS = 5; // how many right-hand sides get solved first int augNumRHS = 10; // how many right-hand sides are augmented to the first group int maxRestarts = 15; // number of restarts allowed int length = 100; int initBlockSize = 5;// blocksize used for the initial pseudo-block GMRES solve int augBlockSize = 3; // blocksize used for the augmented pseudo-block GMRES solve int maxIters = -1; // maximum iterations allowed std::string filename("orsirr1.hb"); MT tol = 1.0e-5; // relative residual tolerance MT aug_tol = 1.0e-5; // relative residual tolerance for augmented system Teuchos::CommandLineProcessor cmdp(false,true); cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix."); cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver."); cmdp.setOption("aug-tol",&aug_tol,"Relative residual tolerance used by GMRES solver for augmented systems."); cmdp.setOption("init-num-rhs",&initNumRHS,"Number of right-hand sides to be initially solved for."); cmdp.setOption("aug-num-rhs",&augNumRHS,"Number of right-hand sides augmenting the initial solve."); cmdp.setOption("max-restarts",&maxRestarts,"Maximum number of restarts allowed for GMRES solver."); cmdp.setOption("block-size",&initBlockSize,"Block size used by GMRES for the initial solve."); cmdp.setOption("aug-block-size",&augBlockSize,"Block size used by GMRES for the augmented solve."); cmdp.setOption("max-iters",&maxIters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size)."); cmdp.setOption("subspace-size",&length,"Dimension of Krylov subspace used by GMRES."); if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { return -1; } if (!verbose) frequency = -1; // reset frequency if test is not verbose procVerbose = verbose && (myPID==0); /* Only print on zero processor */ // Get the problem Belos::Tpetra::HarwellBoeingReader reader( comm ); RCP A = reader.readFromFile( filename ); RCP Map = A->getDomainMap(); // ********Other information used by block solver*********** // *****************(can be user specified)****************** const int numGlobalElements = Map->getGlobalNumElements(); if (maxIters == -1) maxIters = numGlobalElements - 1; // maximum number of iterations to run ParameterList belosList; belosList.set( "Num Blocks", length ); // Maximum number of blocks in Krylov factorization belosList.set( "Block Size", initBlockSize ); // Blocksize to be used by iterative solver belosList.set( "Maximum Iterations", maxIters ); // Maximum number of iterations allowed belosList.set( "Maximum Restarts", maxRestarts ); // Maximum number of restarts allowed belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested belosList.set( "Deflation Quorum", initBlockSize ); // Number of converged linear systems before deflation belosList.set( "Timer Label", "Belos Init" ); // Label used by the timers in this solver if (verbose) { belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + Belos::TimingDetails + Belos::StatusTestDetails ); if (frequency > 0) belosList.set( "Output Frequency", frequency ); } else belosList.set( "Verbosity", Belos::Errors + Belos::Warnings ); // *****Construct solution std::vector and random right-hand-sides ***** RCP initX = rcp( new MV(Map, initNumRHS) ); RCP initB = rcp( new MV(Map, initNumRHS) ); MVT::MvRandom( *initX ); OPT::Apply( *A, *initX, *initB ); initX->putScalar( 0.0 ); Belos::LinearProblem initProblem( A, initX, initB ); initProblem.setLabel("Belos Init"); bool set = initProblem.setProblem(); if (set == false) { if (procVerbose) std::cout << std::endl << "ERROR: Initial Belos::LinearProblem failed to set up correctly!" << std::endl; return -1; } // ******************************************************************* // *********************Perform initial solve************************* // ******************************************************************* RCP< Belos::SolverManager > initSolver = rcp( new Belos::PseudoBlockGmresSolMgr( rcp(&initProblem,false), rcp(&belosList,false) ) ); // Perform solve Belos::ReturnType ret = initSolver->solve(); // Compute actual residuals bool badRes = false; std::vector actualResids( initNumRHS ); std::vector rhsNorm( initNumRHS ); MV initR( Map, initNumRHS ); OPT::Apply( *A, *initX, initR ); MVT::MvAddMv( -1.0, initR, 1.0, *initB, initR ); MVT::MvNorm( initR, actualResids ); MVT::MvNorm( *initB, rhsNorm ); if (procVerbose) { std::cout<< "---------- Actual Residuals (normalized) ----------"< tol) badRes = true; } } if (ret != Belos::Converged || badRes==true) { if (procVerbose) std::cout << std::endl << "ERROR: Initial solve did not converge to solution!" << std::endl; return -1; } // ***************Construct augmented linear system**************** RCP augX = rcp( new MV(Map, initNumRHS+augNumRHS) ); RCP augB = rcp( new MV(Map, initNumRHS+augNumRHS) ); if (augNumRHS) { MVT::MvRandom( *augX ); OPT::Apply( *A, *augX, *augB ); augX->putScalar( 0.0 ); } // Copy previous linear system into RCP tmpX = rcp( new MV( *augX ) ); RCP tmpB = rcp( new MV( *augB ) ); tmpX->scale( 1.0, *augX ); tmpB->scale( 1.0, *augB ); Belos::LinearProblem augProblem( A, augX, augB ); augProblem.setLabel("Belos Aug"); set = augProblem.setProblem(); if (set == false) { if (procVerbose) std::cout << std::endl << "ERROR: Augmented Belos::LinearProblem failed to set up correctly!" << std::endl; return -1; } // ******************************************************************* // *******************Perform augmented solve************************* // ******************************************************************* belosList.set( "Block Size", augBlockSize ); // Blocksize to be used by iterative solver belosList.set( "Convergence Tolerance", aug_tol ); // Relative convergence tolerance requested belosList.set( "Deflation Quorum", augBlockSize ); // Number of converged linear systems before deflation belosList.set( "Timer Label", "Belos Aug" ); // Label used by the timers in this solver belosList.set( "Implicit Residual Scaling", "Norm of RHS" ); // Implicit residual scaling for convergence belosList.set( "Explicit Residual Scaling", "Norm of RHS" ); // Explicit residual scaling for convergence RCP< Belos::SolverManager > augSolver = rcp( new Belos::PseudoBlockGmresSolMgr( rcp(&augProblem,false), rcp(&belosList,false) ) ); // Perform solve ret = augSolver->solve(); if (ret != Belos::Converged) { if (procVerbose) std::cout << std::endl << "ERROR: Augmented solver did not converge to solution!" << std::endl; return -1; } // **********Print out information about problem******************* if (procVerbose) { std::cout << std::endl << std::endl; std::cout << "Dimension of matrix: " << numGlobalElements << std::endl; std::cout << "Number of initial right-hand sides: " << initNumRHS << std::endl; std::cout << "Number of augmented right-hand sides: " << augNumRHS << std::endl; std::cout << "Number of restarts allowed: " << maxRestarts << std::endl; std::cout << "Length of block Arnoldi factorization: " << length < tol ) badRes = true; } } if (ret!=Belos::Converged || badRes==true) { success = false; if (procVerbose) std::cout << "End Result: TEST FAILED" << std::endl; } else { success = true; if (procVerbose) std::cout << "End Result: TEST PASSED" << std::endl; } } TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success); return success ? EXIT_SUCCESS : EXIT_FAILURE; } // run int main(int argc, char *argv[]) { // run with different ST return run(argc,argv); // run(argc,argv); // FAILS }