1#ifndef TPETRA_FILTER_HPP
2#define TPETRA_FILTER_HPP
4#include "Kokkos_Core.hpp"
5#include "Teuchos_RCP.hpp"
16template <
class matrix_type,
class filter_type>
18 using local_ordinal_type =
typename matrix_type::local_ordinal_type;
19 using node_type =
typename matrix_type::node_type;
20 using local_matrix_type =
typename matrix_type::local_matrix_device_type;
21 using rowptr_type =
typename local_matrix_type::row_map_type::non_const_type;
22 using colidx_type =
typename local_matrix_type::index_type::non_const_type;
23 using values_type =
typename local_matrix_type::values_type::non_const_type;
24 using exec_space =
typename node_type::execution_space;
29 auto lclA =
A.getLocalMatrixDevice();
32 local_ordinal_type
NNZ;
34 Kokkos::parallel_scan(
35 "filterMatrix::rowptr_construction", Kokkos::RangePolicy<exec_space>(0,
lclA.numRows()),
KOKKOS_LAMBDA(
const local_ordinal_type
rlid, local_ordinal_type&
nnz,
const bool update) {
37 for (local_ordinal_type
k = 0;
k < row.length; ++
k) {
38 auto val = row.value(
k);
43 if (update && (
rlid + 2 <
lclA.numRows() + 1))
49 auto values = values_type(Kokkos::ViewAllocateWithoutInitializing(
"filtered_values"),
NNZ);
52 "filterMatrix::matrix_fill", Kokkos::RangePolicy<exec_space>(0,
lclA.numRows()),
KOKKOS_LAMBDA(
const local_ordinal_type
rlid) {
54 for (local_ordinal_type
k = 0;
k < row.length; ++
k) {
55 auto val = row.value(
k);
57 auto clid = row.colidx(
k);
68 return Teuchos::rcp(
new matrix_type(
newLclA,
A.getRowMap(),
A.getColMap(),
A.getDomainMap(),
A.getRangeMap()));
78template <
class matrix_type,
class filter_type>
80 using local_ordinal_type =
typename matrix_type::local_ordinal_type;
81 using node_type =
typename matrix_type::node_type;
82 using local_matrix_type =
typename matrix_type::local_matrix_device_type;
83 using rowptr_type =
typename local_matrix_type::row_map_type::non_const_type;
84 using colidx_type =
typename local_matrix_type::index_type::non_const_type;
85 using values_type =
typename local_matrix_type::values_type::non_const_type;
86 using exec_space =
typename node_type::execution_space;
91 auto lclA =
A.getLocalMatrixDevice();
94 local_ordinal_type
NNZ;
96 Kokkos::parallel_scan(
97 "filterMatrix::rowptr_construction", Kokkos::RangePolicy<exec_space>(0,
lclA.numRows()),
KOKKOS_LAMBDA(
const local_ordinal_type
rlid, local_ordinal_type&
nnz,
const bool update) {
99 for (local_ordinal_type
k = 0;
k < row.length; ++
k) {
100 auto clid = row.colidx(
k);
101 auto val = row.value(
k);
106 if (update && (
rlid + 2 <
lclA.numRows() + 1))
112 auto values = values_type(Kokkos::ViewAllocateWithoutInitializing(
"filtered_values"),
NNZ);
114 Kokkos::parallel_for(
115 "filterMatrix::matrix_fill", Kokkos::RangePolicy<exec_space>(0,
lclA.numRows()),
KOKKOS_LAMBDA(
const local_ordinal_type
rlid) {
117 for (local_ordinal_type
k = 0;
k < row.length; ++
k) {
118 auto clid = row.colidx(
k);
119 auto val = row.value(
k);
131 return Teuchos::rcp(
new matrix_type(
newLclA,
A.getRowMap(),
A.getColMap(),
A.getDomainMap(),
A.getRangeMap()));
141template <
class matrix_type,
class filter_type>
143 using local_ordinal_type =
typename matrix_type::local_ordinal_type;
144 using node_type =
typename matrix_type::node_type;
145 using local_matrix_type =
typename matrix_type::local_matrix_device_type;
146 using rowptr_type =
typename local_matrix_type::row_map_type::non_const_type;
147 using colidx_type =
typename local_matrix_type::index_type::non_const_type;
148 using values_type =
typename local_matrix_type::values_type::non_const_type;
149 using exec_space =
typename node_type::execution_space;
154 auto lclA =
A.getLocalMatrixDevice();
157 local_ordinal_type
NNZ;
159 auto lclRowMap =
A.getRowMap()->getLocalMap();
160 auto lclColMap =
A.getColMap()->getLocalMap();
162 Kokkos::parallel_scan(
163 "filterMatrix::rowptr_construction", Kokkos::RangePolicy<exec_space>(0,
lclA.numRows()),
KOKKOS_LAMBDA(
const local_ordinal_type
rlid, local_ordinal_type&
nnz,
const bool update) {
166 for (local_ordinal_type
k = 0;
k < row.length; ++
k) {
167 auto clid = row.colidx(
k);
169 auto val = row.value(
k);
174 if (update && (
rlid + 2 <
lclA.numRows() + 1))
180 auto values = values_type(Kokkos::ViewAllocateWithoutInitializing(
"filtered_values"),
NNZ);
182 Kokkos::parallel_for(
183 "filterMatrix::matrix_fill", Kokkos::RangePolicy<exec_space>(0,
lclA.numRows()),
KOKKOS_LAMBDA(
const local_ordinal_type
rlid) {
186 for (local_ordinal_type
k = 0;
k < row.length; ++
k) {
187 auto clid = row.colidx(
k);
189 auto val = row.value(
k);
201 return Teuchos::rcp(
new matrix_type(
newLclA,
A.getRowMap(),
A.getColMap(),
A.getDomainMap(),
A.getRangeMap()));
204template <
class scalar_type,
class local_ordinal_type>
205struct AbsoluteMagnitudeFilter {
206#if KOKKOS_VERSION >= 40799
207 using ATS = KokkosKernels::ArithTraits<scalar_type>;
208 using impl_scalar_type =
typename ATS::val_type;
209 using implATS = KokkosKernels::ArithTraits<impl_scalar_type>;
211 using ATS = Kokkos::ArithTraits<scalar_type>;
212 using impl_scalar_type =
typename ATS::val_type;
213 using implATS = Kokkos::ArithTraits<impl_scalar_type>;
216 typename implATS::magnitudeType tol_;
218 AbsoluteMagnitudeFilter(
typename implATS::magnitudeType tol)
221 KOKKOS_INLINE_FUNCTION
222 bool operator()(impl_scalar_type val)
const {
223 return (implATS::magnitude(val) > tol_);
Struct that holds views of the contents of a CrsMatrix.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
Teuchos::RCP< matrix_type > applyFilter_GID(const matrix_type &A, const filter_type &filter)
Constructs a matrix consisting only of the entries for which the filter function evaluates to true.
Teuchos::RCP< matrix_type > applyFilter_LID(const matrix_type &A, const filter_type &filter)
Constructs a matrix consisting only of the entries for which the filter function evaluates to true.
Teuchos::RCP< matrix_type > applyFilter_vals(const matrix_type &A, const filter_type &filter)
Constructs a matrix consisting only of the entries for which the filter function evaluates to true.