Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_BlockedMappingStrategy.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 "Epetra/Teko_BlockedMappingStrategy.hpp"
48 #include "Epetra/Teko_EpetraHelpers.hpp"
49 
50 #include "Thyra_EpetraThyraWrappers.hpp"
51 #include "Thyra_EpetraLinearOp.hpp"
52 #include "Thyra_DefaultProductMultiVector.hpp"
53 #include "Thyra_DefaultProductVectorSpace.hpp"
54 #include "Thyra_DefaultSpmdMultiVector.hpp"
55 #include "Thyra_DefaultBlockedLinearOp.hpp"
56 #include "Thyra_get_Epetra_Operator.hpp"
57 
58 using Teuchos::RCP;
59 using Teuchos::rcp;
60 using Teuchos::rcp_dynamic_cast;
61 
62 namespace Teko {
63 namespace Epetra {
64 
65 // Creates a strided mapping strategy. This class is useful
66 // for breaking up nodally ordered matrices (i.e. the unknowns
67 // in a FEM problem are ordered [u0,v0,p0,u1,v1,p1,...]). Current
68 // implimentation only supports a fixed number of variables
69 //
70 // arguments:
71 // vars - Number of different variables
72 // map - original Epetra_Map to be broken up
73 // comm - Epetra_Comm object related to the map
74 //
75 BlockedMappingStrategy::BlockedMappingStrategy(const std::vector<std::vector<int> >& vars,
76  const Teuchos::RCP<const Epetra_Map>& map,
77  const Epetra_Comm& comm) {
78  rangeMap_ = map;
79  domainMap_ = map;
80  buildBlockTransferData(vars, rangeMap_, comm);
81 }
82 
83 // Virtual function defined in MappingStrategy. This copies
84 // an Epetra_MultiVector into a Thyra::MultiVectorBase with
85 // blocking handled by the strides defined in the constructor.
86 //
87 // arguments:
88 // X - source Epetra_MultiVector
89 // thyra_X - destination Thyra::MultiVectorBase
90 //
91 void BlockedMappingStrategy::copyEpetraIntoThyra(
92  const Epetra_MultiVector& X,
93  const Teuchos::Ptr<Thyra::MultiVectorBase<double> >& thyra_X) const {
94  int count = X.NumVectors();
95 
96  std::vector<RCP<Epetra_MultiVector> > subX;
97 
98  // allocate vectors to copy into
99  Blocking::buildSubVectors(blockMaps_, subX, count);
100 
101  // copy source vector to X vector
102  Blocking::one2many(subX, X, blockImport_);
103 
104  // convert subX to an array of multi vectors
105  Teuchos::Array<RCP<Thyra::MultiVectorBase<double> > > thyra_subX;
106  Teuchos::Ptr<Thyra::ProductMultiVectorBase<double> > prod_X =
107  Teuchos::ptr_dynamic_cast<Thyra::ProductMultiVectorBase<double> >(thyra_X);
108  for (unsigned int i = 0; i < blockMaps_.size(); i++) {
109  RCP<Thyra::DefaultSpmdMultiVector<double> > vec =
110  rcp_dynamic_cast<Thyra::DefaultSpmdMultiVector<double> >(
111  prod_X->getNonconstMultiVectorBlock(i));
112  fillDefaultSpmdMultiVector(vec, subX[i]);
113  }
114 }
115 
116 // Virtual function defined in MappingStrategy. This copies
117 // an Epetra_MultiVector 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 Epetra_MultiVector
123 //
124 void BlockedMappingStrategy::copyThyraIntoEpetra(
125  const RCP<const Thyra::MultiVectorBase<double> >& thyra_Y, Epetra_MultiVector& Y) const {
126  std::vector<RCP<const Epetra_MultiVector> > subY;
127  RCP<const Thyra::DefaultProductMultiVector<double> > prod_Y =
128  rcp_dynamic_cast<const Thyra::DefaultProductMultiVector<double> >(thyra_Y);
129 
130  // convert thyra product vector to subY
131  for (unsigned int i = 0; i < blockMaps_.size(); i++)
132  subY.push_back(
133  Thyra::get_Epetra_MultiVector(*blockMaps_[i].second, prod_Y->getMultiVectorBlock(i)));
134 
135  // endow the subVectors with required information about the maps
136  // Blocking::associateSubVectors(blockMaps_,subY);
137 
138  // copy solution vectors to Y vector
139  Blocking::many2one(Y, subY, blockExport_);
140 }
141 
142 // this is the core routine that builds the maps
143 // and importers/exporters neccessary for all the
144 // transfers. Currently it simply calls out to the
145 // interlaced epetra functions. (Comment: this
146 // routine should probably be private or protected
147 // ... it is basically the meat of the constructor)
148 //
149 // arguments:
150 // vars - Vector describing the blocking of variables
151 // baseMap - basic map to use in the transfers
152 // comm - Epetra_Comm object
153 //
154 void BlockedMappingStrategy::buildBlockTransferData(const std::vector<std::vector<int> >& vars,
155  const Teuchos::RCP<const Epetra_Map>& baseMap,
156  const Epetra_Comm& comm) {
157  // build block for each vector
158  for (std::size_t i = 0; i < vars.size(); i++) {
159  // build maps and exporters/importers
160  Blocking::MapPair mapPair = Blocking::buildSubMap(vars[i], comm);
161  Blocking::ImExPair iePair = Blocking::buildExportImport(*baseMap, mapPair);
162 
163  blockMaps_.push_back(mapPair);
164  blockImport_.push_back(iePair.first);
165  blockExport_.push_back(iePair.second);
166  }
167 }
168 
169 // Builds a blocked Thyra operator that uses the strided
170 // mapping strategy to define sub blocks.
171 //
172 // arguments:
173 // mat - Epetra_CrsMatrix with FillComplete called, this
174 // matrix is assumed to be square, with the same
175 // range and domain maps
176 // returns: Blocked Thyra linear operator with sub blocks
177 // defined by this mapping strategy
178 //
179 const Teuchos::RCP<Thyra::BlockedLinearOpBase<double> > BlockedMappingStrategy::buildBlockedThyraOp(
180  const RCP<const Epetra_CrsMatrix>& crsContent, const std::string& label) const {
181  int dim = blockMaps_.size();
182 
183  RCP<Thyra::DefaultBlockedLinearOp<double> > A = Thyra::defaultBlockedLinearOp<double>();
184 
185  A->beginBlockFill(dim, dim);
186  for (int i = 0; i < dim; i++) {
187  for (int j = 0; j < dim; j++) {
188  // label block correctly
189  std::stringstream ss;
190  ss << label << "_" << i << "," << j;
191 
192  // build the blocks and place it the right location
193  RCP<Epetra_CrsMatrix> blk = Blocking::buildSubBlock(i, j, *crsContent, blockMaps_);
194  A->setNonconstBlock(i, j, Thyra::nonconstEpetraLinearOp(blk, ss.str()));
195  }
196  } // end for i
197  A->endBlockFill();
198 
199  return A;
200 }
201 
202 // Rebuilds a blocked Thyra operator that uses the strided
203 // mapping strategy to define sub blocks.
204 //
205 // arguments:
206 // crsContent - Epetra_CrsMatrix with FillComplete called, this
207 // matrix is assumed to be square, with the same
208 // range and domain maps
209 // A - Destination block linear op composed of blocks of
210 // Epetra_CrsMatrix at all relevant locations
211 //
212 void BlockedMappingStrategy::rebuildBlockedThyraOp(
213  const RCP<const Epetra_CrsMatrix>& crsContent,
214  const RCP<Thyra::BlockedLinearOpBase<double> >& A) const {
215  int dim = blockMaps_.size();
216 
217  for (int i = 0; i < dim; i++) {
218  for (int j = 0; j < dim; j++) {
219  // get Epetra version of desired block
220  RCP<Thyra::LinearOpBase<double> > Aij = A->getNonconstBlock(i, j);
221  RCP<Epetra_CrsMatrix> eAij =
222  rcp_dynamic_cast<Epetra_CrsMatrix>(Thyra::get_Epetra_Operator(*Aij), true);
223 
224  // rebuild the blocks and place it the right location
225  Blocking::rebuildSubBlock(i, j, *crsContent, blockMaps_, *eAij);
226  }
227  } // end for i
228 }
229 
230 } // end namespace Epetra
231 } // end namespace Teko