dune-common  2.11
plocalindex.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_COMMON_PARALLEL_PLOCALINDEX_HH
6 #define DUNE_COMMON_PARALLEL_PLOCALINDEX_HH
7 
8 #include <iostream>
9 
10 #if HAVE_MPI
11 #include <mpi.h>
12 #endif
13 
17 
18 namespace Dune
19 {
20 
21 
32  template<class T> class ParallelLocalIndex;
33 
39  template<class T>
40  std::ostream& operator<<(std::ostream& os, const ParallelLocalIndex<T>& index)
41  {
42  os<<"{local="<<index.localIndex_<<", attr="<<T(index.attribute_)<<", public="
43  <<(index.public_ ? true : false)<<"}";
44  return os;
45  }
46 
50  template<typename T>
51  class ParallelLocalIndex
52  {
53 #if HAVE_MPI
54  // friend declaration needed for MPITraits
55  friend struct MPITraits<ParallelLocalIndex<T> >;
56 #endif
57  friend std::ostream& operator<< <>(std::ostream& os, const ParallelLocalIndex<T>& index);
58 
59  public:
67  typedef T Attribute;
77 
86  ParallelLocalIndex(size_t localIndex, const Attribute& attribute, bool isPublic=true);
93 
94 #if 0
95 
104  ParallelLocalIndex(const Attribute& attribute, size_t local, bool isPublic);
105 #endif
106 
111  inline const Attribute attribute() const;
112 
117  inline void setAttribute(const Attribute& attribute);
118 
123  inline size_t local() const;
124 
128  inline operator size_t() const;
129 
135  inline ParallelLocalIndex<Attribute>& operator=(size_t index);
136 
141  inline bool isPublic() const;
142 
147  inline LocalIndexState state() const;
148 
153  inline void setState(const LocalIndexState& state);
154 
155  private:
157  size_t localIndex_;
158 
160  char attribute_;
161 
163  char public_;
164 
171  char state_;
172 
173  };
174 
175  template<typename T>
177  const ParallelLocalIndex<T>& p2)
178  {
179  if(p1.local()!=p2.local())
180  return false;
181  if(p1.attribute()!=p2.attribute())
182  return false;
183  if(p1.isPublic()!=p2.isPublic())
184  return false;
185  return true;
186  }
187  template<typename T>
189  const ParallelLocalIndex<T>& p2)
190  {
191  return !(p1==p2);
192  }
193 
194 
195  template<typename T>
197  {
198  static bool compare(const ParallelLocalIndex<T>& t1,
199  const ParallelLocalIndex<T>& t2){
200  return t1.attribute()<t2.attribute();
201  }
202  };
203 
204 
205 #if HAVE_MPI
206 
208  template<typename T>
210  {
211  public:
212  static MPI_Datatype getType();
213  private:
214  static MPI_Datatype type;
215 
216  };
217 
218 #endif
219 
220  template<class T>
221  ParallelLocalIndex<T>::ParallelLocalIndex(const T& attribute, bool isPublic)
222  : localIndex_(0), attribute_(static_cast<char>(attribute)),
223  public_(static_cast<char>(isPublic)), state_(static_cast<char>(VALID))
224  {}
225 
226 
227  template<class T>
228  ParallelLocalIndex<T>::ParallelLocalIndex(size_t local, const T& attribute, bool isPublic)
229  : localIndex_(local), attribute_(static_cast<char>(attribute)),
230  public_(static_cast<char>(isPublic)), state_(static_cast<char>(VALID))
231  {}
232 
233  template<class T>
235  : localIndex_(0), attribute_(), public_(static_cast<char>(false)),
236  state_(static_cast<char>(VALID))
237  {}
238 
239  template<class T>
240  inline const T ParallelLocalIndex<T>::attribute() const
241  {
242  return T(attribute_);
243  }
244 
245  template<class T>
246  inline void
248  {
249  attribute_ = attribute;
250  }
251 
252  template<class T>
253  inline size_t ParallelLocalIndex<T>::local() const
254  {
255  return localIndex_;
256  }
257 
258  template<class T>
259  inline ParallelLocalIndex<T>::operator size_t() const
260  {
261  return localIndex_;
262  }
263 
264  template<class T>
265  inline ParallelLocalIndex<T>&
267  {
268  localIndex_=index;
269  return *this;
270  }
271 
272  template<class T>
274  {
275  return static_cast<bool>(public_);
276  }
277 
278  template<class T>
280  {
281  return LocalIndexState(state_);
282  }
283 
284  template<class T>
286  {
287  state_=static_cast<char>(state);
288  }
289 
290 #if HAVE_MPI
291 
292  template<typename T>
293  MPI_Datatype MPITraits<ParallelLocalIndex<T> >::getType()
294  {
295 
296  if(type==MPI_DATATYPE_NULL) {
297  int length = 1;
298  MPI_Aint base, disp;
299  MPI_Datatype types[1] = {MPITraits<char>::getType()};
301  MPI_Get_address(&rep, &base);
302  MPI_Get_address(&(rep.attribute_), &disp);
303  disp -= base;
304 
305  MPI_Datatype tmp;
306  MPI_Type_create_struct(1, &length, &disp, types, &tmp);
307 
308  MPI_Type_create_resized(tmp, 0, sizeof(ParallelLocalIndex<T>), &type);
309  MPI_Type_commit(&type);
310 
311  MPI_Type_free(&tmp);
312  }
313  return type;
314  }
315 
316  template<typename T>
317  MPI_Datatype MPITraits<ParallelLocalIndex<T> >::type = MPI_DATATYPE_NULL;
318 
319 #endif // HAVE_MPI
320 
321 
323 } // namespace Dune
324 
325 #endif // DUNE_COMMON_PARALLEL_PLOCALINDEX_HH
An index present on the local process with an additional attribute flag.
Definition: plocalindex.hh:32
size_t local() const
get the local index.
Definition: plocalindex.hh:253
Provides a map between global and local indices.
static bool compare(const ParallelLocalIndex< T > &t1, const ParallelLocalIndex< T > &t2)
Definition: plocalindex.hh:198
LocalIndexState state() const
Get the state.
Definition: plocalindex.hh:279
void setAttribute(const Attribute &attribute)
Set the attribute of the index.
Definition: plocalindex.hh:247
T Attribute
The type of the attributes. Normally this will be an enumeration like.
Definition: plocalindex.hh:67
LocalIndexState
The states available for the local indices.
Definition: localindex.hh:28
Provides classes for use as the local index in ParallelIndexSet.
Dune namespace
Definition: alignedallocator.hh:12
bool isPublic() const
Check whether the index might also be known other processes.
Definition: plocalindex.hh:273
constexpr auto operator!=(const HybridMultiIndex< S... > &lhs, const HybridMultiIndex< T... > &rhs)
Compare two HybridMultiIndexs for inequality.
Definition: hybridmultiindex.hh:441
constexpr bool operator==(const HybridMultiIndex< S... > &lhs, const HybridMultiIndex< T... > &rhs)
Compare two HybridMultiIndexs for value equality.
Definition: hybridmultiindex.hh:404
Definition: localindex.hh:28
ParallelLocalIndex()
Parameterless constructor.
Definition: plocalindex.hh:234
void setState(const LocalIndexState &state)
Set the state.
Definition: plocalindex.hh:285
Traits classes for mapping types onto MPI_Datatype.
A traits class describing the mapping of types onto MPI_Datatypes.
Definition: bigunsignedint.hh:29
static MPI_Datatype getType()
Definition: mpitraits.hh:48
const Attribute attribute() const
Get the attribute of the index.
Definition: plocalindex.hh:240
ParallelLocalIndex< Attribute > & operator=(size_t index)
Assign a new local index.
Definition: plocalindex.hh:266
Definition: indexset.hh:614