10 #include "Tpetra_Core.hpp"
11 #include "Kokkos_Random.hpp"
19 using gno_t =
typename map_t::global_ordinal_type;
28 ColorerTest(
const Teuchos::RCP<
const Teuchos::Comm<int> > &comm,
int multiple)
30 int me = comm->getRank();
31 int np = comm->getSize();
35 Teuchos::Array<gno_t> myRows(myNrows);
36 for (
size_t i = 0; i < myNrows; i++) {
37 myRows[i] = multiple * (me * myNrows + i);
41 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
42 Teuchos::RCP<const map_t> map = rcp(
new map_t(dummy, myRows, 0, comm));
45 JBlock = rcp(
new matrix_t(map, nnz));
46 Teuchos::Array<gno_t> myCols(nnz);
47 Teuchos::Array<zscalar_t> myVals(nnz);
49 for (
size_t i = 0; i < myNrows; i++) {
50 auto gid = map->getGlobalElement(i);
53 if (gid+multiple <= map->getMaxAllGlobalIndex())
54 myCols[cnt++] = gid+multiple;
55 JBlock->insertGlobalValues(gid, myCols(0,cnt), myVals(0, cnt));
57 JBlock->fillComplete();
60 using IST =
typename Kokkos::ArithTraits<zscalar_t>::val_type;
62 Kokkos::Random_XorShift64_Pool<execution_space_t>;
63 pool_type rand_pool(static_cast<uint64_t>(me));
65 Kokkos::fill_random(JBlock->getLocalMatrixDevice().values, rand_pool,
66 static_cast<IST
>(1.),
static_cast<IST
>(9999.));
68 Teuchos::FancyOStream foo(Teuchos::rcp(&std::cout,
false));
69 JBlock->describe(foo, Teuchos::VERB_EXTREME);
75 size_t nIndices = std::max(JBlock->getGlobalNumCols(),
76 JBlock->getGlobalNumRows());
77 Teuchos::Array<gno_t> indices(nIndices);
79 Teuchos::RCP<const map_t> vMapCyclic =
80 getCyclicMap(JBlock->getGlobalNumCols(), indices, np-1,
82 Teuchos::RCP<const map_t> wMapCyclic =
83 getCyclicMap(JBlock->getGlobalNumRows(), indices, np-2,
87 RCP<const graph_t> block_graph = JBlock->getCrsGraph();
88 RCP<graph_t> cyclic_graph = rcp(
new graph_t(*block_graph));
89 cyclic_graph->resumeFill();
90 cyclic_graph->fillComplete(vMapCyclic, wMapCyclic);
91 JCyclic = rcp(
new matrix_t(cyclic_graph));
92 JCyclic->resumeFill();
93 TEUCHOS_ASSERT(block_graph->getLocalNumRows() ==
94 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();
102 JCyclic->describe(foo, Teuchos::VERB_EXTREME);
106 bool run(
const char* testname, Teuchos::ParameterList ¶ms) {
110 params.set(
"symmetric",
false);
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 Kokkos::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,
210 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm)
213 int me = comm->getRank();
214 int np = comm->getSize();
215 if (mapNumProc > np) mapNumProc = np;
216 if (mapNumProc <= 0) mapNumProc = 1;
218 for (
size_t i = 0; i < nIndices; i++)
219 if (me ==
int(i % np)) indices[cnt++] = multiple*i;
222 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
224 return rcp(
new map_t(dummy, indices(0,cnt), 0, comm));
230 Teuchos::RCP<matrix_t> JBlock;
231 Teuchos::RCP<matrix_t> JCyclic;
236 int doTheTest(
const Teuchos::RCP<
const Teuchos::Comm<int> > &comm,
int multiple)
245 Teuchos::ParameterList coloring_params;
246 std::string matrixType =
"Jacobian";
247 bool symmetrize =
true;
249 coloring_params.set(
"MatrixType", matrixType);
250 coloring_params.set(
"symmetrize", symmetrize);
252 ok = testColorer.
run(
"Test One", coloring_params);
257 Teuchos::ParameterList coloring_params;
258 std::string matrixType =
"Jacobian";
259 bool symmetrize =
false;
261 coloring_params.set(
"MatrixType", matrixType);
262 coloring_params.set(
"symmetrize", symmetrize);
264 ok = testColorer.
run(
"Test Two", coloring_params);
269 Teuchos::ParameterList coloring_params;
270 std::string matrixType =
"Jacobian";
272 coloring_params.set(
"MatrixType", matrixType);
274 ok = testColorer.
run(
"Test Three", coloring_params);
283 Tpetra::ScopeGuard scope(&narg, &arg);
284 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
293 Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_SUM, 1, &ierr, &gerr);
294 if (comm->getRank() == 0) {
296 std::cout <<
"TEST PASSED" << std::endl;
298 std::cout <<
"TEST FAILED" << std::endl;
Tpetra::CrsMatrix< zscalar_t > matrix_t
int doTheTest(const Teuchos::RCP< const Teuchos::Comm< int > > &comm, int multiple)
typename matrix_t::device_type::execution_space execution_space_t
int main(int narg, char **arg)
common code used by tests
Tpetra::MultiVector< zscalar_t > multivector_t
ColorerTest(const Teuchos::RCP< const Teuchos::Comm< int > > &comm, int multiple)
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