// @HEADER // ***************************************************************************** // Belos: Block Linear Solvers Package // // Copyright 2004-2016 NTESS and the Belos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /// \file test_solver_factory.cpp /// \brief Test Belos::SolverFactory with Epetra /// \author Mark Hoemmen /// #include "BelosConfigDefs.hpp" #include "BelosOutputManager.hpp" #include "BelosSolverFactory.hpp" #include "BelosEpetraAdapter.hpp" #include #include #include #include #include #include #ifdef EPETRA_MPI # include # include #else # include #endif #include #include #include #include #include // These are added to so we can preserve the type checking of the manager. // TODO: With the new DII system we can consider adding a second test like // this which does not have these included and does not do the type check. // However the test should still work. #include "BelosBlockGmresSolMgr.hpp" #include "BelosPseudoBlockGmresSolMgr.hpp" #include "BelosBlockCGSolMgr.hpp" #include "BelosPseudoBlockCGSolMgr.hpp" #include "BelosGCRODRSolMgr.hpp" #include "BelosMinresSolMgr.hpp" #include "BelosLSQRSolMgr.hpp" #include "BelosTFQMRSolMgr.hpp" #include "BelosRCGSolMgr.hpp" #include "BelosPseudoBlockTFQMRSolMgr.hpp" using std::cout; using std::endl; using std::vector; namespace { // Test creation of a Belos solver, using Belos::SolverFactory. // // Template parameters: // // SolverFactoryType: the Belos::SolverFactory specialization // SolverBaseType: the Belos::SolverManager specialization // SolverImplType: the Belos::SolverManager subclass which the // factory is supposed to produce. // // Function parameters: // // factory: A Belos::SolverFactory instance. Passing it in as a // reference lets us reuse the same factory instance for multiple // tests. This saves the (small) overhead of creating the factory // each time, and also (more importantly) ensures that the // factory's create() method gets tested for multiple calls to the // same factory instance. The reference is nonconst because the // create() method is nonconst. // // solverName: Name of the solver to create. // // out: An output stream reference to which to write verbose output // (verbose == true). When building with MPI, this should be a // black-hole stream (Teuchos::oblackholestream) reference on // every MPI process except the process with Rank 0. // // verbose: Whether to write verbose output. template void testCreatingSolver (SolverFactoryType& factory, const std::string& solverName, std::ostream& out, const bool verbose) { using Teuchos::ParameterList; using Teuchos::parameterList; using Teuchos::RCP; using Teuchos::rcp_dynamic_cast; using Teuchos::TypeNameTraits; using std::endl; if (verbose) { out << "Creating solver named: \"" << solverName << "\"" << endl; } RCP solverParams = parameterList (); RCP solver = factory.create (solverName, solverParams); TEUCHOS_TEST_FOR_EXCEPTION(solver.is_null(), std::logic_error, "Failed to create solver with valid name \"" << solverName << "\"."); if (verbose) { out << "Solver description: " << solver->description() << endl; } TEUCHOS_TEST_FOR_EXCEPTION(rcp_dynamic_cast (solver).is_null(), std::logic_error, "Solver does not have the correct type. Type should be " << TypeNameTraits::name() << "."); } int selectVerbosity (const bool verbose, const bool debug) { // NOTE Calling this a "MsgType" (its correct type) or even an // "enum MsgType" confuses the compiler. int theType = Belos::Errors; // default (always print errors) if (verbose) { // "Verbose" also means printing out Debug messages (as well // as everything else). theType = theType | Belos::Warnings | Belos::IterationDetails | Belos::OrthoDetails | Belos::FinalSummary | Belos::TimingDetails | Belos::StatusTestDetails | Belos::Debug; } if (debug) // "Debug" doesn't necessarily mean the same thing as // "Verbose". We interpret "Debug" to mean printing out // messages marked as Debug (as well as Error messages). theType = theType | Belos::Debug; return theType; } /// The problem is defined on a 2D grid; global size is nx * nx. Teuchos::RCP makeMatrix (const Teuchos::RCP& comm, Teuchos::RCP& domainMap, Teuchos::RCP& rangeMap, const int nx = 30) { using Teuchos::ParameterList; using Teuchos::RCP; using Teuchos::rcp; using Teuchos::rcp_implicit_cast; ParameterList GaleriList; GaleriList.set("n", nx * nx); GaleriList.set("nx", nx); GaleriList.set("ny", nx); RCP rowMap = rcp (Galeri::CreateMap("Linear", *comm, GaleriList)); // "&*rowMap" turns an RCP into a raw pointer // (Epetra_Map*), which is what Galeri::CreateCrsMatrix() wants. RCP A = rcp (Galeri::CreateCrsMatrix("Laplace2D", &*rowMap, GaleriList)); TEUCHOS_TEST_FOR_EXCEPTION(A.is_null(), std::runtime_error, "Galeri returned a null operator A."); domainMap = rowMap; rangeMap = rowMap; return rcp_implicit_cast (A); } } // namespace (anonymous) int main (int argc, char *argv[]) { using Belos::OutputManager; using Belos::SolverFactory; using Teuchos::CommandLineProcessor; using Teuchos::null; using Teuchos::ParameterList; using Teuchos::parameterList; using Teuchos::RCP; using Teuchos::rcp; using Teuchos::rcp_dynamic_cast; using Teuchos::rcp_implicit_cast; using std::cerr; using std::cout; using std::endl; typedef double scalar_type; typedef Epetra_MultiVector MV; typedef Epetra_Operator OP; typedef Belos::SolverManager solver_base_type; typedef Belos::SolverFactory factory_type; Teuchos::oblackholestream blackHole; Teuchos::GlobalMPISession mpiSession (&argc, &argv, &blackHole); RCP comm; { #ifdef EPETRA_MPI RCP commSpecific (new Epetra_MpiComm (MPI_COMM_WORLD)); #else RCP commSpecific (new Epetra_SerialComm); #endif // EPETRA_MPI comm = rcp_implicit_cast (commSpecific); } std::ostream& out = (comm->MyPID() == 0) ? std::cout : blackHole; bool success = false; bool verbose = false; try { int numRHS = 1; bool debug = false; // Define command-line arguments. CommandLineProcessor cmdp (false, true); cmdp.setOption ("numRHS", &numRHS, "Number of right-hand sides in the linear " "system to solve."); cmdp.setOption ("verbose", "quiet", &verbose, "Print messages and results."); cmdp.setOption ("debug", "nodebug", &debug, "Print debugging information."); // Parse the command-line arguments. { const CommandLineProcessor::EParseCommandLineReturn parseResult = cmdp.parse (argc,argv); if (parseResult == CommandLineProcessor::PARSE_HELP_PRINTED) { if (comm->MyPID() == 0) std::cout << "End Result: TEST PASSED" << endl; return EXIT_SUCCESS; } TEUCHOS_TEST_FOR_EXCEPTION(parseResult != CommandLineProcessor::PARSE_SUCCESSFUL, std::invalid_argument, "Failed to parse command-line arguments."); } // Declare an output manager for handling local output. Initialize, // using the caller's desired verbosity level. RCP > outMan = rcp (new OutputManager (selectVerbosity (verbose, debug))); // Stream for debug output. If debug output is not enabled, then // this stream doesn't print anything sent to it (it's a "black // hole" stream). //std::ostream& debugOut = outMan->stream (Belos::Debug); // Create the operator to test, with domain and range maps. RCP domainMap, rangeMap; RCP A = makeMatrix (comm, domainMap, rangeMap); // "Solution" input/output multivector. RCP X_exact = rcp (new MV (*domainMap, numRHS)); X_exact->Random (); RCP X = rcp (new MV (*domainMap, numRHS)); X->PutScalar (0.0); // "Right-hand side" input multivector. RCP B = rcp (new MV (*rangeMap, numRHS)); A->Apply (*X_exact, *B); // // Test creating solver instances using the solver factory. // factory_type factory; // // Test the canonical solver names. // { typedef Belos::BlockGmresSolMgr solver_impl_type; testCreatingSolver (factory, "Block GMRES", out, verbose); } { typedef Belos::PseudoBlockGmresSolMgr solver_impl_type; testCreatingSolver (factory, "Pseudoblock GMRES", out, verbose); } { typedef Belos::BlockCGSolMgr solver_impl_type; testCreatingSolver (factory, "Block CG", out, verbose); } { typedef Belos::PseudoBlockCGSolMgr solver_impl_type; testCreatingSolver (factory, "Pseudoblock CG", out, verbose); } { typedef Belos::GCRODRSolMgr solver_impl_type; testCreatingSolver (factory, "GCRODR", out, verbose); } { typedef Belos::RCGSolMgr solver_impl_type; testCreatingSolver (factory, "RCG", out, verbose); } { typedef Belos::MinresSolMgr solver_impl_type; testCreatingSolver (factory, "MINRES", out, verbose); } { typedef Belos::LSQRSolMgr solver_impl_type; testCreatingSolver (factory, "LSQR", out, verbose); } { typedef Belos::TFQMRSolMgr solver_impl_type; testCreatingSolver (factory, "TFQMR", out, verbose); } { typedef Belos::PseudoBlockTFQMRSolMgr solver_impl_type; testCreatingSolver (factory, "Pseudoblock TFQMR", out, verbose); } // // Test aliases. // { typedef Belos::BlockGmresSolMgr solver_impl_type; testCreatingSolver (factory, "Flexible GMRES", out, verbose); } { typedef Belos::PseudoBlockCGSolMgr solver_impl_type; testCreatingSolver (factory, "CG", out, verbose); } { typedef Belos::RCGSolMgr solver_impl_type; testCreatingSolver (factory, "Recycling CG", out, verbose); } { typedef Belos::GCRODRSolMgr solver_impl_type; testCreatingSolver (factory, "Recycling GMRES", out, verbose); } { typedef Belos::PseudoBlockGmresSolMgr solver_impl_type; testCreatingSolver (factory, "Pseudo Block GMRES", out, verbose); } { typedef Belos::PseudoBlockCGSolMgr solver_impl_type; testCreatingSolver (factory, "Pseudo Block CG", out, verbose); } { typedef Belos::PseudoBlockTFQMRSolMgr solver_impl_type; testCreatingSolver (factory, "Pseudo Block TFQMR", out, verbose); } success = true; if (comm->MyPID() == 0) { cout << "End Result: TEST PASSED" << endl; } } TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); }