plocalindex.hh

Go to the documentation of this file.
00001 // $Id: plocalindex.hh 776 2007-05-10 11:12:19Z mblatt $
00002 
00003 #ifndef DUNE_PLOCALINDEX_HH
00004 #define DUNE_PLOCALINDEX_HH
00005 
00006 #include"localindex.hh"
00007 #include"mpitraits.hh"
00008 
00009 namespace Dune
00010 {
00011 
00012 
00023   template<class T> class ParallelLocalIndex;
00024 
00030   template<class T>
00031   std::ostream& operator<<(std::ostream& os, const ParallelLocalIndex<T>& index)
00032   {
00033     os<<"{local="<<index.localIndex_<<", attr="<<T(index.attribute_)<<", public="
00034       <<(index.public_?true:false)<<"}";
00035     return os;
00036   }
00037 
00041   template<typename T>
00042   class ParallelLocalIndex
00043   {
00044 #if HAVE_MPI
00045     // friend declaration needed for MPITraits
00046     friend class MPITraits<ParallelLocalIndex<T> >;
00047 #endif
00048     friend std::ostream& operator<<<>(std::ostream& os, const ParallelLocalIndex<T>& index);
00049 
00050   public:    
00058     typedef T Attribute;
00067     ParallelLocalIndex(const Attribute& attribute, bool isPublic);
00068 
00077     ParallelLocalIndex(size_t localIndex, const Attribute& attribute, bool isPublic);
00083     ParallelLocalIndex();
00084     
00100     inline const Attribute attribute() const;
00101 
00106     inline void setAttribute(const Attribute& attribute);
00107 
00112     inline size_t local() const;
00113 
00117     inline operator size_t() const;
00118 
00124     inline ParallelLocalIndex<Attribute>& operator=(size_t index);
00125 
00130     inline bool isPublic() const;
00131 
00136     inline LocalIndexState state() const;
00137 
00142     inline void setState(const LocalIndexState& state);
00143 
00144   private:
00146     size_t localIndex_;
00147     
00149     char attribute_;
00150 
00152     char public_;
00153 
00160     char state_;
00161        
00162   };
00163 
00164 #if HAVE_MPI
00165 
00167   template<typename T>
00168   class MPITraits<ParallelLocalIndex<T> >
00169   {
00170   public:
00171     static MPI_Datatype getType();
00172   private:
00173     static MPI_Datatype type;
00174     
00175   };
00176   
00177 #endif
00178 
00179   template<class T>
00180   ParallelLocalIndex<T>::ParallelLocalIndex(const T& attribute, bool isPublic) 
00181     : localIndex_(0), attribute_(static_cast<char>(attribute)), 
00182       public_(static_cast<char>(isPublic)), state_(static_cast<char>(VALID))
00183   {}
00184 
00185 
00186   template<class T>
00187   ParallelLocalIndex<T>::ParallelLocalIndex(size_t local, const T& attribute, bool isPublic) 
00188     : localIndex_(local), attribute_(static_cast<char>(attribute)), 
00189       public_(static_cast<char>(isPublic)), state_(static_cast<char>(VALID))
00190   {}
00191 
00192   template<class T>
00193   ParallelLocalIndex<T>::ParallelLocalIndex() 
00194     : localIndex_(0), attribute_(), public_(static_cast<char>(false)), 
00195       state_(static_cast<char>(VALID))
00196   {}
00197 
00198   template<class T>
00199   inline const T ParallelLocalIndex<T>::attribute() const
00200   {
00201     return T(attribute_);
00202   }
00203 
00204   template<class T>
00205   inline void 
00206   ParallelLocalIndex<T>::setAttribute(const Attribute& attribute)
00207   {
00208     attribute_ = attribute;
00209   }
00210 
00211   template<class T>
00212   inline size_t ParallelLocalIndex<T>::local() const
00213   {
00214     return localIndex_;
00215   }
00216 
00217   template<class T>
00218   inline ParallelLocalIndex<T>::operator size_t() const
00219   {
00220     return localIndex_;
00221   }
00222     
00223   template<class T>
00224   inline ParallelLocalIndex<T>& 
00225   ParallelLocalIndex<T>::operator=(size_t index)
00226   {
00227     localIndex_=index;
00228     return *this;
00229   }
00230 
00231   template<class T>
00232   inline bool ParallelLocalIndex<T>::isPublic() const
00233   {
00234     return static_cast<bool>(public_);
00235   }
00236 
00237   template<class T>
00238   inline LocalIndexState ParallelLocalIndex<T>::state() const
00239   {
00240     return LocalIndexState(state_);
00241   }
00242 
00243   template<class T>
00244   inline void ParallelLocalIndex<T>::setState(const LocalIndexState& state)
00245   {
00246     state_=static_cast<char>(state);
00247   }
00248 
00249 #if HAVE_MPI
00250 
00251   template<typename T>
00252   MPI_Datatype MPITraits<ParallelLocalIndex<T> >::getType()
00253   {
00254     
00255     if(type==MPI_DATATYPE_NULL){
00256       int length[3];
00257       MPI_Aint disp[3];
00258       MPI_Datatype types[3] = {MPI_LB, MPITraits<char>::getType(), MPI_UB};
00259       ParallelLocalIndex<T> rep[2];
00260       length[0]=length[1]=length[2]=1;
00261       MPI_Address(rep, disp); // lower bound of the datatype
00262       MPI_Address(&(rep[0].attribute_), disp+1);
00263       MPI_Address(rep+1, disp+2); // upper bound od the datatype
00264       for(int i=2; i >= 0; --i)
00265         disp[i] -= disp[0];
00266       MPI_Type_struct(3, length, disp, types, &type);
00267       MPI_Type_commit(&type);
00268     }
00269     return type;
00270   }
00271   
00272   template<typename T>
00273   MPI_Datatype MPITraits<ParallelLocalIndex<T> >::type = MPI_DATATYPE_NULL;
00274 
00275 #endif
00276 
00277 
00279 } // namespace Dune
00280 
00281 #endif

Generated on 12 Dec 2007 with Doxygen (ver 1.5.1)