55 #include <Teuchos_ConfigDefs.hpp> 56 #include <Teuchos_GlobalMPISession.hpp> 57 #include <Teuchos_StackedTimer.hpp> 58 #include <Teuchos_RCP.hpp> 59 #include <Teuchos_XMLParameterListHelpers.hpp> 60 #include <Teuchos_YamlParameterListHelpers.hpp> 61 #include <Teuchos_StandardCatchMacros.hpp> 62 #include "Teuchos_AbstractFactoryStd.hpp" 65 #include <Thyra_LinearOpWithSolveBase.hpp> 66 #include <Thyra_VectorBase.hpp> 67 #include <Thyra_SolveSupportTypes.hpp> 73 #include <Xpetra_Parameters.hpp> 74 #include <Xpetra_ThyraUtils.hpp> 77 #include <Galeri_XpetraParameters.hpp> 78 #include <Galeri_XpetraProblemFactory.hpp> 79 #include <Galeri_XpetraUtils.hpp> 80 #include <Galeri_XpetraMaps.hpp> 84 #include <Thyra_Ifpack2PreconditionerFactory.hpp> 88 main(
int argc,
char *argv[]) {
89 typedef double Scalar;
90 typedef Teuchos::ScalarTraits<Scalar> STS;
92 typedef map_type::local_ordinal_type LocalOrdinal;
93 typedef map_type::global_ordinal_type GlobalOrdinal;
94 typedef map_type::node_type Node;
97 using Teuchos::ParameterList;
98 using Teuchos::TimeMonitor;
99 #include <Xpetra_UseShortNames.hpp> 101 bool success =
false;
108 Teuchos::GlobalMPISession session (&argc, &argv, NULL);
109 Teuchos::CommandLineProcessor clp(
false);
110 const auto comm = Teuchos::DefaultComm<int>::getComm ();
116 Galeri::Xpetra::Parameters<GlobalOrdinal> galeriParameters(clp, 100, 100, 100,
"Laplace2D");
118 Xpetra::Parameters xpetraParameters(clp);
121 std::string xmlFileName =
"stratimikos_ParameterList.xml"; clp.setOption(
"xml", &xmlFileName,
"read parameters from an xml file");
122 std::string yamlFileName =
""; clp.setOption(
"yaml", &yamlFileName,
"read parameters from a yaml file");
123 bool printTimings =
false; clp.setOption(
"timings",
"notimings", &printTimings,
"print timings to screen");
124 bool use_stacked_timer =
false; clp.setOption(
"stacked-timer",
"no-stacked-timer", &use_stacked_timer,
"Run with or without stacked timer output");
125 std::string timingsFormat =
"table-fixed"; clp.setOption(
"time-format", &timingsFormat,
"timings format (table-fixed | table-scientific | yaml)");
126 int numVectors = 1; clp.setOption(
"multivector", &numVectors,
"number of rhs to solve simultaneously");
127 int numSolves = 1; clp.setOption(
"numSolves", &numSolves,
"number of times the system should be solved");
129 switch (clp.parse(argc,argv)) {
130 case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED:
return EXIT_SUCCESS;
131 case Teuchos::CommandLineProcessor::PARSE_ERROR:
132 case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION:
return EXIT_FAILURE;
133 case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL:
break;
136 RCP<Teuchos::FancyOStream> fancy = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
137 Teuchos::FancyOStream& out = *fancy;
138 out.setOutputToRootOnly(0);
141 Teuchos::RCP<Teuchos::StackedTimer> stacked_timer;
142 if (use_stacked_timer)
143 stacked_timer = rcp(
new Teuchos::StackedTimer(
"Main"));
144 TimeMonitor::setStackedTimer(stacked_timer);
147 TEUCHOS_TEST_FOR_EXCEPTION(xmlFileName ==
"" && yamlFileName ==
"", std::runtime_error,
148 "Need to provide xml or yaml input file");
149 RCP<ParameterList> paramList = rcp(
new ParameterList(
"params"));
150 if (yamlFileName !=
"")
151 Teuchos::updateParametersFromYamlFileAndBroadcast(yamlFileName, paramList.ptr(), *comm);
153 Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFileName, paramList.ptr(), *comm);
161 RCP<MultiVector> X, B;
163 std::ostringstream galeriStream;
164 Teuchos::ParameterList galeriList = galeriParameters.GetParameterList();
165 galeriStream <<
"========================================================\n" << xpetraParameters;
166 galeriStream << galeriParameters;
177 std::string matrixType = galeriParameters.GetMatrixType();
180 if (matrixType ==
"Laplace1D") {
181 map = Galeri::Xpetra::CreateMap<LO, GO, Node>(xpetraParameters.GetLib(),
"Cartesian1D", comm, galeriList);
183 }
else if (matrixType ==
"Laplace2D" || matrixType ==
"Star2D" ||
184 matrixType ==
"BigStar2D" || matrixType ==
"AnisotropicDiffusion" || matrixType ==
"Elasticity2D") {
185 map = Galeri::Xpetra::CreateMap<LO, GO, Node>(xpetraParameters.GetLib(),
"Cartesian2D", comm, galeriList);
187 }
else if (matrixType ==
"Laplace3D" || matrixType ==
"Brick3D" || matrixType ==
"Elasticity3D") {
188 map = Galeri::Xpetra::CreateMap<LO, GO, Node>(xpetraParameters.GetLib(),
"Cartesian3D", comm, galeriList);
192 if (matrixType ==
"Elasticity2D")
193 map = Xpetra::MapFactory<LO,GO,Node>::Build(map, 2);
194 if (matrixType ==
"Elasticity3D")
195 map = Xpetra::MapFactory<LO,GO,Node>::Build(map, 3);
197 galeriStream <<
"Processor subdomains in x direction: " << galeriList.get<
GO>(
"mx") << std::endl
198 <<
"Processor subdomains in y direction: " << galeriList.get<
GO>(
"my") << std::endl
199 <<
"Processor subdomains in z direction: " << galeriList.get<
GO>(
"mz") << std::endl
200 <<
"========================================================" << std::endl;
202 if (matrixType ==
"Elasticity2D" || matrixType ==
"Elasticity3D") {
204 galeriList.set(
"right boundary" ,
"Neumann");
205 galeriList.set(
"bottom boundary",
"Neumann");
206 galeriList.set(
"top boundary" ,
"Neumann");
207 galeriList.set(
"front boundary" ,
"Neumann");
208 galeriList.set(
"back boundary" ,
"Neumann");
211 RCP<Galeri::Xpetra::Problem<Map,CrsMatrixWrap,MultiVector> > Pr =
212 Galeri::Xpetra::BuildProblem<SC,LO,GO,Map,CrsMatrixWrap,MultiVector>(galeriParameters.GetMatrixType(), map, galeriList);
213 A = Pr->BuildMatrix();
215 if (matrixType ==
"Elasticity2D" ||
216 matrixType ==
"Elasticity3D") {
217 A->SetFixedBlockSize((galeriParameters.GetMatrixType() ==
"Elasticity2D") ? 2 : 3);
220 out << galeriStream.str();
221 X = MultiVectorFactory::Build(map, numVectors);
222 B = MultiVectorFactory::Build(map, numVectors);
230 RCP<const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> > xpCrsA = Teuchos::rcp_dynamic_cast<
const Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(A);
232 RCP<const Thyra::LinearOpBase<Scalar> > thyraA = Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyra(xpCrsA->getCrsMatrix());
233 RCP< Thyra::MultiVectorBase<Scalar> > thyraX = Teuchos::rcp_const_cast<Thyra::MultiVectorBase<Scalar> >(Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyraMultiVector(X));
234 RCP<const Thyra::MultiVectorBase<Scalar> > thyraB = Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyraMultiVector(B);
243 typedef Thyra::PreconditionerFactoryBase<Scalar> Base;
244 typedef Thyra::Ifpack2PreconditionerFactory<Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > Impl;
251 RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > solverFactory = Thyra::createLinearSolveStrategy(linearSolverBuilder);
252 auto precFactory = solverFactory->getPreconditionerFactory();
253 RCP<Thyra::PreconditionerBase<Scalar> > prec;
254 Teuchos::RCP<Thyra::LinearOpWithSolveBase<Scalar> > thyraInverseA;
255 if (!precFactory.is_null()) {
256 prec = precFactory->createPrec();
259 Thyra::initializePrec<double>(*precFactory, thyraA, prec.ptr());
260 thyraInverseA = solverFactory->createOp();
261 Thyra::initializePreconditionedOp<double>(*solverFactory, thyraA, prec, thyraInverseA.ptr());
263 thyraInverseA = Thyra::linearOpWithSolve(*solverFactory, thyraA);
267 Thyra::SolveStatus<Scalar> status = Thyra::solve<Scalar>(*thyraInverseA, Thyra::NOTRANS, *thyraB, thyraX.ptr());
269 success = (status.solveStatus == Thyra::SOLVE_STATUS_CONVERGED);
271 for (
int solveno = 1; solveno < numSolves; solveno++) {
272 if (!precFactory.is_null())
273 Thyra::initializePrec<double>(*precFactory, thyraA, prec.ptr());
276 status = Thyra::solve<Scalar>(*thyraInverseA, Thyra::NOTRANS, *thyraB, thyraX.ptr());
278 success = success && (status.solveStatus == Thyra::SOLVE_STATUS_CONVERGED);
283 if (use_stacked_timer) {
284 stacked_timer->stop(
"Main");
285 Teuchos::StackedTimer::OutputOptions options;
286 options.output_fraction = options.output_histogram = options.output_minmax =
true;
287 stacked_timer->report(out, comm, options);
289 RCP<ParameterList> reportParams = rcp(
new ParameterList);
290 if (timingsFormat ==
"yaml") {
291 reportParams->set(
"Report format",
"YAML");
292 reportParams->set(
"YAML style",
"compact");
294 reportParams->set(
"How to merge timer sets",
"Union");
295 reportParams->set(
"alwaysWriteLocal",
false);
296 reportParams->set(
"writeGlobalStats",
true);
297 reportParams->set(
"writeZeroTimers",
false);
299 const std::string filter =
"";
301 std::ios_base::fmtflags ff(out.flags());
302 if (timingsFormat ==
"table-fixed") out << std::fixed;
303 else out << std::scientific;
304 TimeMonitor::report(comm.ptr(), out, filter, reportParams);
305 out << std::setiosflags(ff);
309 TimeMonitor::clearCounters();
313 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
315 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
void setPreconditioningStrategyFactory(const RCP< const AbstractFactory< Thyra::PreconditionerFactoryBase< double > > > &precStrategyFactory, const std::string &precStrategyName, const bool makeDefault=false)
Set a new preconditioner strategy factory object.
Tpetra::Map< int, int > map_type
int main(int argc, char *argv[])
void setParameterList(RCP< ParameterList > const ¶mList)
Concrete subclass of Thyra::LinearSolverBuilderBase for creating LinearOpWithSolveFactoryBase objects...
map_type::global_ordinal_type GO