PlayaVectorSpaceImpl.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                 Playa: Programmable Linear Algebra
00005 //                 Copyright 2012 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 #ifndef PLAYA_VECTORSPACEIMPL_HPP
00043 #define PLAYA_VECTORSPACEIMPL_HPP
00044 
00045 #include "PlayaVectorSpaceDecl.hpp"
00046 #include "PlayaBlockVectorSpaceDecl.hpp"
00047 #include "PlayaVectorDecl.hpp"
00048 #include "PlayaExceptions.hpp"
00049 #include "Teuchos_Time.hpp"
00050 #include "Teuchos_TimeMonitor.hpp"
00051 
00052 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00053 #include "PlayaBlockVectorSpaceImpl.hpp"
00054 #endif
00055 
00056 using namespace Teuchos;
00057 
00058 namespace Playa
00059 {
00060 
00061 
00062 static inline Time& createVecTimer() 
00063 {
00064   static RCP<Time> rtn 
00065     = TimeMonitor::getNewTimer("vector allocation"); 
00066   return *rtn;
00067 }
00068 
00069  
00070 //========================================================================
00071 template <class Scalar> inline
00072 bool VectorSpace<Scalar>::operator==(const VectorSpace<Scalar>& other) const 
00073 {
00074   return isCompatible(other);  
00075 }
00076 
00077 
00078 //========================================================================
00079 template <class Scalar> inline
00080 bool VectorSpace<Scalar>::operator!=(const VectorSpace<Scalar>& other) const 
00081 {
00082   return !(operator==(other));
00083 }
00084     
00085 
00086 
00087 //========================================================================
00088 template <class Scalar> inline
00089 Vector<Scalar> VectorSpace<Scalar>::createMember() const 
00090 {
00091   Vector<Scalar> rtn = this->ptr()->createMember(*this);
00092   rtn.setToConstant(0.0);
00093   return rtn;
00094 }
00095     
00096 
00097 
00098 //========================================================================
00099 template <class Scalar> inline
00100 int VectorSpace<Scalar>::baseGlobalNaturalIndex() const
00101 {
00102   return this->ptr()->baseGlobalNaturalIndex();
00103 }
00104 
00105 //========================================================================
00106 template <class Scalar> inline
00107 int VectorSpace<Scalar>::numLocalElements() const
00108 {
00109   return this->ptr()->numLocalElements();
00110 }
00111     
00112 
00113 
00114 
00115 //========================================================================
00116 template <class Scalar> inline
00117 bool VectorSpace<Scalar>::isCompatible(const VectorSpace<Scalar>& vecSpc) const 
00118 {
00119   TEUCHOS_TEST_FOR_EXCEPTION(vecSpc.ptr().get() == 0, std::runtime_error,
00120                      "null argument in VectorSpace<Scalar>::isCompatible()");
00121   return this->ptr().get()->isCompatible(vecSpc.ptr().get());
00122 }
00123 
00124 
00125 
00126 
00127 
00128 //========================================================================
00129 template <class Scalar> inline
00130 bool VectorSpace<Scalar>::contains(const Vector<Scalar> &vec) const
00131 {
00132   return (operator==(vec.space()));
00133 }
00134 
00135 
00136 //========================================================================
00137 template <class Scalar>
00138 int VectorSpace<Scalar>::numBlocks() const
00139 {
00140   return this->ptr()->numBlocks();
00141 }
00142 
00143 
00144 //========================================================================
00145 template <class Scalar>
00146 bool VectorSpace<Scalar>::isBlockSpace() const
00147 {
00148   return dynamic_cast<const BlockVectorSpaceBase<Scalar>*>(this->ptr().get())!=0;
00149 }
00150 
00151 
00152 
00153 //========================================================================
00154 template <class Scalar> inline
00155 const VectorSpace<Scalar>& VectorSpace<Scalar>::getBlock(const int i) const
00156 {
00157   const BlockVectorSpaceBase<Scalar>* bvs = 
00158     dynamic_cast<const BlockVectorSpaceBase<Scalar>* > (this->ptr().get());
00159   TEUCHOS_TEST_FOR_EXCEPTION(bvs == 0 && numBlocks()!=1, std::runtime_error,
00160     "getBlock called for a space that "
00161     "is not a BlockVectorSpace" << std::endl);
00162   if (bvs != 0)
00163     {
00164       return bvs->getBlock(i);
00165     }
00166   return *this;
00167 }
00168 
00169 //========================================================================
00170 template <class Scalar> inline
00171 const VectorSpace<Scalar>& VectorSpace<Scalar>
00172 ::getBlock(const BlockIterator<Scalar>& b) const
00173 {
00174   /* Check that the block iterator is valid */
00175   TEUCHOS_TEST_FOR_EXCEPTION(b.atEnd(), RuntimeError, 
00176     "Attempt to use a block iterator that's run off end");
00177 
00178   return this->getBlock(b.blockIndex());
00179 }
00180 
00181 
00182 //========================================================================
00183 template <class Scalar> inline
00184 const VectorSpace<Scalar>& VectorSpace<Scalar>
00185 ::getBlock(const std::deque<int>& b) const
00186 {
00187   /* Check that the block iterator is valid */
00188   if (b.size()==0) return *this;
00189   
00190   if (b.size()==1) 
00191   {
00192     return this->getBlock(b.front());
00193   }
00194 
00195   int b0 = b.front();
00196   std::deque<int> tmp = b;
00197   tmp.pop_front();
00198   return this->getBlock(b0).getBlock(tmp);
00199 }
00200 
00201 
00202 
00203 
00204 
00205 template <class Scalar> inline
00206 BlockIterator<Scalar> VectorSpace<Scalar>::beginBlock() const
00207 {
00208   return BlockIterator<Scalar>(*this, false);
00209 }
00210 
00211 template <class Scalar> inline
00212 BlockIterator<Scalar> VectorSpace<Scalar>::endBlock() const
00213 {
00214   return BlockIterator<Scalar>(*this, true);
00215 }
00216 
00217 
00218 template <class Scalar> inline
00219 int VectorSpace<Scalar>::mapToGNI(
00220   const BlockIterator<Scalar>& b,
00221   int indexWithinBlock) const 
00222 {
00223   int rtn = this->baseGlobalNaturalIndex();
00224   if (this->numBlocks() > 1)
00225   {
00226     for (BlockIterator<Scalar> a=this->beginBlock(); a < b; a++)
00227     {
00228       rtn += getBlock(a).numLocalElements(); 
00229     }
00230   }
00231   return rtn + indexWithinBlock;
00232 }
00233 
00234 template <class Scalar> inline
00235 bool VectorSpace<Scalar>::containsGNI(int gni) const
00236 {
00237   return (gni >= this->baseGlobalNaturalIndex()
00238     && gni < this->baseGlobalNaturalIndex()+this->numLocalElements());
00239 }
00240 
00241 template <class Scalar> inline
00242 void VectorSpace<Scalar>::getBlockAndOffsetFromGNI(int gni,
00243   BlockIterator<Scalar>& block, int& indexWithinBlock) const 
00244 {
00245   int low = this->baseGlobalNaturalIndex();
00246   
00247   if (this->numBlocks() == 1)
00248   {
00249     indexWithinBlock = gni - low;
00250   }
00251   else
00252   {
00253     int sizeSum = low;
00254     for (BlockIterator<Scalar> b=this->beginBlock(); 
00255          b != this->endBlock(); b++)
00256     {
00257       if (this->getBlock(b).containsGNI(gni))
00258       {
00259         block = b;
00260         indexWithinBlock = gni - sizeSum;
00261       }
00262       sizeSum += this->getBlock(b).numLocalElements();
00263     }
00264   }
00265 }
00266 
00267 }
00268 
00269 
00270 
00271 #endif

Site Contact