Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
Tpetra_Apply_Helpers.hpp
1// @HEADER
2// *****************************************************************************
3// Tpetra: Templated Linear Algebra Services Package
4//
5// Copyright 2008 NTESS and the Tpetra contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
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"
16
17namespace Tpetra {
19
20
42
43 template <class MatrixArray, class MultiVectorArray>
44 void batchedApply(const MatrixArray &Matrices,
45 const typename std::remove_pointer<typename MultiVectorArray::value_type>::type &X,
46 MultiVectorArray &Y,
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) {
50 Tpetra::Details::ProfilingRegion regionTotal ("Tpetra::batchedApply");
51
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;
59
60 using Teuchos::RCP;
61 using Teuchos::rcp_const_cast;
62
63 const scalar_type ONE = Teuchos::ScalarTraits<scalar_type>::one();
64 const scalar_type ZERO = Teuchos::ScalarTraits<scalar_type>::zero();
65
66 size_type N = Matrices.size();
67 if(N==0) return;
68 int numRanks = X.getMap()->getComm()->getSize();
69
70 // If X is aliased to any Y but the last one, throw
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.");
73 }
74
75 /* Checks for whether the reduced communication path can be used */
76 RCP<const map_type> compare_colMap = Matrices[0]->getColMap();
77 RCP<const import_type> importer = Matrices[0]->getGraph()->getImporter();
78
79
80 bool can_batch, check_maps;
81 if(params.is_null() || !params->isParameter("can batch")) {
82 can_batch = (importer.is_null() || N==1) ? false :true;
83 check_maps = true;
84 }
85 else {
86 can_batch = (!params->get<bool>("can batch") || importer.is_null() || N==1) ? false : true;
87 check_maps = false;
88 }
89 // We can't batch with replicated Y's
90 if(numRanks > 1) {
91 for(size_type i=0; i<N && can_batch; i++) {
92 if(!Y[i]->isDistributed())
93 can_batch = false;
94 }
95 }
96
97 // Do the domain/column maps all match?
98 for(size_type i=1; i<N && check_maps && can_batch; i++) {
99 if(!Matrices[i]->getColMap()->isSameAs(*compare_colMap)) {
100 can_batch=false;
101 }
102 }
103
104 if(can_batch) {
105 /* Batching path: Guarantees an existing importer and N>1 */
106
107 // Special case for alpha = 0
108 if (alpha == ZERO) {
109 if (beta == ZERO) {
110 for(size_type i=0; i<N; i++) Y[i]->putScalar(ZERO);
111 } else if (beta != ONE) {
112 for(size_type i=0; i<N; i++) Y[i]->scale(beta);
113 }
114 if(!params.is_null()) params->set("can batch",true);
115 return;
116 }
117
118 const bool Y_is_overwritten = (beta == ZERO);
119
120 // Start by importing X to Matrices[0]'s temporary
121 RCP<const MV> X_colMap;
122 {
123 Tpetra::Details::ProfilingRegion regionImport ("Tpetra::batchedApply: Import");
124 RCP<MV> X_colMapNonConst = Matrices[0]->getColumnMapMultiVector(X);
125
126 // Import from the domain Map MV to the column Map MV.
127 X_colMapNonConst->doImport(X, *importer, INSERT);
128 X_colMap = rcp_const_cast<const MV>(X_colMapNonConst);
129 }
130
131 for(size_type i=0; i<N; i++) {
132 RCP<const export_type> exporter = Matrices[i]->getGraph()->getExporter();
133
134 // Temporary MV for doExport (if needed),
135 RCP<MV> Y_rowMap = Matrices[i]->getRowMapMultiVector(*Y[i]);
136 if (!exporter.is_null()) {
137 Matrices[i]->localApply(*X_colMap, *Y_rowMap, Teuchos::NO_TRANS, alpha, ZERO);
138 {
139 Tpetra::Details::ProfilingRegion regionExport ("Tpetra::batchedApply: Export");
140 if (Y_is_overwritten) {
141 Y[i]->putScalar(ZERO);
142 }
143 else {
144 Y[i]->scale (beta);
145 }
146 Y[i]->doExport(*Y_rowMap, *exporter, ADD_ASSIGN);
147 }
148 }
149 else { // Don't do an Export: row Map and range Map are the same.
150 // Check for aliasing
151 if (! Y[i]->isConstantStride() || X_colMap.getRawPtr() == Y[i]) {
152 Y_rowMap = Matrices[i]->getRowMapMultiVector(*Y[i], true);
153 if (beta != ZERO) {
154 Tpetra::deep_copy (*Y_rowMap, *Y[i]);
155 }
156
157 Matrices[i]->localApply(*X_colMap, *Y_rowMap, Teuchos::NO_TRANS, alpha, beta);
158 Tpetra::deep_copy(*Y[i], *Y_rowMap);
159 }
160 else {
161 Matrices[i]->localApply(*X_colMap, *Y[i], Teuchos::NO_TRANS, alpha, beta);
162 }
163 }
164 }
165 if(!params.is_null()) params->set("can batch",true);
166 }
167 else {
168 /* Non-batching path */
169 for(size_type i=0; i<N; i++) {
170 Matrices[i]->apply(X,*Y[i],Teuchos::NO_TRANS, alpha, beta);
171 }
172 if(!params.is_null()) params->set("can batch",false);
173 }
174 }
175
176
177}// namespace Tpetra
178
179#endif // TPETRA_APPLY_HELPERS_HPP
180
Declaration of Tpetra::Details::Profiling, a scope guard for Kokkos Profiling.
Teuchos::RCP< const map_type > getColMap() const override
Namespace Tpetra contains the class and methods constituting the Tpetra library.
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.
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.
@ ADD_ASSIGN
Accumulate new values into existing values (may not be supported in all classes).
@ INSERT
Insert new values that don't currently exist.
@ ZERO
Replace old values with zero.