Debian Codegraph @

packages/muelu/test/viz/AggExport.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 <cstdio>
11 #include <unistd.h>
12 #include <iostream>
13
14 #include <Teuchos_XMLParameterListHelpers.hpp>
15 #include <Teuchos_StandardCatchMacros.hpp>
16
17 #include <Xpetra_MultiVectorFactory.hpp>
18 #include <Xpetra_ImportFactory.hpp>
19 #include <Xpetra_IO.hpp>
20
21 // Galeri
22 #include <Galeri_XpetraParameters.hpp>
23 #include <Galeri_XpetraProblemFactory.hpp>
24 #include <Galeri_XpetraUtils.hpp>
25 #include <Galeri_XpetraMaps.hpp>
26 //
27
28 #include <MueLu.hpp>
29 #include <MueLu_Level.hpp>
30 #include <MueLu_BaseClass.hpp>
31 #include <MueLu_ParameterListInterpreter.hpp> // TODO: move into MueLu.hpp
32
33 #include <MueLu_Utilities.hpp>
34
35 #include <MueLu_MutuallyExclusiveTime.hpp>
36
37 bool compare_to_gold_all_ranks(int myRank, const std::string& baseFile) {
38 bool failed = false;
39
40 // Create a copy of outputs
41 std::string cmd = "cp -f ";
42 int retVal;
43 retVal = system((cmd + baseFile + ".gold " + baseFile + ".gold_filtered").c_str());
44 TEUCHOS_TEST_FOR_EXCEPTION(retVal != 0, std::runtime_error, "goldfile copy failed!");
45 // system((cmd + baseFile + ".out " + baseFile + ".out_filtered").c_str());
46 retVal = system((cmd + baseFile + ".vtu " + baseFile + ".out_filtered").c_str());
47 TEUCHOS_TEST_FOR_EXCEPTION(retVal != 0, std::runtime_error, "goldfile copy failed!");
48
49 // Run comparison (ignoring whitespaces)
50 cmd = "diff -u -w -I\"^\\s*$\" " + baseFile + ".gold_filtered " + baseFile + ".out_filtered";
51 int ret = system(cmd.c_str());
52 std::cout << ret << std::endl;
53 if (ret)
54 failed = true;
55
56 std::cout << baseFile << ": " << (ret ? "failed" : "passed") << std::endl;
57
58 return !failed;
59 }
60
61 std::string replaceAll(std::string result, const std::string& replaceWhat, const std::string& replaceWithWhat) {
62 while (1) {
63 const int pos = result.find(replaceWhat);
64 if (pos == -1)
65 break;
66 result.replace(pos, replaceWhat.size(), replaceWithWhat);
67 }
68 return result;
69 }
70
71 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
72 int main_(Teuchos::CommandLineProcessor& clp, Xpetra::UnderlyingLib& lib, int argc, char* argv[]) {
73 #include <MueLu_UseShortNames.hpp>
74
75 using Teuchos::ArrayRCP;
76 using Teuchos::ParameterList;
77 using Teuchos::RCP;
78 using Teuchos::rcp;
79 using Teuchos::TimeMonitor;
80
81 // =========================================================================
82 // MPI initialization using Teuchos
83 // =========================================================================
84 bool success = false;
85 bool aggMatch = false;
86 bool verbose = true;
87 try {
88 RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
89
90 // =========================================================================
91 // Convenient definitions
92 // =========================================================================
93 typedef Teuchos::ScalarTraits<SC> STS;
94 SC one = STS::one();
95
96 // =========================================================================
97 // Parameters initialization
98 // =========================================================================
99 GO nx = 100, ny = 100, nz = 100;
100 Galeri::Xpetra::Parameters<GO> galeriParameters(clp, nx, ny, nz, "Laplace2D"); // manage parameters of the test case
101 Xpetra::Parameters xpetraParameters(clp); // manage parameters of Xpetra
102
103 std::string xmlFileName = "viztest.xml";
104 clp.setOption("xml", &xmlFileName, "read parameters from a file");
105
106 std::string mapFile;
107 clp.setOption("map", &mapFile, "map data file");
108 std::string colMapFile;
109 clp.setOption("colmap", &colMapFile, "colmap data file");
110 std::string domainMapFile;
111 clp.setOption("domainmap", &domainMapFile, "domainmap data file");
112 std::string rangeMapFile;
113 clp.setOption("rangemap", &rangeMapFile, "rangemap data file");
114 std::string matrixFile;
115 clp.setOption("matrix", &matrixFile, "matrix data file");
116 std::string coordFile;
117 clp.setOption("coords", &coordFile, "coordinates data file");
118 std::string nullFile;
119 clp.setOption("nullspace", &nullFile, "nullspace data file");
120
121 switch (clp.parse(argc, argv)) {
122 case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS;
123 case Teuchos::CommandLineProcessor::PARSE_ERROR:
124 case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE;
125 case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break;
126 }
127
128 ParameterList paramList;
129 Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFileName, Teuchos::Ptr<ParameterList>(&paramList), *comm);
130
131 // Retrieve matrix parameters (they may have been changed on the command line)
132 // [for instance, if we changed matrix type from 2D to 3D we need to update nz]
133 ParameterList galeriList = galeriParameters.GetParameterList();
134
135 // =========================================================================
136 // Problem construction
137 // =========================================================================
138 std::ostringstream galeriStream;
139 comm->barrier();
140 RCP<TimeMonitor> globalTimeMonitor = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("ScalingTest: S - Global Time")));
141 RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("ScalingTest: 1 - Matrix Build")));
142
143 RCP<Matrix> A;
144 RCP<const Map> map;
145 RCP<MultiVector> coordinates;
146 RCP<MultiVector> nullspace;
147 int ndims = 2;
148 if (matrixFile.empty()) {
149 galeriStream << "========================================================\n"
150 << xpetraParameters << galeriParameters;
151
152 // Galeri will attempt to create a square-as-possible distribution of subdomains di, e.g.,
153 // d1 d2 d3
154 // d4 d5 d6
155 // d7 d8 d9
156 // d10 d11 d12
157 // A perfect distribution is only possible when the #processors is a perfect square.
158 // This *will* result in "strip" distribution if the #processors is a prime number or if the factors are very different in
159 // size. For example, np=14 will give a 7-by-2 distribution.
160 // If you don't want Galeri to do this, specify mx or my on the galeriList.
161 std::string matrixType = galeriParameters.GetMatrixType();
162
163 // Create map and coordinates
164 // In the future, we hope to be able to first create a Galeri problem, and then request map and coordinates from it
165 // At the moment, however, things are fragile as we hope that the Problem uses same map and coordinates inside
166 if (matrixType == "Laplace1D") {
167 map = Galeri::Xpetra::CreateMap<LO, GO, Node>(xpetraParameters.GetLib(), "Cartesian1D", comm, galeriList);
168 coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<SC, LO, GO, Map, MultiVector>("1D", map, galeriList);
169
170 } else if (matrixType == "Laplace2D" || matrixType == "Star2D" ||
171 matrixType == "BigStar2D" || matrixType == "Elasticity2D") {
172 map = Galeri::Xpetra::CreateMap<LO, GO, Node>(xpetraParameters.GetLib(), "Cartesian2D", comm, galeriList);
173 coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<SC, LO, GO, Map, MultiVector>("2D", map, galeriList);
174
175 } else if (matrixType == "Laplace3D" || matrixType == "Brick3D" || matrixType == "Elasticity3D") {
176 map = Galeri::Xpetra::CreateMap<LO, GO, Node>(xpetraParameters.GetLib(), "Cartesian3D", comm, galeriList);
177 coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<SC, LO, GO, Map, MultiVector>("3D", map, galeriList);
178 ndims = 3;
179 }
180
181 // Expand map to do multiple DOF per node for block problems
182 if (matrixType == "Elasticity2D")
183 map = Xpetra::MapFactory<LO, GO, Node>::Build(map, 2);
184 if (matrixType == "Elasticity3D")
185 map = Xpetra::MapFactory<LO, GO, Node>::Build(map, 3);
186
187 galeriStream << "Processor subdomains in x direction: " << galeriList.get<GO>("mx") << std::endl
188 << "Processor subdomains in y direction: " << galeriList.get<GO>("my") << std::endl
189 << "Processor subdomains in z direction: " << galeriList.get<GO>("mz") << std::endl
190 << "========================================================" << std::endl;
191
192 if (matrixType == "Elasticity2D" || matrixType == "Elasticity3D") {
193 // Our default test case for elasticity: all boundaries of a square/cube have Neumann b.c. except left which has Dirichlet
194 galeriList.set("right boundary", "Neumann");
195 galeriList.set("bottom boundary", "Neumann");
196 galeriList.set("top boundary", "Neumann");
197 galeriList.set("front boundary", "Neumann");
198 galeriList.set("back boundary", "Neumann");
199 }
200
201 RCP<Galeri::Xpetra::Problem<Map, CrsMatrixWrap, MultiVector> > Pr =
202 Galeri::Xpetra::BuildProblem<SC, LO, GO, Map, CrsMatrixWrap, MultiVector>(galeriParameters.GetMatrixType(), map, galeriList);
203 A = Pr->BuildMatrix();
204
205 if (matrixType == "Elasticity2D" ||
206 matrixType == "Elasticity3D") {
207 nullspace = Pr->BuildNullspace();
208 A->SetFixedBlockSize((galeriParameters.GetMatrixType() == "Elasticity2D") ? 2 : 3);
209 }
210
211 } else {
212 if (!mapFile.empty())
213 map = Xpetra::IO<SC, LO, GO, Node>::ReadMap(mapFile, lib, comm);
214 comm->barrier();
215
216 const bool binaryFormat = false;
217
218 if (!binaryFormat && !map.is_null()) {
219 RCP<const Map> colMap = (!colMapFile.empty() ? Xpetra::IO<SC, LO, GO, Node>::ReadMap(colMapFile, lib, comm) : Teuchos::null);
220 RCP<const Map> domainMap = (!domainMapFile.empty() ? Xpetra::IO<SC, LO, GO, Node>::ReadMap(domainMapFile, lib, comm) : Teuchos::null);
221 RCP<const Map> rangeMap = (!rangeMapFile.empty() ? Xpetra::IO<SC, LO, GO, Node>::ReadMap(rangeMapFile, lib, comm) : Teuchos::null);
222 A = Xpetra::IO<SC, LO, GO, Node>::Read(matrixFile, map, colMap, domainMap, rangeMap);
223
224 } else {
225 A = Xpetra::IO<SC, LO, GO, Node>::Read(matrixFile, lib, comm, binaryFormat);
226
227 if (!map.is_null()) {
228 RCP<Matrix> newMatrix = MatrixFactory::Build(map, 1);
229 RCP<Import> importer = ImportFactory::Build(A->getRowMap(), map);
230 newMatrix->doImport(*A, *importer, Xpetra::INSERT);
231 newMatrix->fillComplete();
232
233 A.swap(newMatrix);
234 }
235 }
236 map = A->getMap();
237
238 comm->barrier();
239
240 if (!coordFile.empty()) {
241 // NOTE: currently we only allow reading scalar matrices, thus coordinate
242 // map is same as matrix map
243 coordinates = Xpetra::IO<SC, LO, GO, Node>::ReadMultiVector(coordFile, map);
244 }
245
246 if (!nullFile.empty())
247 nullspace = Xpetra::IO<SC, LO, GO, Node>::ReadMultiVector(nullFile, map);
248 }
249
250 comm->barrier();
251 tm = Teuchos::null;
252
253 galeriStream << "Galeri complete.\n========================================================" << std::endl;
254
255 std::string matrixType = galeriParameters.GetMatrixType();
256 std::string aggVizType = paramList.get<std::string>("aggregation: output file: agg style");
257 std::string aggQuality = "";
258 if (paramList.isParameter("aggregation: output file: aggregate qualities"))
259 aggQuality = paramList.get<bool>("aggregation: output file: aggregate qualities") ? "-AggregateQuality" : "";
260 aggVizType.erase(std::remove_if(aggVizType.begin(), aggVizType.end(), ::isspace), aggVizType.end());
261 if (ndims == 2)
262 paramList.set<std::string>("aggregation: output filename", "Output/MPI-Viz-Output-2D-Level%LEVELID-Proc%PROCID");
263 else if (ndims == 3) {
264 if (comm->getSize() > 1)
265 paramList.set<std::string>("aggregation: output filename", "Output/MPI-Viz-Output-" + matrixType + "-" + aggVizType + aggQuality + "-Level%LEVELID-Proc%PROCID");
266 else
267 paramList.set<std::string>("aggregation: output filename", "Output/MPI-Viz-Output-" + matrixType + "-" + aggVizType + aggQuality + "-Level%LEVELID");
268 }
269
270 if (nullspace.is_null()) {
271 int blkSize = 1;
272 if (paramList.isSublist("Matrix")) {
273 // Factory style parameter list
274 const Teuchos::ParameterList& operatorList = paramList.sublist("Matrix");
275 if (operatorList.isParameter("PDE equations"))
276 blkSize = operatorList.get<int>("PDE equations");
277
278 } else if (paramList.isParameter("number of equations")) {
279 // Easy style parameter list
280 blkSize = paramList.get<int>("number of equations");
281 }
282
283 nullspace = MultiVectorFactory::Build(map, blkSize);
284 for (int i = 0; i < blkSize; i++) {
285 RCP<const Map> domainMap = A->getDomainMap();
286 GO indexBase = domainMap->getIndexBase();
287
288 ArrayRCP<SC> nsData = nullspace->getDataNonConst(i);
289 for (int j = 0; j < nsData.size(); j++) {
290 GO GID = domainMap->getGlobalElement(j) - indexBase;
291
292 if ((GID - i) % blkSize == 0)
293 nsData[j] = Teuchos::ScalarTraits<SC>::one();
294 }
295 }
296 }
297
298 // Instead of checking each time for rank, create a rank 0 stream
299 RCP<Teuchos::FancyOStream> fancy = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
300 Teuchos::FancyOStream& out = *fancy;
301 out.setOutputToRootOnly(0);
302
303 out << galeriStream.str();
304
305 // =========================================================================
306 // Preconditioner construction
307 // =========================================================================
308 comm->barrier();
309 tm.reset();
310 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("ScalingTest: 1.5 - MueLu read XML")));
311
312 RCP<HierarchyManager> mueLuFactory = rcp(new ParameterListInterpreter(paramList));
313
314 comm->barrier();
315 tm.reset();
316 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("ScalingTest: 2 - MueLu Setup")));
317
318 RCP<Hierarchy> H;
319 A->SetMaxEigenvalueEstimate(-one);
320
321 H = mueLuFactory->CreateHierarchy();
322 H->GetLevel(0)->Set("A", A);
323 H->GetLevel(0)->Set("Nullspace", nullspace);
324 if (!coordinates.is_null())
325 H->GetLevel(0)->Set("Coordinates", coordinates);
326 mueLuFactory->SetupHierarchy(*H);
327
328 comm->barrier();
329 tm = Teuchos::null;
330
331 // =========================================================================
332 // Check aggs
333 // =========================================================================
334 std::string filenameToWrite;
335 if (comm->getSize() > 1)
336 filenameToWrite = "Output/MPI-Viz-Output-" + matrixType + "-" + aggVizType + aggQuality + "-Level0-Proc%PROCID";
337 else
338 filenameToWrite = "Output/MPI-Viz-Output-" + matrixType + "-" + aggVizType + aggQuality + "-Level0";
339 std::string outfileName = replaceAll(filenameToWrite, "%PROCID", MueLu::toString(comm->getRank()));
340 aggMatch = compare_to_gold_all_ranks(comm->getRank(), outfileName);
341
342 // =========================================================================
343 // System solution (Ax = b)
344 // =========================================================================
345 comm->barrier();
346
347 success = aggMatch;
348 }
349 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
350
351 return (success ? EXIT_SUCCESS : EXIT_FAILURE);
352 }
353
354 //- -- --------------------------------------------------------
355 #define MUELU_AUTOMATIC_TEST_ETI_NAME main_
356 #include "MueLu_Test_ETI.hpp"
357
358 int main(int argc, char* argv[]) {
359 return Automatic_Test_ETI(argc, argv);
360 }