Teko Version of the Day
Teko_StridedMappingStrategy.cpp
00001 /*
00002 // @HEADER
00003 // 
00004 // ***********************************************************************
00005 // 
00006 //      Teko: A package for block and physics based preconditioning
00007 //                  Copyright 2010 Sandia Corporation 
00008 //  
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 //  
00012 // Redistribution and use in source and binary forms, with or without
00013 // modification, are permitted provided that the following conditions are
00014 // met:
00015 //  
00016 // 1. Redistributions of source code must retain the above copyright
00017 // notice, this list of conditions and the following disclaimer.
00018 //  
00019 // 2. Redistributions in binary form must reproduce the above copyright
00020 // notice, this list of conditions and the following disclaimer in the
00021 // documentation and/or other materials provided with the distribution.
00022 //  
00023 // 3. Neither the name of the Corporation nor the names of the
00024 // contributors may be used to endorse or promote products derived from
00025 // this software without specific prior written permission. 
00026 //  
00027 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00028 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00030 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00031 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00032 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00033 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00034 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00035 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00036 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00037 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00038 //  
00039 // Questions? Contact Eric C. Cyr (eccyr@sandia.gov)
00040 // 
00041 // ***********************************************************************
00042 // 
00043 // @HEADER
00044 
00045 */
00046 
00047 #include "Epetra/Teko_StridedMappingStrategy.hpp"
00048 #include "Epetra/Teko_InterlacedEpetra.hpp"
00049 #include "Epetra/Teko_EpetraHelpers.hpp"
00050 
00051 #include "Thyra_EpetraThyraWrappers.hpp"
00052 #include "Thyra_EpetraLinearOp.hpp"
00053 #include "Thyra_DefaultProductMultiVector.hpp"
00054 #include "Thyra_DefaultProductVectorSpace.hpp"
00055 #include "Thyra_DefaultSpmdMultiVector.hpp"
00056 #include "Thyra_DefaultBlockedLinearOp.hpp"
00057 #include "Thyra_get_Epetra_Operator.hpp"
00058 
00059 using Teuchos::RCP;
00060 using Teuchos::rcp;
00061 using Teuchos::rcp_dynamic_cast;
00062 
00063 namespace Teko {
00064 namespace Epetra {
00065 
00066 // Creates a strided mapping strategy. This class is useful
00067 // for breaking up nodally ordered matrices (i.e. the unknowns
00068 // in a FEM problem are ordered [u0,v0,p0,u1,v1,p1,...]). Current
00069 // implimentation only supports a fixed number of variables
00070 //
00071 //    arguments: 
00072 //       vars - Number of different variables 
00073 //       map  - original Epetra_Map to be broken up
00074 //       comm - Epetra_Comm object related to the map
00075 //
00076 StridedMappingStrategy::StridedMappingStrategy(const std::vector<int> & vars,const RCP<const Epetra_Map> & map,
00077                                                const Epetra_Comm & comm)
00078 {
00079    rangeMap_ = map;
00080    domainMap_ = map;
00081    buildBlockTransferData(vars, rangeMap_,comm);
00082 }
00083 
00084 // Virtual function defined in MappingStrategy.  This copies
00085 // an Epetra_MultiVector into a Thyra::MultiVectorBase with
00086 // blocking handled by the strides defined in the constructor.
00087 //
00088 //   arguments:
00089 //      X       - source Epetra_MultiVector
00090 //      thyra_X - destination Thyra::MultiVectorBase
00091 //      eow     - Operator that defines the transition...this may
00092 //                be removed in the future
00093 //
00094 void StridedMappingStrategy::copyEpetraIntoThyra(const Epetra_MultiVector& X,
00095                                                  const Teuchos::Ptr<Thyra::MultiVectorBase<double> > & thyra_X,
00096                                                  const Teko::Epetra::EpetraOperatorWrapper & eow) const
00097 {
00098    int count = X.NumVectors(); 
00099 
00100    std::vector<RCP<Epetra_MultiVector> > subX;
00101 
00102    // allocate vectors to copy into
00103    Strided::buildSubVectors(blockMaps_,subX,count);
00104 
00105    // copy source vector to X vector
00106    Strided::one2many(subX,X,blockImport_);
00107 
00108    // convert subX to an array of multi vectors
00109    Teuchos::Array<RCP<Thyra::MultiVectorBase<double> > > thyra_subX;
00110    Teuchos::Ptr<Thyra::ProductMultiVectorBase<double> > prod_X
00111          = Teuchos::ptr_dynamic_cast<Thyra::ProductMultiVectorBase<double> >(thyra_X);
00112    for(unsigned int i=0;i<blockMaps_.size();i++) {
00113       RCP<Thyra::DefaultSpmdMultiVector<double> > vec 
00114             = rcp_dynamic_cast<Thyra::DefaultSpmdMultiVector<double> >(prod_X->getNonconstMultiVectorBlock(i)); 
00115       fillDefaultSpmdMultiVector(vec,subX[i]);
00116    }
00117 }
00118 
00119 // Virtual function defined in MappingStrategy.  This copies
00120 // an Epetra_MultiVector into a Thyra::MultiVectorBase with
00121 // blocking handled by the strides defined in the constructor.
00122 //
00123 //   arguments:
00124 //      thyra_Y - source Thyra::MultiVectorBase
00125 //      Y       - destination Epetra_MultiVector
00126 //      eow     - Operator that defines the transition...this may
00127 //                be removed in the future
00128 //
00129 void StridedMappingStrategy::copyThyraIntoEpetra(const RCP<const Thyra::MultiVectorBase<double> > & thyra_Y,
00130                                                  Epetra_MultiVector& Y,
00131                                                  const Teko::Epetra::EpetraOperatorWrapper & eow) const
00132 {
00133    std::vector<RCP<const Epetra_MultiVector> > subY;
00134    RCP<const Thyra::DefaultProductMultiVector<double> > prod_Y 
00135          = rcp_dynamic_cast<const Thyra::DefaultProductMultiVector<double> >(thyra_Y);
00136 
00137    // convert thyra product vector to subY
00138    for(unsigned int i=0;i<blockMaps_.size();i++)
00139       subY.push_back(Thyra::get_Epetra_MultiVector(*blockMaps_[i].second,prod_Y->getMultiVectorBlock(i)));
00140 
00141    // endow the subVectors with required information about the maps
00142    Strided::associateSubVectors(blockMaps_,subY);
00143 
00144    // copy solution vectors to Y vector
00145    Strided::many2one(Y,subY,blockExport_);
00146 }
00147 
00148 // this is the core routine that builds the maps
00149 // and importers/exporters neccessary for all the
00150 // transfers. Currently it simply calls out to the
00151 // interlaced epetra functions. (Comment: this
00152 // routine should probably be private or protected
00153 // ... it is basically the meat of the constructor)
00154 //
00155 //    arguments:
00156 //       vars - Vector describing the blocking of variables
00157 //       baseMap - basic map to use in the transfers
00158 //       comm    - Epetra_Comm object
00159 //
00160 void StridedMappingStrategy::buildBlockTransferData(const std::vector<int> & vars,const Teuchos::RCP<const Epetra_Map> & baseMap, const Epetra_Comm & comm)
00161 {
00162    // build maps and exporters/importers
00163    Strided::buildSubMaps(*baseMap,vars,comm,blockMaps_);
00164    Strided::buildExportImport(*baseMap, blockMaps_, blockExport_,blockImport_);
00165 }
00166 
00167 // Builds a blocked Thyra operator that uses the strided
00168 // mapping strategy to define sub blocks.
00169 //
00170 //    arguments:
00171 //       mat - Epetra_CrsMatrix with FillComplete called, this
00172 //             matrix is assumed to be square, with the same
00173 //             range and domain maps
00174 //    returns: Blocked Thyra linear operator with sub blocks
00175 //             defined by this mapping strategy
00176 //
00177 const Teuchos::RCP<Thyra::BlockedLinearOpBase<double> > 
00178 StridedMappingStrategy::buildBlockedThyraOp(const RCP<const Epetra_CrsMatrix> & crsContent,const std::string & label) const
00179 {
00180    int dim = blockMaps_.size();
00181 
00182    RCP<Thyra::DefaultBlockedLinearOp<double> > A = Thyra::defaultBlockedLinearOp<double>();
00183 
00184    A->beginBlockFill(dim,dim);
00185    for(int i=0;i<dim;i++) {
00186       for(int j=0;j<dim;j++) {
00187          // label block correctly
00188          std::stringstream ss;
00189          ss << label << "_" << i << "," << j;
00190 
00191          // build the blocks and place it the right location
00192          A->setNonconstBlock(i,j,Thyra::nonconstEpetraLinearOp(Strided::buildSubBlock(i,j,*crsContent,blockMaps_),ss.str()));
00193       }
00194    } // end for i
00195    A->endBlockFill();
00196 
00197    return A;
00198 }
00199 
00200 // Rebuilds a blocked Thyra operator that uses the strided
00201 // mapping strategy to define sub blocks.
00202 //
00203 //    arguments:
00204 //       crsContent - Epetra_CrsMatrix with FillComplete called, this
00205 //                    matrix is assumed to be square, with the same
00206 //                    range and domain maps
00207 //       A - Destination block linear op composed of blocks of
00208 //           Epetra_CrsMatrix at all relevant locations
00209 //
00210 void StridedMappingStrategy::rebuildBlockedThyraOp(const RCP<const Epetra_CrsMatrix> & crsContent,
00211                                                    const RCP<Thyra::BlockedLinearOpBase<double> > & A) const
00212 {
00213    int dim = blockMaps_.size();
00214 
00215    for(int i=0;i<dim;i++) {
00216       for(int j=0;j<dim;j++) {
00217          // get Epetra version of desired block
00218          RCP<Thyra::LinearOpBase<double> > Aij = A->getNonconstBlock(i,j);
00219          RCP<Epetra_CrsMatrix> eAij = rcp_dynamic_cast<Epetra_CrsMatrix>(Thyra::get_Epetra_Operator(*Aij),true);
00220 
00221          // rebuild the blocks and place it the right location
00222          Strided::rebuildSubBlock(i,j,*crsContent,blockMaps_,*eAij);
00223       }
00224    } // end for i
00225 }
00226 
00227 } // end namespace Epetra
00228 } // end namespace Teko
 All Classes Files Functions Variables