plocalindex.hh
Go to the documentation of this file.00001
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
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
00101 inline const Attribute attribute() const;
00102
00107 inline void setAttribute(const Attribute& attribute);
00108
00113 inline size_t local() const;
00114
00118 inline operator size_t() const;
00119
00125 inline ParallelLocalIndex<Attribute>& operator=(size_t index);
00126
00131 inline bool isPublic() const;
00132
00137 inline LocalIndexState state() const;
00138
00143 inline void setState(const LocalIndexState& state);
00144
00145 private:
00147 size_t localIndex_;
00148
00150 char attribute_;
00151
00153 char public_;
00154
00161 char state_;
00162
00163 };
00164
00165 #if HAVE_MPI
00166
00168 template<typename T>
00169 class MPITraits<ParallelLocalIndex<T> >
00170 {
00171 public:
00172 static MPI_Datatype getType();
00173 private:
00174 static MPI_Datatype type;
00175
00176 };
00177
00178 #endif
00179
00180 template<class T>
00181 ParallelLocalIndex<T>::ParallelLocalIndex(const T& attribute, bool isPublic)
00182 : localIndex_(0), attribute_(static_cast<char>(attribute)),
00183 public_(static_cast<char>(isPublic)), state_(static_cast<char>(VALID))
00184 {}
00185
00186
00187 template<class T>
00188 ParallelLocalIndex<T>::ParallelLocalIndex(size_t local, const T& attribute, bool isPublic)
00189 : localIndex_(local), attribute_(static_cast<char>(attribute)),
00190 public_(static_cast<char>(isPublic)), state_(static_cast<char>(VALID))
00191 {}
00192
00193 template<class T>
00194 ParallelLocalIndex<T>::ParallelLocalIndex()
00195 : localIndex_(0), attribute_(), public_(static_cast<char>(false)),
00196 state_(static_cast<char>(VALID))
00197 {}
00198
00199 template<class T>
00200 inline const T ParallelLocalIndex<T>::attribute() const
00201 {
00202 return T(attribute_);
00203 }
00204
00205 template<class T>
00206 inline void
00207 ParallelLocalIndex<T>::setAttribute(const Attribute& attribute)
00208 {
00209 attribute_ = attribute;
00210 }
00211
00212 template<class T>
00213 inline size_t ParallelLocalIndex<T>::local() const
00214 {
00215 return localIndex_;
00216 }
00217
00218 template<class T>
00219 inline ParallelLocalIndex<T>::operator size_t() const
00220 {
00221 return localIndex_;
00222 }
00223
00224 template<class T>
00225 inline ParallelLocalIndex<T>&
00226 ParallelLocalIndex<T>::operator=(size_t index)
00227 {
00228 localIndex_=index;
00229 return *this;
00230 }
00231
00232 template<class T>
00233 inline bool ParallelLocalIndex<T>::isPublic() const
00234 {
00235 return static_cast<bool>(public_);
00236 }
00237
00238 template<class T>
00239 inline LocalIndexState ParallelLocalIndex<T>::state() const
00240 {
00241 return LocalIndexState(state_);
00242 }
00243
00244 template<class T>
00245 inline void ParallelLocalIndex<T>::setState(const LocalIndexState& state)
00246 {
00247 state_=static_cast<char>(state);
00248 }
00249
00250 #if HAVE_MPI
00251
00252 template<typename T>
00253 MPI_Datatype MPITraits<ParallelLocalIndex<T> >::getType()
00254 {
00255
00256 if(type==MPI_DATATYPE_NULL){
00257 int length[3];
00258 MPI_Aint disp[3];
00259 MPI_Datatype types[3] = {MPI_LB, MPITraits<char>::getType(), MPI_UB};
00260 ParallelLocalIndex<T> rep[2];
00261 length[0]=length[1]=length[2]=1;
00262 MPI_Address(rep, disp);
00263 MPI_Address(&(rep[0].attribute_), disp+1);
00264 MPI_Address(rep+1, disp+2);
00265 for(int i=2; i >= 0; --i)
00266 disp[i] -= disp[0];
00267 MPI_Type_struct(3, length, disp, types, &type);
00268 MPI_Type_commit(&type);
00269 }
00270 return type;
00271 }
00272
00273 template<typename T>
00274 MPI_Datatype MPITraits<ParallelLocalIndex<T> >::type = MPI_DATATYPE_NULL;
00275
00276 #endif
00277
00278
00280 }
00281
00282 #endif