10#ifndef TPETRA_CRSGRAPHTRANSPOSER_DEF_HPP
11#define TPETRA_CRSGRAPHTRANSPOSER_DEF_HPP
13#include "Tpetra_CrsGraph.hpp"
14#include "Tpetra_Export.hpp"
16#include "Tpetra_Details_makeColMap.hpp"
18#include "Teuchos_ParameterList.hpp"
19#include "Teuchos_TimeMonitor.hpp"
20#include "KokkosSparse_Utils.hpp"
21#include "KokkosKernels_Handle.hpp"
22#include "KokkosSparse_spadd.hpp"
28 typename LocalIndicesType,
29 typename GlobalIndicesType,
31struct ConvertLocalToGlobalFunctor {
32 ConvertLocalToGlobalFunctor(
33 const LocalIndicesType& colindsOrig_,
34 const GlobalIndicesType& colindsConverted_,
35 const ColMapType& colmap_)
36 : colindsOrig(colindsOrig_)
37 , colindsConverted(colindsConverted_)
39 KOKKOS_INLINE_FUNCTION
void
40 operator()(
const GO i)
const {
41 colindsConverted(i) = colmap.getGlobalElement(colindsOrig(i));
43 LocalIndicesType colindsOrig;
44 GlobalIndicesType colindsConverted;
48template <
class LO,
class GO,
class LOView,
class GOView,
class LocalMap>
49struct ConvertGlobalToLocalFunctor {
50 ConvertGlobalToLocalFunctor(LOView& lids_,
const GOView& gids_,
const LocalMap localColMap_)
53 , localColMap(localColMap_) {}
55 KOKKOS_FUNCTION
void operator()(
const GO i)
const {
56 lids(i) = localColMap.getLocalElement(gids(i));
61 const LocalMap localColMap;
64template <
typename size_type,
typename ordinal_type,
65 typename ArowptrsT,
typename BrowptrsT,
typename CrowptrsT,
66 typename AcolindsT,
typename BcolindsT,
typename CcolindsT>
67struct SortedNumericIndicesOnlyFunctor {
68 SortedNumericIndicesOnlyFunctor(
const ArowptrsT& Arowptrs_,
69 const BrowptrsT& Browptrs_,
70 const CrowptrsT& Crowptrs_,
71 const AcolindsT& Acolinds_,
72 const BcolindsT& Bcolinds_,
73 const CcolindsT& Ccolinds_)
79 , Ccolinds(Ccolinds_) {}
81 KOKKOS_INLINE_FUNCTION
void operator()(
const ordinal_type i)
const {
82 const ordinal_type ORDINAL_MAX = KokkosKernels::ArithTraits<ordinal_type>::max();
87 size_type Arowstart = Arowptrs(i);
88 size_type Arowlen = Arowptrs(i + 1) - Arowstart;
89 size_type Browstart = Browptrs(i);
90 size_type Browlen = Browptrs(i + 1) - Browstart;
91 ordinal_type Acol = (Arowlen == 0) ? ORDINAL_MAX : Acolinds(Arowstart);
92 ordinal_type Bcol = (Browlen == 0) ? ORDINAL_MAX : Bcolinds(Browstart);
93 size_type Coffset = Crowptrs(i);
94 while (Acol != ORDINAL_MAX || Bcol != ORDINAL_MAX) {
95 ordinal_type Ccol = (Acol < Bcol) ? Acol : Bcol;
96 while (Acol == Ccol) {
101 Acol = Acolinds(Arowstart + ai);
103 while (Bcol == Ccol) {
108 Bcol = Bcolinds(Browstart + bi);
110 Ccolinds(Coffset) = Ccol;
115 const ArowptrsT Arowptrs;
116 const BrowptrsT Browptrs;
117 const CrowptrsT Crowptrs;
118 const AcolindsT Acolinds;
119 const BcolindsT Bcolinds;
123template <
typename size_type,
typename ordinal_type,
124 typename ArowptrsT,
typename BrowptrsT,
typename CrowptrsT,
125 typename AcolindsT,
typename BcolindsT,
typename CcolindsT>
126struct UnsortedNumericIndicesOnlyFunctor {
127 UnsortedNumericIndicesOnlyFunctor(
128 const ArowptrsT Arowptrs_,
const BrowptrsT Browptrs_,
const CrowptrsT Crowptrs_,
129 const AcolindsT Acolinds_,
const BcolindsT Bcolinds_, CcolindsT Ccolinds_,
130 const CcolindsT Apos_,
const CcolindsT Bpos_)
131 : Arowptrs(Arowptrs_)
132 , Browptrs(Browptrs_)
133 , Crowptrs(Crowptrs_)
134 , Acolinds(Acolinds_)
135 , Bcolinds(Bcolinds_)
136 , Ccolinds(Ccolinds_)
140 KOKKOS_INLINE_FUNCTION
void operator()(
const ordinal_type i)
const {
141 size_type CrowStart = Crowptrs(i);
142 size_type ArowStart = Arowptrs(i);
143 size_type ArowEnd = Arowptrs(i + 1);
144 size_type BrowStart = Browptrs(i);
145 size_type BrowEnd = Browptrs(i + 1);
147 for (size_type j = ArowStart; j < ArowEnd; j++) {
148 Ccolinds(CrowStart + Apos(j)) = Acolinds(j);
151 for (size_type j = BrowStart; j < BrowEnd; j++) {
152 Ccolinds(CrowStart + Bpos(j)) = Bcolinds(j);
155 const ArowptrsT Arowptrs;
156 const BrowptrsT Browptrs;
157 const CrowptrsT Crowptrs;
158 const AcolindsT Acolinds;
159 const BcolindsT Bcolinds;
161 const CcolindsT Apos;
162 const CcolindsT Bpos;
165template <
class LocalOrdinal,
175Teuchos::RCP<CrsGraph<LocalOrdinal, GlobalOrdinal, Node> >
179 using device_type =
typename Node::device_type;
180 using execution_space =
typename device_type::execution_space;
181 using range_type = Kokkos::RangePolicy<execution_space, size_t>;
183 using impl_scalar_type = ::Tpetra::Details::DefaultTypes::scalar_type;
184 using row_ptrs_array =
typename local_graph_device_type::row_map_type::non_const_type;
185 using col_inds_array =
typename local_graph_device_type::entries_type::non_const_type;
186 using local_map_type =
typename map_type::local_map_type;
187 using global_col_inds_array =
typename Kokkos::View<GlobalOrdinal*, device_type>;
189 auto graph = origGraph_;
216 typename Node::execution_space,
typename Node::memory_space,
typename Node::memory_space>;
218 typename Node::execution_space,
typename Node::memory_space,
typename Node::memory_space>;
225 auto nrows =
rowptrs.extent(0) - 1;
226 auto rowptrsSym = row_ptrs_array(Kokkos::ViewAllocateWithoutInitializing(
"row ptrs sym"), nrows + 1);
233 global_col_inds_array colindsConverted(Kokkos::ViewAllocateWithoutInitializing(
"colinds (converted)"),
colinds.extent(0));
235 Kokkos::parallel_for(
"colInds (converted)", range_type(0,
colinds.extent(0)), convert);
239 global_col_inds_array
colindsTConverted(Kokkos::ViewAllocateWithoutInitializing(
"colindsT (converted)"),
colindsT.extent(0));
241 Kokkos::parallel_for(
"colIndsT (converted)", range_type(0,
colindsT.extent(0)),
convertT);
245 handle.create_spadd_handle(
false);
246 auto addHandle = handle.get_spadd_handle();
250 KokkosSparse::spadd_symbolic(&handle,
251 nrows,
graph->getGlobalNumCols(),
253 globalColindsSym = global_col_inds_array(Kokkos::ViewAllocateWithoutInitializing(
"global colinds sym"),
addHandle->get_c_nnz());
255 UnsortedNumericIndicesOnlyFunctor<
257 typename row_ptrs_array::const_type,
typename row_ptrs_array::const_type, row_ptrs_array,
258 typename global_col_inds_array::const_type,
typename global_col_inds_array::const_type, global_col_inds_array>
262 Kokkos::parallel_for(
"KokkosSparse::SpAdd:Numeric::InputNotSorted",
277 handle.create_spadd_handle(
sorted);
278 auto addHandle = handle.get_spadd_handle();
280 KokkosSparse::spadd_symbolic(&handle,
281 nrows,
graph->getGlobalNumCols(),
283 colindsSym = col_inds_array(Kokkos::ViewAllocateWithoutInitializing(
"C colinds"),
addHandle->get_c_nnz());
286 SortedNumericIndicesOnlyFunctor<
288 typename row_ptrs_array::const_type,
typename row_ptrs_array::const_type, row_ptrs_array,
289 typename col_inds_array::const_type,
typename col_inds_array::const_type, col_inds_array>
292 Kokkos::parallel_for(
"KokkosSparse::SpAdd:Numeric::InputSorted",
296 UnsortedNumericIndicesOnlyFunctor<
298 typename row_ptrs_array::const_type,
typename row_ptrs_array::const_type, row_ptrs_array,
299 typename col_inds_array::const_type,
typename col_inds_array::const_type, col_inds_array>
303 Kokkos::parallel_for(
"KokkosSparse::SpAdd:Numeric::InputNotSorted",
337Teuchos::RCP<CrsGraph<LocalOrdinal, GlobalOrdinal, Node> >
357 const char paramName[] =
"compute global constants";
364 Teuchos::null, Teuchos::rcpFromRef(
labelList));
371Teuchos::RCP<CrsGraph<LocalOrdinal, GlobalOrdinal, Node> >
376 using Teuchos::rcp_dynamic_cast;
384 const bool sort = [&]() {
391 local_graph_device_type
lclGraph = origGraph_->getLocalGraphDevice();
394 using c_rowmap_t =
typename local_graph_device_type::row_map_type;
395 using c_entries_t =
typename local_graph_device_type::entries_type;
396 using rowmap_t =
typename local_graph_device_type::row_map_type::non_const_type;
397 using entries_t =
typename local_graph_device_type::entries_type::non_const_type;
398 LocalOrdinal numCols = origGraph_->getColMap()->getLocalNumElements();
401 Kokkos::ViewAllocateWithoutInitializing(
"Transpose entries"),
lclGraph.entries.extent(0));
402 KokkosSparse::Impl::transpose_graph<
405 rowmap_t,
typename local_graph_device_type::execution_space>(
418 const auto origExport = origGraph_->getExporter();
420 const auto origImport = origGraph_->getImporter();
430 origGraph_->getColMap(),
431 origGraph_->getRowMap(),
432 origGraph_->getRangeMap(),
433 origGraph_->getDomainMap(),
443#define TPETRA_CRSGRAPHTRANSPOSER_INSTANT(LO, GO, NODE) \
444 template class CrsGraphTransposer<LO, GO, NODE>;
Declare and define the functions Tpetra::Details::computeOffsetsFromCounts and Tpetra::computeOffsets...
Declaration and definition of functions for sorting "short" arrays of keys and corresponding values.
Utility functions for packing and unpacking sparse matrix entries.
A distributed graph accessed by rows (adjacency lists) and stored sparsely.
KokkosSparse::StaticCrsGraph< local_ordinal_type, Kokkos::LayoutLeft, device_type, void, size_t > local_graph_device_type
The type of the part of the sparse graph on each MPI process.
Teuchos::RCP< crs_graph_type > createTransposeLocal(const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)
Compute and return the transpose of the graph given to the constructor.
Teuchos::RCP< crs_graph_type > createTranspose(const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)
Compute and return the transpose of the graph given to the constructor.
Teuchos::RCP< crs_graph_type > symmetrize(const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)
Compute and return graph+graph^T of the graph given to the constructor.
CrsGraphTransposer(const Teuchos::RCP< const crs_graph_type > &origGraph)
Constructor that takes the graph to transpose.
Struct that holds views of the contents of a CrsMatrix.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
void sort(View &view, const size_t &size)
Convenience wrapper for std::sort for host-accessible views.