14 #include "Teuchos_RCP.hpp"
15 #include "Teuchos_ParameterList.hpp"
16 #include "Teuchos_TestForException.hpp"
18 #include "Tpetra_Map.hpp"
19 #include "Tpetra_MultiVector.hpp"
20 #include "Tpetra_CrsMatrix.hpp"
21 #include "Tpetra_CrsGraph.hpp"
22 #include "Tpetra_BlockMultiVector.hpp"
23 #include "Tpetra_BlockCrsMatrix.hpp"
33 template <
typename CrsMatrixType>
38 typedef typename matrix_t::crs_graph_type
graph_t;
39 typedef typename matrix_t::scalar_type
scalar_t;
40 typedef typename matrix_t::local_ordinal_type
lno_t;
41 typedef typename matrix_t::global_ordinal_type
gno_t;
42 typedef typename matrix_t::node_type
node_t;
59 template <
typename MultiVectorType>
64 template <
typename MultiVectorType>
69 template <
typename MultiVectorType>
72 template <
typename MultiVectorType>
78 template <
typename MultiVectorType>
81 template <
typename MultiVectorType>
118 template <
typename SC,
typename LO,
typename GO,
typename NO>
122 typedef Tpetra::BlockCrsMatrix<SC, LO, GO, NO>
matrix_t;
124 typedef typename matrix_t::crs_graph_type
graph_t;
126 typedef typename matrix_t::local_ordinal_type
lno_t;
127 typedef typename matrix_t::global_ordinal_type
gno_t;
128 typedef typename matrix_t::node_type
node_t;
176 const int block_size =
matrix->getBlockSize();
177 const size_t block_col = col / block_size;
178 const int offset = col - block_col * block_size;
200 template <
typename CrsMatrixType>
202 const Teuchos::RCP<matrix_t> &matrix_
205 graph(matrix->getCrsGraph()),
207 list_of_colors_host(),
214 template <
typename CrsMatrixType>
217 Teuchos::ParameterList &coloring_params
220 const std::string library = coloring_params.get(
"library",
"zoltan");
222 if (library ==
"zoltan") {
226 num_colors, list_of_colors_host, list_of_colors);
232 num_colors, list_of_colors_host, list_of_colors);
237 template <
typename CrsMatrixType>
238 template <
typename MultiVectorType>
241 MultiVectorType &V,
const int color_beg)
const
243 MultiVectorType V_fitted(graph->getColMap(), V.getNumVectors());
245 computeSeedMatrixFitted(V_fitted, color_beg);
247 Tpetra::Import<lno_t, gno_t, node_t>
248 importer(graph->getColMap(), V.getMap());
250 V.doImport(V_fitted, importer, Tpetra::INSERT);
254 template <
typename CrsMatrixType>
255 template <
typename MultiVectorType>
258 MultiVectorType &V,
const int color_beg)
const
261 TEUCHOS_TEST_FOR_EXCEPTION(
262 !(graph->getColMap()->isLocallyFitted(*(V.getMap()))), std::domain_error,
263 "Map for supplied vector is not locally fitted to the column map of the"
265 "You must call the non-fitted version of this function.");
267 auto V_view_dev = V.getLocalViewDevice(Tpetra::Access::OverwriteAll);
268 const size_t num_local_cols = graph->getLocalNumCols();
269 const int chunk_size = V.getNumVectors();
272 Kokkos::parallel_for(
273 "TpetraCrsColorer::computeSeedMatrixFitted()",
274 Kokkos::RangePolicy<execution_space>(0, num_local_cols),
275 KOKKOS_LAMBDA(
const size_t i) {
276 const int color = my_list_of_colors[i] - 1 - color_beg;
277 if (color >= 0 && color < chunk_size)
278 V_view_dev(i, color) =
scalar_t(1.0);
284 template <
typename CrsMatrixType>
285 template <
typename MultiVectorType>
288 MultiVectorType &W,
const int color_beg)
const
290 reconstructMatrix(W, *matrix, color_beg);
294 template <
typename CrsMatrixType>
295 template <
typename MultiVectorType>
300 const int color_beg)
const
302 MultiVectorType W_fitted(graph->getRowMap(), W.getNumVectors());
304 Tpetra::Import<lno_t, gno_t, node_t>
305 importer(W.getMap(), graph->getRowMap());
307 W_fitted.doImport(W, importer, Tpetra::INSERT);
309 reconstructMatrixFitted(W_fitted, mat, color_beg);
313 template <
typename CrsMatrixType>
314 template <
typename MultiVectorType>
317 MultiVectorType &W,
const int color_beg)
const
319 reconstructMatrixFitted(W, *matrix, color_beg);
323 template <
typename CrsMatrixType>
324 template <
typename MultiVectorType>
329 const int color_beg)
const
332 TEUCHOS_TEST_FOR_EXCEPTION(
333 !(W.getMap()->isLocallyFitted(*(graph->getRowMap()))), std::domain_error,
334 "Row map of the Jacobian graph is not locally fitted to the vector's map."
335 " You must call the non-fitted version of this function.");
337 auto W_view_dev = W.getLocalViewDevice(Tpetra::Access::ReadOnly);
338 auto local_matrix = mat.getLocalMatrixDevice();
339 auto local_graph = local_matrix.graph;
340 const size_t num_local_rows = graph->getLocalNumRows();
341 const int chunk_size = W.getNumVectors();
344 Kokkos::parallel_for(
345 "TpetraCrsColorer::reconstructMatrixFitted()",
346 Kokkos::RangePolicy<execution_space>(0, num_local_rows),
347 KOKKOS_LAMBDA(
const size_t row) {
348 const size_t entry_begin = local_graph.row_map(row);
349 const size_t entry_end = local_graph.row_map(row + 1);
350 for (
size_t entry = entry_begin; entry < entry_end; entry++)
352 const size_t col = local_graph.entries(entry);
353 const int color = my_list_of_colors[col] - 1 - color_beg;
354 if (color >= 0 && color < chunk_size)
355 local_matrix.values(entry) = W_view_dev(row, color);
361 template <
typename SC,
typename LO,
typename GO,
typename NO>
363 const Teuchos::RCP<matrix_t> &matrix_
366 , graph(Teuchos::rcp(&(matrix->getCrsGraph()), false))
368 , list_of_colors_host()
374 template <
typename SC,
typename LO,
typename GO,
typename NO>
377 Teuchos::ParameterList &coloring_params
380 const std::string library = coloring_params.get(
"library",
"zoltan");
382 if (library ==
"zoltan") {
386 num_colors, list_of_colors_host, list_of_colors);
392 num_colors, list_of_colors_host, list_of_colors);
397 template <
typename SC,
typename LO,
typename GO,
typename NO>
402 multivector_t block_V_fitted(*(graph->getColMap()), matrix->getBlockSize(),
403 block_V.getNumVectors());
405 computeSeedMatrixFitted(block_V_fitted, color_beg);
407 const lno_t block_size = block_V.getBlockSize();
408 auto col_point_map = multivector_t::makePointMap(*graph->getColMap(),
410 auto blockV_point_map = multivector_t::makePointMap(*block_V.getMap(),
412 const auto col_point_map_rcp =
413 Teuchos::rcp(
new Tpetra::Map<LO, GO, NO>(col_point_map));
414 const auto blockV_point_map_rcp =
415 Teuchos::rcp(
new Tpetra::Map<LO, GO, NO>(blockV_point_map));
417 Tpetra::Import<LO, GO, NO> importer_point(col_point_map_rcp,
418 blockV_point_map_rcp);
420 block_V.getMultiVectorView().doImport(block_V_fitted.getMultiVectorView(),
421 importer_point, Tpetra::INSERT);
428 template <
typename SC,
typename LO,
typename GO,
typename NO>
434 TEUCHOS_TEST_FOR_EXCEPTION(
435 !(graph->getColMap()->isLocallyFitted(*(blockV.getMap()))),
437 "Map for supplied vector is not locally fitted to the column map of the"
439 "You must call the non-fitted version of this function.");
441 const lno_t block_size = blockV.getBlockSize();
442 auto V = blockV.getMultiVectorView();
443 auto V_view_dev = V.getLocalViewDevice(Tpetra::Access::ReadWrite);
444 const size_t num_local_cols = V_view_dev.extent(0) / block_size;
445 const int chunk_size = V.getNumVectors() / block_size;
446 const int block_color_beg = color_beg / block_size;
449 Kokkos::parallel_for(
450 "TpetraCrsColorer::computeSeedMatrixFitted()",
451 Kokkos::RangePolicy<execution_space>(0, num_local_cols),
452 KOKKOS_LAMBDA(
const size_t i) {
453 const int color = my_list_of_colors[i] - 1 - block_color_beg;
454 if (color >= 0 && color < chunk_size)
455 for (
lno_t j = 0; j < block_size; ++j)
456 V_view_dev(i*block_size+j, color*block_size+j) =
scalar_t(1.0);
462 template <
typename SC,
typename LO,
typename GO,
typename NO>
467 reconstructMatrix(W, *matrix, color_beg);
471 template <
typename SC,
typename LO,
typename GO,
typename NO>
476 const int color_beg)
const
478 multivector_t block_W_fitted(*(graph->getRowMap()), matrix->getBlockSize(),
479 block_W.getNumVectors());
481 const lno_t block_size = block_W.getBlockSize();
482 auto row_point_map = multivector_t::makePointMap(*graph->getRowMap(),
484 auto blockW_point_map = multivector_t::makePointMap(*block_W.getMap(),
486 const auto row_point_map_rcp =
487 Teuchos::rcp(
new Tpetra::Map<LO, GO, NO>(row_point_map));
488 const auto blockW_point_map_rcp =
489 Teuchos::rcp(
new Tpetra::Map<LO, GO, NO>(blockW_point_map));
491 Tpetra::Import<lno_t, gno_t, node_t>
492 importer_point(blockW_point_map_rcp, row_point_map_rcp);
494 block_W_fitted.getMultiVectorView().doImport(block_W.getMultiVectorView(),
495 importer_point, Tpetra::INSERT);
496 reconstructMatrixFitted(block_W_fitted, mat, color_beg);
500 template <
typename SC,
typename LO,
typename GO,
typename NO>
505 reconstructMatrixFitted(W, *matrix, color_beg);
509 template <
typename SC,
typename LO,
typename GO,
typename NO>
514 const int color_beg)
const
517 TEUCHOS_TEST_FOR_EXCEPTION(
518 !(block_W.getMap()->isLocallyFitted(*(graph->getRowMap()))),
520 "Row map of the Jacobian graph is not locally fitted to the vector's map."
521 " You must call the non-fitted version of this function.");
524 const lno_t block_size = block_W.getBlockSize();
525 const lno_t block_stride = block_size * block_size;
526 const lno_t block_row_stride = block_size;
528 auto W = block_W.getMultiVectorView();
529 auto W_view_dev = W.getLocalViewDevice(Tpetra::Access::ReadOnly);
530 auto matrix_vals = mat.getValuesDeviceNonConst();
531 auto local_graph = graph->getLocalGraphDevice();
532 const size_t num_local_rows = graph->getLocalNumRows();
533 const int chunk_size = W.getNumVectors() / block_size;
534 const int block_color_beg = color_beg / block_size;
537 Kokkos::parallel_for(
538 "TpetraCrsColorer::reconstructMatrix()",
539 Kokkos::RangePolicy<execution_space>(0, num_local_rows),
540 KOKKOS_LAMBDA(
const size_t block_row) {
541 const size_t entry_begin = local_graph.row_map(block_row);
542 const size_t entry_end = local_graph.row_map(block_row + 1);
543 for (
size_t block_entry = entry_begin; block_entry < entry_end;
546 const size_t block_col = local_graph.entries(block_entry);
547 const size_t block_offset = block_stride * block_entry;
548 const int block_color = my_list_of_colors[block_col] - 1 - block_color_beg;
549 if (block_color >= 0 && block_color < chunk_size)
551 for (
lno_t i = 0; i < block_size; ++i)
553 const size_t row = block_row * block_size + i;
554 for (
lno_t j = 0; j < block_size; ++j)
556 const size_t entry = block_offset + block_row_stride * i + j;
557 matrix_vals(entry) = W_view_dev(row, block_color*block_size+j);
void computeColoring(Teuchos::ParameterList &coloring_params, int &num_colors, list_of_colors_host_t &list_of_colors_host, list_of_colors_t &list_of_colors) const
matrix_t::global_ordinal_type gno_t
Tpetra::BlockCrsMatrix< SC, LO, GO, NO > matrix_t
Teuchos::RCP< matrix_t > matrix
list_of_colors_host_t list_of_colors_host
node_t::device_type device_t
void computeColoring(Teuchos::ParameterList &coloring_params)
matrix_t::scalar_type scalar_t
void computeColoring(Teuchos::ParameterList &coloring_params, int &num_colors, list_of_colors_host_t &list_of_colors_host, list_of_colors_t &list_of_colors)
matrix_t::local_ordinal_type lno_t
Kokkos::View< int *, device_t > list_of_colors_t
list_of_colors_t::HostMirror list_of_colors_host_t
matrix_t::local_ordinal_type lno_t
Teuchos::RCP< const graph_t > graph
void computeSeedMatrix(MultiVectorType &V, const int color_beg=0) const
matrix_t::node_type node_t
matrix_t::crs_graph_type graph_t
list_of_colors_t list_of_colors
list_of_colors_t list_of_colors
node_t::device_type device_t
list_of_colors_host_t list_of_colors_host
matrix_t::node_type node_t
matrix_t::scalar_type scalar_t
Tpetra::BlockMultiVector< SC, LO, GO, NO > multivector_t
void reconstructMatrix(MultiVectorType &W, const int color_beg=0) const
void reconstructMatrixFitted(MultiVectorType &W, const int color_beg=0) const
bool check_coloring(const Tpetra::CrsGraph< LO, GO, NO > &graph, const list_of_colors_t &list_of_colors)
Kokkos::View< int *, device_t > list_of_colors_t
list_of_colors_t::HostMirror list_of_colors_host_t
bool checkColoring() const
TpetraCrsColorer(const Teuchos::RCP< matrix_t > &matrix_)
device_t::execution_space execution_space
Teuchos::RCP< matrix_t > matrix
matrix_t::global_ordinal_type gno_t
int getColor(const size_t col) const
Teuchos::RCP< const graph_t > graph
void computeSeedMatrixFitted(MultiVectorType &V, const int color_beg=0) const
bool checkColoring() const
int getColor(const size_t col) const
device_t::execution_space execution_space
matrix_t::crs_graph_type graph_t