Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_TpetraStridedMappingStrategy.cpp
1 // @HEADER
2 // *****************************************************************************
3 // Teko: A package for block and physics based preconditioning
4 //
5 // Copyright 2010 NTESS and the Teko contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Teko_TpetraStridedMappingStrategy.hpp"
11 #include "Teko_InterlacedTpetra.hpp"
12 #include "Teko_TpetraHelpers.hpp"
13 
14 #include "Thyra_TpetraThyraWrappers.hpp"
15 #include "Thyra_TpetraLinearOp.hpp"
16 #include "Thyra_DefaultProductMultiVector.hpp"
17 #include "Thyra_DefaultProductVectorSpace.hpp"
18 #include "Thyra_DefaultSpmdMultiVector.hpp"
19 #include "Thyra_DefaultBlockedLinearOp.hpp"
20 
21 using Teuchos::RCP;
22 using Teuchos::rcp;
23 using Teuchos::rcp_dynamic_cast;
24 
25 namespace Teko {
26 namespace TpetraHelpers {
27 
28 // Creates a strided mapping strategy. This class is useful
29 // for breaking up nodally ordered matrices (i.e. the unknowns
30 // in a FEM problem are ordered [u0,v0,p0,u1,v1,p1,...]). Current
31 // implimentation only supports a fixed number of variables
32 //
33 // arguments:
34 // vars - Number of different variables
35 // map - original Tpetra::Map<LO,GO,NT> to be broken up
36 // comm - Teuchos::Comm<int> object related to the map
37 //
38 TpetraStridedMappingStrategy::TpetraStridedMappingStrategy(
39  const std::vector<int>& vars, const RCP<const Tpetra::Map<LO, GO, NT> >& map,
40  const Teuchos::Comm<int>& comm) {
41  rangeMap_ = map;
42  domainMap_ = map;
43  buildBlockTransferData(vars, rangeMap_, comm);
44 }
45 
46 // Virtual function defined in MappingStrategy. This copies
47 // an Tpetra::MultiVector<ST,LO,GO,NT> into a Thyra::MultiVectorBase with
48 // blocking handled by the strides defined in the constructor.
49 //
50 // arguments:
51 // X - source Tpetra::MultiVector<ST,LO,GO,NT>
52 // thyra_X - destination Thyra::MultiVectorBase
53 //
54 void TpetraStridedMappingStrategy::copyTpetraIntoThyra(
55  const Tpetra::MultiVector<ST, LO, GO, NT>& X,
56  const Teuchos::Ptr<Thyra::MultiVectorBase<ST> >& thyra_X) const {
57  int count = X.getNumVectors();
58 
59  std::vector<RCP<Tpetra::MultiVector<ST, LO, GO, NT> > > subX;
60 
61  // allocate vectors to copy into
62  Strided::buildSubVectors(blockMaps_, subX, count);
63 
64  // copy source vector to X vector
65  Strided::one2many(subX, X, blockImport_);
66 
67  // convert subX to an array of multi vectors
68  Teuchos::Ptr<Thyra::ProductMultiVectorBase<ST> > prod_X =
69  Teuchos::ptr_dynamic_cast<Thyra::ProductMultiVectorBase<ST> >(thyra_X);
70  for (unsigned int i = 0; i < blockMaps_.size(); i++) {
71  RCP<Thyra::TpetraMultiVector<ST, LO, GO, NT> > vec =
72  rcp_dynamic_cast<Thyra::TpetraMultiVector<ST, LO, GO, NT> >(
73  prod_X->getNonconstMultiVectorBlock(i), true);
74 
75  fillDefaultSpmdMultiVector(vec, subX[i]);
76  }
77 }
78 
79 // Virtual function defined in MappingStrategy. This copies
80 // an Tpetra::MultiVector<ST,LO,GO,NT> into a Thyra::MultiVectorBase with
81 // blocking handled by the strides defined in the constructor.
82 //
83 // arguments:
84 // thyra_Y - source Thyra::MultiVectorBase
85 // Y - destination Tpetra::MultiVector<ST,LO,GO,NT>
86 //
87 void TpetraStridedMappingStrategy::copyThyraIntoTpetra(
88  const RCP<const Thyra::MultiVectorBase<ST> >& thyra_Y,
89  Tpetra::MultiVector<ST, LO, GO, NT>& Y) const {
90  std::vector<RCP<const Tpetra::MultiVector<ST, LO, GO, NT> > > subY;
91  RCP<const Thyra::DefaultProductMultiVector<ST> > prod_Y =
92  rcp_dynamic_cast<const Thyra::DefaultProductMultiVector<ST> >(thyra_Y);
93 
94  // convert thyra product vector to subY
95  for (unsigned int i = 0; i < blockMaps_.size(); i++) {
96  RCP<const Thyra::TpetraMultiVector<ST, LO, GO, NT> > tmv =
97  rcp_dynamic_cast<const Thyra::TpetraMultiVector<ST, LO, GO, NT> >(
98  prod_Y->getMultiVectorBlock(i), true);
99  subY.push_back(tmv->getConstTpetraMultiVector());
100  }
101 
102  // endow the subVectors with required information about the maps
103  Strided::associateSubVectors(blockMaps_, subY);
104 
105  // copy solution vectors to Y vector
106  Strided::many2one(Y, subY, blockExport_);
107 }
108 
109 // this is the core routine that builds the maps
110 // and importers/exporters neccessary for all the
111 // transfers. Currently it simply calls out to the
112 // interlaced tpetra functions. (Comment: this
113 // routine should probably be private or protected
114 // ... it is basically the meat of the constructor)
115 //
116 // arguments:
117 // vars - Vector describing the blocking of variables
118 // baseMap - basic map to use in the transfers
119 // comm - Teuchos::Comm<int> object
120 //
121 void TpetraStridedMappingStrategy::buildBlockTransferData(
122  const std::vector<int>& vars, const Teuchos::RCP<const Tpetra::Map<LO, GO, NT> >& baseMap,
123  const Teuchos::Comm<int>& comm) {
124  // build maps and exporters/importers
125  Strided::buildSubMaps(*baseMap, vars, comm, blockMaps_);
126  Strided::buildExportImport(*baseMap, blockMaps_, blockExport_, blockImport_);
127 }
128 
129 // Builds a blocked Thyra operator that uses the strided
130 // mapping strategy to define sub blocks.
131 //
132 // arguments:
133 // mat - Tpetra::CrsMatrix<ST,LO,GO,NT> with FillComplete called, this
134 // matrix is assumed to be square, with the same
135 // range and domain maps
136 // returns: Blocked Thyra linear operator with sub blocks
137 // defined by this mapping strategy
138 //
139 const Teuchos::RCP<Thyra::BlockedLinearOpBase<ST> >
140 TpetraStridedMappingStrategy::buildBlockedThyraOp(
141  const RCP<const Tpetra::CrsMatrix<ST, LO, GO, NT> >& crsContent,
142  const std::string& label) const {
143  int dim = blockMaps_.size();
144 
145  RCP<Thyra::DefaultBlockedLinearOp<ST> > A = Thyra::defaultBlockedLinearOp<ST>();
146 
147  A->beginBlockFill(dim, dim);
148  for (int i = 0; i < dim; i++) {
149  for (int j = 0; j < dim; j++) {
150  // label block correctly
151  std::stringstream ss;
152  ss << label << "_" << i << "," << j;
153 
154  // build the blocks and place it the right location
155  RCP<Tpetra::CrsMatrix<ST, LO, GO, NT> > Aij =
156  Strided::buildSubBlock(i, j, crsContent, blockMaps_);
157  A->setNonconstBlock(i, j,
158  Thyra::tpetraLinearOp<ST, LO, GO, NT>(
159  Thyra::tpetraVectorSpace<ST, LO, GO, NT>(Aij->getRangeMap()),
160  Thyra::tpetraVectorSpace<ST, LO, GO, NT>(Aij->getDomainMap()), Aij));
161  }
162  } // end for i
163  A->endBlockFill();
164 
165  return A;
166 }
167 
168 // Rebuilds a blocked Thyra operator that uses the strided
169 // mapping strategy to define sub blocks.
170 //
171 // arguments:
172 // crsContent - Tpetra::CrsMatrix<ST,LO,GO,NT> with FillComplete called, this
173 // matrix is assumed to be square, with the same
174 // range and domain maps
175 // A - Destination block linear op composed of blocks of
176 // Tpetra::CrsMatrix<ST,LO,GO,NT> at all relevant locations
177 //
178 void TpetraStridedMappingStrategy::rebuildBlockedThyraOp(
179  const RCP<const Tpetra::CrsMatrix<ST, LO, GO, NT> >& crsContent,
180  const RCP<Thyra::BlockedLinearOpBase<ST> >& A) const {
181  int dim = blockMaps_.size();
182 
183  for (int i = 0; i < dim; i++) {
184  for (int j = 0; j < dim; j++) {
185  // get Tpetra version of desired block
186  RCP<Thyra::LinearOpBase<ST> > Aij = A->getNonconstBlock(i, j);
187  RCP<Thyra::TpetraLinearOp<ST, LO, GO, NT> > tAij =
188  rcp_dynamic_cast<Thyra::TpetraLinearOp<ST, LO, GO, NT> >(Aij, true);
189  RCP<Tpetra::CrsMatrix<ST, LO, GO, NT> > eAij =
190  rcp_dynamic_cast<Tpetra::CrsMatrix<ST, LO, GO, NT> >(tAij->getTpetraOperator(), true);
191 
192  // rebuild the blocks and place it the right location
193  Strided::rebuildSubBlock(i, j, crsContent, blockMaps_, *eAij);
194  }
195  } // end for i
196 }
197 
198 } // end namespace TpetraHelpers
199 } // end namespace Teko