Debian Codegraph @

packages/muelu/research/max/AdditiveMG/MultiplicativeStride.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 //
11 // To compile and run this example, Trilinos must be configured with
12 // Tpetra, Amesos2, MueLu, Ifpack2, and Belos.
13 //
14 // This example will only work with Tpetra, not Epetra.
15 //
16 // Commonly used options are
17 // --matrixType
18 // --nx
19 // --ny
20 // --xmlFile
21 //
22 // "./MueLu_Repartition_ADR.exe --help" shows all supported command line options.
23 //
24
25 #include <iostream>
26
27 // Belos provides Krylov solvers
28 #include <BelosConfigDefs.hpp>
29 #include <BelosLinearProblem.hpp>
30 #include <BelosBlockCGSolMgr.hpp>
31 #include <BelosPseudoBlockCGSolMgr.hpp>
32 #include <BelosBlockGmresSolMgr.hpp>
33 #include <BelosBiCGStabSolMgr.hpp>
34 #include <BelosTpetraAdapter.hpp>
35
36 // Galeri
37 #include <Galeri_XpetraParameters.hpp>
38 #include <Galeri_XpetraProblemFactory.hpp>
39 #include <Galeri_XpetraUtils.hpp>
40 #include <Galeri_XpetraMaps.hpp>
41
42 // ADR subdirectory
43 #include "CreateADRMatrix.hpp"
44 #include <CreateADRMatrix.hpp>
45 #include <ADRProblemFactory.hpp>
46 #include <ADR_XpetraParameters.hpp>
47
48 #include "Smooth_Prolongation.cpp"
49
50 int main(int argc, char *argv[]) {
51 // Define default types
52 typedef double scalar_type;
53 typedef int local_ordinal_type;
54 typedef int global_ordinal_type;
55 typedef Tpetra::KokkosClassic::DefaultNode::DefaultNodeType node_type;
56
57 // Convenient typedef's
58 typedef Tpetra::Operator<scalar_type, local_ordinal_type, global_ordinal_type, node_type> operator_type;
59 typedef Tpetra::CrsMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> crs_matrix_type;
60 typedef Tpetra::RowMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> row_matrix_type;
61 typedef Tpetra::Vector<scalar_type, local_ordinal_type, global_ordinal_type, node_type> vector_type;
62 typedef Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type> multivector_type;
63 typedef Tpetra::Map<local_ordinal_type, global_ordinal_type, node_type> driver_map_type;
64
65 typedef MueLu::TpetraOperator<scalar_type, local_ordinal_type, global_ordinal_type, node_type> muelu_tpetra_operator_type;
66 typedef MueLu::Utilities<scalar_type, local_ordinal_type, global_ordinal_type, node_type> MueLuUtilities;
67
68 typedef Belos::LinearProblem<scalar_type, multivector_type, operator_type> linear_problem_type;
69 typedef Belos::SolverManager<scalar_type, multivector_type, operator_type> belos_solver_manager_type;
70 typedef Belos::PseudoBlockCGSolMgr<scalar_type, multivector_type, operator_type> belos_pseudocg_manager_type;
71 typedef Belos::BlockGmresSolMgr<scalar_type, multivector_type, operator_type> belos_gmres_manager_type;
72 typedef Belos::BiCGStabSolMgr<scalar_type, multivector_type, operator_type> belos_bicgstab_manager_type;
73
74 typedef Ifpack2::Preconditioner<scalar_type, local_ordinal_type, global_ordinal_type, node_type> precond_type;
75
76 // MueLu_UseShortNames.hpp wants these typedefs.
77 typedef scalar_type Scalar;
78 typedef local_ordinal_type LocalOrdinal;
79 typedef global_ordinal_type GlobalOrdinal;
80 typedef node_type Node;
81 #include <MueLu_UseShortNames.hpp>
82
83 typedef Galeri::Xpetra::Problem<Map, CrsMatrixWrap, MultiVector> GaleriXpetraProblem;
84 typedef ADR::Xpetra::Problem<Map, CrsMatrixWrap, MultiVector> ADRXpetraProblem;
85
86 using Teuchos::RCP; // reference count pointers
87 using Teuchos::rcp; // reference count pointers
88
89 //
90 // MPI initialization using Teuchos
91 //
92
93 Teuchos::GlobalMPISession mpiSession(&argc, &argv, NULL);
94 RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
95 int mypid = comm->getRank();
96 /*
97 int subCommRank[3]={0,1,2};
98 Teuchos::ArrayView<int> arraySubCommRank(subCommRank, 3);
99 auto subComm = comm->createSubcommunicator(arraySubCommRank);
100 */
101 Teuchos::CommandLineProcessor clp(false);
102
103 global_ordinal_type maxIts = 10000;
104 scalar_type tol = 1e-10;
105 std::string solverOptionsFile = "final_parser.xml";
106 std::string krylovSolverType = "bicgstab";
107
108 clp.setOption("xmlFile", &solverOptionsFile, "XML file containing MueLu solver parameters");
109 clp.setOption("maxits", &maxIts, "maximum number of Krylov iterations");
110 clp.setOption("tol", &tol, "tolerance for Krylov solver");
111 clp.setOption("krylovType", &krylovSolverType, "cg or gmres solver");
112
113 switch (clp.parse(argc, argv)) {
114 case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS;
115 case Teuchos::CommandLineProcessor::PARSE_ERROR:
116 case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE;
117 case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break;
118 }
119
120 Teuchos::ParameterList xmlParams;
121 Teuchos::ParameterList mueluParams;
122 Teuchos::ParameterList problemParams;
123 Teuchos::updateParametersFromXmlFile(solverOptionsFile, Teuchos::inoutArg(xmlParams));
124 mueluParams = xmlParams.sublist(static_cast<const std::string>("MueLu"));
125 problemParams = xmlParams.sublist(static_cast<const std::string>("Problem"));
126
127 // Problem definition
128 std::string problem_type = problemParams.get<std::string>(static_cast<const std::string>("problem type"));
129
130 // Parameters
131
132 Scalar Lx = problemParams.get<scalar_type>(static_cast<const std::string>("domain size in x-direction"));
133 Scalar Ly = problemParams.get<scalar_type>(static_cast<const std::string>("domain size in y-direction"));
134 Scalar Lz = problemParams.get<scalar_type>(static_cast<const std::string>("domain size in z-direction"));
135
136 global_ordinal_type nx = problemParams.get<int>(static_cast<const std::string>("nodes in x-direction"));
137 global_ordinal_type ny = problemParams.get<int>(static_cast<const std::string>("nodes in y-direction"));
138 global_ordinal_type nz = problemParams.get<int>(static_cast<const std::string>("nodes in z-direction"));
139
140 global_ordinal_type number_runs = problemParams.get<int>(static_cast<const std::string>("number of runs"));
141
142 MueLu::DomainPartitioning domain;
143
144 int keep_boundary = 0;
145 Scalar stretchx = (Scalar)Lx / nx;
146 Scalar stretchy = (Scalar)Ly / ny;
147 Scalar stretchz = (Scalar)Lz / nz;
148
149 ADR::Xpetra::Parameters<GO> matrixParameters(clp, nx, ny, nz, problem_type, keep_boundary, stretchx, stretchy, stretchz); // manage parameters of the test case
150 Xpetra::Parameters xpetraParameters(clp); // manage parameters of xpetra
151
152 //
153 // Construct the problem
154 //
155
156 global_ordinal_type indexBase = 0;
157 RCP<const Map> xpetraMap;
158 std::vector<global_ordinal_type> ind;
159
160 // Creation of the map where processor 0 gets nothing at the fine level
161 if (comm->getSize() > 1) {
162 ind.reserve(static_cast<int>(matrixParameters.GetNumGlobalElements() / (comm->getSize() - 1) + 1));
163 if (mypid != 0 && mypid != comm->getSize() - 1)
164 for (int i = 0; i <= (static_cast<int>(matrixParameters.GetNumGlobalElements() / (comm->getSize() - 1))) - 1; ++i)
165 ind.emplace_back((mypid - 1) * static_cast<int>(matrixParameters.GetNumGlobalElements() / (comm->getSize() - 1)) + i);
166
167 if (mypid == comm->getSize() - 1)
168 for (int i = (mypid - 1) * static_cast<int>(matrixParameters.GetNumGlobalElements() / (comm->getSize() - 1)); i != matrixParameters.GetNumGlobalElements(); ++i)
169 ind.emplace_back(i);
170
171 ind.shrink_to_fit();
172
173 Teuchos::ArrayView<const global_ordinal_type> elementList(ind);
174 xpetraMap = MapFactory::Build(Xpetra::UseTpetra, matrixParameters.GetNumGlobalElements(), elementList, indexBase, comm);
175 } else if (comm->getSize() == 1)
176 xpetraMap = MapFactory::Build(Xpetra::UseTpetra, matrixParameters.GetNumGlobalElements(), indexBase, comm);
177
178 RCP<MultiVector> coordinates;
179
180 if (problem_type == "ADR1D")
181 coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<scalar_type, local_ordinal_type, global_ordinal_type, Map, MultiVector>("1D", xpetraMap, matrixParameters.GetParameterList());
182 else if (problem_type == "ADR2D")
183 coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<scalar_type, local_ordinal_type, global_ordinal_type, Map, MultiVector>("2D", xpetraMap, matrixParameters.GetParameterList());
184 else if (problem_type == "ADR3D")
185 coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<scalar_type, local_ordinal_type, global_ordinal_type, Map, MultiVector>("3D", xpetraMap, matrixParameters.GetParameterList());
186
187 RCP<ADRXpetraProblem> Pr = ADR::Xpetra::BuildProblem<scalar_type, local_ordinal_type, global_ordinal_type, Map, CrsMatrixWrap, MultiVector>(matrixParameters.GetMatrixType(), xpetraMap, matrixParameters.GetParameterList());
188 RCP<Matrix> xpetraA = Pr->BuildMatrix();
189
190 RCP<crs_matrix_type> A = MueLutoTpetra(xpetraA);
191 RCP<const driver_map_type> map = toTpetra(xpetraMap);
192
193 // Construct a multigrid preconditioner
194 RCP<muelu_tpetra_operator_type> M = MueLu::CreateTpetraPreconditioner((RCP<operator_type>)A, mueluParams, toTpetra(coordinates));
195
196 RCP<multivector_type> X = rcp(new multivector_type(map, 1));
197 RCP<multivector_type> B = rcp(new multivector_type(map, 1));
198
199 for (int iter = 1; iter <= number_runs; ++iter) {
200 X->putScalar((scalar_type)0.0);
201 B->randomize();
202
203 //
204 // Set up Krylov solver and iterate.
205 //
206
207 RCP<multivector_type> X_muelu = rcp(new multivector_type(map, 1));
208 RCP<linear_problem_type> Problem_muelu = rcp(new linear_problem_type(A, X_muelu, B));
209 Problem_muelu->setRightPrec(M);
210 Problem_muelu->setProblem();
211
212 RCP<Teuchos::ParameterList> belosList = rcp(new Teuchos::ParameterList());
213 belosList->set("Maximum Iterations", maxIts); // Maximum number of iterations allowed
214 belosList->set("Convergence Tolerance", tol); // Relative convergence tolerance requested
215 // belosList->set("Verbosity", Belos::Errors + Belos::Warnings + Belos::StatusTestDetails);
216 belosList->set("Verbosity", Belos::Errors);
217 belosList->set("Output Frequency", 1);
218 belosList->set("Output Style", Belos::Brief);
219 belosList->set("Implicit Residual Scaling", "None");
220 RCP<belos_solver_manager_type> solver;
221 if (krylovSolverType == "cg")
222 solver = rcp(new belos_pseudocg_manager_type(Problem_muelu, belosList));
223 else if (krylovSolverType == "gmres")
224 solver = rcp(new belos_gmres_manager_type(Problem_muelu, belosList));
225 else if (krylovSolverType == "bicgstab")
226 solver = rcp(new belos_bicgstab_manager_type(Problem_muelu, belosList));
227 else
228 throw std::invalid_argument("bad Krylov solver type");
229
230 solver->solve();
231 int numIterations_muelu = solver->getNumIters();
232
233 Teuchos::Array<typename Teuchos::ScalarTraits<scalar_type>::magnitudeType> normVec_muelu(1);
234 multivector_type residual_muelu(B->getMap(), 1);
235 A->apply(*X_muelu, residual_muelu);
236 residual_muelu.update(1.0, *B, -1.0);
237 residual_muelu.norm2(normVec_muelu);
238 if (mypid == 0) {
239 std::cout << "number of iterations with MueLu preconditioner= " << numIterations_muelu << std::endl;
240 std::cout << "||Residual|| = " << normVec_muelu[0] << std::endl;
241 }
242 }
243 #include <Teuchos_TimeMonitor.hpp>
244 Teuchos::TimeMonitor::summarize();
245
246 return EXIT_SUCCESS;
247 }