Debian Codegraph @

packages/tpetra/core/example/Additive-Schwarz-Halo/AdditiveSchwarzHalo.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include <cmath>
11 #include <iostream>
12 #include <iomanip>
13 #include <sstream>
14
15 #include <Teuchos_RCP.hpp>
16 #include <Teuchos_FancyOStream.hpp>
17 #include <Teuchos_CommandLineProcessor.hpp>
18 #include <Teuchos_ScalarTraits.hpp>
19 #include <Teuchos_OrdinalTraits.hpp>
20
21 #include <Tpetra_Core.hpp>
22 #include <Tpetra_CrsMatrix.hpp>
23 #include <Tpetra_MultiVector.hpp>
24
25 int main (int argc, char *argv[])
26 {
27 using Teuchos::RCP;
28 using Teuchos::rcp;
29 using Teuchos::TimeMonitor;
30 using Teuchos::Array;
31 using Teuchos::tuple;
32
33 // Tpetra::ScopeGuard initializes MPI and Kokkos, if they haven't
34 // been initialized already. Its destructor will finalize MPI and
35 // Kokkos.
36 Tpetra::ScopeGuard tpetraScope (&argc, &argv);
37 {
38 // Put all Tpetra objects in an inner scope. They must not be
39 // allowed to persist past the end of the Tpetra::ScopeGuard
40 // object's lifetime.
41 auto comm = Tpetra::getDefaultComm ();
42
43 int num_halo_expansions = 1;
44 Teuchos::CommandLineProcessor clp;
45 clp.addOutputSetupOptions(true);
46 clp.setOption("halo-expansions", &num_halo_expansions ,
47 "Number of times to expand the halo for Additive Schwarz");
48 switch (clp.parse(argc, argv)) {
49 case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS;
50 case Teuchos::CommandLineProcessor::PARSE_ERROR:
51 case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE;
52 case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break;
53 }
54
55 using LO = Tpetra::Map<>::local_ordinal_type;
56 using GO = Tpetra::Map<>::global_ordinal_type;
57 using Node = Tpetra::Map<>::node_type;
58 using MAT = Tpetra::CrsMatrix<>;
59 using Scalar = MAT::scalar_type;
60 using ST = Teuchos::ScalarTraits<Scalar>;
61 //using MV = Tpetra::MultiVector<>;
62 using IMP = Tpetra::Import<>;
63
64 const GO ONE = Teuchos::OrdinalTraits<GO>::one();
65 const GO GO_INVALID = Teuchos::OrdinalTraits<GO>::invalid();
66
67 RCP<MAT> A;
68 {
69 TimeMonitor tm (*TimeMonitor::getNewTimer("1) Matrix Fill"));
70
71 const size_t numImages = comm->getSize();
72 const size_t myImageID = comm->getRank();
73
74 if (numImages < 2) return -1;
75 // create a Map
76 RCP<const Tpetra::Map<> > map =
77 Tpetra::createContigMapWithNode<LO,GO,Node>(GO_INVALID, ONE, comm);
78 /* create the following matrix:
79 [2 1 ]
80 [1 1 1 ]
81 [ 1 1 1 ]
82 [ . . . ]
83 [ . . . ]
84 [ . . . ]
85 [ 1 1 1]
86 [ 1 2]
87 this matrix has an eigenvalue lambda=3, with eigenvector v = [1 ... 1]
88 */
89 A = rcp (new MAT(map, 3));
90 if (myImageID == 0) {
91 Array<Scalar> vals(tuple<Scalar>(static_cast<Scalar>(2)*ST::one(), ST::one()));
92 Array<GO> cols(tuple<GO>(myImageID, myImageID+1));
93 A->insertGlobalValues(myImageID,cols(),vals());
94 }
95 else if (myImageID == numImages-1) {
96 Array<Scalar> vals(tuple<Scalar>(ST::one(), static_cast<Scalar>(2)*ST::one()));
97 Array<GO> cols(tuple<GO>(myImageID-1,myImageID));
98 A->insertGlobalValues(myImageID,cols(),vals());
99 }
100 else {
101 Array<Scalar> vals(3,ST::one());
102 Array<GO> cols(tuple<GO>(myImageID-1, myImageID, myImageID+1));
103 A->insertGlobalValues(myImageID,cols(),vals());
104 }
105 A->fillComplete();
106 }
107
108 RCP<MAT> Mold, Mnew;
109 {
110 TimeMonitor tm (*TimeMonitor::getNewTimer("2) Halo Generation"));
111
112 Mold = A;
113 Mnew = Mold;
114
115 for (int i=0; i<num_halo_expansions; ++i) {
116 RCP<const IMP> rowImporter = Mold->getGraph()->getImporter();
117 Mnew = Tpetra::importAndFillCompleteCrsMatrix<MAT>(Mold,*rowImporter);
118 Mold = Mnew;
119 }
120 }
121
122 #if DEBUG_OUTPUT
123 int rank = comm->getRank();
124 {
125 std::ofstream ofs1( std::string("orig.") + std::to_string(rank) + std::string(".dat") );
126 auto out1 = Teuchos::getFancyOStream(Teuchos::rcpFromRef(ofs1));
127 A.describe(*out1,Teuchos::VERB_EXTREME);
128 }
129 {
130 std::ofstream ofs1( std::string("new.") + std::to_string(rank) + std::string(".dat") );
131 auto out1 = Teuchos::getFancyOStream(Teuchos::rcpFromRef(ofs1));
132 Mnew->describe(*out1,Teuchos::VERB_EXTREME);
133 }
134 #endif
135
136 if(comm->getRank() == 0) {
137 // Tell the Trilinos test framework that the test passed.
138 std::cout << "End Result: TEST PASSED" << std::endl;
139 }
140 }
141 return EXIT_SUCCESS;
142 } // END main()