Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
partition2DMatrix.cpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
46 
49 // #include <Zoltan2_XpetraCrsGraphAdapter.hpp>
50 // #include <Zoltan2_XpetraMultiVectorAdapter.hpp>
51 #include <Zoltan2_TestHelpers.hpp>
52 // #include <iostream>
53 // #include <limits>
54 // #include <Teuchos_ParameterList.hpp>
55 // #include <Teuchos_RCP.hpp>
56 // #include <Teuchos_FancyOStream.hpp>
57 // #include <Teuchos_CommandLineProcessor.hpp>
58 // #include <Tpetra_CrsMatrix.hpp>
59 // #include <Tpetra_Vector.hpp>
60 // #include <MatrixMarket_Tpetra.hpp>
61 
62 using Teuchos::RCP;
63 
65 // Program to test Zoltan2 2D partitioning of a TPetra matrix
66 // (read from a MatrixMarket file) -- modified from partitioning1 test.
67 // Usage:
68 // a.out [--inputFile=filename] [--outputFile=outfile] [--verbose]
69 // Karen Devine, 2011
71 
73 // Eventually want to use Teuchos unit tests to vary z2TestLO and
74 // GO. For now, we set them at compile time based on whether Tpetra
75 // is built with explicit instantiation on. (in Zoltan2_TestHelpers.hpp)
76 
77 typedef zlno_t z2TestLO;
78 typedef zgno_t z2TestGO;
80 
81 typedef Tpetra::CrsMatrix<z2TestScalar, z2TestLO, z2TestGO> SparseMatrix;
82 typedef Tpetra::CrsGraph<z2TestLO, z2TestGO> SparseGraph;
83 typedef Tpetra::Vector<z2TestScalar, z2TestLO, z2TestGO> Vector;
84 typedef Vector::node_type Node;
85 
87 //typedef Zoltan2::TpetraRowMatrixAdapter<SparseMatrix> SparseMatrixAdapter;
88 
89 //typedef Zoltan2::XpetraCrsGraphAdapter<SparseGraph> SparseGraphAdapter;
91 
92 
93 // Integer vector
94 typedef Tpetra::Vector<int, z2TestLO, z2TestGO> IntVector;
96 
97 #define epsilon 0.00000001
98 #define NNZ_IDX 1
99 
101 int main(int narg, char** arg)
102 {
103  std::string inputFile = ""; // Matrix Market or Zoltan file to read
104  // std::string outputFile = ""; // Matrix Market or Zoltan file to write
105  std::string inputPath = testDataFilePath; // Directory with input file
106 
107 
108 
109  std::string method = "scotch";
110  bool verbose = false; // Verbosity of output
111  bool distributeInput = true;
112  // bool haveFailure = false;
113  // int nVwgts = 0;
114  // int nEwgts = 0;
115  int testReturn = 0;
116 
118  Tpetra::ScopeGuard tscope(&narg, &arg);
119  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
120  int me = comm->getRank();
121 
122  // Read run-time options.
123  Teuchos::CommandLineProcessor cmdp (false, false);
124  cmdp.setOption("inputPath", &inputPath,
125  "Path to the MatrixMarket or Zoltan file to be read; ",true);
126 
127  cmdp.setOption("inputFile", &inputFile,
128  "Name of the Matrix Market or Zoltan file to read; ",true);
129  // cmdp.setOption("outputFile", &outputFile,
130  // "Name of the Matrix Market sparse matrix file to write, "
131  // "echoing the input/generated matrix.");
132  // cmdp.setOption("method", &method,
133  // "Partitioning method to use: scotch or parmetis.");
134  cmdp.setOption("verbose", "quiet", &verbose,
135  "Print messages and results.");
136  // cmdp.setOption("distribute", "no-distribute", &distributeInput,
137  // "indicate whether or not to distribute "
138  // "input across the communicator");
139 
140 
141  cmdp.parse(narg, arg);
142 
143  RCP<UserInputForTests> uinput;
144 
145  // Input file specified; read a matrix
146  uinput = rcp(new UserInputForTests(inputPath, inputFile, comm,true, distributeInput));
147 
148  RCP<SparseMatrix> origMatrix = uinput->getUITpetraCrsMatrix();
149 
150  if (origMatrix->getGlobalNumRows() < 40)
151  {
152  Teuchos::FancyOStream out(Teuchos::rcp(&std::cout,false));
153  origMatrix->describe(out, Teuchos::VERB_EXTREME);
154  }
155 
156 
157  if (me == 0)
158  {
159  std::cout << "NumRows = " << origMatrix->getGlobalNumRows() << std::endl
160  << "NumNonzeros = " << origMatrix->getGlobalNumEntries() << std::endl
161  << "NumProcs = " << comm->getSize() << std::endl
162  << "NumLocalRows (rank 0) = " << origMatrix->getLocalNumRows() << std::endl;
163  }
164 
165 
166  // ////// Create vectors to use with matrices
167  // // Don't use for now, wait until distribution is done.
168  // RCP<Vector> origVector, origProd;
169  // origProd = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
170  // origMatrix->getRangeMap());
171  // origVector = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
172  // origMatrix->getDomainMap());
173  // origVector->randomize();
174 
176  Teuchos::ParameterList params;
177 
178  params.set("partitioning_approach", "partition");
179 
180 
181  // TODO this needs to be added back once parameter is properly validated
182  // params.set("algorithm", "2D Cartesian");
183 
184 
186  // SparseGraphAdapter adapter(origMatrix->getCrsGraph(), nVwgts, nEwgts);
187 
188  SparseMatrixAdapter adapter(origMatrix, 0);
189 
190  // Zoltan2::TpetraRowMatrixAdapter< User, UserCoord >::TpetraRowMatrixAdapter(const RCP< const User > & inmatrix,
191  // int nWeightsPerRow = 0
192  // )
193 
196 
197  try {
198  if (me == 0) std::cout << "Calling solve() " << std::endl;
199 
200  problem.solve();
201 
202  if (me == 0) std::cout << "Done solve() " << std::endl;
203  }
204 
205  catch (std::runtime_error &e)
206  {
207  std::cout << "Runtime exception returned from solve(): " << e.what();
208  if (!strncmp(e.what(), "BUILD ERROR", 11)) {
209  // Catching build errors as exceptions is OK in the tests
210  std::cout << " PASS" << std::endl;
211  return 0;
212  }
213  else {
214  // All other runtime_errors are failures
215  std::cout << " FAIL" << std::endl;
216  return -1;
217  }
218  }
219  catch (std::logic_error &e) {
220  std::cout << "Logic exception returned from solve(): " << e.what()
221  << " FAIL" << std::endl;
222  return -1;
223  }
224  catch (std::bad_alloc &e) {
225  std::cout << "Bad_alloc exception returned from solve(): " << e.what()
226  << " FAIL" << std::endl;
227  return -1;
228  }
229  catch (std::exception &e) {
230  std::cout << "Unknown exception returned from solve(). " << e.what()
231  << " FAIL" << std::endl;
232  return -1;
233  }
234 
235 
236 
237 
238 // // Don't distribute yet
239 
240 // ////// Redistribute matrix and vector into new matrix and vector.
241 // if (me == 0) std::cout << "Redistributing matrix..." << std::endl;
242 // SparseMatrix *redistribMatrix;
243 // SparseMatrixAdapter adapterMatrix(origMatrix);
244 // adapterMatrix.applyPartitioningSolution(*origMatrix, redistribMatrix,
245 // problem.getSolution());
246 // if (redistribMatrix->getGlobalNumRows() < 40) {
247 // Teuchos::FancyOStream out(Teuchos::rcp(&std::cout,false));
248 // redistribMatrix->describe(out, Teuchos::VERB_EXTREME);
249 // }
250 
251 // if (me == 0) std::cout << "Redistributing vectors..." << std::endl;
252 // Vector *redistribVector;
253 // // std::vector<const zscalar_t *> weights;
254 // // std::vector<int> weightStrides;
255 // MultiVectorAdapter adapterVector(origVector); //, weights, weightStrides);
256 // adapterVector.applyPartitioningSolution(*origVector, redistribVector,
257 // problem.getSolution());
258 
259 // RCP<Vector> redistribProd;
260 // redistribProd = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
261 // redistribMatrix->getRangeMap());
262 
263 // // Test redistributing an integer vector with the same solution.
264 // // This test is mostly to make sure compilation always works.
265 // RCP<IntVector> origIntVec;
266 // IntVector *redistIntVec;
267 // origIntVec = Tpetra::createVector<int,z2TestLO,z2TestGO>(
268 // origMatrix->getRangeMap());
269 // for (size_t i = 0; i < origIntVec->getLocalLength(); i++)
270 // origIntVec->replaceLocalValue(i, me);
271 
272 // IntVectorAdapter int_vec_adapter(origIntVec);
273 // int_vec_adapter.applyPartitioningSolution(*origIntVec, redistIntVec,
274 // problem.getSolution());
275 // int origIntNorm = origIntVec->norm1();
276 // int redistIntNorm = redistIntVec->norm1();
277 // if (me == 0) std::cout << "IntegerVectorTest: " << origIntNorm << " == "
278 // << redistIntNorm << " ?";
279 // if (origIntNorm != redistIntNorm) {
280 // if (me == 0) std::cout << " FAIL" << std::endl;
281 // haveFailure = true;
282 // }
283 // else if (me == 0) std::cout << " OK" << std::endl;
284 // delete redistIntVec;
285 
286 // ////// Verify that redistribution is "correct"; perform matvec with
287 // ////// original and redistributed matrices/vectors and compare norms.
288 
289 // if (me == 0) std::cout << "Matvec original..." << std::endl;
290 // origMatrix->apply(*origVector, *origProd);
291 // z2TestScalar origNorm = origProd->norm2();
292 // if (me == 0)
293 // std::cout << "Norm of Original matvec prod: " << origNorm << std::endl;
294 
295 // if (me == 0) std::cout << "Matvec redistributed..." << std::endl;
296 // redistribMatrix->apply(*redistribVector, *redistribProd);
297 // z2TestScalar redistribNorm = redistribProd->norm2();
298 // if (me == 0)
299 // std::cout << "Norm of Redistributed matvec prod: " << redistribNorm << std::endl;
300 
301 // if (redistribNorm > origNorm+epsilon || redistribNorm < origNorm-epsilon) {
302 // testReturn = 1;
303 // haveFailure = true;
304 // }
305 
306 // delete redistribVector;
307 // delete redistribMatrix;
308 
309 // if (me == 0) {
310 // if (testReturn) {
311 // std::cout << "Mat-Vec product changed; FAIL" << std::endl;
312 // haveFailure = true;
313 // }
314 // if (!haveFailure)
315 // std::cout << "PASS" << std::endl;
316 // }
317 
318  std::cout << "Finished" << std::endl;
319 
320  return testReturn;
321 }
zgno_t z2TestGO
Definition: coloring1.cpp:76
zlno_t z2TestLO
Definition: coloring1.cpp:75
MatrixPartitioningProblem sets up partitioning problems for the user.
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
Tpetra::CrsMatrix< z2TestScalar, z2TestLO, z2TestGO > SparseMatrix
Definition: coloring1.cpp:79
int main(int narg, char **arg)
Definition: coloring1.cpp:199
common code used by tests
Tpetra::Vector< z2TestScalar, z2TestLO, z2TestGO > Vector
Definition: coloring1.cpp:80
Defines the XpetraCrsMatrixAdapter class.
Defines the MatrixPartitioningProblem class.
An adapter for Xpetra::MultiVector.
Tpetra::Map::local_ordinal_type zlno_t
Zoltan2::XpetraMultiVectorAdapter< IntVector > IntVectorAdapter
Tpetra::CrsGraph< z2TestLO, z2TestGO > SparseGraph
void solve(bool updateInputData=true)
Direct the problem to create a solution.
Zoltan2::XpetraCrsMatrixAdapter< SparseMatrix > SparseMatrixAdapter
Definition: coloring1.cpp:85
Tpetra::Vector< int, z2TestLO, z2TestGO > IntVector
zscalar_t z2TestScalar
Definition: coloring1.cpp:77
float zscalar_t
Zoltan2::XpetraMultiVectorAdapter< Vector > MultiVectorAdapter
Tpetra::Map::global_ordinal_type zgno_t
Vector::node_type Node
Definition: coloring1.cpp:81
std::string testDataFilePath(".")