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 using IST =
typename Kokkos::ArithTraits<zscalar_t>::val_type;
87 Kokkos::Random_XorShift64_Pool<execution_space_t>;
88 pool_type rand_pool(static_cast<uint64_t>(me));
90 Kokkos::fill_random(JBlock->getLocalMatrixDevice().values, rand_pool,
91 static_cast<IST
>(1.),
static_cast<IST
>(9999.));
92 JBlock->fillComplete();
96 RCP<const graph_t> block_graph = JBlock->getCrsGraph();
97 RCP<graph_t> cyclic_graph = rcp(
new graph_t(*block_graph));
98 cyclic_graph->resumeFill();
99 cyclic_graph->fillComplete(vMapCyclic, wMapCyclic);
100 JCyclic = rcp(
new matrix_t(cyclic_graph));
101 JCyclic->resumeFill();
102 TEUCHOS_ASSERT(block_graph->getLocalNumRows() == cyclic_graph->getLocalNumRows());
104 auto val_s = JBlock->getLocalMatrixHost().values;
105 auto val_d = JCyclic->getLocalMatrixHost().values;
106 TEUCHOS_ASSERT(val_s.extent(0) == val_d.extent(0));
107 Kokkos::deep_copy(val_d, val_s);
109 JCyclic->fillComplete();
113 bool run(
const char *testname, Teuchos::ParameterList ¶ms) {
117 params.set(
"symmetric", symmetric);
118 params.set(
"library",
"zoltan");
127 params.set(
"library",
"zoltan2");
140 const char *testname,
141 Teuchos::ParameterList ¶ms,
148 Teuchos::RCP<matrix_t> J = (useBlock ? JBlock : JCyclic);
149 int me = J->getRowMap()->getComm()->getRank();
151 std::cout << params.get(
"library",
"zoltan2") <<
" Running " << testname <<
" with "
152 << (useBlock ?
"Block maps" :
"Cyclic maps")
157 colorer.computeColoring(params);
160 if (!colorer.checkColoring()) {
161 std::cout << testname <<
" with "
162 << (useBlock ?
"Block maps" :
"Cyclic maps")
163 <<
" FAILED: invalid coloring returned"
171 const int numColors = colorer.getNumColors();
176 colorer.computeSeedMatrix(V);
185 Teuchos::RCP<matrix_t> Jp = rcp(
new matrix_t(*J, Teuchos::Copy));
186 Jp->setAllToScalar(static_cast<zscalar_t>(-1.));
188 colorer.reconstructMatrix(W, *Jp);
191 auto J_local_matrix = J->getLocalMatrixDevice();
192 auto Jp_local_matrix = Jp->getLocalMatrixDevice();
193 const size_t num_local_nz = J->getLocalNumEntries();
195 Kokkos::parallel_reduce(
196 "TpetraCrsColorer::testReconstructedMatrix()",
197 Kokkos::RangePolicy<execution_space_t>(0, num_local_nz),
198 KOKKOS_LAMBDA(
const size_t nz,
int &errorcnt) {
199 if (J_local_matrix.values(nz) != Jp_local_matrix.values(nz)) {
200 Kokkos::printf(
"Error in nonzero comparison %zu: %g != %g",
201 nz, J_local_matrix.values(nz), Jp_local_matrix.values(nz));
209 std::cout << testname <<
" FAILED on rank " << me <<
" with "
210 << (useBlock ?
"Block maps" :
"Cyclic maps")
222 Teuchos::RCP<const map_t> getCyclicMap(
224 Teuchos::Array<gno_t> &indices,
226 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm)
229 int me = comm->getRank();
230 int np = comm->getSize();
231 if (mapNumProc > np) mapNumProc = np;
232 if (mapNumProc <= 0) mapNumProc = 1;
234 for (
size_t i = 0; i < nIndices; i++)
235 if (me ==
int(i % np)) indices[cnt++] = i;
238 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
240 return rcp(
new map_t(dummy, indices(0,cnt), 0, comm));
246 Teuchos::RCP<matrix_t> JBlock;
247 Teuchos::RCP<matrix_t> JCyclic;
248 std::string matrixFileName;
255 Tpetra::ScopeGuard scope(&narg, &arg);
256 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
264 Teuchos::ParameterList coloring_params;
265 std::string matrixType =
"Jacobian";
266 bool symmetrize =
true;
268 coloring_params.set(
"MatrixType", matrixType);
269 coloring_params.set(
"symmetrize", symmetrize);
271 ok = testColorer.
run(
"Test One", coloring_params);
276 Teuchos::ParameterList coloring_params;
277 std::string matrixType =
"Jacobian";
278 bool symmetrize =
false;
280 coloring_params.set(
"MatrixType", matrixType);
281 coloring_params.set(
"symmetrize", symmetrize);
283 ok = testColorer.
run(
"Test Two", coloring_params);
288 Teuchos::ParameterList coloring_params;
289 std::string matrixType =
"Jacobian";
291 coloring_params.set(
"MatrixType", matrixType);
293 ok = testColorer.
run(
"Test Three", coloring_params);
298 Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_SUM, 1, &ierr, &gerr);
299 if (comm->getRank() == 0) {
301 std::cout <<
"TEST PASSED" << std::endl;
303 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