Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
Tpetra_RowMatrixTransposer_def.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_ROWMATRIXTRANSPOSER_DEF_HPP
11#define TPETRA_ROWMATRIXTRANSPOSER_DEF_HPP
12
13#include "Tpetra_CrsMatrix.hpp"
14#include "Tpetra_BlockCrsMatrix.hpp"
15#include "Tpetra_Export.hpp"
18#include "Teuchos_ParameterList.hpp"
19#include "Teuchos_TimeMonitor.hpp"
20#include "KokkosSparse_Utils.hpp"
21#include "KokkosSparse_SortCrs.hpp"
22
23namespace Tpetra {
24
25template <class Scalar,
26 class LocalOrdinal,
27 class GlobalOrdinal,
28 class Node>
30 RowMatrixTransposer(const Teuchos::RCP<const crs_matrix_type>& origMatrix,
31 const std::string& label)
32 : origMatrix_(origMatrix) {}
33
34template <class Scalar,
35 class LocalOrdinal,
36 class GlobalOrdinal,
37 class Node>
38Teuchos::RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
40 createTranspose(const Teuchos::RCP<Teuchos::ParameterList>& params) {
41 using Teuchos::RCP;
42 // Do the local transpose
44
45 Tpetra::Details::ProfilingRegion MM("Tpetra: Transpose TAFC");
46
47 // If transMatrixWithSharedRows has an exporter, that's what we
48 // want. If it doesn't, the rows aren't actually shared, and we're
49 // done!
52 transMatrixWithSharedRows->getGraph()->getExporter();
53 if (exporter.is_null()) {
55 } else {
56 Teuchos::ParameterList labelList;
57 if (!params.is_null()) {
58 const char paramName[] = "compute global constants";
59 labelList.set(paramName, params->get(paramName, true));
60 }
61 // Use the Export object to do a fused Export and fillComplete.
62 // This always sorts the local matrix after communication, so
63 // no need to set "sorted = false" in parameters.
65 Teuchos::null, Teuchos::rcpFromRef(labelList));
66 }
67}
68
69// mfh 03 Feb 2013: In a definition outside the class like this, the
70// return value is considered outside the class scope (for things like
71// resolving typedefs), but the arguments are considered inside the
72// class scope.
73template <class Scalar,
74 class LocalOrdinal,
75 class GlobalOrdinal,
76 class Node>
77Teuchos::RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
79 createTransposeLocal(const Teuchos::RCP<Teuchos::ParameterList>& params) {
80 using Teuchos::Array;
81 using Teuchos::ArrayRCP;
82 using Teuchos::ArrayView;
83 using Teuchos::RCP;
84 using Teuchos::rcp;
85 using Teuchos::rcp_dynamic_cast;
86 using LO = LocalOrdinal;
87 using GO = GlobalOrdinal;
88 using import_type = Tpetra::Import<LO, GO, Node>;
89 using export_type = Tpetra::Export<LO, GO, Node>;
90
91 Tpetra::Details::ProfilingRegion MM("Tpetra: Transpose Local");
92
93 const bool sort = [&]() {
94 constexpr bool sortDefault = true; // see #4607 discussion
95 const char sortParamName[] = "sort";
96 return params.get() == nullptr ? sortDefault : params->get(sortParamName, sortDefault);
97 }();
98
99 const LO lclNumRows(origMatrix_->getLocalNumRows());
100
103 if (crsMatrix.is_null()) {
104 auto rowMap = origMatrix_->getRowMap();
105 if (rowMap->isOneToOne()) {
106 Teuchos::Array<size_t> numEntPerRow(lclNumRows);
107 for (LO lclRow = 0; lclRow < lclNumRows; ++lclRow) {
108 numEntPerRow[lclRow] = origMatrix_->getNumEntriesInLocalRow(lclRow);
109 }
110 auto colMap = origMatrix_->getColMap();
111
114
115 // When source & target Maps are same, Import just copies.
116 import_type imp(rowMap, rowMap);
117 crsMatrix_nc->doImport(*origMatrix_, imp, Tpetra::REPLACE);
118 crsMatrix_nc->fillComplete(origMatrix_->getDomainMap(),
119 origMatrix_->getRangeMap());
121 } else {
122 TEUCHOS_ASSERT(false); // not implemented (it wasn't before)
123 }
124 }
125
126 using local_matrix_device_type = typename crs_matrix_type::local_matrix_device_type;
127
128 local_matrix_device_type lclMatrix = crsMatrix->getLocalMatrixDevice();
129 local_matrix_device_type lclTransposeMatrix = KokkosSparse::Impl::transpose_matrix(lclMatrix);
130 if (sort)
131 KokkosSparse::sort_crs_matrix(lclTransposeMatrix);
132
133 // Prebuild the importers and exporters the no-communication way,
134 // flipping the importers and exporters around.
135 const auto origExport = origMatrix_->getGraph()->getExporter();
136 RCP<const import_type> myImport = origExport.is_null() ? Teuchos::null : rcp(new import_type(*origExport));
137 const auto origImport = origMatrix_->getGraph()->getImporter();
138 RCP<const export_type> myExport = origImport.is_null() ? Teuchos::null : rcp(new export_type(*origImport));
139
141 if (!sort) {
142 graphParams = rcp(new Teuchos::ParameterList);
143 graphParams->set("sorted", false);
144 }
145 const char computeGlobalConstantsParamName[] = "compute global constants";
146 if (!params.is_null() && params->isParameter(computeGlobalConstantsParamName)) {
147 if (graphParams.is_null())
148 graphParams = rcp(new Teuchos::ParameterList);
150 }
151
153 origMatrix_->getColMap(),
154 origMatrix_->getRowMap(),
155 origMatrix_->getRangeMap(),
156 origMatrix_->getDomainMap(),
158}
159
160/*************************************************************************/
161
162template <class Scalar,
163 class LocalOrdinal,
164 class GlobalOrdinal,
165 class Node>
167 BlockCrsMatrixTransposer(const Teuchos::RCP<const bcrs_matrix_type>& origMatrix,
168 const std::string& label)
169 : origMatrix_(origMatrix) {}
170
171template <class Scalar,
172 class LocalOrdinal,
173 class GlobalOrdinal,
174 class Node>
175Teuchos::RCP<BlockCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
177 createTranspose(const Teuchos::RCP<Teuchos::ParameterList>& params) {
178 using Teuchos::RCP;
179 // Do the local transpose
181
182 Tpetra::Details::ProfilingRegion MM("Tpetra: Transpose TAFC");
183
184 // If transMatrixWithSharedRows has an exporter, that's what we
185 // want. If it doesn't, the rows aren't actually shared, and we're
186 // done!
189 transMatrixWithSharedRows->getGraph()->getExporter();
190 if (exporter.is_null()) {
192 } else {
193 Teuchos::ParameterList labelList;
194 if (!params.is_null()) {
195 const char paramName[] = "compute global constants";
196 labelList.set(paramName, params->get(paramName, true));
197 }
198 // Use the Export object to do a fused Export and fillComplete.
199 // This always sorts the local matrix after communication, so
200 // no need to set "sorted = false" in parameters.
202 }
203}
204
205// mfh 03 Feb 2013: In a definition outside the class like this, the
206// return value is considered outside the class scope (for things like
207// resolving typedefs), but the arguments are considered inside the
208// class scope.
209template <class Scalar,
210 class LocalOrdinal,
211 class GlobalOrdinal,
212 class Node>
213Teuchos::RCP<BlockCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
215 createTransposeLocal(const Teuchos::RCP<Teuchos::ParameterList>& params) {
216 using Teuchos::Array;
217 using Teuchos::ArrayRCP;
218 using Teuchos::ArrayView;
219 using Teuchos::RCP;
220 using Teuchos::rcp;
221 using Teuchos::rcp_dynamic_cast;
222 using LO = LocalOrdinal;
223 using GO = GlobalOrdinal;
224 using import_type = Tpetra::Import<LO, GO, Node>;
225 using export_type = Tpetra::Export<LO, GO, Node>;
226 using crs_graph_type = typename bcrs_matrix_type::crs_graph_type;
227
228 Tpetra::Details::ProfilingRegion MM("Tpetra: Transpose Local");
229
232
233 if (crsMatrix.is_null())
234 TEUCHOS_ASSERT(false); // not implemented
235
236 using local_matrix_device_type = typename bcrs_matrix_type::local_matrix_device_type;
237
238 typename local_matrix_device_type::values_type values;
240 {
241 local_matrix_device_type lclMatrix = crsMatrix->getLocalMatrixDevice();
242
243 local_matrix_device_type lclTransposeMatrix = KokkosSparse::Impl::transpose_bsr_matrix(lclMatrix);
244
245 // BlockCrs requires that we sort stuff
246 KokkosSparse::sort_crs_matrix(lclTransposeMatrix);
247 values = lclTransposeMatrix.values;
248
249 // Prebuild the importers and exporters the no-communication way,
250 // flipping the importers and exporters around.
251 const auto origExport = origMatrix_->getGraph()->getExporter();
252 RCP<const import_type> myImport = origExport.is_null() ? Teuchos::null : rcp(new import_type(*origExport));
253 const auto origImport = origMatrix_->getGraph()->getImporter();
254 RCP<const export_type> myExport = origImport.is_null() ? Teuchos::null : rcp(new export_type(*origImport));
255
257
258 // Make the Transpose Graph
259 graph = rcp(new crs_graph_type(lclTransposeMatrix.graph,
260 origMatrix_->getColMap(),
261 origMatrix_->getRowMap(),
262 origMatrix_->getGraph()->getRangeMap(),
263 origMatrix_->getGraph()->getDomainMap(),
264 myImport,
265 myExport,
266 graphParams));
267 }
268 // Now make the matrix
269 return rcp(new bcrs_matrix_type(*graph,
270 values,
271 origMatrix_->getBlockSize()));
272}
273//
274
275//
276// Explicit instantiation macro
277//
278// Must be expanded from within the Tpetra namespace!
279//
280
281#define TPETRA_ROWMATRIXTRANSPOSER_INSTANT(SCALAR, LO, GO, NODE) \
282 template class RowMatrixTransposer<SCALAR, LO, GO, NODE>; \
283 template class BlockCrsMatrixTransposer<SCALAR, LO, GO, NODE>;
284
285} // namespace Tpetra
286
287#endif
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.
Sparse matrix whose entries are small dense square blocks, all of the same dimensions.
Teuchos::RCP< bcrs_matrix_type > createTransposeLocal(const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Compute and return the transpose of the matrix given to the constructor.
BlockCrsMatrixTransposer(const Teuchos::RCP< const bcrs_matrix_type > &origMatrix, const std::string &label=std::string())
Constructor that takes the matrix to transpose.
Teuchos::RCP< bcrs_matrix_type > createTranspose(const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Compute and return the transpose of the matrix given to the constructor.
Sparse matrix that presents a row-oriented interface that lets users read or modify entries.
KokkosSparse::CrsMatrix< impl_scalar_type, local_ordinal_type, device_type, void, typename local_graph_device_type::size_type > local_matrix_device_type
The specialization of Kokkos::CrsMatrix that represents the part of the sparse matrix on each MPI pro...
Struct that holds views of the contents of a CrsMatrix.
Teuchos::RCP< const map_type > colMap
Col map for the original version of the matrix.
Teuchos::RCP< const map_type > rowMap
Desired row map for "imported" version of the matrix.
Teuchos::RCP< crs_matrix_type > createTranspose(const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Compute and return the transpose of the matrix given to the constructor.
Teuchos::RCP< crs_matrix_type > createTransposeLocal(const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Compute and return the transpose of the matrix given to the constructor.
RowMatrixTransposer(const Teuchos::RCP< const crs_matrix_type > &origMatrix, const std::string &label=std::string())
Constructor that takes the matrix to transpose.
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.
@ REPLACE
Replace existing values with new values.