Zoltan2
Zoltan2_TpetraCrsColorerUtils.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Teuchos_RCP.hpp"
4 #include "Teuchos_Comm.hpp"
5 
6 #include "Tpetra_CrsGraph.hpp"
7 #include "Tpetra_CrsMatrix.hpp"
8 
9 //#include "Tpetra_RowMatrixTransposer.hpp"
10 
11 #include "KokkosKernels_Utils.hpp"
12 
13 namespace Zoltan2
14 {
15 
16 // Some utility functions to help with coloring
17 namespace Impl
18 {
19 
20 // Check coloring is valid for a given graph
21 template <typename LO, typename GO, typename NO, typename list_of_colors_t>
22 bool
24  const Tpetra::CrsGraph<LO, GO, NO> &graph,
25  const list_of_colors_t &list_of_colors)
26 {
27  typedef typename list_of_colors_t::execution_space execution_space;
28 
29  Teuchos::RCP<const Teuchos::Comm<int>> comm = graph.getRowMap()->getComm();
30  const int rank = comm->getRank();
31  auto local_graph = graph.getLocalGraphDevice();
32  const size_t num_rows = graph.getNodeNumRows();
33  size_t num_conflict = 0;
34 
35  Kokkos::parallel_reduce(
36  "check_coloring()", Kokkos::RangePolicy<execution_space>(0, num_rows),
37  KOKKOS_LAMBDA(const size_t row, size_t &lcl_conflict) {
38  const size_t entry_begin = local_graph.row_map(row);
39  const size_t entry_end = local_graph.row_map(row + 1);
40  for (size_t entry = entry_begin; entry < entry_end; entry++)
41  {
42  const size_t col = local_graph.entries(entry);
43  for (size_t entry2 = entry_begin; entry2 < entry_end; ++entry2)
44  {
45  const size_t col2 = local_graph.entries(entry2);
46  if (col != col2 && list_of_colors[col] == list_of_colors[col2])
47  {
48  ++lcl_conflict;
49  printf(
50  "proc = %i : Invalid coloring! Local row %zu"
51  " and columns %zu, %zu have the same color %i\n",
52  rank, row, col, col2, list_of_colors[col]);
53  }
54  }
55  }
56  },
57  num_conflict);
58 
59  size_t global_num_conflict = 0;
60  Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, 1, &num_conflict,
61  &global_num_conflict);
62 
63  return global_num_conflict == 0;
64 }
65 
67 template <typename LocalCrsGraphType>
68 LocalCrsGraphType
70  const LocalCrsGraphType &local_graph,
71  const size_t num_cols)
72 {
73  using KokkosKernels::Impl::transpose_graph;
74 
75  typedef LocalCrsGraphType graph_t;
76  typedef typename graph_t::row_map_type lno_view_t;
77  typedef typename graph_t::entries_type lno_nnz_view_t;
78  typedef typename lno_view_t::non_const_type trans_row_view_t;
79  typedef typename lno_nnz_view_t::non_const_type trans_nnz_view_t;
80  typedef typename lno_view_t::execution_space exec_t;
81 
82  // Build tranpose graph
83  const size_t num_rows = local_graph.row_map.extent(0) - 1;
84  const size_t num_nnz = local_graph.entries.extent(0);
85 
86  trans_row_view_t trans_row_map("trans_row_map", num_cols + 1);
87  trans_nnz_view_t trans_entries("trans_entries", num_nnz);
88 
89  transpose_graph<lno_view_t, lno_nnz_view_t, trans_row_view_t,
90  trans_nnz_view_t, trans_row_view_t, exec_t>(
91  num_rows, num_cols, local_graph.row_map, local_graph.entries,
92  trans_row_map, trans_entries);
93 
94  graph_t local_trans_graph(trans_entries, trans_row_map);
95 
96  return local_trans_graph;
97 }
98 
100 // template <typename LO, typename GO, typename NO>
101 // Teuchos::RCP<const Tpetra::CrsGraph<LO,GO,NO> >
102 // compute_transpose_graph(const Tpetra::CrsGraph<LO,GO,NO>& graph)
103 // {
104 // typedef double SC;
105 // Teuchos::RCP< Tpetra::CrsMatrix<SC,LO,GO,NO> > matrix =
106 // Teuchos::rcp(new Tpetra::CrsMatrix<double,LO,GO,NO>(Teuchos::rcp(&graph,false)));
107 // Tpetra::RowMatrixTransposer<SC,LO,GO,NO> transposer(matrix);
108 // Teuchos::RCP<Tpetra::CrsMatrix<SC,LO,GO,NO> > trans_matrix =
109 // transposer.createTranspose();
110 // return trans_matrix->getCrsGraph();
111 // }
112 
114 template <typename LO, typename GO, typename NO>
115 Teuchos::RCP<Tpetra::CrsGraph<LO, GO, NO>>
116 compute_transpose_graph(const Tpetra::CrsGraph<LO, GO, NO> &graph)
117 {
118  using Teuchos::RCP;
119  using Teuchos::rcp;
120 
121  typedef Tpetra::CrsGraph<LO, GO, NO> graph_t;
122  typedef typename graph_t::local_graph_device_type local_graph_t;
123 
124  // Transpose local graph
125  local_graph_t local_graph = graph.getLocalGraphDevice();
126  local_graph_t local_trans_graph =
127  compute_local_transpose_graph(local_graph,
128  graph.getNodeNumCols());
129 
130  // Build (possibly overlapped) transpose graph using original graph's
131  // column map as the new row map, and vice versa
132  //
133  // Normally, for a transpose graph, we'd use
134  // original matrix's RangeMap as the DomainMap, and
135  // original matrix's DomainMap as the RangeMap.
136  // Moreover, we're not doing SpMV with the transpose graph, so
137  // you might think we wouldn't care about RangeMap and DomainMap.
138  // But we DO care, because exportAndFillCompleteCrsGraph uses the
139  // shared (overlapped) transpose matrix's RangeMap as the RowMap of the
140  // transpose matrix, while the Zoltan callbacks are assuming the
141  // transpose matrix's RowMap is the same as the original matrix's.
142  // So we'll use the original matrix's RowMap as the RangeMap.
143  RCP<graph_t> trans_graph_shared = rcp(new graph_t(
144  local_trans_graph, graph.getColMap(), graph.getRowMap(),
145  graph.getRangeMap(), graph.getRowMap()));
146 
147  RCP<graph_t> trans_graph;
148 
149  // Export graph to non-overlapped distribution if necessary.
150  // If the exporter is null, we don't need to export
151  RCP<const Tpetra::Export<LO, GO, NO>> exporter =
152  trans_graph_shared->getExporter();
153  if (exporter == Teuchos::null)
154  trans_graph = trans_graph_shared;
155  else
156  {
157  RCP<const graph_t> trans_graph_shared_const = trans_graph_shared;
158  trans_graph = Tpetra::exportAndFillCompleteCrsGraph(
159  trans_graph_shared_const, *exporter,
160  Teuchos::null, Teuchos::null, Teuchos::null);
161  }
162 
163  return trans_graph;
164 }
165 
167 template <typename SC, typename LO, typename GO, typename NO>
168 Teuchos::RCP<Tpetra::CrsGraph<LO, GO, NO>>
169 compute_transpose_graph(const Tpetra::CrsMatrix<SC, LO, GO, NO> &matrix)
170 {
171  return compute_transpose_graph(*(matrix.getCrsGraph()));
172 }
173 
175 template <typename SC, typename LO, typename GO, typename NO>
176 Teuchos::RCP<Tpetra::CrsGraph<LO, GO, NO>>
177 compute_transpose_graph(const Tpetra::BlockCrsMatrix<SC, LO, GO, NO> &matrix)
178 {
179  return compute_transpose_graph(matrix.getCrsGraph());
180 }
181 } // namespace Impl
182 } // namespace Zoltan2
Teuchos::RCP< Tpetra::CrsGraph< LO, GO, NO > > compute_transpose_graph(const Tpetra::CrsGraph< LO, GO, NO > &graph)
bool check_coloring(const Tpetra::CrsGraph< LO, GO, NO > &graph, const list_of_colors_t &list_of_colors)
LocalCrsGraphType compute_local_transpose_graph(const LocalCrsGraphType &local_graph, const size_t num_cols)
Created by mbenlioglu on Aug 31, 2020.