Debian Codegraph @

packages/muelu/test/scaling/SpMVPerformance.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 <iostream>
11
12 #include "Teuchos_RCP.hpp"
13 #include "Teuchos_DefaultComm.hpp"
14 #include "Teuchos_TimeMonitor.hpp"
15 #include "Teuchos_StackedTimer.hpp"
16
17 #include "MatrixLoad.hpp"
18
19 class Measurement {
20 public:
21 virtual void measure() = 0;
22 };
23
24 class Reporter {
25 public:
26 Reporter(std::string n)
27 : name(n)
28 , comm(Teuchos::DefaultComm<int>::getComm())
29 , out(Teuchos::rcpFromRef(std::cout))
30 , timer(Teuchos::rcp(new Teuchos::StackedTimer(name.c_str()))) {
31 out.setOutputToRootOnly(0);
32 Teuchos::TimeMonitor::setStackedTimer(timer);
33 }
34
35 void record(Measurement& measurement) const {
36 comm->barrier();
37 measurement.measure();
38 }
39
40 void finalReport() {
41 timer->stopBaseTimer();
42
43 reportCommandLine();
44 reportXML();
45
46 out << "Run Complete!" << std::endl;
47 }
48
49 private:
50 void reportCommandLine() {
51 Teuchos::StackedTimer::OutputOptions options;
52 options.output_fraction = true;
53 options.output_histogram = true;
54 options.output_minmax = true;
55
56 timer->report(out, comm, options);
57 }
58
59 void reportXML() {
60 std::string xmlName = name + " " + std::to_string(comm->getSize()) + " ranks";
61 auto xmlOut = timer->reportWatchrXML(xmlName, comm);
62
63 if (xmlOut.length()) {
64 out << std::endl
65 << "Created Watchr performance report " << xmlOut << std::endl;
66 } else {
67 out << std::endl
68 << "Did not create Watchr performance report" << std::endl;
69 }
70 }
71
72 const std::string name;
73 Teuchos::RCP<const Teuchos::Comm<int>> comm;
74 Teuchos::FancyOStream out;
75 Teuchos::RCP<Teuchos::StackedTimer> timer;
76 };
77
78 class System {
79 public:
80 virtual void apply() = 0;
81 virtual std::string name() const = 0;
82 };
83
84 class SpMVMeasurement : public Measurement {
85 public:
86 SpMVMeasurement(System& sys, int n)
87 : numRuns(n)
88 , system(sys)
89 , name(sys.name() + " SpMV") {}
90
91 void measure() override {
92 for (int i = 0; i < numRuns; i++) {
93 Teuchos::TimeMonitor subTimer(*Teuchos::TimeMonitor::getNewTimer(name));
94 system.apply();
95 }
96 }
97
98 private:
99 const int numRuns;
100 System& system;
101 const std::string name;
102 };
103
104 std::string systemName(const Xpetra::UnderlyingLib& lib) {
105 if (lib == Xpetra::UseEpetra) return "Epetra";
106 if (lib == Xpetra::UseTpetra) return "Tpetra";
107 return "Unknown";
108 }
109
110 template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Node>
111 class LinearSystem : public System {
112 using MultiVector = Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
113 using Matrix = Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
114
115 public:
116 LinearSystem(Xpetra::UnderlyingLib l, int n)
117 : lib(l)
118 , numVectors(n) {}
119
120 void apply() override {
121 A->apply(*X, *B);
122 }
123
124 std::string name() const override {
125 return systemName(lib);
126 }
127
128 Xpetra::UnderlyingLib lib;
129 int numVectors;
130
131 Teuchos::RCP<Matrix> A;
132 Teuchos::RCP<MultiVector> X;
133 Teuchos::RCP<MultiVector> B;
134 };
135
136 template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Node>
137 class SystemLoader {
138 using Map = Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>;
139 using MultiVector = Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
140 using CoordScalar = typename Teuchos::ScalarTraits<Scalar>::coordinateType;
141 using RealValuedMultiVector = Xpetra::MultiVector<CoordScalar, LocalOrdinal, GlobalOrdinal, Node>;
142
143 public:
144 SystemLoader(Teuchos::CommandLineProcessor& c)
145 : clp(c)
146 , comm(Teuchos::DefaultComm<int>::getComm())
147 , galeriParameters(clp, 100, 100, 100, "Laplace2D")
148 , xpetraParameters(clp) {}
149
150 void fill(LinearSystem<Scalar, LocalOrdinal, GlobalOrdinal, Node>& system) {
151 MatrixLoad<Scalar, LocalOrdinal, GlobalOrdinal, Node>(
152 comm, system.lib,
153 binaryFormat, matrixFile, rhsFile, rowMapFile, colMapFile,
154 domainMapFile, rangeMapFile, coordFile, coordMapFile, nullFile, materialFile,
155 map, system.A,
156 coordinates, nullspace, material,
157 system.X, system.B, system.numVectors,
158 galeriParameters, xpetraParameters,
159 galeriStream);
160 }
161
162 private:
163 Teuchos::CommandLineProcessor& clp;
164 Teuchos::RCP<const Teuchos::Comm<int>> comm;
165 Galeri::Xpetra::Parameters<GlobalOrdinal> galeriParameters;
166 Xpetra::Parameters xpetraParameters;
167
168 bool binaryFormat;
169 std::string matrixFile = "";
170 std::string rhsFile = "";
171 std::string rowMapFile = "";
172 std::string colMapFile = "";
173 std::string domainMapFile = "";
174 std::string rangeMapFile = "";
175 std::string coordFile = "";
176 std::string coordMapFile = "";
177 std::string nullFile = "";
178 std::string materialFile = "";
179
180 Teuchos::RCP<const Map> map;
181 Teuchos::RCP<RealValuedMultiVector> coordinates;
182 Teuchos::RCP<MultiVector> nullspace;
183 Teuchos::RCP<MultiVector> material;
184 std::ostringstream galeriStream;
185 };
186
187 template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Node>
188 int main_ETI(Teuchos::CommandLineProcessor& clp, Xpetra::UnderlyingLib& lib, int argc, char* argv[]) {
189 Reporter reporter("SpMV Performance " + systemName(lib));
190 SystemLoader<Scalar, LocalOrdinal, GlobalOrdinal, Node> systemLoader(clp);
191
192 int numRuns = 1;
193 clp.setOption("num-runs", &numRuns, "number of times to run operation");
194
195 int numVectors = 1;
196 clp.setOption("multivector", &numVectors, "number of rhs to solve simultaneously");
197
198 clp.recogniseAllOptions(true);
199 switch (clp.parse(argc, argv)) {
200 case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS;
201 case Teuchos::CommandLineProcessor::PARSE_ERROR:
202 case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE;
203 case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break;
204 }
205
206 LinearSystem<Scalar, LocalOrdinal, GlobalOrdinal, Node> system(lib, numVectors);
207 systemLoader.fill(system);
208
209 SpMVMeasurement SpMV(system, numRuns);
210 reporter.record(SpMV);
211
212 reporter.finalReport();
213 return 0;
214 }
215
216 #define MUELU_AUTOMATIC_TEST_ETI_NAME main_ETI
217 #include "MueLu_Test_ETI.hpp"
218
219 int main(int argc, char* argv[]) {
220 return Automatic_Test_ETI(argc, argv);
221 }