Debian Codegraph @

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