Teko Version of the Day
Teko_EpetraThyraConverter.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 "Teko_EpetraThyraConverter.hpp"
00048 
00049 // Teuchos includes
00050 #include "Teuchos_Array.hpp"
00051 
00052 // Thyra includes
00053 #include "Thyra_DefaultProductVectorSpace.hpp"
00054 #include "Thyra_DefaultProductMultiVector.hpp"
00055 #include "Thyra_SpmdMultiVectorBase.hpp"
00056 #include "Thyra_SpmdVectorSpaceBase.hpp"
00057 #include "Thyra_MultiVectorStdOps.hpp"
00058 
00059 #include <iostream>
00060 #include <vector>
00061 
00062 using Teuchos::RCP;
00063 using Teuchos::Ptr;
00064 using Teuchos::rcp;
00065 using Teuchos::rcpFromRef;
00066 using Teuchos::rcp_dynamic_cast;
00067 using Teuchos::ptr_dynamic_cast;
00068 using Teuchos::null;
00069 
00070 namespace Teko {
00071 namespace Epetra {
00072 
00073 // const Teuchos::RCP<const Thyra::MultiVectorBase<double> > 
00074 // blockEpetraToThyra(int numVectors,const double * epetraData,int leadingDim,const Teuchos::RCP<const Thyra::VectorSpaceBase<double> > & vs,int & localDim)
00075 
00076 void blockEpetraToThyra(int numVectors,const double * epetraData,int leadingDim,const Teuchos::Ptr<Thyra::MultiVectorBase<double> > & mv,int & localDim)
00077 {
00078    localDim = 0;
00079 
00080    // check the base case
00081    const Ptr<Thyra::ProductMultiVectorBase<double> > prodMV 
00082          = ptr_dynamic_cast<Thyra::ProductMultiVectorBase<double> > (mv);
00083    if(prodMV==Teuchos::null) {
00084       // VS object must be a SpmdMultiVector object
00085       const Ptr<Thyra::SpmdMultiVectorBase<double> > spmdX  = ptr_dynamic_cast<Thyra::SpmdMultiVectorBase<double> >(mv,true);
00086       const RCP<const Thyra::SpmdVectorSpaceBase<double> > spmdVS = spmdX->spmdSpace();
00087 
00088       int localSubDim = spmdVS->localSubDim();
00089 
00090       Thyra::Ordinal thyraLeadingDim=0;
00091       double * thyraData=0;
00092       spmdX->getLocalData(&thyraData,&thyraLeadingDim);
00093 
00094       for(int i=0;i<localSubDim;i++) {
00095          // copy each vector
00096          for(int v=0;v<numVectors;v++)
00097             thyraData[i+thyraLeadingDim*v] = epetraData[i+leadingDim*v];
00098       }
00099 
00100       spmdX->commitLocalData(thyraData);
00101 
00102       // set the local dimension
00103       localDim = localSubDim;
00104 
00105       return;
00106    }
00107 
00108    // this keeps track of current location in the epetraData vector
00109    const double * localData = epetraData;
00110 
00111    // loop over all the blocks in the vector space
00112    for(int blkIndex=0;blkIndex<prodMV->productSpace()->numBlocks();blkIndex++) {
00113       int subDim = 0;
00114       const RCP<Thyra::MultiVectorBase<double> > blockVec = prodMV->getNonconstMultiVectorBlock(blkIndex);
00115 
00116       // perorm the recusive copy
00117       blockEpetraToThyra(numVectors, localData,leadingDim,blockVec.ptr(),subDim);
00118 
00119       // shift to the next block
00120       localData += subDim;
00121 
00122       // account for the size of this subblock
00123       localDim += subDim;
00124    }
00125 }
00126 
00127 // Convert a Epetra_MultiVector with assumed block structure dictated by the
00128 // vector space into a Thyra::MultiVectorBase object.
00129 // const Teuchos::RCP<const Thyra::MultiVectorBase<double> > blockEpetraToThyra(const Epetra_MultiVector & e,const Teuchos::RCP<const Thyra::VectorSpaceBase<double> > & vs)
00130 void blockEpetraToThyra(const Epetra_MultiVector & epetraX,const Teuchos::Ptr<Thyra::MultiVectorBase<double> > & thyraX) 
00131 {
00132    TEUCHOS_ASSERT(thyraX->range()->dim()==epetraX.GlobalLength());
00133 
00134    // extract local information from the Epetra_MultiVector
00135    int leadingDim=0,numVectors=0,localDim=0;
00136    double * epetraData=0;
00137    epetraX.ExtractView(&epetraData,&leadingDim);
00138 
00139    numVectors = epetraX.NumVectors();
00140 
00141    blockEpetraToThyra(numVectors,epetraData,leadingDim,thyraX.ptr(),localDim);   
00142 
00143    TEUCHOS_ASSERT(localDim==epetraX.MyLength());
00144 }
00145 
00146 void blockThyraToEpetra(int numVectors,double * epetraData,int leadingDim,const Teuchos::RCP<const Thyra::MultiVectorBase<double> > & tX,int & localDim)
00147 {
00148    localDim = 0;
00149 
00150    // check the base case
00151    const RCP<const Thyra::ProductMultiVectorBase<double> > prodX
00152          = rcp_dynamic_cast<const Thyra::ProductMultiVectorBase<double> > (tX);
00153    if(prodX==Teuchos::null) {
00154       // the base case
00155 
00156       // VS object must be a SpmdMultiVector object
00157       RCP<const Thyra::SpmdMultiVectorBase<double> > spmdX  = rcp_dynamic_cast<const Thyra::SpmdMultiVectorBase<double> >(tX,true);
00158       RCP<const Thyra::SpmdVectorSpaceBase<double> > spmdVS = spmdX->spmdSpace();
00159 
00160       int localSubDim = spmdVS->localSubDim();
00161 
00162       Thyra::Ordinal thyraLeadingDim=0;
00163       const double * thyraData=0;
00164       spmdX->getLocalData(&thyraData,&thyraLeadingDim);
00165 
00166       for(int i=0;i<localSubDim;i++) {
00167          // copy each vector
00168          for(int v=0;v<numVectors;v++)
00169             epetraData[i+leadingDim*v] = thyraData[i+thyraLeadingDim*v];
00170       }
00171 
00172       // set the local dimension
00173       localDim = localSubDim;
00174 
00175       return;
00176    }
00177 
00178    const RCP<const Thyra::ProductVectorSpaceBase<double> > prodVS = prodX->productSpace();
00179 
00180    // this keeps track of current location in the epetraData vector
00181    double * localData = epetraData;
00182 
00183    // loop over all the blocks in the vector space
00184    for(int blkIndex=0;blkIndex<prodVS->numBlocks();blkIndex++) {
00185       int subDim = 0;
00186 
00187       // construct the block vector
00188       blockThyraToEpetra(numVectors, localData,leadingDim,prodX->getMultiVectorBlock(blkIndex),subDim);
00189 
00190       // shift to the next block
00191       localData += subDim;
00192 
00193       // account for the size of this subblock
00194       localDim += subDim;
00195    }
00196 
00197    return;
00198 }
00199 
00200 // Convert a Thyra::MultiVectorBase object to a Epetra_MultiVector object with
00201 // the map defined by the Epetra_Map.
00202 // const Teuchos::RCP<const Epetra_MultiVector> 
00203 // blockThyraToEpetra(const Teuchos::RCP<const Thyra::MultiVectorBase<double> > & tX,const RCP<const Epetra_Map> & map)
00204 void blockThyraToEpetra(const Teuchos::RCP<const Thyra::MultiVectorBase<double> > & thyraX,Epetra_MultiVector & epetraX)
00205 {
00206    // build an Epetra_MultiVector object
00207    int numVectors = thyraX->domain()->dim();
00208 
00209    // make sure the number of vectors are the same
00210    TEUCHOS_ASSERT(numVectors==epetraX.NumVectors());
00211    TEUCHOS_ASSERT(thyraX->range()->dim()==epetraX.GlobalLength());
00212 
00213    // extract local information from the Epetra_MultiVector
00214    int leadingDim=0,localDim=0;
00215    double * epetraData=0;
00216    epetraX.ExtractView(&epetraData,&leadingDim);
00217 
00218    // perform recursive copy
00219    blockThyraToEpetra(numVectors,epetraData,leadingDim,thyraX,localDim);
00220 
00221    // sanity check
00222    TEUCHOS_ASSERT(localDim==epetraX.Map().NumMyElements());
00223 }
00224 
00225 void thyraVSToEpetraMap(std::vector<int> & myIndicies, int blockOffset, const Thyra::VectorSpaceBase<double> & vs, int & localDim)
00226 {
00227    // zero out set local dimension
00228    localDim = 0;
00229 
00230    const RCP<const Thyra::ProductVectorSpaceBase<double> > prodVS
00231          = rcp_dynamic_cast<const Thyra::ProductVectorSpaceBase<double> >(rcpFromRef(vs));
00232 
00233    // is more recursion needed?
00234    if(prodVS==Teuchos::null) {
00235       // base case
00236 
00237       // try to cast to an SPMD capable vector space
00238       const RCP<const Thyra::SpmdVectorSpaceBase<double> > spmdVS 
00239             = rcp_dynamic_cast<const Thyra::SpmdVectorSpaceBase<double> >(rcpFromRef(vs));
00240       TEST_FOR_EXCEPTION(spmdVS==Teuchos::null,std::runtime_error,
00241                          "thyraVSToEpetraMap requires all subblocks to be SPMD");
00242 
00243       // get local data storage information
00244       int localOffset = spmdVS->localOffset();
00245       int localSubDim = spmdVS->localSubDim();
00246 
00247       // add indicies to matrix
00248       for(int i=0;i<localSubDim;i++)
00249          myIndicies.push_back(blockOffset+localOffset+i);
00250 
00251       localDim += localSubDim;
00252 
00253       return;
00254    }
00255 
00256    // loop over all the blocks in the vector space
00257    for(int blkIndex=0;blkIndex<prodVS->numBlocks();blkIndex++) {
00258       int subDim = 0;
00259 
00260       // construct the block vector
00261       thyraVSToEpetraMap(myIndicies, blockOffset,*prodVS->getBlock(blkIndex),subDim);
00262 
00263       blockOffset += prodVS->getBlock(blkIndex)->dim();
00264 
00265       // account for the size of this subblock
00266       localDim += subDim;
00267    }
00268 }
00269 
00270 // From a Thyra vector space create a compatable Epetra_Map
00271 const RCP<Epetra_Map> thyraVSToEpetraMap(const Thyra::VectorSpaceBase<double> & vs,const RCP<const Epetra_Comm> & comm)
00272 {
00273    int localDim = 0;
00274    std::vector<int> myGIDs;
00275    
00276    // call recursive routine that constructs the mapping
00277    thyraVSToEpetraMap(myGIDs,0,vs,localDim);
00278 
00279    TEUCHOS_ASSERT(myGIDs.size()==(unsigned int) localDim);
00280 
00281    // create the map
00282    return rcp(new Epetra_Map(vs.dim(), myGIDs.size(), &(myGIDs[0]), 0, *comm));
00283 }
00284 
00285 } // end namespace Epetra
00286 } // end namespace Teko
 All Classes Files Functions Variables