dune-fem  2.4.1-rc
commoperations.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_COMMOPERATIONS_HH
2 #define DUNE_FEM_COMMOPERATIONS_HH
3 
4 #include <tuple>
5 #include <type_traits>
6 #include <utility>
7 
8 #include <dune/common/tupleutility.hh>
9 
10 #include <dune/grid/common/datahandleif.hh>
11 
12 namespace Dune
13 {
14 
15  namespace Fem
16  {
17 
22  // CombinedDataType
23  // ----------------
24 
25  template< class... DataHandle >
27 
28  template< class DataHandle >
29  struct CombinedDataType< DataHandle >
30  {
31  typedef typename DataHandle::DataType Type;
32  };
33 
34  template< class DataHandle, class... Tail >
35  struct CombinedDataType< DataHandle, Tail... >
36  {
37  typedef typename DataHandle::DataType Type;
38  static_assert( std::is_same< Type, typename CombinedDataType< Tail... >::Type >::value, "Only data handles for the same data type can be combined." );
39  };
40 
41 
42 
43  // CombinedDataHandle
44  // ------------------
45 
49  template< class... DataHandle >
51  : public CommDataHandleIF< CombinedDataHandle< DataHandle... >, typename CombinedDataType< DataHandle... >::Type >
52  {
53  typedef std::tuple< DataHandle... > DataHandlerTupleType;
54 
56  template <class BufferImp, class EntityImp>
57  class DataGather{
58  public:
60  DataGather(BufferImp & buff, const EntityImp & en )
61  : buff_(buff)
62  , en_(en)
63  {}
64 
66  template <class DataHandlerImp>
67  void visit(DataHandlerImp & dh) {
68  dh.gather(buff_,en_);
69  }
70 
71  private:
72  BufferImp & buff_;
73  const EntityImp & en_;
74  };
75 
77  template <class BufferImp, class EntityImp>
78  class DataScatter{
79  public:
82  DataScatter(BufferImp & buff, const EntityImp & en, size_t n)
83  : buff_(buff)
84  , en_(en)
85  , size_(n)
86  {}
87 
89  template <class DataHandlerImp>
90  void visit(DataHandlerImp & dh) {
91  // TODO: here, the wrong size is passed to the subhandles
92  dh.scatter(buff_,en_,size_);
93  }
94 
95  private:
96  BufferImp & buff_;
97  const EntityImp & en_;
98  const size_t size_;
99  };
100 
102  template <class EntityImp>
103  class DataSize
104  {
105  public:
108  DataSize(const EntityImp & en)
109  : en_(en)
110  , size_(0)
111  {}
112 
114  template <class DataHandlerImp>
115  void visit(DataHandlerImp & dh)
116  {
117  size_ += dh.size(en_);
118  }
119 
121  size_t size() const { return size_; }
122 
123  private:
124  const EntityImp & en_;
125  size_t size_;
126  };
127 
129  class FixedSize
130  {
131  public:
135  FixedSize(const int dim, const int codim)
136  : dim_(dim)
137  , codim_(codim)
138  , fixedSize_(true)
139  {}
140 
142  template <class DataHandlerImp>
143  void visit(DataHandlerImp & dh)
144  {
145  bool fs = dh.fixedsize(dim_,codim_);
146  fixedSize_ = (fs == false) ? fs : fixedSize_;
147  }
148 
150  bool fixedSize() const { return fixedSize_; }
151 
152  private:
153  const int dim_;
154  const int codim_;
155  bool fixedSize_;
156  };
157 
159  class Contains
160  {
161  public:
165  Contains(const int dim, const int codim)
166  : dim_(dim)
167  , codim_(codim)
168  , contains_(false)
169  {}
170 
172  template <class DataHandlerImp>
173  void visit(DataHandlerImp & dh)
174  {
175  bool c = dh.contains(dim_,codim_);
176  contains_ = (c == true) ? c : contains_;
177  }
178 
180  bool contains() const { return contains_; }
181 
182  private:
183  const int dim_;
184  const int codim_;
185  bool contains_;
186  };
187 
188  public:
189  typedef typename CombinedDataType< DataHandle... >::Type DataType;
190 
191  CombinedDataHandle ( const DataHandle &... handle )
192  : data_( handle... )
193  {}
194 
195  CombinedDataHandle ( const std::tuple< DataHandle... > &data )
196  : data_( data )
197  {}
198 
199  bool contains (int dim, int codim) const
200  {
201  ForEachValue<DataHandlerTupleType> forEach(data_);
202  Contains dataContains(dim,codim);
203  forEach.apply(dataContains);
204  return dataContains.contains();
205  }
206 
207  bool fixedsize (int dim, int codim) const
208  {
209  ForEachValue<DataHandlerTupleType> forEach(data_);
210  FixedSize dataFixedSize(dim,codim);
211  forEach.apply(dataFixedSize);
212  return dataFixedSize.fixedSize();
213  }
214 
217  template<class MessageBufferImp, class EntityType>
218  void gather (MessageBufferImp& buff, const EntityType& en) const
219  {
220  ForEachValue<DataHandlerTupleType> forEach(data_);
221  DataGather<MessageBufferImp,EntityType> gatherData(buff,en);
222  forEach.apply(gatherData);
223  }
224 
227  template<class MessageBufferImp, class EntityType>
228  void scatter (MessageBufferImp& buff, const EntityType& en, size_t n)
229  {
230  ForEachValue<DataHandlerTupleType> forEach(data_);
231  DataScatter<MessageBufferImp,EntityType> scatterData(buff,en,n);
232  forEach.apply(scatterData);
233  }
234 
237  template<class EntityType>
238  size_t size (const EntityType& en) const
239  {
240  ForEachValue<DataHandlerTupleType> forEach(data_);
241  DataSize<EntityType> dataSize(en);
242  forEach.apply(dataSize);
243  return dataSize.size();
244  }
245 
246  private:
247  DataHandlerTupleType data_;
248  };
249 
251  //
252  // --DiscreteFunctionCommunications
253  //
255 
260  {
263  struct Copy
264  {
265  static const dfCommunicationOperation value = copy;
266  static const char * name ()
267  {
268  return "Copy";
269  }
270 
271  template <class DataType>
272  static inline void apply(const DataType & arg, DataType & dest)
273  {
274  dest = arg;
275  }
276  };
277 
279  struct Add
280  {
281  static const dfCommunicationOperation value = add;
282  static const char * name ()
283  {
284  return "Add";
285  }
286 
287  template <class DataType>
288  static inline void apply(const DataType & arg, DataType & dest)
289  {
290  dest += arg;
291  }
292  };
293 
295  struct Sub
296  {
297  static const dfCommunicationOperation value = sub;
298  static const char * name ()
299  {
300  return "Sub";
301  }
302 
303  template <class DataType>
304  static inline void apply(const DataType & arg, DataType & dest)
305  {
306  dest -= arg;
307  }
308  };
309 
311  struct Min
312  {
313  static const dfCommunicationOperation value = min;
314  static const char * name ()
315  {
316  return "Min";
317  }
318 
319  template <class DataType>
320  static inline void apply(const DataType & arg, DataType & dest)
321  {
322  dest = std::min(dest,arg);
323  }
324  };
325 
327  struct Max
328  {
329  static const dfCommunicationOperation value = max;
330  static const char * name ()
331  {
332  return "Max";
333  }
334 
335  template <class DataType>
336  static inline void apply(const DataType & arg, DataType & dest)
337  {
338  dest = std::max(dest,arg);
339  }
340  };
341  };
342 
344  //
345  // --LoadBalanceContainsCheck
346  //
348 
350  template <class DiscreteFunction>
352  {
353  public:
354  typedef DiscreteFunction DiscreteFunctionType ;
355  typedef typename DiscreteFunctionType :: DiscreteFunctionSpaceType :: IteratorType :: Entity Entity;
356 
357  explicit LoadBalanceLeafData( const DiscreteFunctionType& df ) {}
359  bool contains (const Entity& entity) const
360  {
361  return entity.isLeaf();
362  }
363  };
364 
366 
367  } // namespace Fem
368 
369 } // namespace Dune
370 
371 #endif // #ifndef DUNE_FEM_COMMOPERATIONS_HH
combine multiple data handles into one
Definition: commoperations.hh:50
static constexpr T min(T a)
Definition: utility.hh:81
size_t size(const EntityType &en) const
loop over all internal data handlers and return sum of data size of given entity
Definition: commoperations.hh:238
void scatter(MessageBufferImp &buff, const EntityType &en, size_t n)
loop over all internal data handlers and call scatter for given entity
Definition: commoperations.hh:228
static void apply(const DataType &arg, DataType &dest)
Definition: commoperations.hh:288
static void apply(const DataType &arg, DataType &dest)
Definition: commoperations.hh:320
DiscreteFunctionType::DiscreteFunctionSpaceType::IteratorType::Entity Entity
Definition: commoperations.hh:355
CombinedDataHandle(const std::tuple< DataHandle... > &data)
Definition: commoperations.hh:195
check for sets of entities for the load balance procedure
Definition: commoperations.hh:351
static void apply(const DataType &arg, DataType &dest)
Definition: commoperations.hh:304
static constexpr T max(T a)
Definition: utility.hh:65
Mathematical operation apply during communication to data that is communicated enum of all avialable ...
Definition: commoperations.hh:259
static const char * name()
Definition: commoperations.hh:282
bool contains(const Entity &entity) const
return true if the data of this entity should be transfered during load balance
Definition: commoperations.hh:359
static constexpr T sub(T a)
Definition: utility.hh:49
DataHandle::DataType Type
Definition: commoperations.hh:37
substract data
Definition: commoperations.hh:295
static double max(const Double &v, const double p)
Definition: double.hh:387
Definition: commoperations.hh:261
Definition: coordinate.hh:4
CombinedDataType< DataHandle... >::Type DataType
Definition: commoperations.hh:189
static const char * name()
Definition: commoperations.hh:314
static const char * name()
Definition: commoperations.hh:266
just copy data
Definition: commoperations.hh:263
bool contains(int dim, int codim) const
Definition: commoperations.hh:199
static const char * name()
Definition: commoperations.hh:298
sum up data
Definition: commoperations.hh:279
dfCommunicationOperation
Definition: commoperations.hh:261
static void apply(const DataType &arg, DataType &dest)
Definition: commoperations.hh:336
CombinedDataHandle(const DataHandle &...handle)
Definition: commoperations.hh:191
bool fixedsize(int dim, int codim) const
Definition: commoperations.hh:207
static const char * name()
Definition: commoperations.hh:330
void gather(MessageBufferImp &buff, const EntityType &en) const
loop over all internal data handlers and call gather for given entity
Definition: commoperations.hh:218
Definition: commoperations.hh:26
static void apply(const DataType &arg, DataType &dest)
Definition: commoperations.hh:272
DataHandle::DataType Type
Definition: commoperations.hh:31
keep minimum
Definition: commoperations.hh:311
keep maximum
Definition: commoperations.hh:327
static double min(const Double &v, const double p)
Definition: double.hh:375
LoadBalanceLeafData(const DiscreteFunctionType &df)
Definition: commoperations.hh:357
DiscreteFunction DiscreteFunctionType
Definition: commoperations.hh:354