Zoltan2
Loading...
Searching...
No Matches
partition2DMatrix.cpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Zoltan2: A package of combinatorial algorithms for scientific computing
4//
5// Copyright 2012 NTESS and the Zoltan2 contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
12// #include <Zoltan2_XpetraCrsGraphAdapter.hpp>
13// #include <Zoltan2_XpetraMultiVectorAdapter.hpp>
15// #include <iostream>
16// #include <limits>
17// #include <Teuchos_ParameterList.hpp>
18// #include <Teuchos_RCP.hpp>
19// #include <Teuchos_FancyOStream.hpp>
20// #include <Teuchos_CommandLineProcessor.hpp>
21// #include <Tpetra_CrsMatrix.hpp>
22// #include <Tpetra_Vector.hpp>
23// #include <MatrixMarket_Tpetra.hpp>
24
25using Teuchos::RCP;
26
28// Program to test Zoltan2 2D partitioning of a TPetra matrix
29// (read from a MatrixMarket file) -- modified from partitioning1 test.
30// Usage:
31// a.out [--inputFile=filename] [--outputFile=outfile] [--verbose]
32// Karen Devine, 2011
34
36// Eventually want to use Teuchos unit tests to vary z2TestLO and
37// GO. For now, we set them at compile time based on whether Tpetra
38// is built with explicit instantiation on. (in Zoltan2_TestHelpers.hpp)
39
43
44typedef Tpetra::CrsMatrix<z2TestScalar, z2TestLO, z2TestGO> SparseMatrix;
45typedef Tpetra::CrsGraph<z2TestLO, z2TestGO> SparseGraph;
46typedef Tpetra::Vector<z2TestScalar, z2TestLO, z2TestGO> Vector;
47typedef Vector::node_type Node;
48
50//typedef Zoltan2::TpetraRowMatrixAdapter<SparseMatrix> SparseMatrixAdapter;
51
52//typedef Zoltan2::XpetraCrsGraphAdapter<SparseGraph> SparseGraphAdapter;
54
55
56// Integer vector
57typedef Tpetra::Vector<int, z2TestLO, z2TestGO> IntVector;
59
60#define epsilon 0.00000001
61#define NNZ_IDX 1
62
64int main(int narg, char** arg)
65{
66 std::string inputFile = ""; // Matrix Market or Zoltan file to read
67 // std::string outputFile = ""; // Matrix Market or Zoltan file to write
68 std::string inputPath = testDataFilePath; // Directory with input file
69
70
71
72 std::string method = "scotch";
73 bool verbose = false; // Verbosity of output
74 bool distributeInput = true;
75 // bool haveFailure = false;
76 // int nVwgts = 0;
77 // int nEwgts = 0;
78 int testReturn = 0;
79
81 Tpetra::ScopeGuard tscope(&narg, &arg);
82 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
83 int me = comm->getRank();
84
85 // Read run-time options.
86 Teuchos::CommandLineProcessor cmdp (false, false);
87 cmdp.setOption("inputPath", &inputPath,
88 "Path to the MatrixMarket or Zoltan file to be read; ",true);
89
90 cmdp.setOption("inputFile", &inputFile,
91 "Name of the Matrix Market or Zoltan file to read; ",true);
92 // cmdp.setOption("outputFile", &outputFile,
93 // "Name of the Matrix Market sparse matrix file to write, "
94 // "echoing the input/generated matrix.");
95 // cmdp.setOption("method", &method,
96 // "Partitioning method to use: scotch or parmetis.");
97 cmdp.setOption("verbose", "quiet", &verbose,
98 "Print messages and results.");
99 // cmdp.setOption("distribute", "no-distribute", &distributeInput,
100 // "indicate whether or not to distribute "
101 // "input across the communicator");
102
103
104 cmdp.parse(narg, arg);
105
106 RCP<UserInputForTests> uinput;
107
108 // Input file specified; read a matrix
109 uinput = rcp(new UserInputForTests(inputPath, inputFile, comm,true, distributeInput));
110
111 RCP<SparseMatrix> origMatrix = uinput->getUITpetraCrsMatrix();
112
113 if (origMatrix->getGlobalNumRows() < 40)
114 {
115 Teuchos::FancyOStream out(Teuchos::rcp(&std::cout,false));
116 origMatrix->describe(out, Teuchos::VERB_EXTREME);
117 }
118
119
120 if (me == 0)
121 {
122 std::cout << "NumRows = " << origMatrix->getGlobalNumRows() << std::endl
123 << "NumNonzeros = " << origMatrix->getGlobalNumEntries() << std::endl
124 << "NumProcs = " << comm->getSize() << std::endl
125 << "NumLocalRows (rank 0) = " << origMatrix->getLocalNumRows() << std::endl;
126 }
127
128
129 // ////// Create vectors to use with matrices
130 // // Don't use for now, wait until distribution is done.
131 // RCP<Vector> origVector, origProd;
132 // origProd = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
133 // origMatrix->getRangeMap());
134 // origVector = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
135 // origMatrix->getDomainMap());
136 // origVector->randomize();
137
139 Teuchos::ParameterList params;
140
141 params.set("partitioning_approach", "partition");
142
143
144 // TODO this needs to be added back once parameter is properly validated
145 // params.set("algorithm", "2D Cartesian");
146
147
149 // SparseGraphAdapter adapter(origMatrix->getCrsGraph(), nVwgts, nEwgts);
150
151 SparseMatrixAdapter adapter(origMatrix, 0);
152
153 // Zoltan2::TpetraRowMatrixAdapter< User, UserCoord >::TpetraRowMatrixAdapter(const RCP< const User > & inmatrix,
154 // int nWeightsPerRow = 0
155 // )
156
159
160 try {
161 if (me == 0) std::cout << "Calling solve() " << std::endl;
162
163 problem.solve();
164
165 if (me == 0) std::cout << "Done solve() " << std::endl;
166 }
167
168 catch (std::runtime_error &e)
169 {
170 std::cout << "Runtime exception returned from solve(): " << e.what();
171 if (!strncmp(e.what(), "BUILD ERROR", 11)) {
172 // Catching build errors as exceptions is OK in the tests
173 std::cout << " PASS" << std::endl;
174 return 0;
175 }
176 else {
177 // All other runtime_errors are failures
178 std::cout << " FAIL" << std::endl;
179 return -1;
180 }
181 }
182 catch (std::logic_error &e) {
183 std::cout << "Logic exception returned from solve(): " << e.what()
184 << " FAIL" << std::endl;
185 return -1;
186 }
187 catch (std::bad_alloc &e) {
188 std::cout << "Bad_alloc exception returned from solve(): " << e.what()
189 << " FAIL" << std::endl;
190 return -1;
191 }
192 catch (std::exception &e) {
193 std::cout << "Unknown exception returned from solve(). " << e.what()
194 << " FAIL" << std::endl;
195 return -1;
196 }
197
198
199
200
201// // Don't distribute yet
202
203// ////// Redistribute matrix and vector into new matrix and vector.
204// if (me == 0) std::cout << "Redistributing matrix..." << std::endl;
205// SparseMatrix *redistribMatrix;
206// SparseMatrixAdapter adapterMatrix(origMatrix);
207// adapterMatrix.applyPartitioningSolution(*origMatrix, redistribMatrix,
208// problem.getSolution());
209// if (redistribMatrix->getGlobalNumRows() < 40) {
210// Teuchos::FancyOStream out(Teuchos::rcp(&std::cout,false));
211// redistribMatrix->describe(out, Teuchos::VERB_EXTREME);
212// }
213
214// if (me == 0) std::cout << "Redistributing vectors..." << std::endl;
215// Vector *redistribVector;
216// // std::vector<const zscalar_t *> weights;
217// // std::vector<int> weightStrides;
218// MultiVectorAdapter adapterVector(origVector); //, weights, weightStrides);
219// adapterVector.applyPartitioningSolution(*origVector, redistribVector,
220// problem.getSolution());
221
222// RCP<Vector> redistribProd;
223// redistribProd = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
224// redistribMatrix->getRangeMap());
225
226// // Test redistributing an integer vector with the same solution.
227// // This test is mostly to make sure compilation always works.
228// RCP<IntVector> origIntVec;
229// IntVector *redistIntVec;
230// origIntVec = Tpetra::createVector<int,z2TestLO,z2TestGO>(
231// origMatrix->getRangeMap());
232// for (size_t i = 0; i < origIntVec->getLocalLength(); i++)
233// origIntVec->replaceLocalValue(i, me);
234
235// IntVectorAdapter int_vec_adapter(origIntVec);
236// int_vec_adapter.applyPartitioningSolution(*origIntVec, redistIntVec,
237// problem.getSolution());
238// int origIntNorm = origIntVec->norm1();
239// int redistIntNorm = redistIntVec->norm1();
240// if (me == 0) std::cout << "IntegerVectorTest: " << origIntNorm << " == "
241// << redistIntNorm << " ?";
242// if (origIntNorm != redistIntNorm) {
243// if (me == 0) std::cout << " FAIL" << std::endl;
244// haveFailure = true;
245// }
246// else if (me == 0) std::cout << " OK" << std::endl;
247// delete redistIntVec;
248
249// ////// Verify that redistribution is "correct"; perform matvec with
250// ////// original and redistributed matrices/vectors and compare norms.
251
252// if (me == 0) std::cout << "Matvec original..." << std::endl;
253// origMatrix->apply(*origVector, *origProd);
254// z2TestScalar origNorm = origProd->norm2();
255// if (me == 0)
256// std::cout << "Norm of Original matvec prod: " << origNorm << std::endl;
257
258// if (me == 0) std::cout << "Matvec redistributed..." << std::endl;
259// redistribMatrix->apply(*redistribVector, *redistribProd);
260// z2TestScalar redistribNorm = redistribProd->norm2();
261// if (me == 0)
262// std::cout << "Norm of Redistributed matvec prod: " << redistribNorm << std::endl;
263
264// if (redistribNorm > origNorm+epsilon || redistribNorm < origNorm-epsilon) {
265// testReturn = 1;
266// haveFailure = true;
267// }
268
269// delete redistribVector;
270// delete redistribMatrix;
271
272// if (me == 0) {
273// if (testReturn) {
274// std::cout << "Mat-Vec product changed; FAIL" << std::endl;
275// haveFailure = true;
276// }
277// if (!haveFailure)
278// std::cout << "PASS" << std::endl;
279// }
280
281 std::cout << "Finished" << std::endl;
282
283 return testReturn;
284}
Defines the MatrixPartitioningProblem class.
common code used by tests
float zscalar_t
Tpetra::Map ::local_ordinal_type zlno_t
std::string testDataFilePath(".")
Tpetra::Map ::global_ordinal_type zgno_t
Defines the XpetraCrsMatrixAdapter class.
int main()
MatrixPartitioningProblem sets up partitioning problems for the user.
void solve(bool updateInputData=true)
Direct the problem to create a solution.
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
Vector::node_type Node
zlno_t z2TestLO
Tpetra::CrsGraph< z2TestLO, z2TestGO > SparseGraph
Zoltan2::XpetraMultiVectorAdapter< IntVector > IntVectorAdapter
Tpetra::CrsMatrix< z2TestScalar, z2TestLO, z2TestGO > SparseMatrix
Zoltan2::XpetraMultiVectorAdapter< Vector > MultiVectorAdapter
Zoltan2::XpetraCrsMatrixAdapter< SparseMatrix > SparseMatrixAdapter
Tpetra::Vector< z2TestScalar, z2TestLO, z2TestGO > Vector
zgno_t z2TestGO
Tpetra::Vector< int, z2TestLO, z2TestGO > IntVector
zscalar_t z2TestScalar