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 #if KOKKOS_VERSION >= 40799
61 using IST =
typename KokkosKernels::ArithTraits<zscalar_t>::val_type;
63 using IST =
typename Kokkos::ArithTraits<zscalar_t>::val_type;
66 Kokkos::Random_XorShift64_Pool<execution_space_t>;
67 pool_type rand_pool(static_cast<uint64_t>(me));
69 Kokkos::fill_random(JBlock->getLocalMatrixDevice().values, rand_pool,
70 static_cast<IST
>(1.),
static_cast<IST
>(9999.));
72 Teuchos::FancyOStream foo(Teuchos::rcp(&std::cout,
false));
73 JBlock->describe(foo, Teuchos::VERB_EXTREME);
79 size_t nIndices = std::max(JBlock->getGlobalNumCols(),
80 JBlock->getGlobalNumRows());
81 Teuchos::Array<gno_t> indices(nIndices);
83 Teuchos::RCP<const map_t> vMapCyclic =
84 getCyclicMap(JBlock->getGlobalNumCols(), indices, np-1,
86 Teuchos::RCP<const map_t> wMapCyclic =
87 getCyclicMap(JBlock->getGlobalNumRows(), indices, np-2,
91 RCP<const graph_t> block_graph = JBlock->getCrsGraph();
92 RCP<graph_t> cyclic_graph = rcp(
new graph_t(*block_graph));
93 cyclic_graph->resumeFill();
94 cyclic_graph->fillComplete(vMapCyclic, wMapCyclic);
95 JCyclic = rcp(
new matrix_t(cyclic_graph));
96 JCyclic->resumeFill();
97 TEUCHOS_ASSERT(block_graph->getLocalNumRows() ==
98 cyclic_graph->getLocalNumRows());
100 auto val_s = JBlock->getLocalMatrixHost().values;
101 auto val_d = JCyclic->getLocalMatrixHost().values;
102 TEUCHOS_ASSERT(val_s.extent(0) == val_d.extent(0));
103 Kokkos::deep_copy(val_d, val_s);
105 JCyclic->fillComplete();
106 JCyclic->describe(foo, Teuchos::VERB_EXTREME);
110 bool run(
const char* testname, Teuchos::ParameterList ¶ms) {
114 params.set(
"symmetric",
false);
127 const char *testname,
128 Teuchos::ParameterList ¶ms,
135 Teuchos::RCP<matrix_t> J = (useBlock ? JBlock : JCyclic);
136 int me = J->getRowMap()->getComm()->getRank();
138 std::cout <<
"Running " << testname <<
" with "
139 << (useBlock ?
"Block maps" :
"Cyclic maps")
144 colorer.computeColoring(params);
147 if (!colorer.checkColoring()) {
148 std::cout << testname <<
" with "
149 << (useBlock ?
"Block maps" :
"Cyclic maps")
150 <<
" FAILED: invalid coloring returned"
158 const int numColors = colorer.getNumColors();
163 colorer.computeSeedMatrix(V);
172 Teuchos::RCP<matrix_t> Jp = rcp(
new matrix_t(*J, Teuchos::Copy));
173 Jp->setAllToScalar(static_cast<zscalar_t>(-1.));
175 colorer.reconstructMatrix(W, *Jp);
178 auto J_local_matrix = J->getLocalMatrixDevice();
179 auto Jp_local_matrix = Jp->getLocalMatrixDevice();
180 const size_t num_local_nz = J->getLocalNumEntries();
182 Kokkos::parallel_reduce(
183 "TpetraCrsColorer::testReconstructedMatrix()",
184 Kokkos::RangePolicy<execution_space_t>(0, num_local_nz),
185 KOKKOS_LAMBDA(
const size_t nz,
int &errorcnt) {
186 if (J_local_matrix.values(nz) != Jp_local_matrix.values(nz)) {
187 Kokkos::printf(
"Error in nonzero comparison %zu: %g != %g",
188 nz, J_local_matrix.values(nz), Jp_local_matrix.values(nz));
196 std::cout << testname <<
" FAILED on rank " << me <<
" with "
197 << (useBlock ?
"Block maps" :
"Cyclic maps")
209 Teuchos::RCP<const map_t> getCyclicMap(
211 Teuchos::Array<gno_t> &indices,
214 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm)
217 int me = comm->getRank();
218 int np = comm->getSize();
219 if (mapNumProc > np) mapNumProc = np;
220 if (mapNumProc <= 0) mapNumProc = 1;
222 for (
size_t i = 0; i < nIndices; i++)
223 if (me ==
int(i % np)) indices[cnt++] = multiple*i;
226 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
228 return rcp(
new map_t(dummy, indices(0,cnt), 0, comm));
234 Teuchos::RCP<matrix_t> JBlock;
235 Teuchos::RCP<matrix_t> JCyclic;
240 int doTheTest(
const Teuchos::RCP<
const Teuchos::Comm<int> > &comm,
int multiple)
249 Teuchos::ParameterList coloring_params;
250 std::string matrixType =
"Jacobian";
251 bool symmetrize =
true;
253 coloring_params.set(
"MatrixType", matrixType);
254 coloring_params.set(
"symmetrize", symmetrize);
256 ok = testColorer.
run(
"Test One", coloring_params);
261 Teuchos::ParameterList coloring_params;
262 std::string matrixType =
"Jacobian";
263 bool symmetrize =
false;
265 coloring_params.set(
"MatrixType", matrixType);
266 coloring_params.set(
"symmetrize", symmetrize);
268 ok = testColorer.
run(
"Test Two", coloring_params);
273 Teuchos::ParameterList coloring_params;
274 std::string matrixType =
"Jacobian";
276 coloring_params.set(
"MatrixType", matrixType);
278 ok = testColorer.
run(
"Test Three", coloring_params);
287 Tpetra::ScopeGuard scope(&narg, &arg);
288 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
297 Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_SUM, 1, &ierr, &gerr);
298 if (comm->getRank() == 0) {
300 std::cout <<
"TEST PASSED" << std::endl;
302 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