Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
nd.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
48 #include <Zoltan2_TestHelpers.hpp>
49 #include <iostream>
50 #include <limits>
51 #include <Teuchos_ParameterList.hpp>
52 #include <Teuchos_RCP.hpp>
53 #include <Teuchos_FancyOStream.hpp>
54 #include <Teuchos_CommandLineProcessor.hpp>
55 #include <Tpetra_CrsMatrix.hpp>
56 #include <Tpetra_Vector.hpp>
57 #include <MatrixMarket_Tpetra.hpp>
58 
59 using Teuchos::RCP;
60 
62 // Eventually want to use Teuchos unit tests to vary z2TestLO and
63 // GO. For now, we set them at compile time based on whether Tpetra
64 // is built with explicit instantiation on. (in Zoltan2_TestHelpers.hpp)
65 
66 typedef zlno_t z2TestLO;
67 typedef zgno_t z2TestGO;
69 
70 typedef Tpetra::CrsMatrix<z2TestScalar, z2TestLO, z2TestGO> SparseMatrix_t;
71 typedef Tpetra::Vector<z2TestScalar, z2TestLO, z2TestGO> Vector;
72 typedef Vector::node_type Node;
73 
74 typedef Tpetra::MultiVector<z2TestScalar, z2TestLO, z2TestGO,znode_t> tMVector_t;
75 
76 
77 //typedef Zoltan2::XpetraCrsMatrixAdapter<SparseMatrix_t> SparseMatrixAdapter;
79 
81 
82 #define epsilon 0.00000001
83 
84 int testNDwithRCB(RCP<SparseMatrix_t> &origMatrix,RCP<tMVector_t> &coords, int numParts, int me);
85 int testNDwithPHG(RCP<SparseMatrix_t> &origMatrix,int numParts, int me);
86 
89 int main(int narg, char** arg)
90 {
94  Tpetra::ScopeGuard tscope(&narg, &arg);
95  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
96  int me = comm->getRank();
98 
99  std::string inputFile = ""; // Matrix Market or Zoltan file to read
100  std::string inputPath = testDataFilePath; // Directory with input file
101  bool distributeInput = true;
102  int success = 0;
103  int numParts = 2;
104 
106  // Read run-time options.
108  Teuchos::CommandLineProcessor cmdp (false, false);
109  cmdp.setOption("inputPath", &inputPath,
110  "Path to the MatrixMarket or Zoltan file to be read; "
111  "if not specified, a default path will be used.");
112  cmdp.setOption("inputFile", &inputFile,
113  "Name of the Matrix Market or Zoltan file to read; "
114  "");
115  cmdp.setOption("distribute", "no-distribute", &distributeInput,
116  "for Zoltan input files only, "
117  "indicate whether or not to distribute "
118  "input across the communicator");
119  cmdp.setOption("numParts", &numParts,
120  "Global number of parts;");
121 
122  Teuchos::CommandLineProcessor::EParseCommandLineReturn
123  parseReturn= cmdp.parse( narg, arg );
124 
125  if( parseReturn == Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED )
126  {
127  return 0;
128  }
130 
132  // Construct matrix from file
134  RCP<UserInputForTests> uinput;
135 
136  if (inputFile != "") // Input file specified; read a matrix
137  {
138  uinput = rcp(new UserInputForTests(inputPath, inputFile, comm,
139  true, distributeInput));
140  }
141  else
142  {
143  std::cout << "Input file must be specified." << std::endl;
144  }
145 
146  RCP<SparseMatrix_t> origMatrix = uinput->getUITpetraCrsMatrix();
147 
148 
149  if (me == 0)
150  {
151  std::cout << "NumRows = " << origMatrix->getGlobalNumRows() << std::endl
152  << "NumNonzeros = " << origMatrix->getGlobalNumEntries() << std::endl
153  << "NumProcs = " << comm->getSize() << std::endl
154  << "NumParts = " << numParts << std::endl;
155  }
156 
157  if (origMatrix->getGlobalNumRows() < 40)
158  {
159  Teuchos::FancyOStream out(Teuchos::rcp(&std::cout,false));
160  origMatrix->describe(out, Teuchos::VERB_EXTREME);
161  }
163 
165  // Get coordinates, corresponding to graph vertices
167  RCP<tMVector_t> coords;
168  try
169  {
170  coords = uinput->getUICoordinates();
171  }
172  catch(...)
173  {
174  if (me == 0)
175  std::cout << "FAIL: get coordinates" << std::endl;
176  return 1;
177  }
179 
181  // Test ND ordering with RCB to compute the separator
183  testNDwithRCB(origMatrix,coords,numParts,me);
185 
187  // Test ND ordering with PHG to compute the separator
189  testNDwithPHG(origMatrix,numParts,me);
191 
192 
193 
194  return success;
195 }
197 
200 int testNDwithRCB(RCP<SparseMatrix_t> &origMatrix,RCP<tMVector_t> &coords, int numParts, int me)
201 {
202  int success=0;
203 
207  Teuchos::ParameterList params;
208 
209  params.set("num_global_parts", numParts);
210  params.set("order_method", "nd");
211  params.set("edge_separator_method", "rcb");
213 
217  SparseMatrixAdapter_t matAdapter(origMatrix);
218 
219  MultiVectorAdapter_t *ca = NULL;
220 
221  try
222  {
223  ca = new MultiVectorAdapter_t(coords);
224  }
225  catch(...)
226  {
227  if (me == 0)
228  std::cout << "FAIL: vector adapter" << std::endl;
229  return 1;
230  }
231 
232  matAdapter.setCoordinateInput(ca);
234 
238  Zoltan2::OrderingProblem<SparseMatrixAdapter_t> problem(&matAdapter, &params);
239 
240 
241  try
242  {
243  if (me == 0) std::cout << "Calling solve() " << std::endl;
244  problem.solve();
245  if (me == 0) std::cout << "Done solve() " << std::endl;
246  }
247  catch (std::runtime_error &e)
248  {
249  std::cout << "Runtime exception returned from solve(): " << e.what();
250  if (!strncmp(e.what(), "BUILD ERROR", 11)) {
251  // Catching build errors as exceptions is OK in the tests
252  std::cout << " PASS" << std::endl;
253  return 0;
254  }
255  else {
256  // All other runtime_errors are failures
257  std::cout << " FAIL" << std::endl;
258  return -1;
259  }
260  }
261  catch (std::logic_error &e)
262  {
263  std::cout << "Logic exception returned from solve(): " << e.what()
264  << " FAIL" << std::endl;
265  return -1;
266  }
267  catch (std::bad_alloc &e)
268  {
269  std::cout << "Bad_alloc exception returned from solve(): " << e.what()
270  << " FAIL" << std::endl;
271  return -1;
272  }
273  catch (std::exception &e)
274  {
275  std::cout << "Unknown exception returned from solve(). " << e.what()
276  << " FAIL" << std::endl;
277  return -1;
278  }
279 
281 
282  delete ca;
283 
284 
285  std::cout << "PASS" << std::endl;
286  return success;
287 }
289 
290 
293 int testNDwithPHG(RCP<SparseMatrix_t> &origMatrix,int numParts, int me)
294 {
295  int success=0;
296 
300  Teuchos::ParameterList params;
301 
302  params.set("num_global_parts", numParts);
303  params.set("order_method", "nd");
304  params.set("edge_separator_method", "phg");
306 
310  SparseMatrixAdapter_t matAdapter(origMatrix);
312 
316  Zoltan2::OrderingProblem<SparseMatrixAdapter_t> problem(&matAdapter, &params);
317 
318 
319  try
320  {
321  if (me == 0) std::cout << "Calling solve() " << std::endl;
322  problem.solve();
323  if (me == 0) std::cout << "Done solve() " << std::endl;
324  }
325  catch (std::runtime_error &e)
326  {
327  std::cout << "Runtime exception returned from solve(): " << e.what();
328  if (!strncmp(e.what(), "BUILD ERROR", 11)) {
329  // Catching build errors as exceptions is OK in the tests
330  std::cout << " PASS" << std::endl;
331  return 0;
332  }
333  else {
334  // All other runtime_errors are failures
335  std::cout << " FAIL" << std::endl;
336  return -1;
337  }
338  }
339  catch (std::logic_error &e)
340  {
341  std::cout << "Logic exception returned from solve(): " << e.what()
342  << " FAIL" << std::endl;
343  return -1;
344  }
345  catch (std::bad_alloc &e)
346  {
347  std::cout << "Bad_alloc exception returned from solve(): " << e.what()
348  << " FAIL" << std::endl;
349  return -1;
350  }
351  catch (std::exception &e)
352  {
353  std::cout << "Unknown exception returned from solve(). " << e.what()
354  << " FAIL" << std::endl;
355  return -1;
356  }
358 
359  std::cout << "PASS" << std::endl;
360  return success;
361 }
zgno_t z2TestGO
Definition: coloring1.cpp:76
zlno_t z2TestLO
Definition: coloring1.cpp:75
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
int testNDwithRCB(RCP< SparseMatrix_t > &origMatrix, RCP< tMVector_t > &coords, int numParts, int me)
Definition: nd.cpp:200
Tpetra::CrsMatrix< z2TestScalar, z2TestLO, z2TestGO > SparseMatrix_t
Definition: nd.cpp:70
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 XpetraMultiVectorAdapter.
OrderingProblem sets up ordering problems for the user.
Defines the XpetraCrsMatrixAdapter class.
Zoltan2::XpetraCrsMatrixAdapter< SparseMatrix_t, tMVector_t > SparseMatrixAdapter_t
Definition: nd.cpp:78
void solve(bool updateInputData=true)
Direct the problem to create a solution.
Zoltan2::XpetraMultiVectorAdapter< tMVector_t > MultiVectorAdapter_t
Definition: nd.cpp:80
An adapter for Xpetra::MultiVector.
Tpetra::Map::local_ordinal_type zlno_t
Defines the OrderingProblem class.
int testNDwithPHG(RCP< SparseMatrix_t > &origMatrix, int numParts, int me)
Definition: nd.cpp:293
void setCoordinateInput(VectorAdapter< UserCoord > *coordData) override
Allow user to provide additional data that contains coordinate info associated with the MatrixAdapter...
zscalar_t z2TestScalar
Definition: coloring1.cpp:77
float zscalar_t
Tpetra::MultiVector< zscalar_t, zlno_t, zgno_t, znode_t > tMVector_t
Tpetra::Map::global_ordinal_type zgno_t
Vector::node_type Node
Definition: coloring1.cpp:81
std::string testDataFilePath(".")