Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
orderingMetis.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
47 #include <Zoltan2_TestHelpers.hpp>
48 #include <iostream>
49 #include <limits>
50 #include <vector>
51 #include <Teuchos_ParameterList.hpp>
52 #include <Teuchos_RCP.hpp>
53 #include <Teuchos_CommandLineProcessor.hpp>
54 #include <Tpetra_CrsMatrix.hpp>
55 #include <Tpetra_Vector.hpp>
56 #include <MatrixMarket_Tpetra.hpp>
57 
58 //#include <Zoltan2_Memory.hpp> KDD User app wouldn't include our memory mgr.
59 
60 using Teuchos::RCP;
61 
63 // Program to demonstrate use of Zoltan2 to order 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::Vector<z2TestScalar, z2TestLO, z2TestGO> Vector;
82 typedef Vector::node_type Node;
83 
85 
86 #define epsilon 0.00000001
87 
88 int validatePerm(size_t n, z2TestLO *perm)
89 // returns 0 if permutation is valid
90 {
91  std::vector<int> count(n);
92  int status = 0;
93 
94  for (size_t i=0; i<n; i++)
95  count[i]=0;
96 
97  for (size_t i=0; i<n; i++){
98  if (perm[i] < 0 || static_cast<size_t> (perm[i]) >= n)
99  status = -1;
100  else
101  count[perm[i]]++;
102  }
103 
104  // Each index should occur exactly once (count==1)
105  for (size_t i=0; i<n; i++){
106  if (count[i] != 1){
107  status = -2;
108  break;
109  }
110  }
111 
112  return status;
113 }
114 
116 int main(int narg, char** arg)
117 {
118  std::string inputFile = ""; // Matrix Market file to read
119  std::string outputFile = ""; // Matrix Market file to write
120  bool verbose = false; // Verbosity of output
121  int testReturn = 0;
122 
123  // Initialize Tpetra and get default communicator.
124  Tpetra::ScopeGuard tscope(&narg, &arg);
125  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
126  int me = comm->getRank();
127 
128  // Read run-time options.
129  Teuchos::CommandLineProcessor cmdp (false, false);
130  cmdp.setOption("inputFile", &inputFile,
131  "Name of a Matrix Market file in the data directory; "
132  "if not specified, a matrix will be generated by MueLu.");
133  cmdp.setOption("outputFile", &outputFile,
134  "Name of the Matrix Market sparse matrix file to write, "
135  "echoing the input/generated matrix.");
136  cmdp.setOption("verbose", "quiet", &verbose,
137  "Print messages and results.");
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 
166  RCP<UserInputForTests> uinput;
167 
168  if (inputFile != "") // Input file specified; read a matrix
169  uinput = rcp(new UserInputForTests(testDataFilePath, inputFile, comm, true));
170 
171  else // Let MueLu generate a matrix
172  uinput = rcp(new UserInputForTests(xdim, ydim, zdim, matrixType, comm, true, true));
173 
174  RCP<SparseMatrix> origMatrix = uinput->getUITpetraCrsMatrix();
175 
176  if (outputFile != "") {
177  // Just a sanity check.
178  Tpetra::MatrixMarket::Writer<SparseMatrix>::writeSparseFile(outputFile,
179  origMatrix, verbose);
180  }
181 
182  if (me == 0)
183  std::cout << "NumRows = " << origMatrix->getGlobalNumRows() << std::endl
184  << "NumNonzeros = " << origMatrix->getGlobalNumEntries() << std::endl
185  << "NumProcs = " << comm->getSize() << std::endl;
186 
188  RCP<Vector> origVector, origProd;
189  origProd = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
190  origMatrix->getRangeMap());
191  origVector = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
192  origMatrix->getDomainMap());
193  origVector->randomize();
194 
196  Teuchos::ParameterList params;
198  size_t checkLength;
199  z2TestLO *checkPerm;
200 
202  SparseMatrixAdapter adapter(origMatrix);
203 
204  params.set("order_method", "metis");
205  params.set("order_method_type", "local");
206 
208  try
209  {
210  Zoltan2::OrderingProblem<SparseMatrixAdapter> problem(&adapter, &params);
211  problem.solve();
212 
214  problem.getLocalOrderingSolution();
215 
216  // Check that the solution is really a permutation
217  checkLength = soln->getPermutationSize();
218  checkPerm = soln->getPermutationView();
219 
220  for (size_t ii = 0; ii < checkLength; ii++)
221  std::cout << checkPerm[ii] << " ";
222  std::cout << std::endl;
223  // Verify that checkPerm is a permutation
224  testReturn = validatePerm(checkLength, checkPerm);
225 
226  } catch (std::exception &e){
227 #ifdef HAVE_ZOLTAN2_METIS
228  // AMD is defined and still got an exception.
229  std::cout << "Exception from METIS Algorithm" << std::endl;
230  std::cout << "FAIL" << std::endl;
231  return 0;
232 #else
233  std::cout << "METIS is not enabled. METIS Algorithm threw an exception."
234  << std::endl;
235  std::cout << "PASS" << std::endl;
236  return 0;
237 #endif
238  }
239 
240  if (me == 0) {
241  if (testReturn)
242  std::cout << "Solution is not a permutation; FAIL" << std::endl;
243  else
244  std::cout << "PASS" << std::endl;
245  }
246  return 0;
247 }
248 
zgno_t z2TestGO
Definition: coloring1.cpp:76
zlno_t z2TestLO
Definition: coloring1.cpp:75
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
OrderingProblem sets up ordering problems for the user.
int validatePerm(size_t n, z2TestLO *perm)
Definition: orderingAMD.cpp:88
Defines the XpetraCrsMatrixAdapter class.
size_t getPermutationSize() const
Get (local) size of permutation.
lno_t * getPermutationView(bool inverse=false) const
Get pointer to permutation. If inverse = true, return inverse permutation. By default, perm[i] is where new index i can be found in the old ordering. When inverse==true, perm[i] is where old index i can be found in the new ordering.
void solve(bool updateInputData=true)
Direct the problem to create a solution.
Tpetra::Map::local_ordinal_type zlno_t
Defines the OrderingProblem class.
Zoltan2::XpetraCrsMatrixAdapter< SparseMatrix > SparseMatrixAdapter
Definition: coloring1.cpp:85
zscalar_t z2TestScalar
Definition: coloring1.cpp:77
float zscalar_t
Tpetra::Map::global_ordinal_type zgno_t
Vector::node_type Node
Definition: coloring1.cpp:81
LocalOrderingSolution< lno_t > * getLocalOrderingSolution()
Get the local ordering solution to the problem.
std::string testDataFilePath(".")