48 #ifndef __IFPACK2_CRSARRAYS_DECL_HPP__
49 #define __IFPACK2_CRSARRAYS_DECL_HPP__
51 #include <Tpetra_RowMatrix.hpp>
52 #include <Tpetra_CrsMatrix.hpp>
53 #include <Kokkos_DefaultNode.hpp>
54 #include <KokkosSparse_CrsMatrix.hpp>
55 #include <Ifpack2_LocalFilter.hpp>
56 #include <Ifpack2_ReorderFilter.hpp>
65 template<
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
typename Node>
68 typedef typename Node::device_type device_type;
69 typedef typename device_type::execution_space execution_space;
70 typedef Tpetra::RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> TRowMatrix;
71 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> TCrsMatrix;
74 typedef KokkosSparse::CrsMatrix<Scalar, LocalOrdinal, execution_space> KCrsMatrix;
75 typedef Kokkos::View<LocalOrdinal*, execution_space> OrdinalArray;
76 typedef Kokkos::View<Scalar*, execution_space> ScalarArray;
77 typedef Kokkos::View<LocalOrdinal*, Kokkos::HostSpace> OrdinalArrayHost;
78 typedef Kokkos::View<Scalar*, Kokkos::HostSpace> ScalarArrayHost;
80 typedef Kokkos::Serial functor_space;
81 typedef Kokkos::RangePolicy<functor_space, int> RangePol;
86 CountEntriesFunctor(
const TRowMatrix* A_, OrdinalArrayHost& rowptrs_) : A(A_), rowptrs(rowptrs_) {}
87 KOKKOS_INLINE_FUNCTION
void operator()(
const int row)
const
89 rowptrs(row) = A->getNumEntriesInLocalRow(row);
92 OrdinalArrayHost& rowptrs;
98 GetIndicesFunctor(
const TRowMatrix* A_,
const OrdinalArrayHost& rowptrs_, OrdinalArrayHost& colinds_) :
99 A(A_), rowptrs(rowptrs_), colinds(colinds_) {}
100 KOKKOS_INLINE_FUNCTION
void operator()(
const int row)
const
102 LocalOrdinal offset = rowptrs(row);
103 size_t entries = rowptrs(row + 1) - offset;
106 auto valsView = valsArray();
107 auto indicesView = indicesArray();
108 A->getLocalRowCopy(row, indicesView, valsView, entries);
109 std::sort(indicesView.getRawPtr(), indicesView().getRawPtr() + entries);
110 for(LocalOrdinal i = 0; i < entries; i++)
112 colinds(offset + i) = indicesView[i];
116 const OrdinalArrayHost& rowptrs;
117 OrdinalArrayHost& colinds;
123 GetValuesFunctor(
const TRowMatrix* A_,
const OrdinalArrayHost& rowptrs_, ScalarArrayHost& vals_) :
124 A(A_), rowptrs(rowptrs_), vals(vals_) {}
125 KOKKOS_INLINE_FUNCTION
void operator()(
const int row)
const
127 LocalOrdinal offset = rowptrs(row);
128 size_t entries = rowptrs(row + 1) - offset;
131 auto valsView = valsArray();
132 auto indicesView = indicesArray();
133 A->getLocalRowCopy(row, indicesView, valsView, entries);
134 Tpetra::sort2(indicesView.getRawPtr(), indicesView.getRawPtr() + entries, valsView.getRawPtr());
135 for(LocalOrdinal i = 0; i < entries; i++)
137 vals(offset + i) = valsView[i];
141 const OrdinalArrayHost& rowptrs;
142 ScalarArrayHost& vals;
149 static void getValues(
const TRowMatrix* A, ScalarArray& vals, OrdinalArrayHost& rowptrs)
151 auto Acrs =
dynamic_cast<const TCrsMatrix*
>(A);
154 getValuesCrs(Acrs, vals);
158 LocalOrdinal nrows = A->getNodeNumRows();
159 ScalarArrayHost valsHost(
"Values (host)", rowptrs[nrows]);
160 vals = ScalarArray(
"Values", rowptrs[nrows]);
161 GetValuesFunctor funct(A, rowptrs, valsHost);
162 Kokkos::parallel_for(RangePol(0, nrows), funct);
163 Kokkos::deep_copy(vals, valsHost);
171 static void getStructure(
const TRowMatrix* A, OrdinalArrayHost& rowptrsHost, OrdinalArray& rowptrs, OrdinalArray& colinds)
173 auto Acrs =
dynamic_cast<const TCrsMatrix*
>(A);
176 getStructureCrs(Acrs, rowptrs, colinds);
182 LocalOrdinal nrows = A->getNodeNumRows();
183 rowptrsHost = OrdinalArrayHost(
"RowPtrs (host)", nrows + 1);
184 rowptrs = OrdinalArray(
"RowPtrs", nrows + 1);
185 CountEntriesFunctor countFunct(A, rowptrsHost);
186 Kokkos::parallel_for(RangePol(0, nrows), countFunct);
189 LocalOrdinal accum = 0;
190 for(LocalOrdinal i = 0; i < nrows; i++)
192 LocalOrdinal temp = rowptrsHost[i];
193 rowptrsHost[i] = accum;
196 rowptrsHost[nrows] = accum;
199 OrdinalArrayHost colindsHost = OrdinalArrayHost(
"ColInds (host)", rowptrsHost[nrows]);
200 colinds = OrdinalArray(
"ColInds", rowptrsHost[nrows]);
201 GetIndicesFunctor indicesFunct(A, rowptrsHost, colindsHost);
202 Kokkos::parallel_for(RangePol(0, nrows), indicesFunct);
204 Kokkos::deep_copy(rowptrs, rowptrsHost);
205 Kokkos::deep_copy(colinds, colindsHost);
211 static void getValuesCrs(
const TCrsMatrix* A, ScalarArray& vals)
213 vals = A->getLocalMatrix().values;
217 static void getStructureCrs(
const TCrsMatrix* A, OrdinalArray& rowptrs, OrdinalArray& colinds)
220 auto rowmap = A->getLocalMatrix().graph.row_map;
222 rowptrs = OrdinalArray(
"RowPtrs", A->getNodeNumRows() + 1);
223 for(
size_t i = 0; i < rowmap.extent(0); i++)
225 rowptrs[i] = rowmap[i];
227 colinds = A->getLocalMatrix().graph.entries;
Wraps a Tpetra::RowMatrix in a filter that reorders local rows and columns.
Definition: Ifpack2_ReorderFilter_decl.hpp:69
Functor for counting matrix entries per row in parallel.
Definition: Ifpack2_Details_CrsArrays.hpp:84
Functor for getting matrix values in parallel.
Definition: Ifpack2_Details_CrsArrays.hpp:121
Functor for getting column indices in parallel.
Definition: Ifpack2_Details_CrsArrays.hpp:96
Access only local rows and columns of a sparse matrix.
Definition: Ifpack2_LocalFilter_decl.hpp:160