RTOpPack_Types.hpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // RTOp: Interfaces and Support Software for Vector Reduction Transformation
00005 //       Operations
00006 //                Copyright (2006) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //  
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00026 // 
00027 // ***********************************************************************
00028 // @HEADER
00029 
00030 #ifndef RTOPPACK_TYPES_HPP
00031 #define RTOPPACK_TYPES_HPP
00032 
00033 #include "RTOp_ConfigDefs.hpp"
00034 #include "Teuchos_Array.hpp"
00035 #include "Teuchos_TestForException.hpp"
00036 
00037 namespace RTOpPack {
00038 
00039 //
00040 // Basic types
00041 //
00042 
00044 typedef Teuchos_Index index_type;
00046 typedef char  char_type;
00048 typedef index_type Index;
00049 
00050 //
00051 // Exceptions
00052 //
00053 
00055 class UnknownError : public std::logic_error
00056 {public: UnknownError(const std::string& what_arg) : std::logic_error(what_arg) {}};
00058 class InvalidUsage : public std::logic_error
00059 {public: InvalidUsage(const std::string& what_arg) : std::logic_error(what_arg) {}};
00061 class InvalidNumVecs : public std::logic_error
00062 {public: InvalidNumVecs(const std::string& what_arg) : std::logic_error(what_arg) {}};
00064 class InvalidNumTargVecs : public std::logic_error
00065 {public: InvalidNumTargVecs(const std::string& what_arg) : std::logic_error(what_arg) {}};
00067 class IncompatibleVecs : public std::logic_error
00068 {public: IncompatibleVecs(const std::string& what_arg) : std::logic_error(what_arg) {}};
00069 
00070 //
00071 // VectorBase subviews
00072 //
00073 
00094 template<class Scalar>
00095 class ConstSubVectorView {
00096 public:
00098   ConstSubVectorView() : globalOffset_(0), subDim_(0), values_(NULL), stride_(0) {}
00100   ConstSubVectorView(Teuchos_Index globalOffset, Teuchos_Index subDim, const Scalar *values, ptrdiff_t stride)
00101     :globalOffset_(globalOffset), subDim_(subDim), values_(values), stride_(stride) 
00102     {}
00104   ConstSubVectorView( const ConstSubVectorView<Scalar>& sv )
00105     :globalOffset_(sv.globalOffset()), subDim_(sv.subDim()), values_(sv.values()), stride_(sv.stride()) 
00106     {}
00108   void initialize(Teuchos_Index globalOffset, Teuchos_Index subDim, const Scalar *values, ptrdiff_t stride)
00109     { globalOffset_=globalOffset; subDim_=subDim; values_=values; stride_=stride;  }
00111   void set_uninitialized()
00112     { globalOffset_ = 0; subDim_=0; values_=NULL; stride_ = 0; }
00114   void setGlobalOffset(Teuchos_Index globalOffset) { globalOffset_ = globalOffset; } 
00116   Teuchos_Index   globalOffset() const { return globalOffset_; }
00118   Teuchos_Index   subDim()       const { return subDim_;  }
00120   const Scalar*     values()       const { return values_;  }
00122   ptrdiff_t         stride()       const { return stride_;  }
00124   const Scalar& operator[](Teuchos_Index i) const
00125     {
00126 #ifdef TEUCHOS_DEBUG
00127       TEST_FOR_EXCEPTION(
00128         !( 0 <= i && i < subDim_ ), std::logic_error
00129         ,"Error, index i="<<i<<" does not fall in the range [0,"<<(subDim_-1)<<"]!"
00130         );
00131 #endif
00132       return values_[ stride_*i ];
00133     }
00135   const Scalar& operator()(Teuchos_Index i) const { return (*this)[i]; }
00136 private:
00137   Teuchos_Index     globalOffset_;
00138   Teuchos_Index     subDim_;
00139   const Scalar      *values_;
00140   ptrdiff_t         stride_;
00141 };
00142 
00155 template<class Scalar>
00156 class SubVectorView : public ConstSubVectorView<Scalar> {
00157 public:
00159   SubVectorView() {}
00161   SubVectorView(Teuchos_Index globalOffset, Teuchos_Index subDim, Scalar *values, ptrdiff_t stride)
00162     :ConstSubVectorView<Scalar>(globalOffset, subDim, values, stride)
00163     {}
00165   SubVectorView( const SubVectorView<Scalar> & sv)
00166     :ConstSubVectorView<Scalar>(sv)
00167     {}
00169   void initialize(Teuchos_Index globalOffset, Teuchos_Index subDim, Scalar *values, ptrdiff_t stride)
00170     { ConstSubVectorView<Scalar>::initialize(globalOffset, subDim, values, stride); }
00172   void set_uninitialized()
00173     { ConstSubVectorView<Scalar>::set_uninitialized(); }
00175   Scalar* values() const { return const_cast<Scalar*>(ConstSubVectorView<Scalar>::values());  }
00177   Scalar& operator[](Teuchos_Index i) const { return const_cast<Scalar&>(ConstSubVectorView<Scalar>::operator[](i)); } // Is range changed in subclass!
00179   Scalar& operator()(Teuchos_Index i) const { return (*this)[i]; }
00180 };
00181 
00257 template<class Scalar>
00258 class SparseSubVectorT {
00259 public:
00261   SparseSubVectorT()
00262     :globalOffset_(0),subDim_(0),subNz_(0)
00263     ,values_(NULL),valuesStride_(0),indices_(NULL)
00264     ,indicesStride_(0),localOffset_(0),isSorted_(0)
00265     {}
00267   SparseSubVectorT(
00268     Teuchos_Index globalOffset, Teuchos_Index subDim, Teuchos_Index subNz
00269     ,const Scalar values[], ptrdiff_t valuesStride
00270     ,const Teuchos_Index indices[], ptrdiff_t indicesStride
00271     ,ptrdiff_t localOffset, int isSorted
00272     )
00273     :globalOffset_(globalOffset),subDim_(subDim),subNz_(subNz)
00274     ,values_(values),valuesStride_(valuesStride),indices_(indices)
00275     ,indicesStride_(indicesStride),localOffset_(localOffset),isSorted_(isSorted)
00276     {}
00278   SparseSubVectorT(
00279     Teuchos_Index globalOffset, Teuchos_Index subDim
00280     ,const Scalar values[], ptrdiff_t valuesStride
00281     )
00282     :globalOffset_(globalOffset),subDim_(subDim),subNz_(subDim)
00283     ,values_(values),valuesStride_(valuesStride),indices_(NULL)
00284     ,indicesStride_(0),localOffset_(0),isSorted_(0)
00285     {}
00287   SparseSubVectorT( const ConstSubVectorView<Scalar>& sv )
00288     :globalOffset_(sv.globalOffset()), subDim_(sv.subDim()), subNz_(sv.subDim()), values_(sv.values())
00289     ,valuesStride_(sv.stride()), indices_(NULL),indicesStride_(0),localOffset_(0),isSorted_(0)
00290     {}
00292   void initialize(
00293     Teuchos_Index globalOffset, Teuchos_Index subDim, Teuchos_Index subNz
00294     ,const Scalar values[], ptrdiff_t valuesStride
00295     ,const Teuchos_Index indices[], ptrdiff_t indicesStride
00296     ,ptrdiff_t localOffset, int isSorted
00297     )
00298     {
00299       globalOffset_ = globalOffset; subDim_ = subDim; subNz_ = subNz;
00300       values_ = values; valuesStride_ = valuesStride; indices_ = indices;
00301       indicesStride_ = indicesStride; localOffset_ = localOffset; isSorted_ = isSorted;
00302     }
00304   void initialize(
00305     Teuchos_Index globalOffset, Teuchos_Index subDim
00306     ,const Scalar values[], ptrdiff_t valuesStride
00307     )
00308     {
00309       globalOffset_ = globalOffset; subDim_ = subDim; subNz_ = subDim;
00310       values_ = values; valuesStride_ = valuesStride; indices_ = NULL;
00311       indicesStride_ = 0; localOffset_ = 0; isSorted_ = 1;
00312     }
00314   void set_uninitialized()
00315     {
00316       globalOffset_ = 0; subDim_ = 0; subNz_ = 0;
00317       values_ = NULL; valuesStride_ = 0; indices_ = NULL;
00318       indicesStride_ = 0; localOffset_ = 0; isSorted_ = 1;
00319     }
00321   void setGlobalOffset(Teuchos_Index globalOffset) { globalOffset_ = globalOffset; } 
00323   Teuchos_Index                  globalOffset() const { return globalOffset_; } 
00325   Teuchos_Index                  subDim() const { return subDim_; }
00327   Teuchos_Index                  subNz() const { return subNz_; }
00329   const Scalar*                    values() const { return values_; }
00331   ptrdiff_t                        valuesStride() const { return valuesStride_; }
00335   const Teuchos_Index*           indices() const { return indices_; }
00337   ptrdiff_t                        indicesStride() const { return indicesStride_; }
00339   ptrdiff_t                        localOffset() const { return localOffset_; }
00341   int                              isSorted() const { return isSorted_; }
00342 private:
00343   Teuchos_Index                  globalOffset_;
00344   Teuchos_Index                  subDim_;
00345   Teuchos_Index                  subNz_;
00346   const Scalar                     *values_;
00347   ptrdiff_t                        valuesStride_;
00348   const Teuchos_Index            *indices_;
00349   ptrdiff_t                        indicesStride_;
00350   ptrdiff_t                        localOffset_;
00351   int                              isSorted_;
00352 };
00353 
00354 template<class Scalar>
00355 void assign_entries( const SubVectorView<Scalar> *msv, const ConstSubVectorView<Scalar> &sv )
00356 {
00357 #ifdef TEUCHOS_DEBUG
00358   TEST_FOR_EXCEPT(msv==NULL);
00359   TEST_FOR_EXCEPT(msv->subDim() != sv.subDim());
00360 #endif
00361   for( int i = 0; i < sv.subDim(); ++i ) {
00362     (*msv)(i) = sv(i);
00363   }
00364 }
00365 
00366 //
00367 // MultiVectorBase subviews
00368 //
00369 
00390 template<class Scalar>
00391 class ConstSubMultiVectorView {
00392 public:
00394   ConstSubMultiVectorView()
00395     :globalOffset_(0), subDim_(0), colOffset_(0), numSubCols_(0)
00396     ,values_(NULL), leadingDim_(0)
00397     {}
00399   ConstSubMultiVectorView(
00400     Teuchos_Index globalOffset, Teuchos_Index subDim
00401     ,Teuchos_Index colOffset, Teuchos_Index numSubCols
00402     ,const Scalar *values, Teuchos_Index leadingDim
00403     )
00404     :globalOffset_(globalOffset), subDim_(subDim)
00405     ,colOffset_(colOffset), numSubCols_(numSubCols)
00406     ,values_(values), leadingDim_(leadingDim)
00407     {}
00409   ConstSubMultiVectorView( const ConstSubMultiVectorView<Scalar>& smv )
00410     :globalOffset_(smv.globalOffset()), subDim_(smv.subDim())
00411     ,colOffset_(smv.colOffset()), numSubCols_(smv.numSubCols())
00412     ,values_(smv.values()), leadingDim_(smv.leadingDim())
00413     {}
00415   void initialize(
00416     Teuchos_Index globalOffset, Teuchos_Index subDim
00417     ,Teuchos_Index colOffset, Teuchos_Index numSubCols
00418     ,const Scalar *values, Teuchos_Index leadingDim
00419     )
00420     { globalOffset_=globalOffset; subDim_=subDim; colOffset_=colOffset; numSubCols_=numSubCols;
00421     values_=values; leadingDim_=leadingDim; }
00423   void set_uninitialized()
00424     { globalOffset_ = 0; subDim_=0; colOffset_=0, numSubCols_=0; values_=NULL; leadingDim_=0; }
00426   void setGlobalOffset(Teuchos_Index globalOffset) { globalOffset_ = globalOffset; } 
00428   Teuchos_Index   globalOffset()   const { return globalOffset_; }
00430   Teuchos_Index   subDim()         const { return subDim_; }
00432   Teuchos_Index   colOffset()      const { return colOffset_; }
00434   Teuchos_Index   numSubCols()     const { return numSubCols_; }
00436   const Scalar*     values()         const { return values_; }
00438   Teuchos_Index   leadingDim()     const { return leadingDim_;  }
00440   const Scalar& operator()(Teuchos_Index i, Teuchos_Index j) const
00441     {
00442 #ifdef TEUCHOS_DEBUG
00443       TEST_FOR_EXCEPTION(
00444         !( 0 <= i && i < subDim_ ), std::logic_error
00445         ,"Error, index i="<<i<<" does not fall in the range [0,"<<(subDim_-1)<<"]!"
00446         );
00447       TEST_FOR_EXCEPTION(
00448         !( 0 <= j && j < numSubCols_ ), std::logic_error
00449         ,"Error, index j="<<j<<" does not fall in the range [0,"<<(numSubCols_-1)<<"]!"
00450         );
00451 #endif
00452       return values_[ i + leadingDim_*j ];
00453     }
00455   ConstSubVectorView<Scalar> col( const Teuchos_Index j ) const
00456     {
00457 #ifdef TEUCHOS_DEBUG
00458       TEST_FOR_EXCEPTION(
00459         !( 0 <= j && j < numSubCols_ ), std::logic_error
00460         ,"Error, index j="<<j<<" does not fall in the range [0,"<<(numSubCols_-1)<<"]!"
00461         );
00462 #endif
00463       return ConstSubVectorView<Scalar>(globalOffset(),subDim(),values()+j*leadingDim(),1);
00464     }
00465 private:
00466   Teuchos_Index     globalOffset_;
00467   Teuchos_Index     subDim_;
00468   Teuchos_Index     colOffset_;
00469   Teuchos_Index     numSubCols_;
00470   const Scalar        *values_;
00471   Teuchos_Index     leadingDim_;
00472 };
00473 
00486 template<class Scalar>
00487 class SubMultiVectorView : public ConstSubMultiVectorView<Scalar> {
00488 public:
00490   SubMultiVectorView() {}
00492   SubMultiVectorView(
00493     Teuchos_Index globalOffset, Teuchos_Index subDim
00494     ,Teuchos_Index colOffset, Teuchos_Index numSubCols
00495     ,const Scalar *values, Teuchos_Index leadingDim
00496     )
00497     :ConstSubMultiVectorView<Scalar>(globalOffset,subDim,colOffset,numSubCols,values,leadingDim)
00498     {}
00500   SubMultiVectorView( const SubMultiVectorView<Scalar> & smv)
00501     :ConstSubMultiVectorView<Scalar>(smv)
00502     {}
00504   void initialize(
00505     Teuchos_Index globalOffset, Teuchos_Index subDim
00506     ,Teuchos_Index colOffset, Teuchos_Index numSubCols
00507     ,const Scalar *values, Teuchos_Index leadingDim
00508     )
00509     { ConstSubMultiVectorView<Scalar>::initialize(globalOffset,subDim,colOffset,numSubCols,values,leadingDim); }
00511   void set_uninitialized()
00512     { ConstSubMultiVectorView<Scalar>::set_uninitialized(); }
00514   Scalar* values() const { return const_cast<Scalar*>(ConstSubMultiVectorView<Scalar>::values());  }
00516   Scalar& operator()(Teuchos_Index i, Teuchos_Index j) const
00517     { return const_cast<Scalar&>(ConstSubMultiVectorView<Scalar>::operator()(i,j)); } // Is range checked in subclass
00519   SubVectorView<Scalar> col( const Teuchos_Index j ) const
00520     {
00521 #ifdef TEUCHOS_DEBUG
00522       TEST_FOR_EXCEPTION(
00523         !( 0 <= j && j < this->numSubCols() ), std::logic_error
00524         ,"Error, index j="<<j<<" does not fall in the range [0,"<<(this->numSubCols()-1)<<"]!"
00525         );
00526 #endif
00527       return SubVectorView<Scalar>(this->globalOffset(),this->subDim(),values()+j*this->leadingDim(),1);
00528     }
00529 };
00530 
00531 template<class Scalar>
00532 void assign_entries( const SubMultiVectorView<Scalar> *msmv, const ConstSubMultiVectorView<Scalar> &smv )
00533 {
00534 #ifdef TEUCHOS_DEBUG
00535   TEST_FOR_EXCEPT(msmv==NULL);
00536   TEST_FOR_EXCEPT(msmv->subDim() != smv.subDim());
00537   TEST_FOR_EXCEPT(msmv->numSubCols() != smv.numSubCols());
00538 #endif
00539   for( Teuchos_Index j = 0; j < smv.numSubCols(); ++j ) {
00540     for( Teuchos_Index i = 0; i < smv.subDim(); ++i ) {
00541       (*msmv)(i,j) = smv(i,j);
00542     }
00543   }
00544 }
00545 
00546 //
00547 // Templated types
00548 //
00549 
00551 template<class Scalar>  class RTOpT;
00552 
00553 } // namespace RTOpPack
00554 
00555 #endif // RTOPPACK_TYPES_HPP
00556 

Generated on Tue Oct 20 12:46:11 2009 for Fundamental Vector Reduction/Transformation Operator (RTOp) Interfaces by doxygen 1.4.7