// @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. // The right-hand-side corresponds to a randomly generated solution. // The initial guesses are all set to zero. // // NOTE: No preconditioner is used in this case. // #include "BelosConfigDefs.hpp" #include "BelosLinearProblem.hpp" #include "BelosTpetraAdapter.hpp" #include "BelosRCGSolMgr.hpp" #include #include #include #include #include // I/O for Harwell-Boeing files #define HIDE_TPETRA_INOUT_IMPLEMENTATIONS #include using namespace Teuchos; using Tpetra::Operator; using Tpetra::CrsMatrix; using Tpetra::MultiVector; using std::endl; using std::cout; using std::vector; using Teuchos::tuple; int main(int argc, char *argv[]) { typedef Tpetra::MultiVector<>::scalar_type ST; typedef ScalarTraits SCT; typedef SCT::magnitudeType MT; typedef Tpetra::Operator OP; typedef Tpetra::MultiVector MV; typedef Belos::OperatorTraits OPT; typedef Belos::MultiVecTraits MVT; GlobalMPISession mpisess(&argc,&argv,&cout); const ST one = SCT::one(); RCP > comm = Tpetra::getDefaultComm(); int MyPID = rank(*comm); // // Get test parameters from command-line processor // bool verbose = false, proc_verbose = false, debug = false; int frequency = -1; // how often residuals are printed by solver std::string filename("bcsstk14.hb"); int numBlocks = 30; // maximum number of blocks the solver can use for the Krylov subspace int recycleBlocks = 3; // maximum number of blocks the solver can use for the recycle space int numrhs = 1; // number of right-hand sides to solve for int maxiters = -1; // maximum number of iterations allowed per linear system MT tol = 1.0e-5; // relative residual tolerance CommandLineProcessor cmdp(false,true); cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); cmdp.setOption("debug","nodebug",&debug,"Run debugging checks."); cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); cmdp.setOption("tol",&tol,"Relative residual tolerance used by the RCG solver."); cmdp.setOption("max-subspace",&numBlocks,"Maximum number of vectors in search space (not including recycle space)."); cmdp.setOption("recycle",&recycleBlocks,"Number of vectors in recycle space."); cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for."); cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size)."); cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix."); if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) { return -1; } if (debug) { verbose = true; } if (!verbose) { frequency = -1; // reset frequency if test is not verbose } proc_verbose = ( verbose && (MyPID==0) ); if (proc_verbose) { std::cout << Belos::Belos_Version() << std::endl << std::endl; } RCP > A; Tpetra::Utils::readHBMatrix(filename,comm,A); RCP > map = A->getDomainMap(); // Create initial vectors RCP B, X; X = rcp( new MV(map,numrhs) ); MVT::MvRandom( *X ); B = rcp( new MV(map,numrhs) ); OPT::Apply( *A, *X, *B ); MVT::MvInit( *X, 0.0 ); // // ********Other information used by block solver*********** // *****************(can be user specified)****************** // int numIters1; const int NumGlobalElements = B->getGlobalLength(); if (maxiters == -1) maxiters = NumGlobalElements - 1; // maximum number of iterations to run // ParameterList belosList; belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed belosList.set( "Num Blocks", numBlocks); // Maximum number of blocks in Krylov space belosList.set( "Num Recycled Blocks", recycleBlocks ); // Number of vectors in recycle space belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested if (verbose) { belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails ); if (frequency > 0) belosList.set( "Output Frequency", frequency ); } else belosList.set( "Verbosity", Belos::Errors + Belos::Warnings ); // // Construct an unpreconditioned linear problem instance. // Belos::LinearProblem problem( A, X, B ); bool set = problem.setProblem(); if (set == false) { if (proc_verbose) std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; return -1; } // // ******************************************************************* // *********************Start the RCG iteration*********************** // ******************************************************************* // Belos::RCGSolMgr solver( rcpFromRef(problem), rcpFromRef(belosList) ); // // **********Print out information about problem******************* // if (proc_verbose) { std::cout << std::endl << std::endl; std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl; std::cout << "Number of right-hand sides: " << numrhs << std::endl; std::cout << "Max number of RCG iterations: " << maxiters << std::endl; std::cout << "Relative residual tolerance: " << tol << std::endl; std::cout << std::endl; } // // Perform solve // Belos::ReturnType ret = solver.solve(); numIters1=solver.getNumIters(); // // Compute actual residuals. // bool badRes = false; std::vector actual_resids( numrhs ); std::vector rhs_norm( numrhs ); MV resid(map, numrhs); OPT::Apply( *A, *X, resid ); MVT::MvAddMv( -one, resid, one, *B, resid ); MVT::MvNorm( resid, actual_resids ); MVT::MvNorm( *B, rhs_norm ); if (proc_verbose) { std::cout<< "---------- Actual Residuals (normalized) ----------"< tol) badRes = true; } if (proc_verbose) { std::cout << "Solve took " << numIters1 << " iterations." << std::endl; } if ( ret!=Belos::Converged || badRes ) { if (proc_verbose) { std::cout << "\nEnd Result: TEST FAILED" << std::endl; } return -1; } // // Default return value // if (proc_verbose) { std::cout << "\nEnd Result: TEST PASSED" << std::endl; } return 0; // } // end test_rcg_hb.cpp