dune-common  2.2.1
plocalindex.hh
Go to the documentation of this file.
1 // $Id$
2 
3 #ifndef DUNE_PLOCALINDEX_HH
4 #define DUNE_PLOCALINDEX_HH
5 
6 #include "localindex.hh"
7 #include "indexset.hh"
8 #include <iostream>
9 
10 #include <dune/common/mpitraits.hh>
11 
12 namespace Dune
13 {
14 
15 
26  template<class T> class ParallelLocalIndex;
27 
33  template<class T>
34  std::ostream& operator<<(std::ostream& os, const ParallelLocalIndex<T>& index)
35  {
36  os<<"{local="<<index.localIndex_<<", attr="<<T(index.attribute_)<<", public="
37  <<(index.public_?true:false)<<"}";
38  return os;
39  }
40 
44  template<typename T>
45  class ParallelLocalIndex
46  {
47 #if HAVE_MPI
48  // friend declaration needed for MPITraits
49  friend class MPITraits<ParallelLocalIndex<T> >;
50 #endif
51  friend std::ostream& operator<<<>(std::ostream& os, const ParallelLocalIndex<T>& index);
52 
53  public:
61  typedef T Attribute;
71 
80  ParallelLocalIndex(size_t localIndex, const Attribute& attribute, bool isPublic=true);
87 
88 #if 0
89 
98  ParallelLocalIndex(const Attribute& attribute, size_t local, bool isPublic);
99 #endif
100 
105  inline const Attribute attribute() const;
106 
111  inline void setAttribute(const Attribute& attribute);
112 
117  inline size_t local() const;
118 
122  inline operator size_t() const;
123 
129  inline ParallelLocalIndex<Attribute>& operator=(size_t index);
130 
135  inline bool isPublic() const;
136 
141  inline LocalIndexState state() const;
142 
147  inline void setState(const LocalIndexState& state);
148 
149  private:
151  size_t localIndex_;
152 
154  char attribute_;
155 
157  char public_;
158 
165  char state_;
166 
167  };
168 
169  template<typename T>
171  const ParallelLocalIndex<T>& p2)
172  {
173  if(p1.local()!=p2.local())
174  return false;
175  if(p1.attribute()!=p2.attribute())
176  return false;
177  if(p1.isPublic()!=p2.isPublic())
178  return false;
179  return true;
180  }
181  template<typename T>
183  const ParallelLocalIndex<T>& p2)
184  {
185  return !(p1==p2);
186  }
187 
188 
189  template<typename T>
191  {
192  static bool compare(const ParallelLocalIndex<T>& t1,
193  const ParallelLocalIndex<T>& t2){
194  return t1.attribute()<t2.attribute();
195  }
196  };
197 
198 
199 #if HAVE_MPI
200 
202  template<typename T>
204  {
205  public:
206  static MPI_Datatype getType();
207  private:
208  static MPI_Datatype type;
209 
210  };
211 
212 #endif
213 
214  template<class T>
215  ParallelLocalIndex<T>::ParallelLocalIndex(const Attribute& attribute, bool isPublic)
216  : localIndex_(0), attribute_(static_cast<char>(attribute)),
217  public_(static_cast<char>(isPublic)), state_(static_cast<char>(VALID))
218  {}
219 
220 
221  template<class T>
222  ParallelLocalIndex<T>::ParallelLocalIndex(size_t local, const Attribute& attribute, bool isPublic)
223  : localIndex_(local), attribute_(static_cast<char>(attribute)),
224  public_(static_cast<char>(isPublic)), state_(static_cast<char>(VALID))
225  {}
226 
227  template<class T>
229  : localIndex_(0), attribute_(), public_(static_cast<char>(false)),
230  state_(static_cast<char>(VALID))
231  {}
232 
233  template<class T>
234  inline const T ParallelLocalIndex<T>::attribute() const
235  {
236  return T(attribute_);
237  }
238 
239  template<class T>
240  inline void
242  {
243  attribute_ = attribute;
244  }
245 
246  template<class T>
247  inline size_t ParallelLocalIndex<T>::local() const
248  {
249  return localIndex_;
250  }
251 
252  template<class T>
253  inline ParallelLocalIndex<T>::operator size_t() const
254  {
255  return localIndex_;
256  }
257 
258  template<class T>
259  inline ParallelLocalIndex<T>&
261  {
262  localIndex_=index;
263  return *this;
264  }
265 
266  template<class T>
268  {
269  return static_cast<bool>(public_);
270  }
271 
272  template<class T>
274  {
275  return LocalIndexState(state_);
276  }
277 
278  template<class T>
280  {
281  state_=static_cast<char>(state);
282  }
283 
284 #if HAVE_MPI
285 
286  template<typename T>
287  MPI_Datatype MPITraits<ParallelLocalIndex<T> >::getType()
288  {
289 
290  if(type==MPI_DATATYPE_NULL){
291  int length[3];
292  MPI_Aint disp[3];
293  MPI_Datatype types[3] = {MPI_LB, MPITraits<char>::getType(), MPI_UB};
294  ParallelLocalIndex<T> rep[2];
295  length[0]=length[1]=length[2]=1;
296  MPI_Address(rep, disp); // lower bound of the datatype
297  MPI_Address(&(rep[0].attribute_), disp+1);
298  MPI_Address(rep+1, disp+2); // upper bound of the datatype
299  for(int i=2; i >= 0; --i)
300  disp[i] -= disp[0];
301  MPI_Type_struct(3, length, disp, types, &type);
302  MPI_Type_commit(&type);
303  }
304  return type;
305  }
306 
307  template<typename T>
308  MPI_Datatype MPITraits<ParallelLocalIndex<T> >::type = MPI_DATATYPE_NULL;
309 
310 #endif
311 
312 
314 } // namespace Dune
315 
316 #endif