Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
Tpetra_SolverMap_CrsMatrix_def.hpp
Go to the documentation of this file.
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_SOLVERMAP_CRSMATRIX_DEF_HPP
11#define TPETRA_SOLVERMAP_CRSMATRIX_DEF_HPP
12
22
24
25#include <vector>
26
27namespace Tpetra {
28
29template <class Scalar,
30 class LocalOrdinal,
31 class GlobalOrdinal,
32 class Node>
34 : StructuralSameTypeTransform<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >()
35 , newColMap_(Teuchos::null)
36 , newGraph_(Teuchos::null) {
37 // Nothing to do
38}
39
40template <class Scalar,
41 class LocalOrdinal,
42 class GlobalOrdinal,
43 class Node>
47
48template <class Scalar,
49 class LocalOrdinal,
50 class GlobalOrdinal,
51 class Node>
52typename SolverMap_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::NewType
55
56 assert(!origMatrix->isGloballyIndexed());
57
58 this->origObj_ = origMatrix;
59
60 // *******************************************************************
61 // Step 1/7: Check if domain map and col map are different
62 // *******************************************************************
63 Teuchos::RCP<map_t const> origDomainMap = origMatrix->getDomainMap();
64 Teuchos::RCP<map_t const> origColMap = origMatrix->getColMap();
65
66 typename map_t::local_map_type localOrigDomainMap(origDomainMap->getLocalMap());
67 typename map_t::local_map_type localOrigColMap(origColMap->getLocalMap());
68
69 if (origDomainMap->isLocallyFitted(*origColMap)) {
70 this->newObj_ = this->origObj_;
71 } else {
74
75 // *****************************************************************
76 // Step 2/7: Fill newColMap_globalColIndices with the global indices
77 // of all entries in origDomainMap
78 // *****************************************************************
79
80 // Initialize the value of 'newColMap_localSize'
81 size_t const origDomainMap_localSize = origDomainMap->getLocalNumElements();
83
84 // Increase the value of 'newColMap_localSize' as necessary
85 size_t newColMap_extraSize(0);
86 size_t const origColMap_localSize = origColMap->getLocalNumElements();
87 {
88 Kokkos::parallel_reduce(
89 "Tpetra::SolverMap_CrsMatrix::construct::newColMap_extraSize", Kokkos::RangePolicy<typename Node::device_type::execution_space, size_t>(0, origColMap_localSize), KOKKOS_LAMBDA(size_t const i, size_t& sizeToUpdate)->void {
90 GlobalOrdinal const globalColIndex(localOrigColMap.getGlobalElement(i));
91 if (localOrigDomainMap.getLocalElement(globalColIndex) == ::Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid()) {
92 sizeToUpdate += 1;
93 }
94 },
96 }
98
99 // Instantiate newColMap_globalColIndices with the correct size 'newColMap_localSize'
100 Kokkos::View<GlobalOrdinal*, typename Node::device_type> newColMap_globalColIndices("", newColMap_localSize);
101
102 // Fill newColMap_globalColIndices with the global indices of all entries in origDomainMap
103 {
104 Kokkos::parallel_for(
105 "Tpetra::SolverMap_CrsMatrix::construct::copyDomainMapToNewColMap", Kokkos::RangePolicy<typename Node::device_type::execution_space, size_t>(0, origDomainMap_localSize), KOKKOS_LAMBDA(size_t const i)->void {
107 });
108 }
109
110 // *****************************************************************
111 // Step 3/7: Append newColMap_globalColIndices with those origColMap
112 // entries that are not in newColMap_globalColIndices yet
113 // *****************************************************************
114 {
115 Kokkos::parallel_scan(
116 "Tpetra::SolverMap_CrsMatrix::construct::appendNewColMap", Kokkos::RangePolicy<typename Node::device_type::execution_space, size_t>(0, origColMap_localSize), KOKKOS_LAMBDA(size_t const i, size_t& jToUpdate, bool const final)->void {
117 GlobalOrdinal const globalColIndex(localOrigColMap.getGlobalElement(i));
118 if (localOrigDomainMap.getLocalElement(globalColIndex) == ::Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid()) {
119 if (final) {
121 }
122 jToUpdate += 1;
123 }
124 });
125 }
126
127 // *****************************************************************
128 // Step 4/7: Create a new column map using newColMap_globalColIndices
129 // *****************************************************************
130 Teuchos::RCP<map_t const> origRowMap = origMatrix->getRowMap();
131 Teuchos::RCP<Teuchos::Comm<int> const> Comm = origRowMap->getComm();
133 size_t newColMap_globalNumCols(0);
134 Teuchos::reduceAll(*Comm, Teuchos::REDUCE_SUM, 1, &newColMap_localNumCols, &newColMap_globalNumCols);
135
136 newColMap_ = Teuchos::rcp<map_t>(new map_t(newColMap_globalNumCols, newColMap_globalColIndices, origDomainMap->getIndexBase(), Comm));
137
138 // *****************************************************************
139 // Step 5/7: Create new graph
140 // *****************************************************************
141 cg_t const* origGraph = dynamic_cast<cg_t const*>(origMatrix->getGraph().get());
142 newGraph_ = Teuchos::rcp<cg_t>(new cg_t(*origGraph));
143 newGraph_->resumeFill();
144 newGraph_->reindexColumns(newColMap_);
145 newGraph_->fillComplete();
146
147 // *****************************************************************
148 // Step 6/7: Create new CRS matrix
149 // *****************************************************************
150 Teuchos::RCP<cm_t> newMatrix = Teuchos::rcp<cm_t>(new cm_t(*origMatrix, newGraph_));
151
152 // *****************************************************************
153 // Step 7/7: Update newObj_
154 // *****************************************************************
155 this->newObj_ = newMatrix;
156 }
157
158 return this->newObj_;
159}
160
161//
162// Explicit instantiation macro
163//
164// Must be expanded from within the Tpetra namespace!
165//
166
167#define TPETRA_SOLVERMAPCRSMATRIX_INSTANT(SCALAR, LO, GO, NODE) \
168 template class SolverMap_CrsMatrix<SCALAR, LO, GO, NODE>;
169
170} // namespace Tpetra
171
172#endif // TPETRA_SOLVERMAP_CRSMATRIX_DEF_HPP
Declaration of the Tpetra::SolverMap_CrsMatrix class.
Struct that holds views of the contents of a CrsMatrix.
Teuchos::RCP< const CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > origMatrix
The original matrix.
Teuchos::RCP< const map_type > origRowMap
Original row map of matrix.
Sparse matrix that presents a row-oriented interface that lets users read or modify entries.
NewType operator()(OriginalType const &origMatrix)
Namespace Tpetra contains the class and methods constituting the Tpetra library.