15 #include <Teuchos_ParameterList.hpp>
16 #include <Teuchos_RCP.hpp>
17 #include <Teuchos_CommandLineProcessor.hpp>
18 #include <Tpetra_CrsMatrix.hpp>
19 #include <Tpetra_Vector.hpp>
20 #include <Galeri_XpetraMaps.hpp>
21 #include <Galeri_XpetraProblemFactory.hpp>
30 int main(
int narg,
char** arg)
33 Tpetra::ScopeGuard tscope(&narg, &arg);
34 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
35 int me = comm->getRank();
39 typedef Tpetra::Map<> Map_t;
40 typedef Map_t::local_ordinal_type localId_t;
41 typedef Map_t::global_ordinal_type globalId_t;
42 typedef Tpetra::Details::DefaultTypes::scalar_type scalar_t;
43 typedef Tpetra::CrsMatrix<scalar_t, localId_t, globalId_t> Matrix_t;
44 typedef Tpetra::MultiVector<scalar_t, localId_t, globalId_t> MultiVector_t;
45 typedef Tpetra::Vector<scalar_t, localId_t, globalId_t> Vector_t;
52 std::string method =
"scotch";
53 globalId_t nx = 50, ny = 40, nz = 30;
57 Teuchos::CommandLineProcessor cmdp (
false,
false);
58 cmdp.setOption(
"method", &method,
59 "Partitioning method to use: scotch or parmetis.");
60 cmdp.setOption(
"nx", &nx,
61 "number of gridpoints in X dimension for "
62 "mesh used to generate matrix; must be >= 1.");
63 cmdp.setOption(
"ny", &ny,
64 "number of gridpoints in Y dimension for "
65 "mesh used to generate matrix; must be >= 1.");
66 cmdp.setOption(
"nz", &nz,
67 "number of gridpoints in Z dimension for "
68 "mesh used to generate matrix; must be >= 1.");
69 cmdp.parse(narg, arg);
71 if ((nx < 1) || (ny < 1) || (nz < 1)) {
72 std::cout <<
"Input error: nx, ny and nz must be >= 1" << std::endl;
80 Teuchos::ParameterList galeriList;
81 galeriList.set(
"nx", nx);
82 galeriList.set(
"ny", ny);
83 galeriList.set(
"nz", nz);
86 RCP<Matrix_t> origMatrix;
89 RCP<const Map_t> map = rcp(
new Map_t(nGlobalElements, 0, comm));
91 typedef Galeri::Xpetra::Problem<Map_t,Matrix_t,MultiVector_t> Galeri_t;
92 RCP<Galeri_t> galeriProblem =
93 Galeri::Xpetra::BuildProblem<scalar_t, localId_t, globalId_t,
94 Map_t, Matrix_t, MultiVector_t>
95 (
"Laplace3D", map, galeriList);
96 origMatrix = galeriProblem->BuildMatrix();
98 catch (std::exception &e) {
99 std::cout <<
"Exception in Galeri matrix generation. " << e.what() << std::endl;
104 std::cout <<
"NumRows = " << origMatrix->getGlobalNumRows() << std::endl
105 <<
"NumNonzeros = " << origMatrix->getGlobalNumEntries() << std::endl
106 <<
"NumProcs = " << comm->getSize() << std::endl;
109 RCP<Vector_t> origVector, origProd;
110 origProd = Tpetra::createVector<scalar_t,localId_t,globalId_t>(
111 origMatrix->getRangeMap());
112 origVector = Tpetra::createVector<scalar_t,localId_t,globalId_t>(
113 origMatrix->getDomainMap());
114 origVector->randomize();
117 Teuchos::ParameterList param;
118 param.set(
"partitioning_approach",
"partition");
119 param.set(
"algorithm", method);
122 MatrixAdapter_t adapter(origMatrix);
130 catch (std::exception &e) {
131 std::cout <<
"Exception returned from solve(). " << e.what() << std::endl;
138 if (me == 0) std::cout <<
"Redistributing matrix..." << std::endl;
139 RCP<Matrix_t> redistribMatrix;
140 adapter.applyPartitioningSolution(*origMatrix, redistribMatrix,
143 if (me == 0) std::cout <<
"Redistributing vectors..." << std::endl;
144 RCP<Vector_t> redistribVector;
145 MultiVectorAdapter_t adapterVector(origVector);
150 RCP<Vector_t> redistribProd;
151 redistribProd = Tpetra::createVector<scalar_t,localId_t,globalId_t>(
152 redistribMatrix->getRangeMap());
156 if (origMatrix->getGlobalNumRows() <= 50) {
157 std::cout << me <<
" ORIGINAL: ";
158 for (
size_t i = 0; i < origVector->getLocalLength(); i++)
159 std::cout << origVector->getMap()->getGlobalElement(i) <<
" ";
160 std::cout << std::endl;
161 std::cout << me <<
" REDISTRIB: ";
162 for (
size_t i = 0; i < redistribVector->getLocalLength(); i++)
163 std::cout << redistribVector->getMap()->getGlobalElement(i) <<
" ";
164 std::cout << std::endl;
171 if (me == 0) std::cout <<
"Matvec original..." << std::endl;
172 origMatrix->apply(*origVector, *origProd);
173 scalar_t origNorm = origProd->norm2();
175 std::cout <<
"Norm of Original matvec prod: " << origNorm << std::endl;
177 if (me == 0) std::cout <<
"Matvec redistributed..." << std::endl;
178 redistribMatrix->apply(*redistribVector, *redistribProd);
179 scalar_t redistribNorm = redistribProd->norm2();
181 std::cout <<
"Norm of Redistributed matvec prod: " << redistribNorm << std::endl;
184 const scalar_t
epsilon = 0.001;
185 if (redistribNorm > origNorm+epsilon || redistribNorm < origNorm-epsilon)
186 std::cout <<
"Mat-Vec product changed; FAIL" << std::endl;
188 std::cout <<
"PASS" << std::endl;
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
int main(int narg, char **arg)
Defines the XpetraMultiVectorAdapter.
Defines the XpetraCrsMatrixAdapter class.
void applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
Zoltan2::XpetraMultiVectorAdapter< tMVector_t > MultiVectorAdapter_t
An adapter for Xpetra::MultiVector.
const PartitioningSolution< Adapter > & getSolution()
Get the solution to the problem.
PartitioningProblem sets up partitioning problems for the user.
Tpetra::global_size_t global_size_t
Defines the PartitioningProblem class.
void solve(bool updateInputData=true)
Direct the problem to create a solution.