Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
partitioning1.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
49 #include <Zoltan2_TestHelpers.hpp>
50 #include <iostream>
51 #include <limits>
52 #include <Teuchos_ParameterList.hpp>
53 #include <Teuchos_RCP.hpp>
54 #include <Teuchos_FancyOStream.hpp>
55 #include <Teuchos_CommandLineProcessor.hpp>
56 #include <Tpetra_CrsMatrix.hpp>
57 #include <Tpetra_Vector.hpp>
58 #include <MatrixMarket_Tpetra.hpp>
59 
60 using Teuchos::RCP;
61 
63 // Program to demonstrate use of Zoltan2 to partition a TPetra matrix
64 // (read from a MatrixMarket file or generated by Galeri::Xpetra).
65 // Usage:
66 // a.out [--inputFile=filename] [--outputFile=outfile] [--verbose]
67 // [--x=#] [--y=#] [--z=#] [--matrix={Laplace1D,Laplace2D,Laplace3D}
68 // Karen Devine, 2011
70 
72 // Eventually want to use Teuchos unit tests to vary z2TestLO and
73 // GO. For now, we set them at compile time based on whether Tpetra
74 // is built with explicit instantiation on. (in Zoltan2_TestHelpers.hpp)
75 
76 typedef zlno_t z2TestLO;
77 typedef zgno_t z2TestGO;
79 
80 typedef Tpetra::CrsMatrix<z2TestScalar, z2TestLO, z2TestGO> SparseMatrix;
81 typedef Tpetra::CrsGraph<z2TestLO, z2TestGO> SparseGraph;
82 typedef Tpetra::Vector<z2TestScalar, z2TestLO, z2TestGO> Vector;
83 typedef Vector::node_type Node;
84 
88 
89 
90 // Integer vector
91 typedef Tpetra::Vector<int, z2TestLO, z2TestGO> IntVector;
93 
94 #define epsilon 0.00000001
95 #define NNZ_IDX 1
96 
98 int main(int narg, char** arg)
99 {
100  std::string inputFile = ""; // Matrix Market or Zoltan file to read
101  std::string outputFile = ""; // Matrix Market or Zoltan file to write
102  std::string inputPath = testDataFilePath; // Directory with input file
103  std::string method = "scotch";
104  bool verbose = false; // Verbosity of output
105  bool distributeInput = true;
106  bool haveFailure = false;
107  int nVwgts = 0;
108  int nEwgts = 0;
109  int testReturn = 0;
110 
112  Tpetra::ScopeGuard tscope(&narg, &arg);
113  RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
114  int me = comm->getRank();
115 
116  // Read run-time options.
117  Teuchos::CommandLineProcessor cmdp (false, false);
118  cmdp.setOption("inputPath", &inputPath,
119  "Path to the MatrixMarket or Zoltan file to be read; "
120  "if not specified, a default path will be used.");
121  cmdp.setOption("inputFile", &inputFile,
122  "Name of the Matrix Market or Zoltan file to read; "
123  "if not specified, a matrix will be generated by MueLu.");
124  cmdp.setOption("outputFile", &outputFile,
125  "Name of the Matrix Market sparse matrix file to write, "
126  "echoing the input/generated matrix.");
127  cmdp.setOption("method", &method,
128  "Partitioning method to use: scotch or parmetis.");
129  cmdp.setOption("vertexWeights", &nVwgts,
130  "Number of weights to generate for each vertex");
131  cmdp.setOption("edgeWeights", &nEwgts,
132  "Number of weights to generate for each edge");
133  cmdp.setOption("verbose", "quiet", &verbose,
134  "Print messages and results.");
135  cmdp.setOption("distribute", "no-distribute", &distributeInput,
136  "indicate whether or not to distribute "
137  "input across the communicator");
138 
140  // Even with cmdp option "true", I get errors for having these
141  // arguments on the command line. (On redsky build)
142  // KDDKDD Should just be warnings, right? Code should still work with these
143  // KDDKDD params in the create-a-matrix file. Better to have them where
144  // KDDKDD they are used.
145  int xdim=10;
146  int ydim=10;
147  int zdim=10;
148  std::string matrixType("Laplace3D");
149 
150  cmdp.setOption("x", &xdim,
151  "number of gridpoints in X dimension for "
152  "mesh used to generate matrix.");
153  cmdp.setOption("y", &ydim,
154  "number of gridpoints in Y dimension for "
155  "mesh used to generate matrix.");
156  cmdp.setOption("z", &zdim,
157  "number of gridpoints in Z dimension for "
158  "mesh used to generate matrix.");
159  cmdp.setOption("matrix", &matrixType,
160  "Matrix type: Laplace1D, Laplace2D, or Laplace3D");
162 
163  cmdp.parse(narg, arg);
164 
165  RCP<UserInputForTests> uinput;
166 
167  if (inputFile != "") // Input file specified; read a matrix
168  uinput = rcp(new UserInputForTests(inputPath, inputFile, comm,
169  true, distributeInput));
170 
171  else // Let MueLu generate a default matrix
172  uinput = rcp(new UserInputForTests(xdim, ydim, zdim, string(""), comm,
173  true, distributeInput));
174 
175  RCP<SparseMatrix> origMatrix = uinput->getUITpetraCrsMatrix();
176 
177  if (origMatrix->getGlobalNumRows() < 40) {
178  Teuchos::FancyOStream out(Teuchos::rcp(&std::cout,false));
179  origMatrix->describe(out, Teuchos::VERB_EXTREME);
180  }
181 
182 
183  if (outputFile != "") {
184  // Just a sanity check.
185  Tpetra::MatrixMarket::Writer<SparseMatrix>::writeSparseFile(outputFile,
186  origMatrix, verbose);
187  }
188 
189  if (me == 0)
190  std::cout << "NumRows = " << origMatrix->getGlobalNumRows() << std::endl
191  << "NumNonzeros = " << origMatrix->getGlobalNumEntries() << std::endl
192  << "NumProcs = " << comm->getSize() << std::endl
193  << "NumLocalRows (rank 0) = " << origMatrix->getNodeNumRows() << std::endl;
194 
196  RCP<Vector> origVector, origProd;
197  origProd = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
198  origMatrix->getRangeMap());
199  origVector = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
200  origMatrix->getDomainMap());
201  origVector->randomize();
202 
204  Teuchos::ParameterList params;
205 
206  params.set("partitioning_approach", "partition");
207  params.set("algorithm", method);
208 
210  SparseGraphAdapter adapter(origMatrix->getCrsGraph(), nVwgts, nEwgts);
211 
215 
216  zscalar_t *vwgts = NULL, *ewgts = NULL;
217  if (nVwgts) {
218  // Test vertex weights with stride nVwgts.
219  size_t nrows = origMatrix->getNodeNumRows();
220  if (nrows) {
221  vwgts = new zscalar_t[nVwgts * nrows];
222  for (size_t i = 0; i < nrows; i++) {
223  size_t idx = i * nVwgts;
224  vwgts[idx] = zscalar_t(origMatrix->getRowMap()->getGlobalElement(i))
225  ;// + zscalar_t(0.5);
226  for (int j = 1; j < nVwgts; j++) vwgts[idx+j] = 1.;
227  }
228  for (int j = 0; j < nVwgts; j++) {
229  if (j != NNZ_IDX) adapter.setVertexWeights(&vwgts[j], nVwgts, j);
230  else adapter.setVertexWeightIsDegree(NNZ_IDX);
231  }
232  }
233  }
234 
235  if (nEwgts) {
236  // Test edge weights with stride 1.
237  size_t nnz = origMatrix->getNodeNumEntries();
238  if (nnz) {
239  size_t nrows = origMatrix->getNodeNumRows();
240  size_t maxnzrow = origMatrix->getNodeMaxNumRowEntries();
241  ewgts = new zscalar_t[nEwgts * nnz];
242  size_t cnt = 0;
243  Array<z2TestGO> egids(maxnzrow);
244  Array<zscalar_t> evals(maxnzrow);
245  for (size_t i = 0; i < nrows; i++) {
246  size_t nnzinrow;
247  z2TestGO gid = origMatrix->getRowMap()->getGlobalElement(i);
248  origMatrix->getGlobalRowCopy(gid, egids(), evals(), nnzinrow);
249  for (size_t k = 0; k < nnzinrow; k++) {
250  ewgts[cnt] = (gid < egids[k] ? gid : egids[k]);
251  if (nEwgts > 1) ewgts[cnt+nnz] = (gid < egids[k] ? egids[k] : gid);
252  for (int j = 2; j < nEwgts; j++) ewgts[cnt+nnz*j] = 1.;
253  cnt++;
254  }
255  }
256  for (int j = 0; j < nEwgts; j++) {
257  adapter.setEdgeWeights(&ewgts[j*nnz], 1, j);
258  }
259  }
260  }
261 
262 
264  Zoltan2::PartitioningProblem<SparseGraphAdapter> problem(&adapter, &params);
265 
266  try {
267  if (me == 0) std::cout << "Calling solve() " << std::endl;
268 
269  problem.solve();
270 
271  if (me == 0) std::cout << "Done solve() " << std::endl;
272  }
273  catch (std::runtime_error &e) {
274  delete [] vwgts;
275  delete [] ewgts;
276  std::cout << "Runtime exception returned from solve(): " << e.what();
277  if (!strncmp(e.what(), "BUILD ERROR", 11)) {
278  // Catching build errors as exceptions is OK in the tests
279  std::cout << " PASS" << std::endl;
280  return 0;
281  }
282  else {
283  // All other runtime_errors are failures
284  std::cout << " FAIL" << std::endl;
285  return -1;
286  }
287  }
288  catch (std::logic_error &e) {
289  delete [] vwgts;
290  delete [] ewgts;
291  std::cout << "Logic exception returned from solve(): " << e.what()
292  << " FAIL" << std::endl;
293  return -1;
294  }
295  catch (std::bad_alloc &e) {
296  delete [] vwgts;
297  delete [] ewgts;
298  std::cout << "Bad_alloc exception returned from solve(): " << e.what()
299  << " FAIL" << std::endl;
300  return -1;
301  }
302  catch (std::exception &e) {
303  delete [] vwgts;
304  delete [] ewgts;
305  std::cout << "Unknown exception returned from solve(). " << e.what()
306  << " FAIL" << std::endl;
307  return -1;
308  }
309 
312  size_t checkNparts = comm->getSize();
313 
314  size_t checkLength = origMatrix->getNodeNumRows();
315  const SparseGraphAdapter::part_t *checkParts = problem.getSolution().getPartListView();
316 
317  // Check for load balance
318  size_t *countPerPart = new size_t[checkNparts];
319  size_t *globalCountPerPart = new size_t[checkNparts];
320  zscalar_t *wtPerPart = (nVwgts ? new zscalar_t[checkNparts*nVwgts] : NULL);
321  zscalar_t *globalWtPerPart = (nVwgts ? new zscalar_t[checkNparts*nVwgts] : NULL);
322  for (size_t i = 0; i < checkNparts; i++) countPerPart[i] = 0;
323  for (size_t i = 0; i < checkNparts * nVwgts; i++) wtPerPart[i] = 0.;
324 
325  for (size_t i = 0; i < checkLength; i++) {
326  if (size_t(checkParts[i]) >= checkNparts)
327  std::cout << "Invalid Part " << checkParts[i] << ": FAIL" << std::endl;
328  countPerPart[checkParts[i]]++;
329  for (int j = 0; j < nVwgts; j++) {
330  if (j != NNZ_IDX)
331  wtPerPart[checkParts[i]*nVwgts+j] += vwgts[i*nVwgts+j];
332  else
333  wtPerPart[checkParts[i]*nVwgts+j] += origMatrix->getNumEntriesInLocalRow(i);
334  }
335  }
336  Teuchos::reduceAll<int, size_t>(*comm, Teuchos::REDUCE_SUM, checkNparts,
337  countPerPart, globalCountPerPart);
338  Teuchos::reduceAll<int, zscalar_t>(*comm, Teuchos::REDUCE_SUM,
339  checkNparts*nVwgts,
340  wtPerPart, globalWtPerPart);
341 
342  size_t min = std::numeric_limits<std::size_t>::max();
343  size_t max = 0;
344  size_t sum = 0;
345  size_t minrank = 0, maxrank = 0;
346  for (size_t i = 0; i < checkNparts; i++) {
347  if (globalCountPerPart[i] < min) {min = globalCountPerPart[i]; minrank = i;}
348  if (globalCountPerPart[i] > max) {max = globalCountPerPart[i]; maxrank = i;}
349  sum += globalCountPerPart[i];
350  }
351 
352  if (me == 0) {
353  float avg = (float) sum / (float) checkNparts;
354  std::cout << "Minimum count: " << min << " on rank " << minrank << std::endl;
355  std::cout << "Maximum count: " << max << " on rank " << maxrank << std::endl;
356  std::cout << "Average count: " << avg << std::endl;
357  std::cout << "Total count: " << sum
358  << (sum != origMatrix->getGlobalNumRows()
359  ? "Work was lost; FAIL"
360  : " ")
361  << std::endl;
362  std::cout << "Imbalance: " << max / avg << std::endl;
363  if (nVwgts) {
364  std::vector<zscalar_t> minwt(nVwgts, std::numeric_limits<zscalar_t>::max());
365  std::vector<zscalar_t> maxwt(nVwgts, 0.);
366  std::vector<zscalar_t> sumwt(nVwgts, 0.);
367  for (size_t i = 0; i < checkNparts; i++) {
368  for (int j = 0; j < nVwgts; j++) {
369  size_t idx = i*nVwgts+j;
370  if (globalWtPerPart[idx] < minwt[j]) minwt[j] = globalWtPerPart[idx];
371  if (globalWtPerPart[idx] > maxwt[j]) maxwt[j] = globalWtPerPart[idx];
372  sumwt[j] += globalWtPerPart[idx];
373  }
374  }
375  for (int j = 0; j < nVwgts; j++) {
376  float avgwt = (float) sumwt[j] / (float) checkNparts;
377  std::cout << std::endl;
378  std::cout << "Minimum weight[" << j << "]: " << minwt[j] << std::endl;
379  std::cout << "Maximum weight[" << j << "]: " << maxwt[j] << std::endl;
380  std::cout << "Average weight[" << j << "]: " << avgwt << std::endl;
381  std::cout << "Imbalance: " << maxwt[j] / avgwt << std::endl;
382  }
383  }
384  }
385 
386  delete [] countPerPart;
387  delete [] wtPerPart;
388  delete [] globalCountPerPart;
389  delete [] globalWtPerPart;
390  delete [] vwgts;
391  delete [] ewgts;
392 
393 
395  if (me == 0) std::cout << "Redistributing matrix..." << std::endl;
396  SparseMatrix *redistribMatrix;
397  SparseMatrixAdapter adapterMatrix(origMatrix);
398  adapterMatrix.applyPartitioningSolution(*origMatrix, redistribMatrix,
399  problem.getSolution());
400  if (redistribMatrix->getGlobalNumRows() < 40) {
401  Teuchos::FancyOStream out(Teuchos::rcp(&std::cout,false));
402  redistribMatrix->describe(out, Teuchos::VERB_EXTREME);
403  }
404 
405  if (me == 0) std::cout << "Redistributing vectors..." << std::endl;
406  Vector *redistribVector;
407 // std::vector<const zscalar_t *> weights;
408 // std::vector<int> weightStrides;
409  MultiVectorAdapter adapterVector(origVector); //, weights, weightStrides);
410  adapterVector.applyPartitioningSolution(*origVector, redistribVector,
411  problem.getSolution());
412 
413  RCP<Vector> redistribProd;
414  redistribProd = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
415  redistribMatrix->getRangeMap());
416 
417  // Test redistributing an integer vector with the same solution.
418  // This test is mostly to make sure compilation always works.
419  RCP<IntVector> origIntVec;
420  IntVector *redistIntVec;
421  origIntVec = Tpetra::createVector<int,z2TestLO,z2TestGO>(
422  origMatrix->getRangeMap());
423  for (size_t i = 0; i < origIntVec->getLocalLength(); i++)
424  origIntVec->replaceLocalValue(i, me);
425 
426  IntVectorAdapter int_vec_adapter(origIntVec);
427  int_vec_adapter.applyPartitioningSolution(*origIntVec, redistIntVec,
428  problem.getSolution());
429  int origIntNorm = origIntVec->norm1();
430  int redistIntNorm = redistIntVec->norm1();
431  if (me == 0) std::cout << "IntegerVectorTest: " << origIntNorm << " == "
432  << redistIntNorm << " ?";
433  if (origIntNorm != redistIntNorm) {
434  if (me == 0) std::cout << " FAIL" << std::endl;
435  haveFailure = true;
436  }
437  else if (me == 0) std::cout << " OK" << std::endl;
438  delete redistIntVec;
439 
442 
443  if (me == 0) std::cout << "Matvec original..." << std::endl;
444  origMatrix->apply(*origVector, *origProd);
445  z2TestScalar origNorm = origProd->norm2();
446  if (me == 0)
447  std::cout << "Norm of Original matvec prod: " << origNorm << std::endl;
448 
449  if (me == 0) std::cout << "Matvec redistributed..." << std::endl;
450  redistribMatrix->apply(*redistribVector, *redistribProd);
451  z2TestScalar redistribNorm = redistribProd->norm2();
452  if (me == 0)
453  std::cout << "Norm of Redistributed matvec prod: " << redistribNorm << std::endl;
454 
455  if (redistribNorm > origNorm+epsilon || redistribNorm < origNorm-epsilon) {
456  testReturn = 1;
457  haveFailure = true;
458  }
459 
460  delete redistribVector;
461  delete redistribMatrix;
462 
463  if (me == 0) {
464  if (testReturn) {
465  std::cout << "Mat-Vec product changed; FAIL" << std::endl;
466  haveFailure = true;
467  }
468  if (!haveFailure)
469  std::cout << "PASS" << std::endl;
470  }
471 
472  return testReturn;
473 }
Tpetra::Vector< z2TestScalar, z2TestLO, z2TestGO > Vector
zlno_t z2TestLO
zscalar_t z2TestScalar
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
int main(int narg, char *arg[])
double zscalar_t
#define NNZ_IDX
Provides access for Zoltan2 to Xpetra::CrsGraph data.
Zoltan2::XpetraCrsGraphAdapter< SparseGraph > SparseGraphAdapter
int zlno_t
common code used by tests
Defines the XpetraMultiVectorAdapter.
Defines XpetraCrsGraphAdapter class.
zgno_t z2TestGO
Defines the XpetraCrsMatrixAdapter class.
void applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
Tpetra::CrsMatrix< z2TestScalar, z2TestLO, z2TestGO > SparseMatrix
InputTraits< User >::part_t part_t
An adapter for Xpetra::MultiVector.
int zgno_t
Vector::node_type Node
Zoltan2::XpetraMultiVectorAdapter< IntVector > IntVectorAdapter
const PartitioningSolution< Adapter > & getSolution()
Get the solution to the problem.
Tpetra::CrsGraph< z2TestLO, z2TestGO > SparseGraph
PartitioningProblem sets up partitioning problems for the user.
Zoltan2::XpetraCrsMatrixAdapter< SparseMatrix > SparseMatrixAdapter
Tpetra::Vector< int, z2TestLO, z2TestGO > IntVector
Defines the PartitioningProblem class.
void applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
#define epsilon
Zoltan2::XpetraMultiVectorAdapter< Vector > MultiVectorAdapter
void solve(bool updateInputData=true)
Direct the problem to create a solution.
std::string testDataFilePath(".")