10 #ifndef TPETRA_APPLY_HELPERS_HPP
11 #define TPETRA_APPLY_HELPERS_HPP
12 #include <type_traits>
13 #include "Teuchos_ScalarTraits.hpp"
14 #include "Tpetra_CrsMatrix.hpp"
43 template <
class MatrixArray,
class MultiVectorArray>
45 const typename std::remove_pointer<typename MultiVectorArray::value_type>::type &X,
47 typename std::remove_pointer<typename MatrixArray::value_type>::type::scalar_type alpha = Teuchos::ScalarTraits<
typename std::remove_pointer<typename MatrixArray::value_type>::type::scalar_type>::one(),
48 typename std::remove_pointer<typename MatrixArray::value_type>::type::scalar_type beta = Teuchos::ScalarTraits<
typename std::remove_pointer<typename MatrixArray::value_type>::type::scalar_type>::zero(),
49 Teuchos::RCP<Teuchos::ParameterList> params = Teuchos::null) {
52 using size_type =
typename MatrixArray::size_type;
53 using matrix_type =
typename std::remove_pointer<typename MatrixArray::value_type>::type;
54 using map_type =
typename matrix_type::map_type;
55 using import_type =
typename matrix_type::import_type;
56 using export_type =
typename matrix_type::export_type;
57 using MV =
typename matrix_type::MV;
58 using scalar_type =
typename matrix_type::scalar_type;
61 using Teuchos::rcp_const_cast;
63 const scalar_type ONE = Teuchos::ScalarTraits<scalar_type>::one();
64 const scalar_type
ZERO = Teuchos::ScalarTraits<scalar_type>::zero();
66 size_type N = Matrices.size();
68 int numRanks = X.getMap()->getComm()->getSize();
71 for (size_type i = 0; i < N - 1; i++) {
72 TEUCHOS_TEST_FOR_EXCEPTION(&X == Y[i], std::runtime_error,
"Tpetra::batchedApply(): X cannot be aliased to any Y except the final one.");
76 RCP<const map_type> compare_colMap = Matrices[0]->getColMap();
77 RCP<const import_type> importer = Matrices[0]->getGraph()->getImporter();
79 bool can_batch, check_maps;
80 if (params.is_null() || !params->isParameter(
"can batch")) {
81 can_batch = (importer.is_null() || N == 1) ?
false :
true;
84 can_batch = (!params->get<
bool>(
"can batch") || importer.is_null() || N == 1) ?
false :
true;
89 for (size_type i = 0; i < N && can_batch; i++) {
90 if (!Y[i]->isDistributed())
96 for (size_type i = 1; i < N && check_maps && can_batch; i++) {
97 if (!Matrices[i]->getColMap()->isSameAs(*compare_colMap)) {
108 for (size_type i = 0; i < N; i++) Y[i]->putScalar(ZERO);
109 }
else if (beta != ONE) {
110 for (size_type i = 0; i < N; i++) Y[i]->scale(beta);
112 if (!params.is_null()) params->set(
"can batch",
true);
116 const bool Y_is_overwritten = (beta ==
ZERO);
119 RCP<const MV> X_colMap;
122 RCP<MV> X_colMapNonConst = Matrices[0]->getColumnMapMultiVector(X);
125 X_colMapNonConst->doImport(X, *importer,
INSERT);
126 X_colMap = rcp_const_cast<
const MV>(X_colMapNonConst);
129 for (size_type i = 0; i < N; i++) {
130 RCP<const export_type> exporter = Matrices[i]->getGraph()->getExporter();
133 RCP<MV> Y_rowMap = Matrices[i]->getRowMapMultiVector(*Y[i]);
134 if (!exporter.is_null()) {
135 Matrices[i]->localApply(*X_colMap, *Y_rowMap, Teuchos::NO_TRANS, alpha, ZERO);
138 if (Y_is_overwritten) {
139 Y[i]->putScalar(ZERO);
143 Y[i]->doExport(*Y_rowMap, *exporter,
ADD_ASSIGN);
147 if (!Y[i]->isConstantStride() || X_colMap.getRawPtr() == Y[i]) {
148 Y_rowMap = Matrices[i]->getRowMapMultiVector(*Y[i],
true);
153 Matrices[i]->localApply(*X_colMap, *Y_rowMap, Teuchos::NO_TRANS, alpha, beta);
156 Matrices[i]->localApply(*X_colMap, *Y[i], Teuchos::NO_TRANS, alpha, beta);
160 if (!params.is_null()) params->set(
"can batch",
true);
163 for (size_type i = 0; i < N; i++) {
164 Matrices[i]->apply(X, *Y[i], Teuchos::NO_TRANS, alpha, beta);
166 if (!params.is_null()) params->set(
"can batch",
false);
173 #endif // TPETRA_APPLY_HELPERS_HPP
Declaration of Tpetra::Details::Profiling, a scope guard for Kokkos Profiling.
void batchedApply(const MatrixArray &Matrices, const typename std::remove_pointer< typename MultiVectorArray::value_type >::type &X, MultiVectorArray &Y, typename std::remove_pointer< typename MatrixArray::value_type >::type::scalar_type alpha=Teuchos::ScalarTraits< typename std::remove_pointer< typename MatrixArray::value_type >::type::scalar_type >::one(), typename std::remove_pointer< typename MatrixArray::value_type >::type::scalar_type beta=Teuchos::ScalarTraits< typename std::remove_pointer< typename MatrixArray::value_type >::type::scalar_type >::zero(), Teuchos::RCP< Teuchos::ParameterList > params=Teuchos::null)
Does multiply matrix apply() calls with a single X vector.
void deep_copy(MultiVector< DS, DL, DG, DN > &dst, const MultiVector< SS, SL, SG, SN > &src)
Copy the contents of the MultiVector src into dst.
Insert new values that don't currently exist.
Replace old values with zero.
Accumulate new values into existing values (may not be supported in all classes)