Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_TpetraBlockedMappingStrategy.cpp
1 /*
2 // @HEADER
3 //
4 // ***********************************************************************
5 //
6 // Teko: A package for block and physics based preconditioning
7 // Copyright 2010 Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Eric C. Cyr (eccyr@sandia.gov)
40 //
41 // ***********************************************************************
42 //
43 // @HEADER
44 
45 */
46 
47 #include "Teko_TpetraBlockedMappingStrategy.hpp"
48 #include "Teko_TpetraHelpers.hpp"
49 
50 #include "Thyra_TpetraThyraWrappers.hpp"
51 #include "Thyra_TpetraLinearOp.hpp"
52 #include "Thyra_DefaultProductMultiVector.hpp"
53 #include "Thyra_DefaultProductVectorSpace.hpp"
54 #include "Thyra_DefaultSpmdMultiVector.hpp"
55 #include "Thyra_DefaultBlockedLinearOp.hpp"
56 
57 using Teuchos::RCP;
58 using Teuchos::rcp;
59 using Teuchos::rcp_dynamic_cast;
60 
61 namespace Teko {
62 namespace TpetraHelpers {
63 
64 // Creates a strided mapping strategy. This class is useful
65 // for breaking up nodally ordered matrices (i.e. the unknowns
66 // in a FEM problem are ordered [u0,v0,p0,u1,v1,p1,...]). Current
67 // implimentation only supports a fixed number of variables
68 //
69 // arguments:
70 // vars - Number of different variables
71 // map - original Tpetra::Map<LO,GO,NT> to be broken up
72 // comm - Teuchos::RCP<Teuchos::Comm<int> > object related to the map
73 //
74 TpetraBlockedMappingStrategy::TpetraBlockedMappingStrategy(
75  const std::vector<std::vector<GO> >& vars,
76  const Teuchos::RCP<const Tpetra::Map<LO, GO, NT> >& map, const Teuchos::Comm<int>& comm) {
77  rangeMap_ = map;
78  domainMap_ = map;
79  buildBlockTransferData(vars, rangeMap_, comm);
80 }
81 
82 // Virtual function defined in MappingStrategy. This copies
83 // an Tpetra::MultiVector<ST,LO,GO,NT> into a Thyra::MultiVectorBase with
84 // blocking handled by the strides defined in the constructor.
85 //
86 // arguments:
87 // X - source Tpetra::MultiVector<ST,LO,GO,NT>
88 // thyra_X - destination Thyra::MultiVectorBase
89 //
90 void TpetraBlockedMappingStrategy::copyTpetraIntoThyra(
91  const Tpetra::MultiVector<ST, LO, GO, NT>& X,
92  const Teuchos::Ptr<Thyra::MultiVectorBase<ST> >& thyra_X) const {
93  int count = X.getNumVectors();
94 
95  std::vector<RCP<Tpetra::MultiVector<ST, LO, GO, NT> > > subX;
96 
97  // allocate vectors to copy into
98  Blocking::buildSubVectors(blockMaps_, subX, count);
99 
100  // copy source vector to X vector
101  Blocking::one2many(subX, X, blockImport_);
102 
103  // convert subX to an array of multi vectors
104  Teuchos::Array<RCP<Thyra::MultiVectorBase<ST> > > thyra_subX;
105  Teuchos::Ptr<Thyra::ProductMultiVectorBase<ST> > prod_X =
106  Teuchos::ptr_dynamic_cast<Thyra::ProductMultiVectorBase<ST> >(thyra_X);
107  for (unsigned int i = 0; i < blockMaps_.size(); i++) {
108  RCP<Thyra::TpetraMultiVector<ST, LO, GO, NT> > vec =
109  rcp_dynamic_cast<Thyra::TpetraMultiVector<ST, LO, GO, NT> >(
110  prod_X->getNonconstMultiVectorBlock(i), true);
111 
112  fillDefaultSpmdMultiVector(vec, subX[i]);
113  }
114 }
115 
116 // Virtual function defined in MappingStrategy. This copies
117 // an Tpetra::MultiVector<ST,LO,GO,NT> into a Thyra::MultiVectorBase with
118 // blocking handled by the strides defined in the constructor.
119 //
120 // arguments:
121 // thyra_Y - source Thyra::MultiVectorBase
122 // Y - destination Tpetra::MultiVector<ST,LO,GO,NT>
123 //
124 void TpetraBlockedMappingStrategy::copyThyraIntoTpetra(
125  const RCP<const Thyra::MultiVectorBase<ST> >& thyra_Y,
126  Tpetra::MultiVector<ST, LO, GO, NT>& Y) const {
127  std::vector<RCP<const Tpetra::MultiVector<ST, LO, GO, NT> > > subY;
128  RCP<const Thyra::DefaultProductMultiVector<ST> > prod_Y =
129  rcp_dynamic_cast<const Thyra::DefaultProductMultiVector<ST> >(thyra_Y);
130 
131  // convert thyra product vector to subY
132  for (unsigned int i = 0; i < blockMaps_.size(); i++) {
133  RCP<const Thyra::TpetraMultiVector<ST, LO, GO, NT> > tmv =
134  rcp_dynamic_cast<const Thyra::TpetraMultiVector<ST, LO, GO, NT> >(
135  prod_Y->getMultiVectorBlock(i), true);
136  subY.push_back(tmv->getConstTpetraMultiVector());
137  }
138 
139  // endow the subVectors with required information about the maps
140  // Blocking::associateSubVectors(blockMaps_,subY);
141 
142  // copy solution vectors to Y vector
143  Blocking::many2one(Y, subY, blockExport_);
144 }
145 
146 // this is the core routine that builds the maps
147 // and importers/exporters neccessary for all the
148 // transfers. Currently it simply calls out to the
149 // interlaced tpetra functions. (Comment: this
150 // routine should probably be private or protected
151 // ... it is basically the meat of the constructor)
152 //
153 // arguments:
154 // vars - Vector describing the blocking of variables
155 // baseMap - basic map to use in the transfers
156 // comm - Teuchos::RCP<Teuchos::Comm<int> > object
157 //
158 void TpetraBlockedMappingStrategy::buildBlockTransferData(
159  const std::vector<std::vector<GO> >& vars,
160  const Teuchos::RCP<const Tpetra::Map<LO, GO, NT> >& baseMap, const Teuchos::Comm<int>& comm) {
161  // build block for each vector
162  for (std::size_t i = 0; i < vars.size(); i++) {
163  // build maps and exporters/importers
164  Blocking::MapPair mapPair = Blocking::buildSubMap(vars[i], comm);
165  Blocking::ImExPair iePair = Blocking::buildExportImport(*baseMap, mapPair);
166 
167  blockMaps_.push_back(mapPair);
168  blockImport_.push_back(iePair.first);
169  blockExport_.push_back(iePair.second);
170  }
171 }
172 
173 // Builds a blocked Thyra operator that uses the strided
174 // mapping strategy to define sub blocks.
175 //
176 // arguments:
177 // mat - Tpetra::CrsMatrix<ST,LO,GO,NT> with FillComplete called, this
178 // matrix is assumed to be square, with the same
179 // range and domain maps
180 // returns: Blocked Thyra linear operator with sub blocks
181 // defined by this mapping strategy
182 //
183 const Teuchos::RCP<Thyra::BlockedLinearOpBase<ST> >
184 TpetraBlockedMappingStrategy::buildBlockedThyraOp(
185  const RCP<const Tpetra::CrsMatrix<ST, LO, GO, NT> >& crsContent,
186  const std::string& label) const {
187  int dim = blockMaps_.size();
188 
189  RCP<Thyra::DefaultBlockedLinearOp<ST> > A = Thyra::defaultBlockedLinearOp<ST>();
190 
191  A->beginBlockFill(dim, dim);
192  for (int i = 0; i < dim; i++) {
193  for (int j = 0; j < dim; j++) {
194  // label block correctly
195  std::stringstream ss;
196  ss << label << "_" << i << "," << j;
197 
198  // build the blocks and place it the right location
199  RCP<Tpetra::CrsMatrix<ST, LO, GO, NT> > blk =
200  Blocking::buildSubBlock(i, j, crsContent, blockMaps_);
201  A->setNonconstBlock(i, j,
202  Thyra::tpetraLinearOp<ST, LO, GO, NT>(
203  Thyra::tpetraVectorSpace<ST, LO, GO, NT>(blk->getRangeMap()),
204  Thyra::tpetraVectorSpace<ST, LO, GO, NT>(blk->getDomainMap()), blk));
205  }
206  } // end for i
207  A->endBlockFill();
208 
209  return A;
210 }
211 
212 // Rebuilds a blocked Thyra operator that uses the strided
213 // mapping strategy to define sub blocks.
214 //
215 // arguments:
216 // crsContent - Tpetra::CrsMatrix<ST,LO,GO,NT> with FillComplete called, this
217 // matrix is assumed to be square, with the same
218 // range and domain maps
219 // A - Destination block linear op composed of blocks of
220 // Tpetra::CrsMatrix<ST,LO,GO,NT> at all relevant locations
221 //
222 void TpetraBlockedMappingStrategy::rebuildBlockedThyraOp(
223  const RCP<const Tpetra::CrsMatrix<ST, LO, GO, NT> >& crsContent,
224  const RCP<Thyra::BlockedLinearOpBase<ST> >& A) const {
225  int dim = blockMaps_.size();
226 
227  for (int i = 0; i < dim; i++) {
228  for (int j = 0; j < dim; j++) {
229  // get Tpetra version of desired block
230  RCP<Thyra::LinearOpBase<ST> > Aij = A->getNonconstBlock(i, j);
231  RCP<Thyra::TpetraLinearOp<ST, LO, GO, NT> > tAij =
232  rcp_dynamic_cast<Thyra::TpetraLinearOp<ST, LO, GO, NT> >(Aij, true);
233  RCP<Tpetra::CrsMatrix<ST, LO, GO, NT> > eAij =
234  rcp_dynamic_cast<Tpetra::CrsMatrix<ST, LO, GO, NT> >(tAij->getTpetraOperator(), true);
235 
236  // rebuild the blocks and place it the right location
237  Blocking::rebuildSubBlock(i, j, crsContent, blockMaps_, *eAij);
238  }
239  } // end for i
240 }
241 
242 } // namespace TpetraHelpers
243 } // end namespace Teko