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 std::string filename =
"";
32 size_t xdim = 10, ydim = 11, zdim = 12;
34 Teuchos::CommandLineProcessor cmdp(
false,
false);
35 cmdp.setOption(
"file", &filename,
36 "Name of the Matrix Market file to use");
37 cmdp.setOption(
"xdim", &xdim,
38 "Number of nodes in x-direction for generated matrix");
39 cmdp.setOption(
"ydim", &ydim,
40 "Number of nodes in y-direction for generated matrix");
41 cmdp.setOption(
"zdim", &zdim,
42 "Number of nodes in z-direction for generated matrix");
43 cmdp.setOption(
"distribute",
"no-distribute", &distributeInput,
44 "Should Zoltan2 distribute the matrix as it is read?");
45 cmdp.setOption(
"symmetric",
"non-symmetric", &symmetric,
46 "Is the matrix symmetric?");
47 cmdp.parse(narg, arg);
58 true, distributeInput);
66 size_t nIndices = std::max(JBlock->getGlobalNumCols(),
67 JBlock->getGlobalNumRows());
68 Teuchos::Array<gno_t> indices(nIndices);
70 Teuchos::RCP<const map_t> vMapCyclic =
71 getCyclicMap(JBlock->getGlobalNumCols(), indices, np-1, comm);
72 Teuchos::RCP<const map_t> wMapCyclic =
73 getCyclicMap(JBlock->getGlobalNumRows(), indices, np-2, comm);
78 using IST =
typename Kokkos::Details::ArithTraits<zscalar_t>::val_type;
80 Kokkos::Random_XorShift64_Pool<execution_space_t>;
81 pool_type rand_pool(
static_cast<uint64_t
>(me));
83 Kokkos::fill_random(JBlock->getLocalMatrixDevice().values, rand_pool,
84 static_cast<IST
>(1.),
static_cast<IST
>(9999.));
85 JBlock->fillComplete();
89 RCP<const graph_t> block_graph = JBlock->getCrsGraph();
90 RCP<graph_t> cyclic_graph = rcp(
new graph_t(*block_graph));
91 cyclic_graph->resumeFill();
92 cyclic_graph->fillComplete(vMapCyclic, wMapCyclic);
93 JCyclic = rcp(
new matrix_t(cyclic_graph));
94 JCyclic->resumeFill();
95 TEUCHOS_ASSERT(block_graph->getLocalNumRows() == cyclic_graph->getLocalNumRows());
97 auto val_s = JBlock->getLocalMatrixHost().values;
98 auto val_d = JCyclic->getLocalMatrixHost().values;
99 TEUCHOS_ASSERT(val_s.extent(0) == val_d.extent(0));
100 Kokkos::deep_copy(val_d, val_s);
102 JCyclic->fillComplete();
106 bool run(
const char* testname, Teuchos::ParameterList ¶ms) {
110 params.set(
"symmetric", symmetric);
123 const char *testname,
124 Teuchos::ParameterList ¶ms,
131 Teuchos::RCP<matrix_t> J = (useBlock ? JBlock : JCyclic);
132 int me = J->getRowMap()->getComm()->getRank();
134 std::cout <<
"Running " << testname <<
" with "
135 << (useBlock ?
"Block maps" :
"Cyclic maps")
140 colorer.computeColoring(params);
143 if (!colorer.checkColoring()) {
144 std::cout << testname <<
" with "
145 << (useBlock ?
"Block maps" :
"Cyclic maps")
146 <<
" FAILED: invalid coloring returned"
154 const int numColors = colorer.getNumColors();
159 colorer.computeSeedMatrix(V);
168 Teuchos::RCP<matrix_t> Jp = rcp(
new matrix_t(*J, Teuchos::Copy));
169 Jp->setAllToScalar(
static_cast<zscalar_t>(-1.));
171 colorer.reconstructMatrix(W, *Jp);
174 auto J_local_matrix = J->getLocalMatrixDevice();
175 auto Jp_local_matrix = Jp->getLocalMatrixDevice();
176 const size_t num_local_nz = J->getLocalNumEntries();
178 Kokkos::parallel_reduce(
179 "TpetraCrsColorer::testReconstructedMatrix()",
180 Kokkos::RangePolicy<execution_space_t>(0, num_local_nz),
181 KOKKOS_LAMBDA(
const size_t nz,
int &errorcnt) {
182 if (J_local_matrix.values(nz) != Jp_local_matrix.values(nz)) {
183 printf(
"Error in nonzero comparison %zu: %g != %g",
184 nz, J_local_matrix.values(nz), Jp_local_matrix.values(nz));
192 std::cout << testname <<
" FAILED on rank " << me <<
" with "
193 << (useBlock ?
"Block maps" :
"Cyclic maps")
205 Teuchos::RCP<const map_t> getCyclicMap(
207 Teuchos::Array<gno_t> &indices,
209 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm)
212 int me = comm->getRank();
213 int np = comm->getSize();
214 if (mapNumProc > np) mapNumProc = np;
215 if (mapNumProc <= 0) mapNumProc = 1;
217 for (
size_t i = 0; i < nIndices; i++)
218 if (me ==
int(i % np)) indices[cnt++] = i;
220 Tpetra::global_size_t dummy =
221 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
223 return rcp(
new map_t(dummy, indices(0,cnt), 0, comm));
229 Teuchos::RCP<matrix_t> JBlock;
230 Teuchos::RCP<matrix_t> JCyclic;
237 Tpetra::ScopeGuard scope(&narg, &arg);
238 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
246 Teuchos::ParameterList coloring_params;
247 std::string matrixType =
"Jacobian";
248 bool symmetrize =
true;
250 coloring_params.set(
"MatrixType", matrixType);
251 coloring_params.set(
"symmetrize", symmetrize);
253 ok = testColorer.
run(
"Test One", coloring_params);
258 Teuchos::ParameterList coloring_params;
259 std::string matrixType =
"Jacobian";
260 bool symmetrize =
false;
262 coloring_params.set(
"MatrixType", matrixType);
263 coloring_params.set(
"symmetrize", symmetrize);
265 ok = testColorer.
run(
"Test Two", coloring_params);
270 Teuchos::ParameterList coloring_params;
271 std::string matrixType =
"Jacobian";
273 coloring_params.set(
"MatrixType", matrixType);
275 ok = testColorer.
run(
"Test Three", coloring_params);
280 Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_SUM, 1, &ierr, &gerr);
281 if (comm->getRank() == 0) {
283 std::cout <<
"TEST PASSED" << std::endl;
285 std::cout <<
"TEST FAILED" << std::endl;
common code used by tests
ColorerTest(Teuchos::RCP< const Teuchos::Comm< int > > &comm, int narg, char **arg)
bool run(const char *testname, Teuchos::ParameterList ¶ms)
Tpetra::CrsMatrix< zscalar_t > matrix_t
typename map_t::global_ordinal_type gno_t
bool buildAndCheckSeedMatrix(const char *testname, Teuchos::ParameterList ¶ms, const bool useBlock)
Tpetra::MultiVector< zscalar_t > multivector_t
typename matrix_t::device_type::execution_space execution_space_t
Tpetra::CrsGraph<> graph_t