Debian Codegraph @

packages/muelu/research/caglusa/main.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include <Teuchos_StandardCatchMacros.hpp>
11 #include <Teuchos_XMLParameterListHelpers.hpp>
12
13 #include <Xpetra_HierarchicalOperator_decl.hpp>
14 #include <Xpetra_HierarchicalOperator_def.hpp>
15
16 #include <Xpetra_IO.hpp>
17 #include <MueLu_IOhelpers.hpp>
18 #include <auxiliaryOperators.hpp>
19
20 #include <MueLu.hpp>
21 #include "MueLu_Exceptions.hpp"
22 #include <MueLu_CreateXpetraPreconditioner.hpp>
23
24 #ifdef HAVE_MUELU_BELOS
25 #include <BelosConfigDefs.hpp>
26 #include <BelosLinearProblem.hpp>
27 #include <BelosXpetraAdapter.hpp>
28 #include <BelosMueLuAdapter.hpp>
29 #endif
30
31 #define MUELU_HIERARCHICAL_DEBUG
32
33 using Teuchos::RCP;
34 using Teuchos::rcp;
35
36 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
37 int main_(Teuchos::CommandLineProcessor &clp, Xpetra::UnderlyingLib lib, int argc, char *argv[]) {
38 #include "MueLu_UseShortNames.hpp"
39
40 std::string xmlHierarchical = "1d-binary/hierarchical.xml";
41 clp.setOption("xmlHierarchical", &xmlHierarchical, "XML describing the hierarchical operator");
42 std::string xmlProblem = "1d-binary/problem.xml";
43 clp.setOption("xmlProblem", &xmlProblem, "XML describing the problem");
44 std::string xmlBelos = "belos.xml";
45 clp.setOption("xmlBelos", &xmlBelos, "XML with Belos parameters");
46 std::string xmlMueLu = "muelu.xml";
47 clp.setOption("xmlMueLu", &xmlMueLu, "XML with MueLu parameters");
48 std::string xmlAuxHierarchy = "auxiliary.xml";
49 clp.setOption("xmlAux", &xmlAuxHierarchy, "XML with MueLu parameters for the auxiliary hierarchy");
50 bool printTimings = true;
51 clp.setOption("timings", "notimings", &printTimings, "print timings to screen");
52 bool doTests = true;
53 clp.setOption("tests", "notests", &doTests, "Test operator using known LHS & RHS.");
54 bool doUnPrecSolve = true;
55 clp.setOption("unPrec", "noUnPrec", &doUnPrecSolve, "Solve unpreconditioned");
56 bool failOnUnPrecSolve = true;
57 clp.setOption("failOnUnPrec", "noFailOnUnPrec", &failOnUnPrecSolve, "Set error condition if unpreconditioned solve does not converge.");
58 bool doPrecSolve = true;
59 clp.setOption("prec", "noPrec", &doPrecSolve, "Solve preconditioned with AMG");
60 bool debugOutput = false;
61 clp.setOption("debug", "noDebug", &debugOutput, "Debug output");
62
63 switch (clp.parse(argc, argv)) {
64 case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS; break;
65 case Teuchos::CommandLineProcessor::PARSE_ERROR:
66 case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE; break;
67 case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break;
68 }
69
70 RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
71
72 Teuchos::RCP<Teuchos::StackedTimer> stacked_timer = rcp(new Teuchos::StackedTimer("Hierarchical Driver"));
73 Teuchos::RCP<Teuchos::FancyOStream> verbose_out = Teuchos::rcp(new Teuchos::FancyOStream(Teuchos::rcpFromRef(std::cout)));
74 verbose_out->setShowProcRank(true);
75 stacked_timer->setVerboseOstream(verbose_out);
76 Teuchos::TimeMonitor::setStackedTimer(stacked_timer);
77
78 using HOp = Xpetra::HierarchicalOperator<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
79 using op_type = Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
80 using blocked_matrix_type = typename HOp::blocked_matrix_type;
81 using blocked_map_type = typename blocked_matrix_type::blocked_map_type;
82 using matrix_type = typename HOp::matrix_type;
83 using map_type = typename HOp::map_type;
84 using mv_type = typename HOp::mv_type;
85 using lo_vec_type = typename blocked_map_type::lo_vec_type;
86 using coord_mv = Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node>;
87 using MagnitudeType = typename Teuchos::ScalarTraits<Scalar>::magnitudeType;
88 using IO = Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
89 using IOhelpers = MueLu::IOhelpers<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
90
91 RCP<Teuchos::FancyOStream> fancy = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
92 Teuchos::FancyOStream &out = *fancy;
93 out.setOutputToRootOnly(0);
94 bool success = true;
95 const Scalar one = Teuchos::ScalarTraits<Scalar>::one();
96 const Scalar zero = Teuchos::ScalarTraits<Scalar>::zero();
97 const MagnitudeType tol = 100000 * Teuchos::ScalarTraits<MagnitudeType>::eps();
98
99 RCP<op_type> op;
100 RCP<HOp> hop;
101 {
102 comm->barrier();
103 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string("Read hierarchical matrix")));
104
105 op = IOhelpers::Read(xmlHierarchical, comm);
106 hop = Teuchos::rcp_dynamic_cast<HOp>(op);
107
108 comm->barrier();
109 }
110
111 if (!hop.is_null()) {
112 if (debugOutput)
113 hop->setDebugOutput(debugOutput);
114 out << "Compression: " << hop->getCompression() << " of dense matrix." << std::endl;
115 }
116
117 Teuchos::ParameterList problemParams;
118 Teuchos::updateParametersFromXmlFileAndBroadcast(xmlProblem, Teuchos::Ptr<Teuchos::ParameterList>(&problemParams), *comm);
119
120 RCP<const map_type> map = op->getDomainMap();
121 RCP<matrix_type> auxOp;
122 RCP<matrix_type> mass;
123 RCP<mv_type> X_ex;
124 RCP<mv_type> RHS;
125 RCP<mv_type> X;
126 RCP<coord_mv> coords;
127 {
128 // Read in auxiliary stuff
129
130 // coordinates
131 coords = Xpetra::IO<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node>::ReadMultiVector(problemParams.get<std::string>("coordinates"), map);
132
133 // Auxiliary matrix used for multigrid construction
134 {
135 comm->barrier();
136 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string("Construct auxiliary operator")));
137
138 auxOp = MueLu::constructAuxiliaryOperator(op, problemParams);
139
140 comm->barrier();
141 }
142
143 // Mass matrix for L2 error computation
144 {
145 const bool readBinary = problemParams.get<bool>("read binary", false);
146 const bool readLocal = problemParams.get<bool>("read local", false);
147 // colmap of auxiliary operator
148 RCP<const map_type> aux_colmap = IO::ReadMap(problemParams.get<std::string>("aux colmap"), lib, comm, readBinary);
149
150 mass = IOhelpers::Read(problemParams.get<std::string>("mass matrix"), map, aux_colmap, map, map, true, readBinary, readLocal);
151 }
152
153 // known pair of LHS, RHS
154 X_ex = IO::ReadMultiVector(problemParams.get<std::string>("exact solution"), map);
155 RHS = IO::ReadMultiVector(problemParams.get<std::string>("right-hand side"), map);
156 // solution vector
157 X = MultiVectorFactory::Build(map, 1);
158 }
159
160 if (doTests) {
161 // Some simple apply tests
162 Scalar opX_exRHS;
163 Scalar MopX_exRHS;
164 Scalar MopTX_exRHS;
165 {
166 op->apply(*X_ex, *X);
167
168 // Xpetra::IO<Scalar,LocalOrdinal,GlobalOrdinal,Node>::Write("X.mtx", *X);
169 X->update(one, *RHS, -one);
170 opX_exRHS = X->getVector(0)->norm2();
171 out << "|op*X_ex - RHS| = " << opX_exRHS << std::endl;
172 // Xpetra::IO<Scalar,LocalOrdinal,GlobalOrdinal,Node>::Write("diff.mtx", *X);
173 }
174
175 {
176 op->apply(*X_ex, *X, Teuchos::NO_TRANS, -one);
177
178 // Xpetra::IO<Scalar,LocalOrdinal,GlobalOrdinal,Node>::Write("X2.mtx", *X);
179 X->update(one, *RHS, one);
180 MopX_exRHS = X->getVector(0)->norm2();
181 out << "|(-op)*X_ex + RHS| = " << MopX_exRHS << std::endl;
182 // Xpetra::IO<Scalar,LocalOrdinal,GlobalOrdinal,Node>::Write("diff2.mtx", *X);
183 }
184
185 {
186 op->apply(*X_ex, *X, Teuchos::TRANS, -one);
187
188 // Xpetra::IO<Scalar,LocalOrdinal,GlobalOrdinal,Node>::Write("X2.mtx", *X);
189 X->update(one, *RHS, one);
190 MopTX_exRHS = X->getVector(0)->norm2();
191 out << "|(-op^T)*X_ex + RHS| = " << MopTX_exRHS << std::endl;
192 // Xpetra::IO<Scalar,LocalOrdinal,GlobalOrdinal,Node>::Write("diff2.mtx", *X);
193 }
194
195 TEUCHOS_ASSERT(opX_exRHS < tol);
196 TEUCHOS_ASSERT(MopX_exRHS < tol);
197 TEUCHOS_ASSERT(MopTX_exRHS < tol);
198 }
199
200 #ifdef HAVE_MUELU_BELOS
201 Teuchos::ParameterList belosParams;
202 Teuchos::updateParametersFromXmlFileAndBroadcast(xmlBelos, Teuchos::Ptr<Teuchos::ParameterList>(&belosParams), *comm);
203 if (doUnPrecSolve) {
204 // Solve linear system using unpreconditioned Krylov method
205 out << "\n*********************************************************\n";
206 out << "Unpreconditioned Krylov method\n";
207 out << "*********************************************************\n\n";
208
209 Belos::ReturnType ret;
210 int numIts;
211 {
212 comm->barrier();
213 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string("Unpreconditioned solve")));
214
215 using MV = typename HOp::mv_type;
216 using OP = Belos::OperatorT<MV>;
217
218 X->putScalar(zero);
219 RCP<OP> belosOp = rcp(new Belos::XpetraOp<Scalar, LocalOrdinal, GlobalOrdinal, Node>(op));
220 RCP<Belos::LinearProblem<Scalar, MV, OP> > belosProblem = rcp(new Belos::LinearProblem<Scalar, MV, OP>(belosOp, X, RHS));
221
222 std::string belosType = "Pseudoblock CG";
223 auto belosSolverList = rcpFromRef(belosParams.sublist(belosType));
224
225 bool set = belosProblem->setProblem();
226 if (!set) {
227 throw MueLu::Exceptions::RuntimeError("ERROR: Belos::LinearProblem failed to set up correctly!");
228 }
229
230 // Create an iterative solver manager
231 Belos::SolverFactory<Scalar, MV, OP> solverFactory;
232 RCP<Belos::SolverManager<Scalar, MV, OP> > solver = solverFactory.create(belosType, belosSolverList);
233 solver->setProblem(belosProblem);
234
235 // Perform solve
236 ret = solver->solve();
237 numIts = solver->getNumIters();
238 comm->barrier();
239 }
240
241 // Get the number of iterations for this solve.
242 out << "Number of iterations performed for this solve: " << numIts << std::endl;
243
244 // Xpetra::IO<Scalar,LocalOrdinal,GlobalOrdinal,Node>::Write("X.mtx", *X);
245 X->update(one, *X_ex, -one);
246 out << "|X-X_ex| = " << X->getVector(0)->norm2() << std::endl
247 << std::endl;
248
249 if (failOnUnPrecSolve)
250 success &= (ret == Belos::Converged);
251 }
252 #endif // HAVE_MUELU_BELOS
253
254 if (doPrecSolve) {
255 // Solve linear system using a AMG preconditioned Krylov method
256
257 RCP<Hierarchy> auxH;
258 RCP<Hierarchy> H;
259
260 {
261 ////////////////////////////////////////////////////////////////
262 // Build the auxiliary hierarchy
263 out << "\n*********************************************************\n";
264 out << "Building the auxiliary hierarchy\n";
265 out << "*********************************************************\n\n";
266
267 comm->barrier();
268 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string("Construct auxiliary hierarchy")));
269
270 Teuchos::ParameterList auxParams;
271 Teuchos::updateParametersFromXmlFileAndBroadcast(xmlAuxHierarchy, Teuchos::Ptr<Teuchos::ParameterList>(&auxParams), *comm);
272 auxParams.set("hierarchy label", "Auxiliary");
273 auxParams.sublist("user data").set("Coordinates", coords);
274 // No rebalancing yet.
275 auxParams.set("coarse: max size", std::max(auxParams.get("coarse: max size", 2 * comm->getSize()),
276 2 * comm->getSize()));
277
278 auxH = MueLu::CreateXpetraPreconditioner(auxOp, auxParams);
279 comm->barrier();
280 }
281
282 {
283 ////////////////////////////////////////////////////////////////
284 // Construct the main hierarchy
285 out << "\n*********************************************************\n";
286 out << "Building the main hierarchy\n";
287 out << "*********************************************************\n\n";
288
289 comm->barrier();
290 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string("Construct hierarchy")));
291
292 Teuchos::ParameterList params;
293 Teuchos::updateParametersFromXmlFileAndBroadcast(xmlMueLu, Teuchos::Ptr<Teuchos::ParameterList>(&params), *comm);
294 params.sublist("user data").set("Coordinates", coords);
295
296 H = MueLu::constructHierarchyFromAuxiliary(op, auxH, params, out);
297 comm->barrier();
298 }
299
300 #ifdef HAVE_MUELU_BELOS
301 {
302 ////////////////////////////////////////////////////////////////
303 // Set up the Krylov solver
304
305 Belos::ReturnType ret;
306 int numIts;
307 {
308 comm->barrier();
309 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string("Preconditioned solve")));
310
311 using MV = typename HOp::mv_type;
312 using OP = Belos::OperatorT<MV>;
313
314 X->putScalar(zero);
315 RCP<OP> belosOp = rcp(new Belos::XpetraOp<Scalar, LocalOrdinal, GlobalOrdinal, Node>(op));
316 RCP<OP> belosPrec = rcp(new Belos::MueLuOp<SC, LO, GO, NO>(H));
317 RCP<Belos::LinearProblem<Scalar, MV, OP> > belosProblem = rcp(new Belos::LinearProblem<Scalar, MV, OP>(belosOp, X, RHS));
318
319 std::string belosType = "Pseudoblock CG";
320 auto belosSolverList = rcpFromRef(belosParams.sublist(belosType));
321
322 belosProblem->setRightPrec(belosPrec);
323
324 bool set = belosProblem->setProblem();
325 if (!set) {
326 throw MueLu::Exceptions::RuntimeError("ERROR: Belos::LinearProblem failed to set up correctly!");
327 }
328
329 // Create an iterative solver manager
330 Belos::SolverFactory<Scalar, MV, OP> solverFactory;
331 RCP<Belos::SolverManager<Scalar, MV, OP> > solver = solverFactory.create(belosType, belosSolverList);
332 solver->setProblem(belosProblem);
333
334 // Perform solve
335 ret = solver->solve();
336 numIts = solver->getNumIters();
337 comm->barrier();
338 }
339
340 // Get the number of iterations for this solve.
341 out << "Number of iterations performed for this solve: " << numIts << std::endl;
342
343 // Xpetra::IO<Scalar,LocalOrdinal,GlobalOrdinal,Node>::Write("X.mtx", *X);
344 X->update(one, *X_ex, -one);
345 out << "|X-X_ex|_2 = " << X->getVector(0)->norm2() << std::endl;
346
347 auto massDiff = MultiVectorFactory::Build(map, 1);
348 mass->apply(*X, *massDiff);
349 out << "|x-x_ex|_L2 = " << Teuchos::ScalarTraits<Scalar>::squareroot(X->getVector(0)->dot(*massDiff->getVector(0))) << std::endl;
350
351 success &= (ret == Belos::Converged);
352 }
353 #endif // HAVE_MUELU_BELOS
354 }
355
356 stacked_timer->stop("Hierarchical Driver");
357 Teuchos::StackedTimer::OutputOptions options;
358 options.output_fraction = options.output_histogram = options.output_minmax = true;
359 if (printTimings)
360 stacked_timer->report(out, comm, options);
361
362 return (success ? EXIT_SUCCESS : EXIT_FAILURE);
363 } // main
364
365 //- -- --------------------------------------------------------
366 #define MUELU_AUTOMATIC_TEST_ETI_NAME main_
367 #include "MueLu_Test_ETI.hpp"
368
369 int main(int argc, char *argv[]) {
370 return Automatic_Test_ETI(argc, argv);
371 }