Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_tpetra_belos.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stratimikos: Thyra-based strategies for linear solvers
4 //
5 // Copyright 2006 NTESS and the Stratimikos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 
15 #include "Tpetra_Core.hpp"
16 #include "Tpetra_CrsMatrix.hpp"
17 #include "MatrixMarket_Tpetra.hpp"
18 
21 #include "Thyra_LinearOpBase.hpp"
22 #include "Thyra_LinearOpWithSolveFactoryExamples.hpp"
23 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
24 
26 
27 int main(int argc, char* argv[])
28 {
29 
30  using Teuchos::rcp;
31  using Teuchos::RCP;
32  using Teuchos::OSTab;
34  using Teuchos::getParameter;
35  typedef double Scalar;
36  typedef Tpetra::Map<>::local_ordinal_type LO;
37  typedef Tpetra::Map<>::global_ordinal_type GO;
38  typedef Tpetra::Map<>::node_type NO;
39 
41 
42  bool success = true;
43  bool verbose = true;
44 
45 
47  out = Teuchos::VerboseObjectBase::getDefaultOStream();
48 
49  try {
50  Tpetra::ScopeGuard tpetraScope(&argc,&argv);
51  {
52 
53  //
54  // Read options from command-line
55  //
56 
57  std::string inputFile = "";
58  std::string extraParams = "";
59  bool printUnusedParams = false;
60  RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
61  int MyPID = rank(*comm);
62  verbose = ( out && (MyPID==0) );
63 
64  CommandLineProcessor clp(false); // Don't throw exceptions
65  clp.addOutputSetupOptions(true);
66  clp.setOption( "input-file", &inputFile, "Input file [Required].", true );
67  clp.setOption( "extra-params", &extraParams, "Extra parameters overriding the parameters read in from --input-file");
68  clp.setOption( "print-unused-params", "no-print-unused-params", &printUnusedParams, "Whether to print all unused parameters at the end.");
69  clp.setDocString(
70  "Testing program for Trilinos Tpetra linear solvers access through Thyra."
71  );
72 
73  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
74  if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
75 
76  RCP<ParameterList> paramList = rcp(new Teuchos::ParameterList());
77  if(verbose) *out << "\nReading parameters from XML file \""<<inputFile<<"\" ...\n";
78  Teuchos::updateParametersFromXmlFile(inputFile, Teuchos::inOutArg(*paramList));
79  if(extraParams.length()) {
80  if(verbose) *out << "\nAppending extra parameters from the XML string \""<<extraParams<<"\" ...\n";
81  Teuchos::updateParametersFromXmlString(extraParams, Teuchos::inOutArg(*paramList));
82  }
83 
84  if(verbose) {
85  *out << "\nEchoing input parameters ...\n";
86  paramList->print(*out,1,true,false);
87  }
88 
89  // Create list of valid parameter sublists
90  Teuchos::ParameterList validParamList("test_tpetra_stratimikos_solver");
91  validParamList.set("Matrix File","fileName");
92  validParamList.sublist("Linear Solver Builder").disableRecursiveValidation();
93 
94  if(verbose) *out << "\nValidating top-level input parameters ...\n";
95  paramList->validateParametersAndSetDefaults(validParamList);
96 
97  const std::string
98  &matrixFile = getParameter<std::string>(*paramList,"Matrix File");
99  RCP<ParameterList>
100  solverBuilderSL = sublist(paramList,"Linear Solver Builder",true);
101 
102  if(verbose) *out << "\nReading in an tpetra matrix A from the file \'"<<matrixFile<<"\' ...\n";
103 
104  RCP<const Tpetra::CrsMatrix<Scalar, LO, GO, NO>> tpetra_A = Tpetra::MatrixMarket::Reader<Tpetra::CrsMatrix<Scalar, LO, GO, NO>>::readSparseFile( matrixFile, comm );
105 
106  RCP<Tpetra::Vector<Scalar,LO,GO,NO>> tpetra_x0 = rcp(new Tpetra::Vector<Scalar,LO,GO,NO>(tpetra_A->getDomainMap()));
107  RCP<Tpetra::Vector<Scalar,LO,GO,NO>> tpetra_b = rcp(new Tpetra::Vector<Scalar,LO,GO,NO>(tpetra_A->getRangeMap()));
108  tpetra_x0->putScalar(1.0);
109  tpetra_A->apply(*tpetra_x0, *tpetra_b);
110  tpetra_x0->putScalar(0.0);
111 
112  RCP<const Thyra::LinearOpBase<double> >
113  A = Thyra::createConstLinearOp(Teuchos::rcp_dynamic_cast<const Tpetra::Operator<Scalar,LO,GO,NO>>(tpetra_A));
114 
115  RCP<Thyra::VectorBase<double> >
116  x0 = Thyra::createVector( tpetra_x0);
117  RCP<const Thyra::VectorBase<double> >
118  b = Thyra::createVector( tpetra_b);
119 
120  if(verbose) *out << "\nCreating a Stratimikos::DefaultLinearSolverBuilder object ...\n";
121 
122  // This is the Stratimikos main class (= factory of solver factory).
123  Stratimikos::LinearSolverBuilder<Scalar> linearSolverBuilder;
124 
125  // Register Belos+Tpetra as preconditioner:
126  Stratimikos::enableBelosPrecTpetra<Tpetra::CrsMatrix<Scalar,LO,GO,NO>>(linearSolverBuilder);
127 
128  linearSolverBuilder.setParameterList(solverBuilderSL);
129 
130  if(verbose) *out << "\nCreating the LinearOpWithSolveFactoryBase object lowsFactory ...\n";
131  RCP<Thyra::LinearOpWithSolveFactoryBase<double> >
132  lowsFactory = createLinearSolveStrategy(linearSolverBuilder);
133 
134  if(verbose) *out << "\nChecking the LOWSB interface ...\n";
135  RCP<Thyra::LinearOpWithSolveBase<Scalar> >
136  lowsA = Thyra::linearOpWithSolve<Scalar>(*lowsFactory, A);
137 
138  // Solve the linear system
139  Thyra::SolveStatus<double> status;
140  status = Thyra::solve<double>(*lowsA, Thyra::NOTRANS, *b, x0.ptr());
141 
142  success = (status.solveStatus == Thyra::SOLVE_STATUS_CONVERGED ? true : false);
143 
144  if(verbose && printUnusedParams) {
145  *out << "\nPrinting the parameter list (showing what was used) ...\n";
146  paramList->print(*out,1,true,true);
147  }
148 
149  } //End Tpetra scope
150  }
151  catch( const std::exception &excpt ) {
152  std::cerr << "*** Caught standard exception : " << excpt.what() << std::endl;
153  success = false;
154  }
155 
156  if (verbose) {
157  if(success) *out << "\nCongratulations! The test passed!\n";
158  else *out << "\nOh no! At least one of the solves failed!\n";
159  }
160 
161  return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
162 }
int main(int argc, char *argv[])
RCP< ParameterList > sublist(const RCP< ParameterList > &paramList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
basic_OSTab< char > OSTab
RCP< const LinearOpBase< Scalar > > createConstLinearOp(const RCP< const Tpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &tpetraOperator, const RCP< const VectorSpaceBase< Scalar > > rangeSpace=Teuchos::null, const RCP< const VectorSpaceBase< Scalar > > domainSpace=Teuchos::null)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
RCP< VectorBase< Scalar > > createVector(const RCP< Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &tpetraVector, const RCP< const VectorSpaceBase< Scalar > > space=Teuchos::null)
void setParameterList(RCP< ParameterList > const &paramList)
Concrete subclass of Thyra::LinearSolverBuilderBase for creating Thyra::LinearOpWithSolveFactoryBase ...
map_type::local_ordinal_type LO
map_type::global_ordinal_type GO