Dune Core Modules (2.9.0)

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 (C) DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 
6 #ifndef DUNE_PLOCALINDEX_HH
7 #define DUNE_PLOCALINDEX_HH
8 
9 #include "localindex.hh"
10 #include "indexset.hh"
11 #include "mpitraits.hh"
12 
13 #include <iostream>
14 
15 namespace Dune
16 {
17 
18 
29  template<class T> class ParallelLocalIndex;
30 
36  template<class T>
37  std::ostream& operator<<(std::ostream& os, const ParallelLocalIndex<T>& index)
38  {
39  os<<"{local="<<index.localIndex_<<", attr="<<T(index.attribute_)<<", public="
40  <<(index.public_ ? true : false)<<"}";
41  return os;
42  }
43 
47  template<typename T>
49  {
50 #if HAVE_MPI
51  // friend declaration needed for MPITraits
52  friend struct MPITraits<ParallelLocalIndex<T> >;
53 #endif
54  friend std::ostream& operator<<<>(std::ostream& os, const ParallelLocalIndex<T>& index);
55 
56  public:
64  typedef T Attribute;
74 
83  ParallelLocalIndex(size_t localIndex, const Attribute& attribute, bool isPublic=true);
90 
91 #if 0
101  ParallelLocalIndex(const Attribute& attribute, size_t local, bool isPublic);
102 #endif
103 
108  inline const Attribute attribute() const;
109 
114  inline void setAttribute(const Attribute& attribute);
115 
120  inline size_t local() const;
121 
125  inline operator size_t() const;
126 
132  inline ParallelLocalIndex<Attribute>& operator=(size_t index);
133 
138  inline bool isPublic() const;
139 
144  inline LocalIndexState state() const;
145 
150  inline void setState(const LocalIndexState& state);
151 
152  private:
154  size_t localIndex_;
155 
157  char attribute_;
158 
160  char public_;
161 
168  char state_;
169 
170  };
171 
172  template<typename T>
173  bool operator==(const ParallelLocalIndex<T>& p1,
174  const ParallelLocalIndex<T>& p2)
175  {
176  if(p1.local()!=p2.local())
177  return false;
178  if(p1.attribute()!=p2.attribute())
179  return false;
180  if(p1.isPublic()!=p2.isPublic())
181  return false;
182  return true;
183  }
184  template<typename T>
185  bool operator!=(const ParallelLocalIndex<T>& p1,
186  const ParallelLocalIndex<T>& p2)
187  {
188  return !(p1==p2);
189  }
190 
191 
192  template<typename T>
193  struct LocalIndexComparator<ParallelLocalIndex<T> >
194  {
195  static bool compare(const ParallelLocalIndex<T>& t1,
196  const ParallelLocalIndex<T>& t2){
197  return t1.attribute()<t2.attribute();
198  }
199  };
200 
201 
202 #if HAVE_MPI
203 
205  template<typename T>
207  {
208  public:
209  static MPI_Datatype getType();
210  private:
211  static MPI_Datatype type;
212 
213  };
214 
215 #endif
216 
217  template<class T>
218  ParallelLocalIndex<T>::ParallelLocalIndex(const T& attribute, bool isPublic)
219  : localIndex_(0), attribute_(static_cast<char>(attribute)),
220  public_(static_cast<char>(isPublic)), state_(static_cast<char>(VALID))
221  {}
222 
223 
224  template<class T>
225  ParallelLocalIndex<T>::ParallelLocalIndex(size_t local, const T& attribute, bool isPublic)
226  : localIndex_(local), attribute_(static_cast<char>(attribute)),
227  public_(static_cast<char>(isPublic)), state_(static_cast<char>(VALID))
228  {}
229 
230  template<class T>
232  : localIndex_(0), attribute_(), public_(static_cast<char>(false)),
233  state_(static_cast<char>(VALID))
234  {}
235 
236  template<class T>
237  inline const T ParallelLocalIndex<T>::attribute() const
238  {
239  return T(attribute_);
240  }
241 
242  template<class T>
243  inline void
245  {
246  attribute_ = attribute;
247  }
248 
249  template<class T>
250  inline size_t ParallelLocalIndex<T>::local() const
251  {
252  return localIndex_;
253  }
254 
255  template<class T>
256  inline ParallelLocalIndex<T>::operator size_t() const
257  {
258  return localIndex_;
259  }
260 
261  template<class T>
262  inline ParallelLocalIndex<T>&
264  {
265  localIndex_=index;
266  return *this;
267  }
268 
269  template<class T>
271  {
272  return static_cast<bool>(public_);
273  }
274 
275  template<class T>
277  {
278  return LocalIndexState(state_);
279  }
280 
281  template<class T>
283  {
284  state_=static_cast<char>(state);
285  }
286 
287 #if HAVE_MPI
288 
289  template<typename T>
290  MPI_Datatype MPITraits<ParallelLocalIndex<T> >::getType()
291  {
292 
293  if(type==MPI_DATATYPE_NULL) {
294  int length = 1;
295  MPI_Aint base, disp;
296  MPI_Datatype types[1] = {MPITraits<char>::getType()};
297  ParallelLocalIndex<T> rep;
298  MPI_Get_address(&rep, &base);
299  MPI_Get_address(&(rep.attribute_), &disp);
300  disp -= base;
301 
302  MPI_Datatype tmp;
303  MPI_Type_create_struct(1, &length, &disp, types, &tmp);
304 
305  MPI_Type_create_resized(tmp, 0, sizeof(ParallelLocalIndex<T>), &type);
306  MPI_Type_commit(&type);
307 
308  MPI_Type_free(&tmp);
309  }
310  return type;
311  }
312 
313  template<typename T>
314  MPI_Datatype MPITraits<ParallelLocalIndex<T> >::type = MPI_DATATYPE_NULL;
315 
316 #endif
317 
318 
320 } // namespace Dune
321 
322 #endif
An index present on the local process with an additional attribute flag.
Definition: plocalindex.hh:49
T Attribute
The type of the attributes. Normally this will be an enumeration like.
Definition: plocalindex.hh:64
bool isPublic() const
Check whether the index might also be known other processes.
Definition: plocalindex.hh:270
void setAttribute(const Attribute &attribute)
Set the attribute of the index.
Definition: plocalindex.hh:244
size_t local() const
get the local index.
Definition: plocalindex.hh:250
LocalIndexState
The states available for the local indices.
Definition: localindex.hh:28
std::ostream & operator<<(std::ostream &os, const ParallelLocalIndex< T > &index)
Print the local index to a stream.
Definition: plocalindex.hh:37
void setState(const LocalIndexState &state)
Set the state.
Definition: plocalindex.hh:282
ParallelLocalIndex< Attribute > & operator=(size_t index)
Assign a new local index.
Definition: plocalindex.hh:263
LocalIndexState state() const
Get the state.
Definition: plocalindex.hh:276
ParallelLocalIndex()
Parameterless constructor.
Definition: plocalindex.hh:231
const Attribute attribute() const
Get the attribute of the index.
Definition: plocalindex.hh:237
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:259
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:237
Provides classes for use as the local index in ParallelIndexSet.
Traits classes for mapping types onto MPI_Datatype.
Dune namespace.
Definition: alignedallocator.hh:13
A traits class describing the mapping of types onto MPI_Datatypes.
Definition: mpitraits.hh:41
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 2, 22:35, 2024)