plocalindex.hh

Go to the documentation of this file.
00001 // $Id: plocalindex.hh 1117 2009-11-03 19:43:14Z christi $
00002 
00003 #ifndef DUNE_PLOCALINDEX_HH
00004 #define DUNE_PLOCALINDEX_HH
00005 
00006 #include "localindex.hh"
00007 #include "mpitraits.hh"
00008 #include <iostream>
00009 
00010 namespace Dune
00011 {
00012 
00013 
00024   template<class T> class ParallelLocalIndex;
00025 
00031   template<class T>
00032   std::ostream& operator<<(std::ostream& os, const ParallelLocalIndex<T>& index)
00033   {
00034     os<<"{local="<<index.localIndex_<<", attr="<<T(index.attribute_)<<", public="
00035       <<(index.public_?true:false)<<"}";
00036     return os;
00037   }
00038 
00042   template<typename T>
00043   class ParallelLocalIndex
00044   {
00045 #if HAVE_MPI
00046     // friend declaration needed for MPITraits
00047     friend class MPITraits<ParallelLocalIndex<T> >;
00048 #endif
00049     friend std::ostream& operator<<<>(std::ostream& os, const ParallelLocalIndex<T>& index);
00050 
00051   public:    
00059     typedef T Attribute;
00068     ParallelLocalIndex(const Attribute& attribute, bool isPublic);
00069 
00078     ParallelLocalIndex(size_t localIndex, const Attribute& attribute, bool isPublic=true);
00084     ParallelLocalIndex();
00085 
00086 #if 0
00087 
00096     ParallelLocalIndex(const Attribute& attribute, size_t local, bool isPublic);
00097 #endif
00098 
00103     inline const Attribute attribute() const;
00104 
00109     inline void setAttribute(const Attribute& attribute);
00110 
00115     inline size_t local() const;
00116 
00120     inline operator size_t() const;
00121 
00127     inline ParallelLocalIndex<Attribute>& operator=(size_t index);
00128 
00133     inline bool isPublic() const;
00134 
00139     inline LocalIndexState state() const;
00140 
00145     inline void setState(const LocalIndexState& state);
00146 
00147   private:
00149     size_t localIndex_;
00150     
00152     char attribute_;
00153 
00155     char public_;
00156 
00163     char state_;
00164        
00165   };
00166 
00167 #if HAVE_MPI
00168 
00170   template<typename T>
00171   class MPITraits<ParallelLocalIndex<T> >
00172   {
00173   public:
00174     static MPI_Datatype getType();
00175   private:
00176     static MPI_Datatype type;
00177     
00178   };
00179   
00180 #endif
00181 
00182   template<class T>
00183   ParallelLocalIndex<T>::ParallelLocalIndex(const T& attribute, bool isPublic) 
00184     : localIndex_(0), attribute_(static_cast<char>(attribute)), 
00185       public_(static_cast<char>(isPublic)), state_(static_cast<char>(VALID))
00186   {}
00187 
00188 
00189   template<class T>
00190   ParallelLocalIndex<T>::ParallelLocalIndex(size_t local, const T& attribute, bool isPublic) 
00191     : localIndex_(local), attribute_(static_cast<char>(attribute)), 
00192       public_(static_cast<char>(isPublic)), state_(static_cast<char>(VALID))
00193   {}
00194 
00195   template<class T>
00196   ParallelLocalIndex<T>::ParallelLocalIndex() 
00197     : localIndex_(0), attribute_(), public_(static_cast<char>(false)), 
00198       state_(static_cast<char>(VALID))
00199   {}
00200 
00201   template<class T>
00202   inline const T ParallelLocalIndex<T>::attribute() const
00203   {
00204     return T(attribute_);
00205   }
00206 
00207   template<class T>
00208   inline void 
00209   ParallelLocalIndex<T>::setAttribute(const Attribute& attribute)
00210   {
00211     attribute_ = attribute;
00212   }
00213 
00214   template<class T>
00215   inline size_t ParallelLocalIndex<T>::local() const
00216   {
00217     return localIndex_;
00218   }
00219 
00220   template<class T>
00221   inline ParallelLocalIndex<T>::operator size_t() const
00222   {
00223     return localIndex_;
00224   }
00225     
00226   template<class T>
00227   inline ParallelLocalIndex<T>& 
00228   ParallelLocalIndex<T>::operator=(size_t index)
00229   {
00230     localIndex_=index;
00231     return *this;
00232   }
00233 
00234   template<class T>
00235   inline bool ParallelLocalIndex<T>::isPublic() const
00236   {
00237     return static_cast<bool>(public_);
00238   }
00239 
00240   template<class T>
00241   inline LocalIndexState ParallelLocalIndex<T>::state() const
00242   {
00243     return LocalIndexState(state_);
00244   }
00245 
00246   template<class T>
00247   inline void ParallelLocalIndex<T>::setState(const LocalIndexState& state)
00248   {
00249     state_=static_cast<char>(state);
00250   }
00251 
00252 #if HAVE_MPI
00253 
00254   template<typename T>
00255   MPI_Datatype MPITraits<ParallelLocalIndex<T> >::getType()
00256   {
00257     
00258     if(type==MPI_DATATYPE_NULL){
00259       int length[3];
00260       MPI_Aint disp[3];
00261       MPI_Datatype types[3] = {MPI_LB, MPITraits<char>::getType(), MPI_UB};
00262       ParallelLocalIndex<T> rep[2];
00263       length[0]=length[1]=length[2]=1;
00264       MPI_Address(rep, disp); // lower bound of the datatype
00265       MPI_Address(&(rep[0].attribute_), disp+1);
00266       MPI_Address(rep+1, disp+2); // upper bound od the datatype
00267       for(int i=2; i >= 0; --i)
00268         disp[i] -= disp[0];
00269       MPI_Type_struct(3, length, disp, types, &type);
00270       MPI_Type_commit(&type);
00271     }
00272     return type;
00273   }
00274   
00275   template<typename T>
00276   MPI_Datatype MPITraits<ParallelLocalIndex<T> >::type = MPI_DATATYPE_NULL;
00277 
00278 #endif
00279 
00280 
00282 } // namespace Dune
00283 
00284 #endif
Generated on Sat Apr 24 11:13:47 2010 for dune-istl by  doxygen 1.6.3