10 #include "Tpetra_Core.hpp"
11 #include "Kokkos_Random.hpp"
19 using gno_t =
typename map_t::global_ordinal_type;
34 int me = comm->getRank();
35 int np = comm->getSize();
38 bool distributeInput =
true;
39 size_t xdim = 10, ydim = 11, zdim = 12;
41 Teuchos::CommandLineProcessor cmdp(
false,
false);
42 cmdp.setOption(
"file", &matrixFileName,
43 "Name of the Matrix Market file to use");
44 cmdp.setOption(
"xdim", &xdim,
45 "Number of nodes in x-direction for generated matrix");
46 cmdp.setOption(
"ydim", &ydim,
47 "Number of nodes in y-direction for generated matrix");
48 cmdp.setOption(
"zdim", &zdim,
49 "Number of nodes in z-direction for generated matrix");
50 cmdp.setOption(
"distribute",
"no-distribute", &distributeInput,
51 "Should Zoltan2 distribute the matrix as it is read?");
52 cmdp.setOption(
"symmetric",
"non-symmetric", &symmetric,
53 "Is the matrix symmetric?");
54 cmdp.parse(narg, arg);
57 if (matrixFileName !=
"") {
65 true, distributeInput);
73 size_t nIndices = std::max(JBlock->getGlobalNumCols(),
74 JBlock->getGlobalNumRows());
75 Teuchos::Array<gno_t> indices(nIndices);
77 Teuchos::RCP<const map_t> vMapCyclic =
78 getCyclicMap(JBlock->getGlobalNumCols(), indices, np-1, comm);
79 Teuchos::RCP<const map_t> wMapCyclic =
80 getCyclicMap(JBlock->getGlobalNumRows(), indices, np-2, comm);
85 #if KOKKOS_VERSION >= 40799
86 using IST =
typename KokkosKernels::ArithTraits<zscalar_t>::val_type;
88 using IST =
typename Kokkos::ArithTraits<zscalar_t>::val_type;
91 Kokkos::Random_XorShift64_Pool<execution_space_t>;
92 pool_type rand_pool(static_cast<uint64_t>(me));
94 Kokkos::fill_random(JBlock->getLocalMatrixDevice().values, rand_pool,
95 static_cast<IST
>(1.),
static_cast<IST
>(9999.));
96 JBlock->fillComplete();
100 RCP<const graph_t> block_graph = JBlock->getCrsGraph();
101 RCP<graph_t> cyclic_graph = rcp(
new graph_t(*block_graph));
102 cyclic_graph->resumeFill();
103 cyclic_graph->fillComplete(vMapCyclic, wMapCyclic);
104 JCyclic = rcp(
new matrix_t(cyclic_graph));
105 JCyclic->resumeFill();
106 TEUCHOS_ASSERT(block_graph->getLocalNumRows() == cyclic_graph->getLocalNumRows());
108 auto val_s = JBlock->getLocalMatrixHost().values;
109 auto val_d = JCyclic->getLocalMatrixHost().values;
110 TEUCHOS_ASSERT(val_s.extent(0) == val_d.extent(0));
111 Kokkos::deep_copy(val_d, val_s);
113 JCyclic->fillComplete();
117 bool run(
const char *testname, Teuchos::ParameterList ¶ms) {
121 params.set(
"symmetric", symmetric);
122 params.set(
"library",
"zoltan");
131 params.set(
"library",
"zoltan2");
144 const char *testname,
145 Teuchos::ParameterList ¶ms,
152 Teuchos::RCP<matrix_t> J = (useBlock ? JBlock : JCyclic);
153 int me = J->getRowMap()->getComm()->getRank();
155 std::cout << params.get(
"library",
"zoltan2") <<
" Running " << testname <<
" with "
156 << (useBlock ?
"Block maps" :
"Cyclic maps")
161 colorer.computeColoring(params);
164 if (!colorer.checkColoring()) {
165 std::cout << testname <<
" with "
166 << (useBlock ?
"Block maps" :
"Cyclic maps")
167 <<
" FAILED: invalid coloring returned"
175 const int numColors = colorer.getNumColors();
180 colorer.computeSeedMatrix(V);
189 Teuchos::RCP<matrix_t> Jp = rcp(
new matrix_t(*J, Teuchos::Copy));
190 Jp->setAllToScalar(static_cast<zscalar_t>(-1.));
192 colorer.reconstructMatrix(W, *Jp);
195 auto J_local_matrix = J->getLocalMatrixDevice();
196 auto Jp_local_matrix = Jp->getLocalMatrixDevice();
197 const size_t num_local_nz = J->getLocalNumEntries();
199 Kokkos::parallel_reduce(
200 "TpetraCrsColorer::testReconstructedMatrix()",
201 Kokkos::RangePolicy<execution_space_t>(0, num_local_nz),
202 KOKKOS_LAMBDA(
const size_t nz,
int &errorcnt) {
203 if (J_local_matrix.values(nz) != Jp_local_matrix.values(nz)) {
204 Kokkos::printf(
"Error in nonzero comparison %zu: %g != %g",
205 nz, J_local_matrix.values(nz), Jp_local_matrix.values(nz));
213 std::cout << testname <<
" FAILED on rank " << me <<
" with "
214 << (useBlock ?
"Block maps" :
"Cyclic maps")
226 Teuchos::RCP<const map_t> getCyclicMap(
228 Teuchos::Array<gno_t> &indices,
230 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm)
233 int me = comm->getRank();
234 int np = comm->getSize();
235 if (mapNumProc > np) mapNumProc = np;
236 if (mapNumProc <= 0) mapNumProc = 1;
238 for (
size_t i = 0; i < nIndices; i++)
239 if (me ==
int(i % np)) indices[cnt++] = i;
242 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
244 return rcp(
new map_t(dummy, indices(0,cnt), 0, comm));
250 Teuchos::RCP<matrix_t> JBlock;
251 Teuchos::RCP<matrix_t> JCyclic;
252 std::string matrixFileName;
259 Tpetra::ScopeGuard scope(&narg, &arg);
260 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
268 Teuchos::ParameterList coloring_params;
269 std::string matrixType =
"Jacobian";
270 bool symmetrize =
true;
272 coloring_params.set(
"MatrixType", matrixType);
273 coloring_params.set(
"symmetrize", symmetrize);
275 ok = testColorer.
run(
"Test One", coloring_params);
280 Teuchos::ParameterList coloring_params;
281 std::string matrixType =
"Jacobian";
282 bool symmetrize =
false;
284 coloring_params.set(
"MatrixType", matrixType);
285 coloring_params.set(
"symmetrize", symmetrize);
287 ok = testColorer.
run(
"Test Two", coloring_params);
292 Teuchos::ParameterList coloring_params;
293 std::string matrixType =
"Jacobian";
295 coloring_params.set(
"MatrixType", matrixType);
297 ok = testColorer.
run(
"Test Three", coloring_params);
302 Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_SUM, 1, &ierr, &gerr);
303 if (comm->getRank() == 0) {
305 std::cout <<
"TEST PASSED" << std::endl;
307 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