53 #include <Teuchos_ParameterList.hpp>
54 #include <Teuchos_RCP.hpp>
55 #include <Teuchos_FancyOStream.hpp>
56 #include <Teuchos_CommandLineProcessor.hpp>
57 #include <Tpetra_CrsMatrix.hpp>
58 #include <Tpetra_Vector.hpp>
59 #include <MatrixMarket_Tpetra.hpp>
83 typedef Tpetra::CrsMatrix<z2TestScalar, z2TestLO, z2TestGO>
SparseMatrix;
85 typedef Tpetra::Vector<z2TestScalar, z2TestLO, z2TestGO>
Vector;
86 typedef Vector::node_type
Node;
94 typedef Tpetra::Vector<int, z2TestLO, z2TestGO>
IntVector;
97 #define epsilon 0.00000001
103 std::string inputFile =
"";
104 std::string outputFile =
"";
106 bool verbose =
false;
107 bool distributeInput =
true;
108 bool haveFailure =
false;
113 bool isNormalized =
false;
114 bool isGeneralized =
false;
115 std::string precType =
"jacobi";
116 std::string initialGuess =
"random";
117 bool useFullOrtho =
true;
120 Tpetra::ScopeGuard tscope(&narg, &arg);
121 RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
122 int me = comm->getRank();
123 int commsize = comm->getSize();
126 Teuchos::CommandLineProcessor cmdp (
false,
false);
127 cmdp.setOption(
"inputPath", &inputPath,
128 "Path to the MatrixMarket or Zoltan file to be read; "
129 "if not specified, a default path will be used.");
130 cmdp.setOption(
"inputFile", &inputFile,
131 "Name of the Matrix Market or Zoltan file to read; "
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(
"vertexWeights", &nVwgts,
137 "Number of weights to generate for each vertex");
138 cmdp.setOption(
"verbose",
"quiet", &verbose,
139 "Print messages and results.");
140 cmdp.setOption(
"distribute",
"no-distribute", &distributeInput,
141 "indicate whether or not to distribute "
142 "input across the communicator");
144 cmdp.setOption(
"normalized",
"combinatorial", &isNormalized,
145 "indicate whether or not to use a normalized Laplacian.");
146 cmdp.setOption(
"generalized",
"non-generalized", &isGeneralized,
147 "indicate whether or not to use a generalized Laplacian.");
148 cmdp.setOption(
"precond", &precType,
149 "indicate which preconditioner to use [muelu|jacobi|polynomial].");
150 cmdp.setOption(
"initialGuess", &initialGuess,
151 "initial guess for LOBPCG");
152 cmdp.setOption(
"useFullOrtho",
"partialOrtho", &useFullOrtho,
153 "use full orthogonalization.");
164 std::string matrixType(
"Laplace3D");
166 cmdp.setOption(
"x", &xdim,
167 "number of gridpoints in X dimension for "
168 "mesh used to generate matrix.");
169 cmdp.setOption(
"y", &ydim,
170 "number of gridpoints in Y dimension for "
171 "mesh used to generate matrix.");
172 cmdp.setOption(
"z", &zdim,
173 "number of gridpoints in Z dimension for "
174 "mesh used to generate matrix.");
175 cmdp.setOption(
"matrix", &matrixType,
176 "Matrix type: Laplace1D, Laplace2D, or Laplace3D");
179 cmdp.parse(narg, arg);
181 RCP<UserInputForTests> uinput;
185 true, distributeInput));
189 true, distributeInput));
191 RCP<SparseMatrix> origMatrix = uinput->getUITpetraCrsMatrix();
193 if (origMatrix->getGlobalNumRows() < 40) {
194 Teuchos::FancyOStream out(Teuchos::rcp(&std::cout,
false));
195 origMatrix->describe(out, Teuchos::VERB_EXTREME);
199 if (outputFile !=
"") {
201 Tpetra::MatrixMarket::Writer<SparseMatrix>::writeSparseFile(outputFile,
202 origMatrix, verbose);
206 std::cout <<
"NumRows = " << origMatrix->getGlobalNumRows() << std::endl
207 <<
"NumNonzeros = " << origMatrix->getGlobalNumEntries() << std::endl
208 <<
"NumProcs = " << comm->getSize() << std::endl
209 <<
"NumLocalRows (rank 0) = " << origMatrix->getNodeNumRows() << std::endl;
212 RCP<Vector> origVector, origProd;
213 origProd = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
214 origMatrix->getRangeMap());
215 origVector = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
216 origMatrix->getDomainMap());
217 origVector->randomize();
220 Teuchos::RCP<Teuchos::ParameterList> params(
new Teuchos::ParameterList);
221 params->set(
"num_global_parts", commsize);
222 params->set(
"sphynx_skip_preprocessing",
true);
223 params->set(
"sphynx_preconditioner_type", precType);
224 params->set(
"sphynx_verbosity", verbose ? 1 : 0);
225 params->set(
"sphynx_initial_guess", initialGuess);
226 params->set(
"sphynx_use_full_ortho", useFullOrtho);
227 std::string problemType =
"combinatorial";
229 problemType =
"normalized";
230 else if(isGeneralized)
231 problemType =
"generalized";
232 params->set(
"sphynx_problem_type", problemType);
235 Teuchos::RCP<SparseGraphAdapter> adapter = Teuchos::rcp(
new SparseGraphAdapter(origMatrix->getCrsGraph(), nVwgts));
244 size_t nrows = origMatrix->getNodeNumRows();
247 for (
size_t i = 0; i < nrows; i++) {
248 size_t idx = i * nVwgts;
249 vwgts[idx] =
zscalar_t(origMatrix->getRowMap()->getGlobalElement(i));
250 for (
int j = 1; j < nVwgts; j++) vwgts[idx+j] = 1.;
252 for (
int j = 0; j < nVwgts; j++) {
253 if (j !=
NNZ_IDX) adapter->setVertexWeights(&vwgts[j], nVwgts, j);
254 else adapter->setVertexWeightIsDegree(
NNZ_IDX);
263 if (me == 0) std::cout <<
"Calling solve() " << std::endl;
267 if (me == 0) std::cout <<
"Done solve() " << std::endl;
269 catch (std::runtime_error &e) {
271 std::cout <<
"Runtime exception returned from solve(): " << e.what();
272 if (!strncmp(e.what(),
"BUILD ERROR", 11)) {
274 std::cout <<
" PASS" << std::endl;
279 std::cout <<
" FAIL" << std::endl;
283 catch (std::logic_error &e) {
285 std::cout <<
"Logic exception returned from solve(): " << e.what()
286 <<
" FAIL" << std::endl;
289 catch (std::bad_alloc &e) {
291 std::cout <<
"Bad_alloc exception returned from solve(): " << e.what()
292 <<
" FAIL" << std::endl;
295 catch (std::exception &e) {
297 std::cout <<
"Unknown exception returned from solve(). " << e.what()
298 <<
" FAIL" << std::endl;
304 size_t checkNparts = comm->getSize();
306 size_t checkLength = origMatrix->getNodeNumRows();
310 size_t *countPerPart =
new size_t[checkNparts];
311 size_t *globalCountPerPart =
new size_t[checkNparts];
314 for (
size_t i = 0; i < checkNparts; i++) countPerPart[i] = 0;
315 for (
size_t i = 0; i < checkNparts * nVwgts; i++) wtPerPart[i] = 0.;
317 for (
size_t i = 0; i < checkLength; i++) {
318 if (
size_t(checkParts[i]) >= checkNparts)
319 std::cout <<
"Invalid Part " << checkParts[i] <<
": FAIL" << std::endl;
320 countPerPart[checkParts[i]]++;
321 for (
int j = 0; j < nVwgts; j++) {
323 wtPerPart[checkParts[i]*nVwgts+j] += vwgts[i*nVwgts+j];
325 wtPerPart[checkParts[i]*nVwgts+j] += origMatrix->getNumEntriesInLocalRow(i);
328 Teuchos::reduceAll<int, size_t>(*comm, Teuchos::REDUCE_SUM, checkNparts,
329 countPerPart, globalCountPerPart);
330 Teuchos::reduceAll<int, zscalar_t>(*comm, Teuchos::REDUCE_SUM,
332 wtPerPart, globalWtPerPart);
334 size_t min = std::numeric_limits<std::size_t>::max();
337 size_t minrank = 0, maxrank = 0;
338 for (
size_t i = 0; i < checkNparts; i++) {
339 if (globalCountPerPart[i] < min) {min = globalCountPerPart[i]; minrank = i;}
340 if (globalCountPerPart[i] > max) {max = globalCountPerPart[i]; maxrank = i;}
341 sum += globalCountPerPart[i];
345 float avg = (float) sum / (
float) checkNparts;
346 std::cout <<
"Minimum count: " << min <<
" on rank " << minrank << std::endl;
347 std::cout <<
"Maximum count: " << max <<
" on rank " << maxrank << std::endl;
348 std::cout <<
"Average count: " << avg << std::endl;
349 std::cout <<
"Total count: " << sum
350 << (sum != origMatrix->getGlobalNumRows()
351 ?
"Work was lost; FAIL"
354 std::cout <<
"Imbalance: " << max / avg << std::endl;
356 std::vector<zscalar_t> minwt(nVwgts, std::numeric_limits<zscalar_t>::max());
357 std::vector<zscalar_t> maxwt(nVwgts, 0.);
358 std::vector<zscalar_t> sumwt(nVwgts, 0.);
359 for (
size_t i = 0; i < checkNparts; i++) {
360 for (
int j = 0; j < nVwgts; j++) {
361 size_t idx = i*nVwgts+j;
362 if (globalWtPerPart[idx] < minwt[j]) minwt[j] = globalWtPerPart[idx];
363 if (globalWtPerPart[idx] > maxwt[j]) maxwt[j] = globalWtPerPart[idx];
364 sumwt[j] += globalWtPerPart[idx];
367 for (
int j = 0; j < nVwgts; j++) {
368 float avgwt = (float) sumwt[j] / (
float) checkNparts;
369 std::cout << std::endl;
370 std::cout <<
"Minimum weight[" << j <<
"]: " << minwt[j] << std::endl;
371 std::cout <<
"Maximum weight[" << j <<
"]: " << maxwt[j] << std::endl;
372 std::cout <<
"Average weight[" << j <<
"]: " << avgwt << std::endl;
373 std::cout <<
"Imbalance: " << maxwt[j] / avgwt << std::endl;
378 delete [] countPerPart;
380 delete [] globalCountPerPart;
381 delete [] globalWtPerPart;
385 if (me == 0) std::cout <<
"Redistributing matrix..." << std::endl;
390 if (redistribMatrix->getGlobalNumRows() < 40) {
391 Teuchos::FancyOStream out(Teuchos::rcp(&std::cout,
false));
392 redistribMatrix->describe(out, Teuchos::VERB_EXTREME);
395 if (me == 0) std::cout <<
"Redistributing vectors..." << std::endl;
401 RCP<Vector> redistribProd;
402 redistribProd = Tpetra::createVector<z2TestScalar,z2TestLO,z2TestGO>(
403 redistribMatrix->getRangeMap());
407 RCP<IntVector> origIntVec;
409 origIntVec = Tpetra::createVector<int,z2TestLO,z2TestGO>(
410 origMatrix->getRangeMap());
411 for (
size_t i = 0; i < origIntVec->getLocalLength(); i++)
412 origIntVec->replaceLocalValue(i, me);
417 int origIntNorm = origIntVec->norm1();
418 int redistIntNorm = redistIntVec->norm1();
419 if (me == 0) std::cout <<
"IntegerVectorTest: " << origIntNorm <<
" == "
420 << redistIntNorm <<
" ?";
421 if (origIntNorm != redistIntNorm) {
422 if (me == 0) std::cout <<
" FAIL" << std::endl;
425 else if (me == 0) std::cout <<
" OK" << std::endl;
431 if (me == 0) std::cout <<
"Matvec original..." << std::endl;
432 origMatrix->apply(*origVector, *origProd);
435 std::cout <<
"Norm of Original matvec prod: " << origNorm << std::endl;
437 if (me == 0) std::cout <<
"Matvec redistributed..." << std::endl;
438 redistribMatrix->apply(*redistribVector, *redistribProd);
441 std::cout <<
"Norm of Redistributed matvec prod: " << redistribNorm << std::endl;
443 if (redistribNorm > origNorm+
epsilon || redistribNorm < origNorm-
epsilon) {
448 delete redistribVector;
449 delete redistribMatrix;
453 std::cout <<
"Mat-Vec product changed; FAIL" << std::endl;
457 std::cout <<
"PASS" << std::endl;
Tpetra::CrsGraph< z2TestLO, z2TestGO > SparseGraph
Zoltan2::XpetraMultiVectorAdapter< IntVector > IntVectorAdapter
Tpetra::CrsMatrix< z2TestScalar, z2TestLO, z2TestGO > SparseMatrix
Zoltan2::XpetraMultiVectorAdapter< Vector > MultiVectorAdapter
Zoltan2::XpetraCrsMatrixAdapter< SparseMatrix > SparseMatrixAdapter
Tpetra::Vector< z2TestScalar, z2TestLO, z2TestGO > Vector
Tpetra::Vector< int, z2TestLO, z2TestGO > IntVector
Zoltan2::XpetraCrsGraphAdapter< SparseGraph > SparseGraphAdapter
int main(int narg, char **arg)
common code used by tests
Tpetra::Map ::local_ordinal_type zlno_t
std::string testDataFilePath(".")
Tpetra::Map ::global_ordinal_type zgno_t
Defines XpetraCrsGraphAdapter class.
Defines the XpetraCrsMatrixAdapter class.
Defines the XpetraMultiVectorAdapter.
InputTraits< User >::part_t part_t
const PartitioningSolution< Adapter > & getSolution()
Provides access for Zoltan2 to Xpetra::CrsGraph data.
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
void applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
An adapter for Xpetra::MultiVector.
void applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
Tpetra::CrsMatrix< z2TestScalar, z2TestLO, z2TestGO > SparseMatrix
Tpetra::Vector< z2TestScalar, z2TestLO, z2TestGO > Vector
Tpetra::Vector< int, z2TestLO, z2TestGO > IntVector