2 #include "Tpetra_Core.hpp"
3 #include "Kokkos_Random.hpp"
11 using gno_t =
typename map_t::global_ordinal_type;
26 int me = comm->getRank();
27 int np = comm->getSize();
30 bool distributeInput =
true;
31 size_t xdim = 10, ydim = 11, zdim = 12;
33 Teuchos::CommandLineProcessor cmdp(
false,
false);
34 cmdp.setOption(
"file", &matrixFileName,
35 "Name of the Matrix Market file to use");
36 cmdp.setOption(
"xdim", &xdim,
37 "Number of nodes in x-direction for generated matrix");
38 cmdp.setOption(
"ydim", &ydim,
39 "Number of nodes in y-direction for generated matrix");
40 cmdp.setOption(
"zdim", &zdim,
41 "Number of nodes in z-direction for generated matrix");
42 cmdp.setOption(
"distribute",
"no-distribute", &distributeInput,
43 "Should Zoltan2 distribute the matrix as it is read?");
44 cmdp.setOption(
"symmetric",
"non-symmetric", &symmetric,
45 "Is the matrix symmetric?");
46 cmdp.parse(narg, arg);
49 if (matrixFileName !=
"") {
57 true, distributeInput);
65 size_t nIndices = std::max(JBlock->getGlobalNumCols(),
66 JBlock->getGlobalNumRows());
67 Teuchos::Array<gno_t> indices(nIndices);
69 Teuchos::RCP<const map_t> vMapCyclic =
70 getCyclicMap(JBlock->getGlobalNumCols(), indices, np-1, comm);
71 Teuchos::RCP<const map_t> wMapCyclic =
72 getCyclicMap(JBlock->getGlobalNumRows(), indices, np-2, comm);
77 using IST =
typename Kokkos::ArithTraits<zscalar_t>::val_type;
79 Kokkos::Random_XorShift64_Pool<execution_space_t>;
80 pool_type rand_pool(static_cast<uint64_t>(me));
82 Kokkos::fill_random(JBlock->getLocalMatrixDevice().values, rand_pool,
83 static_cast<IST
>(1.),
static_cast<IST
>(9999.));
84 JBlock->fillComplete();
88 RCP<const graph_t> block_graph = JBlock->getCrsGraph();
89 RCP<graph_t> cyclic_graph = rcp(
new graph_t(*block_graph));
90 cyclic_graph->resumeFill();
91 cyclic_graph->fillComplete(vMapCyclic, wMapCyclic);
92 JCyclic = rcp(
new matrix_t(cyclic_graph));
93 JCyclic->resumeFill();
94 TEUCHOS_ASSERT(block_graph->getLocalNumRows() == cyclic_graph->getLocalNumRows());
96 auto val_s = JBlock->getLocalMatrixHost().values;
97 auto val_d = JCyclic->getLocalMatrixHost().values;
98 TEUCHOS_ASSERT(val_s.extent(0) == val_d.extent(0));
99 Kokkos::deep_copy(val_d, val_s);
101 JCyclic->fillComplete();
105 bool run(
const char *testname, Teuchos::ParameterList ¶ms) {
109 params.set(
"symmetric", symmetric);
110 params.set(
"library",
"zoltan");
119 params.set(
"library",
"zoltan2");
132 const char *testname,
133 Teuchos::ParameterList ¶ms,
140 Teuchos::RCP<matrix_t> J = (useBlock ? JBlock : JCyclic);
141 int me = J->getRowMap()->getComm()->getRank();
143 std::cout << params.get(
"library",
"zoltan2") <<
" Running " << testname <<
" with "
144 << (useBlock ?
"Block maps" :
"Cyclic maps")
149 colorer.computeColoring(params);
152 if (!colorer.checkColoring()) {
153 std::cout << testname <<
" with "
154 << (useBlock ?
"Block maps" :
"Cyclic maps")
155 <<
" FAILED: invalid coloring returned"
163 const int numColors = colorer.getNumColors();
168 colorer.computeSeedMatrix(V);
177 Teuchos::RCP<matrix_t> Jp = rcp(
new matrix_t(*J, Teuchos::Copy));
178 Jp->setAllToScalar(static_cast<zscalar_t>(-1.));
180 colorer.reconstructMatrix(W, *Jp);
183 auto J_local_matrix = J->getLocalMatrixDevice();
184 auto Jp_local_matrix = Jp->getLocalMatrixDevice();
185 const size_t num_local_nz = J->getLocalNumEntries();
187 Kokkos::parallel_reduce(
188 "TpetraCrsColorer::testReconstructedMatrix()",
189 Kokkos::RangePolicy<execution_space_t>(0, num_local_nz),
190 KOKKOS_LAMBDA(
const size_t nz,
int &errorcnt) {
191 if (J_local_matrix.values(nz) != Jp_local_matrix.values(nz)) {
192 Kokkos::printf(
"Error in nonzero comparison %zu: %g != %g",
193 nz, J_local_matrix.values(nz), Jp_local_matrix.values(nz));
201 std::cout << testname <<
" FAILED on rank " << me <<
" with "
202 << (useBlock ?
"Block maps" :
"Cyclic maps")
214 Teuchos::RCP<const map_t> getCyclicMap(
216 Teuchos::Array<gno_t> &indices,
218 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm)
221 int me = comm->getRank();
222 int np = comm->getSize();
223 if (mapNumProc > np) mapNumProc = np;
224 if (mapNumProc <= 0) mapNumProc = 1;
226 for (
size_t i = 0; i < nIndices; i++)
227 if (me ==
int(i % np)) indices[cnt++] = i;
230 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
232 return rcp(
new map_t(dummy, indices(0,cnt), 0, comm));
238 Teuchos::RCP<matrix_t> JBlock;
239 Teuchos::RCP<matrix_t> JCyclic;
240 std::string matrixFileName;
247 Tpetra::ScopeGuard scope(&narg, &arg);
248 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
256 Teuchos::ParameterList coloring_params;
257 std::string matrixType =
"Jacobian";
258 bool symmetrize =
true;
260 coloring_params.set(
"MatrixType", matrixType);
261 coloring_params.set(
"symmetrize", symmetrize);
263 ok = testColorer.
run(
"Test One", coloring_params);
268 Teuchos::ParameterList coloring_params;
269 std::string matrixType =
"Jacobian";
270 bool symmetrize =
false;
272 coloring_params.set(
"MatrixType", matrixType);
273 coloring_params.set(
"symmetrize", symmetrize);
275 ok = testColorer.
run(
"Test Two", coloring_params);
280 Teuchos::ParameterList coloring_params;
281 std::string matrixType =
"Jacobian";
283 coloring_params.set(
"MatrixType", matrixType);
285 ok = testColorer.
run(
"Test Three", coloring_params);
290 Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_SUM, 1, &ierr, &gerr);
291 if (comm->getRank() == 0) {
293 std::cout <<
"TEST PASSED" << std::endl;
295 std::cout <<
"TEST FAILED" << std::endl;
Tpetra::CrsMatrix< zscalar_t > matrix_t
typename matrix_t::device_type::execution_space execution_space_t
ColorerTest(Teuchos::RCP< const Teuchos::Comm< int > > &comm, int narg, char **arg)
int main(int narg, char **arg)
common code used by tests
Tpetra::MultiVector< zscalar_t > multivector_t
bool run(const char *testname, Teuchos::ParameterList ¶ms)
typename map_t::global_ordinal_type gno_t
bool buildAndCheckSeedMatrix(const char *testname, Teuchos::ParameterList ¶ms, const bool useBlock)
Tpetra::global_size_t global_size_t
Tpetra::CrsGraph<> graph_t