Zoltan2
Loading...
Searching...
No Matches
orderingAMD.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
13#include <iostream>
14#include <limits>
15#include <vector>
16#include <Teuchos_ParameterList.hpp>
17#include <Teuchos_RCP.hpp>
18#include <Teuchos_CommandLineProcessor.hpp>
19#include <Tpetra_CrsMatrix.hpp>
20#include <Tpetra_Vector.hpp>
21#include <MatrixMarket_Tpetra.hpp>
22
23//#include <Zoltan2_Memory.hpp> KDD User app wouldn't include our memory mgr.
24
25using Teuchos::RCP;
26
28// Program to demonstrate use of Zoltan2 to order a TPetra matrix
29// (read from a MatrixMarket file or generated by Galeri::Xpetra).
30// Usage:
31// a.out [--inputFile=filename] [--outputFile=outfile] [--verbose]
32// [--x=#] [--y=#] [--z=#] [--matrix={Laplace1D,Laplace2D,Laplace3D}
33// Karen Devine, 2011
35
37// Eventually want to use Teuchos unit tests to vary z2TestLO and
38// GO. For now, we set them at compile time based on whether Tpetra
39// is built with explicit instantiation on. (in Zoltan2_TestHelpers.hpp)
40
44
45typedef Tpetra::CrsMatrix<z2TestScalar, z2TestLO, z2TestGO> SparseMatrix;
46typedef Tpetra::Vector<z2TestScalar, z2TestLO, z2TestGO> Vector;
47typedef Vector::node_type Node;
48
50
51#define epsilon 0.00000001
52
53int validatePerm(size_t n, z2TestLO *perm)
54// returns 0 if permutation is valid
55{
56 std::vector<int> count(n);
57 int status = 0;
58
59 for (size_t i=0; i<n; i++)
60 count[i]=0;
61
62 for (size_t i=0; i<n; i++){
63 if (perm[i] < 0 || static_cast<size_t> (perm[i]) >= n)
64 status = -1;
65 else
66 count[perm[i]]++;
67 }
68
69 // Each index should occur exactly once (count==1)
70 for (size_t i=0; i<n; i++){
71 if (count[i] != 1){
72 status = -2;
73 break;
74 }
75 }
76
77 return status;
78}
79
81int main(int narg, char** arg)
82{
83 std::string inputFile = ""; // Matrix Market file to read
84 std::string outputFile = ""; // Matrix Market file to write
85 bool verbose = false; // Verbosity of output
86 int testReturn = 0;
87
88 // Initialize Tpetra and get default communicator.
89 Tpetra::ScopeGuard tscope(&narg, &arg);
90 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
91 int me = comm->getRank();
92
93 // Read run-time options.
94 Teuchos::CommandLineProcessor cmdp (false, false);
95 cmdp.setOption("inputFile", &inputFile,
96 "Name of a Matrix Market file in the data directory; "
97 "if not specified, a matrix will be generated by MueLu.");
98 cmdp.setOption("outputFile", &outputFile,
99 "Name of the Matrix Market sparse matrix file to write, "
100 "echoing the input/generated matrix.");
101 cmdp.setOption("verbose", "quiet", &verbose,
102 "Print messages and results.");
103
105 // Even with cmdp option "true", I get errors for having these
106 // arguments on the command line. (On redsky build)
107 // KDDKDD Should just be warnings, right? Code should still work with these
108 // KDDKDD params in the create-a-matrix file. Better to have them where
109 // KDDKDD they are used.
110 int xdim=10;
111 int ydim=10;
112 int zdim=10;
113 std::string matrixType("Laplace3D");
114
115 cmdp.setOption("x", &xdim,
116 "number of gridpoints in X dimension for "
117 "mesh used to generate matrix.");
118 cmdp.setOption("y", &ydim,
119 "number of gridpoints in Y dimension for "
120 "mesh used to generate matrix.");
121 cmdp.setOption("z", &zdim,
122 "number of gridpoints in Z dimension for "
123 "mesh used to generate matrix.");
124 cmdp.setOption("matrix", &matrixType,
125 "Matrix type: Laplace1D, Laplace2D, or Laplace3D");
127
128 cmdp.parse(narg, arg);
129
130
131 RCP<UserInputForTests> uinput;
132
133 if (inputFile != "") // Input file specified; read a matrix
134 uinput = rcp(new UserInputForTests(testDataFilePath, inputFile, comm, true));
135
136 else // Let MueLu generate a matrix
137 uinput = rcp(new UserInputForTests(xdim, ydim, zdim, matrixType, comm, true, true));
138
139 RCP<SparseMatrix> origMatrix = uinput->getUITpetraCrsMatrix();
140
141 if (outputFile != "") {
142 // Just a sanity check.
143 Tpetra::MatrixMarket::Writer<SparseMatrix>::writeSparseFile(outputFile,
144 origMatrix, verbose);
145 }
146
147 if (me == 0)
148 std::cout << "NumRows = " << origMatrix->getGlobalNumRows() << std::endl
149 << "NumNonzeros = " << origMatrix->getGlobalNumEntries() << std::endl
150 << "NumProcs = " << comm->getSize() << std::endl;
151
153 RCP<Vector> origVector, origProd;
154 origProd = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
155 origMatrix->getRangeMap());
156 origVector = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
157 origMatrix->getDomainMap());
158 origVector->randomize();
159
161 Teuchos::ParameterList params;
163 size_t checkLength;
164 z2TestLO *checkPerm;
165
167 SparseMatrixAdapter adapter(origMatrix);
168
169 params.set("order_method", "minimum_degree");
170 params.set("order_method_type", "local");
171 params.set("order_package", "amd");
172
174 try
175 {
176 Zoltan2::OrderingProblem<SparseMatrixAdapter> problem(&adapter, &params);
177 problem.solve();
178
180 problem.getLocalOrderingSolution();
181
182 // Check that the solution is really a permutation
183 checkLength = soln->getPermutationSize();
184 checkPerm = soln->getPermutationView();
185
186 for (size_t ii = 0; ii < checkLength; ii++)
187 std::cout << checkPerm[ii] << " ";
188 std::cout << std::endl;
189 // Verify that checkPerm is a permutation
190 testReturn = validatePerm(checkLength, checkPerm);
191
192 } catch (std::exception &e){
193#ifdef HAVE_ZOLTAN2_AMD
194 // AMD is defined and still got an exception.
195 std::cout << "Exception from AMD Algorithm" << std::endl;
196 std::cout << "FAIL" << std::endl;
197 return 0;
198#else
199 std::cout << "AMD is not enabled. AMD Algorithm threw an exception."
200 << std::endl;
201 std::cout << "PASS" << std::endl;
202 return 0;
203#endif
204 }
205
206 if (me == 0) {
207 if (testReturn)
208 std::cout << "Solution is not a permutation; FAIL" << std::endl;
209 else
210 std::cout << "PASS" << std::endl;
211 }
212 return 0;
213}
214
Defines the OrderingProblem 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()
OrderingProblem sets up ordering problems for the user.
void solve(bool updateInputData=true)
Direct the problem to create a solution.
LocalOrderingSolution< lno_t > * getLocalOrderingSolution()
Get the local ordering solution to the problem.
index_t * getPermutationView(bool inverse=false) const
Get pointer to permutation. If inverse = true, return inverse permutation. By default,...
size_t getPermutationSize() const
Get (local) size of permutation.
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
zlno_t z2TestLO
Definition coloring1.cpp:40
Vector::node_type Node
zlno_t z2TestLO
Tpetra::CrsMatrix< z2TestScalar, z2TestLO, z2TestGO > SparseMatrix
int validatePerm(size_t n, z2TestLO *perm)
Zoltan2::XpetraCrsMatrixAdapter< SparseMatrix > SparseMatrixAdapter
Tpetra::Vector< z2TestScalar, z2TestLO, z2TestGO > Vector
zgno_t z2TestGO
zscalar_t z2TestScalar