Debian Codegraph @

packages/muelu/test/unit_tests_kokkos/MueLu_TestHelpers_kokkos.hpp

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 #ifndef MUELU_TEST_HELPERS_KOKKOS_H
11 #define MUELU_TEST_HELPERS_KOKKOS_H
12
13 #include <string>
14 #ifndef _MSC_VER
15 #include <dirent.h>
16 #endif
17
18 // Teuchos
19 #include <Teuchos_Comm.hpp>
20 #include <Teuchos_OrdinalTraits.hpp>
21 #include <Teuchos_ScalarTraits.hpp>
22
23 // Xpetra
24 #include <Xpetra_ConfigDefs.hpp>
25 #include <Xpetra_DefaultPlatform.hpp>
26 #include <Xpetra_Parameters.hpp>
27 #include <Xpetra_Map.hpp>
28 #include <Xpetra_MapFactory.hpp>
29 #include <Xpetra_CrsMatrixWrap.hpp>
30 #include <Xpetra_CrsGraph.hpp>
31
32 // MueLu
33 #include "MueLu_ConfigDefs.hpp"
34 #include "MueLu_Exceptions.hpp"
35 #include "MueLu_Hierarchy.hpp"
36 #include "MueLu_FactoryManagerBase.hpp"
37 #include "MueLu_FactoryManager.hpp"
38
39 #include "MueLu_IfpackSmoother.hpp"
40 #include "MueLu_Level.hpp"
41
42 // Galeri
43 #include <Galeri_XpetraProblemFactory.hpp>
44 #include <Galeri_XpetraMatrixTypes.hpp>
45
46 #include "MueLu_NoFactory.hpp"
47
48 // Conditional Tpetra stuff
49 #include <TpetraCore_config.h>
50 #include <Xpetra_TpetraCrsGraph.hpp>
51 #include <Xpetra_TpetraRowMatrix.hpp>
52 #include <Xpetra_TpetraBlockCrsMatrix.hpp>
53 #include <Tpetra_CrsGraph.hpp>
54 #include <Tpetra_Map.hpp>
55 #include <Tpetra_BlockCrsMatrix.hpp>
56
57 #include <MueLu_TestHelpers_Common_kokkos.hpp>
58
59 namespace MueLuTests {
60 using Teuchos::arcp;
61 using Teuchos::arcp_reinterpret_cast;
62 using Teuchos::arcpFromArrayView;
63 using Teuchos::Array;
64 using Teuchos::ArrayRCP;
65 using Teuchos::ArrayView;
66 using Teuchos::as;
67 using Teuchos::null;
68 using Teuchos::ParameterList;
69 using Teuchos::rcp;
70 using Teuchos::RCP;
71 using Teuchos::rcp_dynamic_cast;
72 using Teuchos::rcp_implicit_cast;
73 using Teuchos::rcpFromRef;
74
75 #include <MueLu_TestHelpers_Common_kokkos.hpp>
76
77 namespace TestHelpers_kokkos {
78
79 using Xpetra::global_size_t;
80
81 class Parameters {
82 private:
83 Parameters() {} // static class
84
85 public:
86 static Xpetra::Parameters xpetraParameters;
87
88 inline static RCP<const Teuchos::Comm<int> > getDefaultComm() {
89 return Xpetra::DefaultPlatform::getDefaultPlatform().getComm();
90 }
91
92 inline static Xpetra::UnderlyingLib getLib() {
93 return TestHelpers_kokkos::Parameters::xpetraParameters.GetLib();
94 }
95 };
96
97 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
98 class TestFactory {
99 #include "MueLu_UseShortNames.hpp"
100
101 private:
102 TestFactory() {} // static class
103
104 public:
105 typedef typename Teuchos::ScalarTraits<SC>::magnitudeType real_type;
106 typedef Xpetra::MultiVector<real_type, LO, GO, NO> RealValuedMultiVector;
107 typedef Xpetra::MultiVectorFactory<real_type, LO, GO, NO> RealValuedMultiVectorFactory;
108
109 // Create a map containing a specified number of local elements per process.
110 static const RCP<const Map> BuildMap(LO numElementsPerProc) {
111 RCP<const Teuchos::Comm<int> > comm = TestHelpers_kokkos::Parameters::getDefaultComm();
112 const global_size_t INVALID = Teuchos::OrdinalTraits<global_size_t>::invalid();
113
114 return MapFactory::Build(TestHelpers_kokkos::Parameters::getLib(), INVALID, numElementsPerProc, 0, comm);
115
116 } // BuildMap()
117
118 // Create a matrix as specified by parameter list options
119 static RCP<Matrix> BuildMatrix(ParameterList& matrixList, Xpetra::UnderlyingLib lib) {
120 RCP<const Teuchos::Comm<int> > comm = TestHelpers_kokkos::Parameters::getDefaultComm();
121
122 if (lib == Xpetra::NotSpecified)
123 lib = TestHelpers_kokkos::Parameters::getLib();
124
125 GO nx, ny, nz;
126 nx = ny = nz = 5;
127 nx = matrixList.get("nx", nx);
128 ny = matrixList.get("ny", ny);
129 nz = matrixList.get("nz", nz);
130
131 std::string matrixType = matrixList.get("matrixType", "Laplace1D");
132 GO numGlobalElements; // global_size_t
133 if (matrixType == "Laplace1D")
134 numGlobalElements = nx;
135 else if (matrixType == "Laplace2D" || matrixType == "Star2D")
136 numGlobalElements = nx * ny;
137 else if (matrixType == "Laplace3D")
138 numGlobalElements = nx * ny * nz;
139 else {
140 std::string msg = matrixType + " is unsupported (in unit testing)";
141 throw(MueLu::Exceptions::RuntimeError(msg));
142 }
143
144 RCP<const Map> map = MapFactory::Build(lib, numGlobalElements, 0, comm);
145 RCP<Galeri::Xpetra::Problem<Map, CrsMatrixWrap, MultiVector> > Pr =
146 Galeri::Xpetra::BuildProblem<SC, LO, GO, Map, CrsMatrixWrap, MultiVector>(matrixType, map, matrixList);
147 RCP<Matrix> Op = Pr->BuildMatrix();
148
149 return Op;
150 }
151
152 static typename Matrix::local_matrix_type::HostMirror buildLocal2x2Host(Scalar a00, Scalar a01, Scalar a10, Scalar a11, const bool keepZeros) {
153 using local_matrix_type = typename Matrix::local_matrix_type::HostMirror;
154 using local_graph_type = typename CrsGraph::local_graph_type::HostMirror;
155 using rowptr_type = typename local_graph_type::row_map_type::non_const_type;
156 using entries_type = typename local_graph_type::entries_type::non_const_type;
157 using values_type = typename local_matrix_type::values_type::non_const_type;
158
159 using TST = Teuchos::ScalarTraits<Scalar>;
160 size_t nnz;
161 if (keepZeros)
162 nnz = 4;
163 else
164 nnz = (TST::magnitude(a00) > TST::eps()) + (TST::magnitude(a01) > TST::eps()) + (TST::magnitude(a10) > TST::eps()) + (TST::magnitude(a11) > TST::eps());
165
166 auto rowptr = rowptr_type("rowptr", 3);
167 auto entries = entries_type("entries", nnz);
168 auto values = values_type("entries", nnz);
169 {
170 auto rowptr_h = Kokkos::create_mirror_view(rowptr);
171 auto entries_h = Kokkos::create_mirror_view(entries);
172 auto values_h = Kokkos::create_mirror_view(values);
173
174 size_t k = 0;
175
176 rowptr_h(0) = k;
177 if (keepZeros || TST::magnitude(a00) > TST::eps()) {
178 entries_h(k) = 0;
179 values_h(k) = a00;
180 ++k;
181 }
182 if (keepZeros || TST::magnitude(a01) > TST::eps()) {
183 entries_h(k) = 1;
184 values_h(k) = a01;
185 ++k;
186 }
187
188 rowptr_h(1) = k;
189 if (keepZeros || TST::magnitude(a10) > TST::eps()) {
190 entries_h(k) = 0;
191 values_h(k) = a10;
192 ++k;
193 }
194
195 if (keepZeros || TST::magnitude(a11) > TST::eps()) {
196 entries_h(k) = 1;
197 values_h(k) = a11;
198 ++k;
199 }
200 rowptr_h(2) = k;
201
202 Kokkos::deep_copy(rowptr, rowptr_h);
203 Kokkos::deep_copy(entries, entries_h);
204 Kokkos::deep_copy(values, values_h);
205 }
206 auto lclA = local_matrix_type("A", 2, 2, nnz, values, rowptr, entries);
207 return lclA;
208 }
209
210 static std::string localMatToString(typename Matrix::local_matrix_type::HostMirror& mat) {
211 std::stringstream s;
212 typename Matrix::local_ordinal_type numCols = mat.numCols();
213 for (typename Matrix::local_ordinal_type row_id = 0; row_id < mat.numRows(); ++row_id) {
214 auto row = mat.row(row_id);
215 for (typename Matrix::local_ordinal_type col_id = 0; col_id < numCols; ++col_id) {
216 bool found = false;
217 for (typename Matrix::local_ordinal_type colPtr = 0; colPtr < row.length; ++colPtr) {
218 if (row.colidx(colPtr) == col_id) {
219 s << row.value(colPtr) << " ";
220 found = true;
221 }
222 }
223 if (not found)
224 s << 0.0 << " ";
225 }
226 s << "\n";
227 }
228 return s.str();
229 }
230
231 static typename Matrix::local_matrix_type buildLocal2x2(Scalar a00, Scalar a01, Scalar a10, Scalar a11) {
232 using local_matrix_type = typename Matrix::local_matrix_type;
233 using local_graph_type = typename CrsGraph::local_graph_type;
234 using rowptr_type = typename local_graph_type::row_map_type::non_const_type;
235 using entries_type = typename local_graph_type::entries_type::non_const_type;
236 using values_type = typename local_matrix_type::values_type::non_const_type;
237
238 using TST = Teuchos::ScalarTraits<Scalar>;
239 size_t nnz = (TST::magnitude(a00) > TST::eps()) + (TST::magnitude(a01) > TST::eps()) + (TST::magnitude(a10) > TST::eps()) + (TST::magnitude(a11) > TST::eps());
240
241 auto rowptr = rowptr_type("rowptr", 3);
242 auto entries = entries_type("entries", nnz);
243 auto values = values_type("entries", nnz);
244 {
245 auto rowptr_h = Kokkos::create_mirror_view(rowptr);
246 auto entries_h = Kokkos::create_mirror_view(entries);
247 auto values_h = Kokkos::create_mirror_view(values);
248
249 size_t k = 0;
250
251 rowptr_h(0) = k;
252 if (TST::magnitude(a00) > TST::eps()) {
253 entries_h(k) = 0;
254 values_h(k) = a00;
255 ++k;
256 }
257 if (TST::magnitude(a01) > TST::eps()) {
258 entries_h(k) = 1;
259 values_h(k) = a01;
260 ++k;
261 }
262
263 rowptr_h(1) = k;
264 if (TST::magnitude(a10) > TST::eps()) {
265 entries_h(k) = 0;
266 values_h(k) = a10;
267 ++k;
268 }
269
270 if (TST::magnitude(a11) > TST::eps()) {
271 entries_h(k) = 1;
272 values_h(k) = a11;
273 ++k;
274 }
275 rowptr_h(2) = k;
276
277 Kokkos::deep_copy(rowptr, rowptr_h);
278 Kokkos::deep_copy(entries, entries_h);
279 Kokkos::deep_copy(values, values_h);
280 }
281 auto lclA = local_matrix_type("A", 2, 2, nnz, values, rowptr, entries);
282 return lclA;
283 }
284
285 static RCP<Matrix> build2x2(Xpetra::UnderlyingLib lib, Scalar a00, Scalar a01, Scalar a10, Scalar a11) {
286 auto lclA = buildLocal2x2(a00, a01, a10, a11);
287
288 RCP<const Teuchos::Comm<int> > comm = TestHelpers_kokkos::Parameters::getDefaultComm();
289 if (lib == Xpetra::NotSpecified)
290 lib = TestHelpers_kokkos::Parameters::getLib();
291 RCP<const Map> map = MapFactory::Build(lib, 2 * comm->getSize(), 0, comm);
292
293 return MatrixFactory::Build(lclA, map, map);
294 }
295
296 // Create a 1D Poisson matrix with the specified number of rows
297 // nx: global number of rows
298 static RCP<Matrix> Build1DPoisson(GO nx, Xpetra::UnderlyingLib lib = Xpetra::NotSpecified) { // global_size_t
299 ParameterList matrixList;
300 matrixList.set("nx", nx);
301 matrixList.set("matrixType", "Laplace1D");
302 return BuildMatrix(matrixList, lib);
303 }
304
305 // Create a 2D Poisson matrix with the specified number of rows
306 // nx: global number of rows
307 // ny: global number of rows
308 static RCP<Matrix> Build2DPoisson(GO nx, GO ny = -1, Xpetra::UnderlyingLib lib = Xpetra::NotSpecified) { // global_size_t
309 if (ny == -1) ny = nx;
310
311 ParameterList matrixList;
312 matrixList.set("nx", nx);
313 matrixList.set("ny", ny);
314 matrixList.set("matrixType", "Laplace2D");
315
316 return BuildMatrix(matrixList, lib);
317 }
318
319 // Create a tridiagonal matrix (stencil = [b,a,c]) with the specified number of rows
320 static RCP<Matrix> BuildTridiag(RCP<const Map> rowMap, SC a, SC b, SC c, Xpetra::UnderlyingLib lib = Xpetra::NotSpecified) {
321 if (lib == Xpetra::NotSpecified)
322 lib = TestHelpers_kokkos::Parameters::getLib();
323
324 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
325
326 RCP<Matrix> mtx = MatrixFactory::Build(rowMap, 3);
327
328 ArrayView<const GO> MyGlobalElements = rowMap->getLocalElementList();
329 GO indexBase = rowMap->getIndexBase();
330
331 GO NumEntries;
332 LO nnz = 3;
333 std::vector<SC> Values(nnz);
334 std::vector<GO> Indices(nnz);
335
336 // access information from strided block map
337 std::vector<size_t> strInfo = std::vector<size_t>();
338 size_t blockSize = 1;
339 LO blockId = -1;
340 GO offset = 0;
341
342 RCP<const StridedMap> strrowMap = rcp_dynamic_cast<const StridedMap>(rowMap);
343 if (strrowMap != Teuchos::null) {
344 strInfo = strrowMap->getStridingData();
345 blockSize = strrowMap->getFixedBlockSize();
346 blockId = strrowMap->getStridedBlockId();
347 offset = strrowMap->getOffset();
348 TEUCHOS_TEST_FOR_EXCEPTION(blockId > -1 && strInfo[blockId] == 1, MueLu::Exceptions::RuntimeError,
349 "MueLu::TestHelpers_kokkos::BuildTridiag: strInfo block size must be > 1.");
350 // todo: write one more special case for a row being first and last bock row
351 } else {
352 // no striding information. emulate block matrix
353 blockId = 0;
354 strInfo.push_back(blockSize); // default block size = 1
355 }
356
357 GO rrmax = (rowMap->getMaxAllGlobalIndex() - offset - indexBase) / blockSize;
358
359 // loop over all rows
360 LO numMyElements = rowMap->getLocalNumElements();
361 for (LO i = 0; i < numMyElements; i++) {
362 GO rr = (MyGlobalElements[i] - offset - indexBase) / blockSize + indexBase; // node index
363
364 // distinguish 5 different cases
365
366 GO blockOffset = 0;
367 for (LO k = 0; k < blockId; k++)
368 blockOffset += Teuchos::as<GO>(strInfo[k]);
369
370 if (MyGlobalElements[i] == blockOffset + offset + indexBase) {
371 // very first row
372 Indices[0] = MyGlobalElements[i];
373 Values[0] = a;
374 Indices[1] = MyGlobalElements[i] + 1;
375 Values[1] = c;
376 NumEntries = 2;
377
378 } else if (MyGlobalElements[i] == rrmax * Teuchos::as<GO>(blockSize) + blockOffset + Teuchos::as<GO>(strInfo[blockId]) - 1 + offset + indexBase) {
379 // very last row
380 Indices[0] = MyGlobalElements[i] - 1;
381 Values[0] = b;
382 Indices[1] = MyGlobalElements[i];
383 Values[1] = a;
384 NumEntries = 2;
385
386 } else if (MyGlobalElements[i] == rr * Teuchos::as<GO>(blockSize) + blockOffset + Teuchos::as<GO>(strInfo[blockId]) - 1 + offset + indexBase) {
387 // last row in current node block
388 Indices[0] = MyGlobalElements[i] - 1;
389 Values[0] = b;
390 Indices[1] = MyGlobalElements[i];
391 Values[1] = a;
392 Indices[2] = (rr + 1) * blockSize + blockOffset + offset + indexBase;
393 Values[2] = c;
394 NumEntries = 3;
395
396 } else if (MyGlobalElements[i] == rr * Teuchos::as<GO>(blockSize) + blockOffset + offset + indexBase) {
397 // first row in current node block
398 Indices[0] = (rr - 1) * blockSize + blockOffset + strInfo[blockId] - 1 + offset + indexBase;
399 Values[0] = b;
400 Indices[1] = MyGlobalElements[i];
401 Values[1] = a;
402 Indices[2] = MyGlobalElements[i] + 1;
403 Values[2] = c;
404 NumEntries = 3;
405
406 } else {
407 // usual row entries in block rows
408 Indices[0] = MyGlobalElements[i] - 1;
409 Values[0] = b;
410 Indices[1] = MyGlobalElements[i];
411 Values[1] = a;
412 Indices[2] = MyGlobalElements[i] + 1;
413 Values[2] = c;
414 NumEntries = 3;
415 }
416
417 // debug output
418 /*std::cout << "--------> Proc " << comm->getRank() << " lrow " << i << " grow " << MyGlobalElements[i] << " node " << rr << " values = " ;
419 for (size_t k = 0; k < NumEntries; k++) {
420 std::cout << " " << Indices[k] << "->" << Values[k] << " ";
421 }
422 std::cout << std::endl;*/
423
424 // put the off-diagonal entries
425 // Xpetra wants ArrayViews (sigh)
426 ArrayView<SC> av(&Values[0], NumEntries);
427 ArrayView<GO> iv(&Indices[0], NumEntries);
428 mtx->insertGlobalValues(MyGlobalElements[i], iv, av);
429 }
430
431 mtx->fillComplete();
432
433 return mtx;
434 }
435
436 static RCP<RealValuedMultiVector>
437 BuildGeoCoordinates(const int numDimensions, const Array<GO> gNodesPerDir,
438 Array<LO>& lNodesPerDir, Array<GO>& meshData,
439 const std::string meshLayout = "Global Lexicographic") {
440 // Get MPI infos
441 Xpetra::UnderlyingLib lib = TestHelpers_kokkos::Parameters::getLib();
442 RCP<const Teuchos::Comm<int> > comm = TestHelpers_kokkos::Parameters::getDefaultComm();
443 LO numRanks = comm->getSize();
444 LO myRank = comm->getRank();
445
446 meshData.resize(10 * numRanks);
447
448 ////////////////////////////////////
449 // //
450 // Step 1: Compute map layout //
451 // //
452 ////////////////////////////////////
453 if (numRanks == 1) {
454 if (meshLayout == "Local Lexicographic") {
455 meshData[0] = 0; // Local Proc
456 meshData[1] = 0; // Local Proc?
457 meshData[2] = 0; // Parent Proc
458 }
459 for (int dim = 0; dim < 3; ++dim) {
460 lNodesPerDir[dim] = gNodesPerDir[dim];
461 if (meshLayout == "Local Lexicographic") {
462 meshData[3 + 2 * dim] = 0; // Lowest index in direction dim
463 meshData[3 + 2 * dim + 1] = gNodesPerDir[dim] - 1; // Highest index in direction dim
464 }
465 }
466 if (meshLayout == "Local Lexicographic") {
467 meshData[9] = 0;
468 }
469 } else if (numRanks == 4) {
470 if (meshLayout == "Local Lexicographic") {
471 // First fill the rank related info in meshData
472 for (int rank = 0; rank < numRanks; ++rank) {
473 meshData[10 * rank + 0] = rank; // Local Proc
474 meshData[10 * rank + 1] = rank; // Local Proc?
475 meshData[10 * rank + 2] = 0; // Parent Proc
476 }
477 }
478
479 if (numDimensions == 1) {
480 LO numNodesPerRank = std::ceil(Teuchos::as<real_type>(gNodesPerDir[0]) / numRanks);
481 if (myRank == numRanks - 1) {
482 lNodesPerDir[0] = gNodesPerDir[0] - myRank * numNodesPerRank;
483 } else {
484 lNodesPerDir[0] = numNodesPerRank;
485 }
486 lNodesPerDir[1] = gNodesPerDir[1];
487
488 if (meshLayout == "Local Lexicographic") {
489 for (int rank = 0; rank < numRanks; ++rank) {
490 meshData[10 * rank + 3] = rank * numNodesPerRank;
491 meshData[10 * rank + 4] =
492 (rank == numRanks - 1) ? (gNodesPerDir[0] - 1) : ((rank + 1) * numNodesPerRank - 1);
493 meshData[10 * rank + 5] = 0;
494 meshData[10 * rank + 6] = 0;
495 }
496 }
497 } else {
498 if (myRank == 0 || myRank == 2) {
499 lNodesPerDir[0] = std::ceil(Teuchos::as<real_type>(gNodesPerDir[0]) / 2);
500 } else {
501 lNodesPerDir[0] = std::floor(Teuchos::as<real_type>(gNodesPerDir[0]) / 2);
502 }
503 if (myRank == 0 || myRank == 1) {
504 lNodesPerDir[1] = std::ceil(Teuchos::as<real_type>(gNodesPerDir[1]) / 2);
505 } else {
506 lNodesPerDir[1] = std::floor(Teuchos::as<real_type>(gNodesPerDir[1]) / 2);
507 }
508 if (meshLayout == "Local Lexicographic") {
509 for (int j = 0; j < 2; ++j) {
510 for (int i = 0; i < 2; ++i) {
511 int rank = 2 * j + i;
512 if (i == 0) {
513 meshData[10 * rank + 3] = 0;
514 meshData[10 * rank + 4] = std::ceil(Teuchos::as<real_type>(gNodesPerDir[0]) / 2) - 1;
515 } else if (i == 1) {
516 meshData[10 * rank + 3] = std::ceil(Teuchos::as<real_type>(gNodesPerDir[0]) / 2);
517 meshData[10 * rank + 4] = gNodesPerDir[0] - 1;
518 }
519 if (j == 0) {
520 meshData[10 * rank + 5] = 0;
521 meshData[10 * rank + 6] = std::ceil(Teuchos::as<real_type>(gNodesPerDir[1]) / 2) - 1;
522 } else if (j == 1) {
523 meshData[10 * rank + 5] = std::ceil(Teuchos::as<real_type>(gNodesPerDir[1]) / 2);
524 meshData[10 * rank + 6] = gNodesPerDir[1] - 1;
525 }
526 }
527 }
528 }
529 }
530 lNodesPerDir[2] = gNodesPerDir[2];
531 if (meshLayout == "Local Lexicographic") {
532 for (int rank = 0; rank < numRanks; ++rank) {
533 meshData[10 * rank + 7] = 0;
534 meshData[10 * rank + 8] = gNodesPerDir[2] - 1;
535 }
536 meshData[9] = 0;
537 for (int rank = 1; rank < numRanks; ++rank) {
538 meshData[10 * rank + 9] = meshData[10 * (rank - 1) + 9] + (meshData[10 * (rank - 1) + 4] - meshData[10 * (rank - 1) + 3] + 1) * (meshData[10 * (rank - 1) + 6] - meshData[10 * (rank - 1) + 5] + 1) * (meshData[10 * (rank - 1) + 8] - meshData[10 * (rank - 1) + 7] + 1);
539 }
540 }
541 }
542
543 GO gNumNodes = 1;
544 LO lNumNodes = 1;
545 for (int dim = 0; dim < 3; ++dim) {
546 gNumNodes = gNumNodes * gNodesPerDir[dim];
547 lNumNodes = lNumNodes * lNodesPerDir[dim];
548 }
549
550 GO myGIDOffset = 0;
551 Array<GO> myLowestTuple(3);
552 if (numDimensions == 1) {
553 myGIDOffset = myRank * std::ceil(Teuchos::as<real_type>(gNodesPerDir[0]) / numRanks);
554 myLowestTuple[0] = myGIDOffset;
555 } else {
556 if (myRank == 1) {
557 myLowestTuple[0] = std::ceil(Teuchos::as<real_type>(gNodesPerDir[0]) / 2);
558 if (meshLayout == "Global Lexicographic") {
559 myGIDOffset = myLowestTuple[0];
560 } else if (meshLayout == "Local Lexicographic") {
561 myGIDOffset = myLowestTuple[0] * std::ceil(Teuchos::as<real_type>(gNodesPerDir[1]) / 2) * gNodesPerDir[2];
562 }
563 } else if (myRank == 2) {
564 myLowestTuple[1] = std::ceil(Teuchos::as<real_type>(gNodesPerDir[1]) / 2);
565 if (meshLayout == "Global Lexicographic") {
566 myGIDOffset = std::ceil(Teuchos::as<real_type>(gNodesPerDir[1]) / 2) * gNodesPerDir[0];
567 } else if (meshLayout == "Local Lexicographic") {
568 myGIDOffset = myLowestTuple[1] * gNodesPerDir[0] * gNodesPerDir[2];
569 }
570 } else if (myRank == 3) {
571 myLowestTuple[0] = std::ceil(Teuchos::as<real_type>(gNodesPerDir[0]) / 2);
572 myLowestTuple[1] = std::ceil(Teuchos::as<real_type>(gNodesPerDir[1]) / 2);
573 if (meshLayout == "Global Lexicographic") {
574 myGIDOffset = myLowestTuple[1] * gNodesPerDir[0] + myLowestTuple[0];
575 } else if (meshLayout == "Local Lexicographic") {
576 myGIDOffset = (myLowestTuple[0] * (gNodesPerDir[1] - myLowestTuple[1]) + myLowestTuple[1] * gNodesPerDir[0]) * gNodesPerDir[2];
577 }
578 }
579 }
580
581 ////////////////////////////////////
582 // //
583 // Step 2: Compute map GIDs //
584 // //
585 ////////////////////////////////////
586 Array<GO> myGIDs(lNumNodes);
587 if (meshLayout == "Global Lexicographic") {
588 for (LO k = 0; k < lNodesPerDir[2]; ++k) {
589 for (LO j = 0; j < lNodesPerDir[1]; ++j) {
590 for (LO i = 0; i < lNodesPerDir[0]; ++i) {
591 myGIDs[k * lNodesPerDir[1] * lNodesPerDir[0] + j * lNodesPerDir[0] + i] =
592 myGIDOffset + k * gNodesPerDir[1] * gNodesPerDir[0] + j * gNodesPerDir[0] + i;
593 }
594 }
595 }
596 } else if (meshLayout == "Local Lexicographic") {
597 for (LO nodeIdx = 0; nodeIdx < lNumNodes; ++nodeIdx) {
598 myGIDs[nodeIdx] = myGIDOffset + nodeIdx;
599 }
600 }
601
602 RCP<const Map> coordMap = MapFactory::Build(lib, gNumNodes, myGIDs(), 0, comm);
603
604 ///////////////////////////////////////
605 // //
606 // Step 3: Compute coordinates //
607 // //
608 ///////////////////////////////////////
609 RCP<RealValuedMultiVector> Coordinates = RealValuedMultiVectorFactory::Build(coordMap,
610 numDimensions);
611 Array<ArrayRCP<real_type> > myCoords(numDimensions);
612 for (int dim = 0; dim < numDimensions; ++dim) {
613 myCoords[dim] = Coordinates->getDataNonConst(dim);
614 }
615
616 LO nodeIdx = 0;
617 Array<LO> ijk(3);
618 for (ijk[2] = 0; ijk[2] < lNodesPerDir[2]; ++ijk[2]) {
619 for (ijk[1] = 0; ijk[1] < lNodesPerDir[1]; ++ijk[1]) {
620 for (ijk[0] = 0; ijk[0] < lNodesPerDir[0]; ++ijk[0]) {
621 nodeIdx = ijk[2] * lNodesPerDir[1] * lNodesPerDir[0] + ijk[1] * lNodesPerDir[0] + ijk[0];
622 for (int dim = 0; dim < numDimensions; ++dim) {
623 if (gNodesPerDir[dim] == 1) {
624 myCoords[dim][nodeIdx] = 0.0;
625 } else {
626 myCoords[dim][nodeIdx] =
627 Teuchos::as<real_type>(myLowestTuple[dim] + ijk[dim]) / (gNodesPerDir[dim] - 1);
628 }
629 }
630 }
631 }
632 }
633
634 return Coordinates;
635 }
636
637 // Needed to initialize correctly a level used for testing SingleLevel factory Build() methods.
638 // This method initializes LevelID and linked list of level
639 static void createSingleLevelHierarchy(Level& currentLevel) {
640 RCP<MueLu::FactoryManagerBase> factoryHandler = rcp(new FactoryManager());
641 currentLevel.SetFactoryManager(factoryHandler);
642
643 currentLevel.SetLevelID(0);
644 #ifdef HAVE_MUELU_TIMER_SYNCHRONIZATION
645 currentLevel.SetComm(TestHelpers_kokkos::Parameters::getDefaultComm());
646 #endif
647 }
648
649 // Needed to initialize correctly levels used for testing TwoLevel factory Build() methods.
650 // This method initializes LevelID and linked list of level
651 static void createTwoLevelHierarchy(Level& fineLevel, Level& coarseLevel) {
652 RCP<MueLu::FactoryManagerBase> factoryHandler = rcp(new FactoryManager());
653 fineLevel.SetFactoryManager(factoryHandler);
654 coarseLevel.SetFactoryManager(factoryHandler);
655
656 coarseLevel.SetPreviousLevel(rcpFromRef(fineLevel));
657
658 fineLevel.SetLevelID(0);
659 coarseLevel.SetLevelID(1);
660 #ifdef HAVE_MUELU_TIMER_SYNCHRONIZATION
661 fineLevel.SetComm(TestHelpers_kokkos::Parameters::getDefaultComm());
662 coarseLevel.SetComm(TestHelpers_kokkos::Parameters::getDefaultComm());
663 #endif
664 }
665
666 #if 0
667 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_IFPACK)
668 static RCP<SmootherPrototype> createSmootherPrototype(const std::string& type="Gauss-Seidel", LO sweeps=1) {
669 Teuchos::ParameterList ifpackList;
670 ifpackList.set("relaxation: type", type);
671 ifpackList.set("relaxation: sweeps", (LO) sweeps);
672 ifpackList.set("relaxation: damping factor", (SC) 1.0);
673 return rcp( new IfpackSmoother("point relaxation stand-alone",ifpackList) );
674 }
675 #endif
676 #endif
677
678 // Create a matrix as specified by parameter list options
679 static RCP<Matrix> BuildBlockMatrixAsPoint(Teuchos::ParameterList& matrixList, Xpetra::UnderlyingLib lib) {
680 RCP<const Teuchos::Comm<int> > comm = TestHelpers_kokkos::Parameters::getDefaultComm();
681 GO GO_INVALID = Teuchos::OrdinalTraits<GO>::invalid();
682 RCP<Matrix> Op;
683
684 if (lib == Xpetra::NotSpecified)
685 lib = TestHelpers_kokkos::Parameters::getLib();
686
687 // Make the base graph
688 RCP<Matrix> old_matrix = TestHelpers_kokkos::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildMatrix(matrixList, lib);
689 RCP<const CrsGraph> old_graph = old_matrix->getCrsGraph();
690 RCP<const Map> old_rowmap = old_graph->getRowMap();
691 RCP<const Map> old_colmap = old_graph->getColMap();
692 int blocksize = 3;
693
694 // Block Map
695 LO orig_num_rows = (LO)old_graph->getRowMap()->getLocalNumElements();
696 Teuchos::Array<GlobalOrdinal> owned_rows(blocksize * orig_num_rows);
697 for (LO i = 0; i < orig_num_rows; i++) {
698 GO old_gid = old_rowmap->getGlobalElement(i);
699 for (int j = 0; j < blocksize; j++) {
700 owned_rows[i * blocksize + j] = old_gid * blocksize + j;
701 }
702 }
703 RCP<Map> new_map = Xpetra::MapFactory<LO, GO, NO>::Build(lib, GO_INVALID, owned_rows(), 0, comm);
704 if (new_map.is_null()) throw std::runtime_error("BuildBlockMatrixAsPoint: Map constructor failed");
705
706 // Block Graph / Matrix
707 RCP<CrsMatrix> new_matrix = Xpetra::CrsMatrixFactory<SC, LO, GO, NO>::Build(new_map, blocksize * old_graph->getLocalMaxNumRowEntries());
708 if (new_matrix.is_null()) throw std::runtime_error("BuildBlockMatrixAsPoint: Matrix constructor failed");
709 for (LO i = 0; i < orig_num_rows; i++) {
710 Teuchos::ArrayView<const LO> old_indices;
711 Teuchos::ArrayView<const SC> old_values;
712 Teuchos::Array<GO> new_indices(1);
713 Teuchos::Array<SC> new_values(1);
714 old_matrix->getLocalRowView(i, old_indices, old_values);
715 for (int ii = 0; ii < blocksize; ii++) {
716 GO GRID = new_map->getGlobalElement(i * blocksize + ii);
717 for (LO j = 0; j < (LO)old_indices.size(); j++) {
718 for (int jj = 0; jj < blocksize; jj++) {
719 new_indices[0] = old_colmap->getGlobalElement(old_indices[j]) * blocksize + jj;
720 new_values[0] = old_values[j] * (SC)((ii == jj && i == old_indices[j]) ? blocksize * blocksize : 1);
721 new_matrix->insertGlobalValues(GRID, new_indices(), new_values);
722 }
723 }
724 }
725 }
726 new_matrix->fillComplete();
727 Op = rcp(new CrsMatrixWrap(new_matrix));
728 if (new_map.is_null()) throw std::runtime_error("BuildBlockMatrixAsPoint: CrsMatrixWrap constructor failed");
729 Op->SetFixedBlockSize(blocksize);
730
731 return Op;
732 } // BuildBlockMatrixAsPoint()
733 }; // class TestFactory
734
735 // Helper class which has some Tpetra specific code inside
736 // We put this into an extra helper class as we need partial specializations and
737 // do not want to introduce partial specializations for the full TestFactory class
738 //
739 // The BuildBlockMatrix is only available with Teptra. However, if both Epetra
740 // and Tpetra are enabled it may be that Tpetra is not instantiated on either
741 // GO=int/long long and/or Node=Serial/OpenMP. We need partial specializations
742 // with an empty BuildBlockMatrix routine for all instantiations Teptra is not
743 // enabled for, but are existing in Xpetra due to Epetra enabled.
744 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
745 class TpetraTestFactory {
746 #include "MueLu_UseShortNames.hpp"
747 public:
748 // Create a matrix as specified by parameter list options
749 static RCP<Matrix> BuildBlockMatrix(Teuchos::ParameterList& matrixList, Xpetra::UnderlyingLib lib) {
750 RCP<const Teuchos::Comm<int> > comm = TestHelpers_kokkos::Parameters::getDefaultComm();
751 RCP<Matrix> Op;
752
753 if (lib == Xpetra::NotSpecified)
754 lib = TestHelpers_kokkos::Parameters::getLib();
755
756 // This only works for Tpetra
757 if (lib != Xpetra::UseTpetra) return Op;
758
759 // Thanks for the code, Travis!
760
761 // Make the graph
762 RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > FirstMatrix = TestHelpers_kokkos::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildMatrix(matrixList, lib);
763 RCP<const Xpetra::CrsGraph<LocalOrdinal, GlobalOrdinal, Node> > Graph = FirstMatrix->getCrsGraph();
764
765 int blocksize = 3;
766 RCP<const Xpetra::TpetraCrsGraph<LocalOrdinal, GlobalOrdinal, Node> > TGraph = rcp_dynamic_cast<const Xpetra::TpetraCrsGraph<LocalOrdinal, GlobalOrdinal, Node> >(Graph);
767 RCP<const Tpetra::CrsGraph<LocalOrdinal, GlobalOrdinal, Node> > TTGraph = TGraph->getTpetra_CrsGraph();
768
769 RCP<Tpetra::BlockCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > bcrsmatrix = rcp(new Tpetra::BlockCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>(*TTGraph, blocksize));
770
771 const Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node>& meshRowMap = *bcrsmatrix->getRowMap();
772 const Scalar zero = Teuchos::ScalarTraits<Scalar>::zero();
773 const Scalar one = Teuchos::ScalarTraits<Scalar>::one();
774 const Scalar two = one + one;
775 const Scalar three = two + one;
776
777 Teuchos::Array<Scalar> basematrix(blocksize * blocksize, zero);
778 basematrix[0] = two;
779 basematrix[2] = three;
780 basematrix[3] = three;
781 basematrix[4] = two;
782 basematrix[7] = three;
783 basematrix[8] = two;
784 Teuchos::Array<LocalOrdinal> lclColInds(1);
785 for (LocalOrdinal lclRowInd = meshRowMap.getMinLocalIndex(); lclRowInd <= meshRowMap.getMaxLocalIndex(); ++lclRowInd) {
786 lclColInds[0] = lclRowInd;
787 bcrsmatrix->replaceLocalValues(lclRowInd, lclColInds.getRawPtr(), &basematrix[0], 1);
788 }
789
790 RCP<Xpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > temp = rcp(new Xpetra::TpetraBlockCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>(bcrsmatrix));
791 Op = rcp(new Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(temp));
792
793 return Op;
794 } // BuildBlockMatrix()
795
796 private:
797 TpetraTestFactory() {} // static class
798
799 }; // class TpetraTestFactory
800
801 // TAW: 3/14/2016: If both Epetra and Tpetra are enabled we need partial specializations
802 // on GO=int/long long as well as NO=EpetraNode to disable BuildBlockMatrix
803 #ifdef HAVE_MUELU_EPETRA
804 // partial specializations (GO=int not enabled with Tpetra)
805 #if !defined(HAVE_TPETRA_INST_INT_INT)
806 template <class Scalar, class LocalOrdinal, class Node>
807 class TpetraTestFactory<Scalar, LocalOrdinal, int, Node> {
808 typedef int GlobalOrdinal;
809 #include "MueLu_UseShortNames.hpp"
810 public:
811 static RCP<Matrix> BuildBlockMatrix(Teuchos::ParameterList& matrixList, Xpetra::UnderlyingLib lib) { return Teuchos::null; }
812
813 private:
814 TpetraTestFactory() {} // static class
815 }; // class TpetraTestFactory
816 #endif
817
818 // partial specializations (GO=long long not enabled with Tpetra)
819 #if !defined(HAVE_TPETRA_INST_INT_LONG_LONG)
820 template <class Scalar, class LocalOrdinal, class Node>
821 class TpetraTestFactory<Scalar, LocalOrdinal, long long, Node> {
822 typedef long long GlobalOrdinal;
823 #include "MueLu_UseShortNames.hpp"
824 public:
825 static RCP<Matrix> BuildBlockMatrix(Teuchos::ParameterList& matrixList, Xpetra::UnderlyingLib lib) { return Teuchos::null; }
826
827 private:
828 TpetraTestFactory() {} // static class
829 }; // class TpetraTestFactory
830 #endif
831
832 // partial specializations (NO=EpetraNode not enabled with Tpetra)
833 #if ((defined(EPETRA_HAVE_OMP) && !(defined(HAVE_TPETRA_INST_OPENMP))) || \
834 (!defined(EPETRA_HAVE_OMP) && !(defined(HAVE_TPETRA_INST_SERIAL))))
835
836 template <class Scalar, class LocalOrdinal, class GlobalOrdinal>
837 class TpetraTestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Xpetra::EpetraNode> {
838 typedef Xpetra::EpetraNode Node;
839 #include "MueLu_UseShortNames.hpp"
840 public:
841 static RCP<Matrix> BuildBlockMatrix(Teuchos::ParameterList& matrixList, Xpetra::UnderlyingLib lib) { return Teuchos::null; }
842
843 private:
844 TpetraTestFactory() {} // static class
845 }; // class TpetraTestFactory
846 #endif
847 #endif // endif HAVE_MUELU_EPETRA
848
849 //! Return the list of files in the directory. Only files that are matching '*filter*' are returned.
850 ArrayRCP<std::string> GetFileList(const std::string& dirPath, const std::string& filter);
851
852 } // namespace TestHelpers_kokkos
853
854 } // namespace MueLuTests
855
856 // Macro to skip a test when UnderlyingLib==Epetra or Tpetra
857 #define MUELU_TEST_ONLY_FOR(UnderlyingLib) \
858 if (TestHelpers_kokkos::Parameters::getLib() != UnderlyingLib) { \
859 out << "Skipping test for " << ((TestHelpers_kokkos::Parameters::getLib() == Xpetra::UseEpetra) ? "Epetra" : "Tpetra") << std::endl; \
860 return; \
861 }
862
863 // Macro to skip a test when Epetra is used with Ordinal != int
864 #define MUELU_TEST_EPETRA_ONLY_FOR_INT(LocalOrdinal, GlobalOrdinal) \
865 if (!(TestHelpers_kokkos::Parameters::getLib() == Xpetra::UseEpetra && (Teuchos::OrdinalTraits<LocalOrdinal>::name() != string("int") || Teuchos::OrdinalTraits<GlobalOrdinal>::name() != string("int"))))
866
867 // Macro to skip a test when Epetra is used with Scalar != double or Ordinal != int
868 #define MUELU_TEST_EPETRA_ONLY_FOR_DOUBLE_AND_INT(Scalar, LocalOrdinal, GlobalOrdinal) \
869 if (!(TestHelpers_kokkos::Parameters::getLib() == Xpetra::UseEpetra && Teuchos::ScalarTraits<Scalar>::name() != string("double"))) \
870 MUELU_TEST_EPETRA_ONLY_FOR_INT(LocalOrdinal, GlobalOrdinal)
871
872 //! Namespace for MueLu test classes
873 namespace MueLuTests {
874
875 using namespace TestHelpers_kokkos;
876 }
877
878 #endif // ifndef MUELU_TEST_HELPERS_KOKKOS_H