Thyra_SpmdVectorSpaceDefaultBase.hpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef THYRA_SPMD_VECTOR_SPACE_BASE_HPP
00030 #define THYRA_SPMD_VECTOR_SPACE_BASE_HPP
00031 
00032 #include "Thyra_SpmdVectorSpaceDefaultBaseDecl.hpp"
00033 #include "Thyra_ScalarProdVectorSpaceBase.hpp"
00034 #include "Thyra_DefaultSpmdVectorSpaceFactory.hpp"
00035 #include "Thyra_SpmdVectorSpaceUtilities.hpp"
00036 
00037 namespace Thyra {
00038 
00039 template<class Scalar>
00040 SpmdVectorSpaceDefaultBase<Scalar>::SpmdVectorSpaceDefaultBase()
00041   :mapCode_(-1),defaultLocalOffset_(-1),defaultGlobalDim_(-1),localSubDim_(-1)
00042 {}
00043 
00044 // Virtual methods with default implementations
00045 
00046 template<class Scalar>
00047 Index SpmdVectorSpaceDefaultBase<Scalar>::localOffset() const
00048 {
00049   return defaultLocalOffset_;
00050 }
00051 
00052 template<class Scalar>
00053 Index SpmdVectorSpaceDefaultBase<Scalar>::mapCode() const
00054 {
00055   return mapCode_;
00056 }
00057 
00058 
00059 template<class Scalar>
00060 std::string SpmdVectorSpaceDefaultBase<Scalar>::description() const
00061 {
00062   using Teuchos::RCP; using Teuchos::Comm; using Teuchos::null;
00063   std::ostringstream ostr;
00064   ostr << Teuchos::typeName(*this) << "{";
00065   ostr << "globalDim="<<this->dim();
00066   ostr << ",localSubDim="<<this->localSubDim();
00067   ostr << ",localOffset="<<this->localOffset();
00068   ostr << ",comm=";
00069   RCP<const Comm<Index> > comm;
00070   if ( (comm=this->getComm())!=null ) {
00071     ostr << comm->description();
00072   }
00073   else {
00074     ostr << "NULL";
00075   }
00076   ostr << "}";
00077   return ostr.str();
00078 }
00079 
00080 
00081 // Overridden from VectorSpaceBase
00082 
00083 template<class Scalar>
00084 Index SpmdVectorSpaceDefaultBase<Scalar>::dim() const
00085 {
00086   return defaultGlobalDim_;
00087 }
00088 
00089 template<class Scalar>
00090 Teuchos::RCP< const VectorSpaceFactoryBase<Scalar> >
00091 SpmdVectorSpaceDefaultBase<Scalar>::smallVecSpcFcty() const
00092 {
00093   return smallVecSpcFcty_;
00094 }
00095 
00096 template<class Scalar>
00097 bool SpmdVectorSpaceDefaultBase<Scalar>::isCompatible(
00098   const VectorSpaceBase<Scalar>& vecSpc
00099   ) const
00100 {
00101   if( this->hasInCoreView() && vecSpc.hasInCoreView() )
00102     return this->dim() == vecSpc.dim();
00103   const SpmdVectorSpaceBase<Scalar>
00104     *mpiVecSpc = dynamic_cast<const SpmdVectorSpaceBase<Scalar>*>(&vecSpc);
00105   if(mpiVecSpc)
00106     return mapCode() == mpiVecSpc->mapCode();
00107   return false;
00108 }
00109 
00110 // protected
00111 
00112 template<class Scalar>
00113 void SpmdVectorSpaceDefaultBase<Scalar>::updateState( const Index globalDim )
00114 {
00115   localSubDim_ = this->localSubDim(); 
00116   const Teuchos::RCP<const Teuchos::Comm<Index> >
00117     comm = this->getComm();
00118   if( localSubDim_ >= 0 ) {
00119     int numProc = 1;
00120     int procRank = 0;
00121     if( comm.get() ) {
00122       numProc = comm->getSize();
00123       procRank = comm->getRank();
00124     }
00125     if( numProc > 1 && (localSubDim_ < globalDim || globalDim < 0) ) {
00126       mapCode_ = SpmdVectorSpaceUtilities::computeMapCode(*comm,localSubDim_);
00127       defaultLocalOffset_
00128         = SpmdVectorSpaceUtilities::computeLocalOffset(*comm,localSubDim_);
00129       if( globalDim < 1 ) {
00130         defaultGlobalDim_
00131           = SpmdVectorSpaceUtilities::computeGlobalDim(*comm,localSubDim_);
00132       }
00133       else {
00134         defaultGlobalDim_ = globalDim;
00135         // ToDo: Perform global reduction to check that this is correct in
00136         // debug build
00137       }
00138     }
00139     else {
00140       // This is a serial or a locally-replicated parallel
00141       // vector space.
00142       mapCode_ = localSubDim_;
00143       defaultLocalOffset_ = 0;
00144       defaultGlobalDim_ = localSubDim_;
00145     }
00146   }
00147   else {
00148     mapCode_  = -1;     // Uninitialized!
00149     defaultLocalOffset_ = -1;
00150     defaultGlobalDim_ = -1;
00151   }
00152   smallVecSpcFcty_
00153     = Teuchos::rcp(new DefaultSpmdVectorSpaceFactory<Scalar>(comm));
00154 }
00155   
00156 } // end namespace Thyra
00157 
00158 #endif // THYRA_SPMD_VECTOR_SPACE_BASE_HPP

Generated on Tue Oct 20 12:46:59 2009 for Thyra Operator/Vector Support by doxygen 1.4.7