ug4
compol_attachment_reduce.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG_PCL_COMPOL_ATTACHMENT_REDUCE__
34 #define __H__UG_PCL_COMPOL_ATTACHMENT_REDUCE__
35 
36 #include <algorithm>
37 #include "pcl/pcl_methods.h"
38 #include "common/serialization.h"
39 
40 namespace ug{
41 
43 
44 template <class TValue>
46 {
47  typedef TValue value_t;
48  static inline value_t min(value_t v1, value_t v2) {return std::min(v1, v2);}
49  static inline value_t max(value_t v1, value_t v2) {return std::max(v1, v2);}
50  static inline value_t sum(value_t v1, value_t v2) {return v1 + v2;}
51  static inline value_t prod(value_t v1, value_t v2) {return v1 * v2;}
52  static inline value_t land(value_t v1, value_t v2) {return v1 && v2;}
53  static inline value_t band(value_t v1, value_t v2) {return v1 & v2;}
54  static inline value_t lor(value_t v1, value_t v2) {return v1 || v2;}
55  static inline value_t bor(value_t v1, value_t v2) {return v1 | v2;}
56 };
57 
59 template <>
61 {
62  typedef float value_t;
63  static inline value_t min(value_t v1, value_t v2) {return std::min(v1, v2);}
64  static inline value_t max(value_t v1, value_t v2) {return std::max(v1, v2);}
65  static inline value_t sum(value_t v1, value_t v2) {return v1 + v2;}
66  static inline value_t prod(value_t v1, value_t v2) {return v1 * v2;}
67  static inline value_t land(value_t v1, value_t v2) {return v1 && v2;}
68  static inline value_t band(value_t v1, value_t v2) {UG_THROW("floats do not support a binary and operation.");}
69  static inline value_t lor(value_t v1, value_t v2) {return v1 || v2;}
70  static inline value_t bor(value_t v1, value_t v2) {UG_THROW("floats do not support a binary or operation.");}
71 };
72 
74 template <>
76 {
77  typedef double value_t;
78  static inline value_t min(value_t v1, value_t v2) {return std::min(v1, v2);}
79  static inline value_t max(value_t v1, value_t v2) {return std::max(v1, v2);}
80  static inline value_t sum(value_t v1, value_t v2) {return v1 + v2;}
81  static inline value_t prod(value_t v1, value_t v2) {return v1 * v2;}
82  static inline value_t land(value_t v1, value_t v2) {return v1 && v2;}
83  static inline value_t band(value_t v1, value_t v2) {UG_THROW("doubles do not support a binary and operation.");}
84  static inline value_t lor(value_t v1, value_t v2) {return v1 || v2;}
85  static inline value_t bor(value_t v1, value_t v2) {UG_THROW("doubles do not support a binary or operation.");}
86 };
87 
88 template <int dim>
90 {
92  static inline value_t min(value_t v1, value_t v2)
93  {
94  value_t v;
95  for(int i = 0; i < dim; ++i)
96  v[i] = std::min(v1[i], v2[i]);
97  return v;
98  }
99  static inline value_t max(value_t v1, value_t v2)
100  {
101  value_t v;
102  for(int i = 0; i < dim; ++i)
103  v[i] = std::max(v1[i], v2[i]);
104  return v;
105  }
106  static inline value_t sum(value_t v1, value_t v2)
107  {
108  value_t v = v1;
109  v += v2;
110  return v;
111  }
112  static inline value_t prod(value_t v1, value_t v2)
113  {
114  value_t v;
115  for(int i = 0; i < dim; ++i)
116  v[i] = v1[i] * v2[i];
117  return v;
118  }
119  static inline value_t land(value_t v1, value_t v2)
120  {
121  value_t v;
122  for(int i = 0; i < dim; ++i)
123  v[i] = v1[i] && v2[i];
124  return v;
125  }
126  static inline value_t band(value_t v1, value_t v2) {UG_THROW("vectors do not support a binary and operation.");}
127  static inline value_t lor(value_t v1, value_t v2)
128  {
129  value_t v;
130  for(int i = 0; i < dim; ++i)
131  v[i] = v1[i] || v2[i];
132  return v;
133  }
134  static inline value_t bor(value_t v1, value_t v2) {UG_THROW("vectors do not support a binary or operation.");}
135 };
136 
137 template <>
139  public vector_attachment_reduce_traits<1> {};
140 
141 template <>
143  public vector_attachment_reduce_traits<2> {};
144 
145 template <>
147  public vector_attachment_reduce_traits<3> {};
148 
149 template <>
151  public vector_attachment_reduce_traits<4> {};
152 
153 
154 // implementation for a std::vector<number> of arbitrary size
156 {
157  typedef std::vector<number> value_t;
158 
159  // we have to make sure the two vectors are of the same length
160  // if they are not, the smaller one will be resized to fit the larger one's size
161  // fill-in with zeros
162  static inline void check_length(value_t& v1, value_t& v2)
163  {
164  if (v1.size() == v2.size()) return;
165 
166  if (v1.size() > v2.size())
167  {
168  v2.resize(v1.size(), 0.0);
169  return;
170  }
171 
172  v1.resize(v2.size(), 0.0);
173  }
174 
175  static inline value_t min(value_t v1, value_t v2)
176  {
177  check_length(v1,v2);
178  size_t sz = v1.size();
179  value_t v(sz);
180  for (size_t i = 0; i < sz; i++)
181  v[i] = std::min(v1[i], v2[i]);
182  return v;
183  }
184 
185  static inline value_t max(value_t v1, value_t v2)
186  {
187  check_length(v1,v2);
188  size_t sz = v1.size();
189  value_t v(sz);
190  for (size_t i = 0; i < sz; i++)
191  v[i] = std::max(v1[i], v2[i]);
192  return v;
193  }
194 
195  static inline value_t sum(value_t v1, value_t v2)
196  {
197  check_length(v1,v2);
198  size_t sz = v1.size();
199  value_t v(sz);
200  for (size_t i = 0; i < sz; i++)
201  v[i] = v1[i] + v2[i];
202  return v;
203  }
204 
205  static inline value_t prod(value_t v1, value_t v2)
206  {
207  check_length(v1,v2);
208  size_t sz = v1.size();
209  value_t v(sz);
210  for (size_t i = 0; i < sz; i++)
211  v[i] = v1[i] * v2[i];
212  return v;
213  }
214 
215  static inline value_t land(value_t v1, value_t v2)
216  {
217  check_length(v1,v2);
218  size_t sz = v1.size();
219  value_t v(sz);
220  for (size_t i = 0; i < sz; i++)
221  v[i] = v1[i] && v2[i];
222  return v;
223  }
224 
225  static inline value_t band(value_t v1, value_t v2)
226  {
227  UG_THROW("Vectors of number do not support a binary and operation.");
228  }
229 
230  static inline value_t lor(value_t v1, value_t v2)
231  {
232  check_length(v1,v2);
233  size_t sz = v1.size();
234  value_t v(sz);
235  for (size_t i = 0; i < sz; i++)
236  v[i] = v1[i] || v2[i];
237  return v;
238  }
239 
240  static inline value_t bor(value_t v1, value_t v2)
241  {
242  UG_THROW("Vectors of number do not support a binary or operation.");
243  }
244 };
245 
246 template <>
247 struct attachment_reduce_traits<std::vector<number> > :
249 
250 
252 
272 template <class TLayout, class TAttachment>
274 {
275  public:
276  typedef TLayout Layout;
277  typedef typename Layout::Type GeomObj;
278  typedef typename Layout::Element Element;
279  typedef typename Layout::Interface Interface;
280  typedef typename TAttachment::ValueType Value;
281  typedef typename Interface::const_iterator iiter_t;
283 
293  BOR
294  };
295 
296  ComPol_AttachmentReduce(Grid& grid, TAttachment& attachment,
298  m_grid(grid),
299  m_aa(grid, attachment),
300  m_op(NONE)
301  {
302  if(op == PCL_RO_MAX) m_op = MAX;
303  else if(op == PCL_RO_MIN) m_op = MIN;
304  else if(op == PCL_RO_SUM) m_op = SUM;
305  else if(op == PCL_RO_PROD) m_op = PROD;
306  else if(op == PCL_RO_LAND) m_op = LAND;
307  else if(op == PCL_RO_BAND) m_op = BAND;
308  else if(op == PCL_RO_LOR) m_op = LOR;
309  else if(op == PCL_RO_BOR) m_op = BOR;
310  }
311 
312  ComPol_AttachmentReduce(Grid& grid, TAttachment& attachment,
313  ReduceOperation op) :
314  m_grid(grid),
315  m_aa(grid, attachment),
316  m_op(op)
317  {}
318 
320  bool collect(BinaryBuffer& buff, const Interface& interface);
321 
323  bool extract(BinaryBuffer& buff, const Interface& interface);
324 
325  protected:
326  bool extract_max(BinaryBuffer& buff, const Interface& interface);
327  bool extract_min(BinaryBuffer& buff, const Interface& interface);
328  bool extract_sum(BinaryBuffer& buff, const Interface& interface);
329  bool extract_prod(BinaryBuffer& buff, const Interface& interface);
330  bool extract_land(BinaryBuffer& buff, const Interface& interface);
331  bool extract_band(BinaryBuffer& buff, const Interface& interface);
332  bool extract_lor(BinaryBuffer& buff, const Interface& interface);
333  bool extract_bor(BinaryBuffer& buff, const Interface& interface);
334 
335  private:
339 };
340 
341 
342 template <class TLayout, class TAttachment>
344 collect(BinaryBuffer& buff, const Interface& interface)
345 {
346  for(iiter_t iter = interface.begin(); iter != interface.end(); ++iter)
347  Serialize(buff, m_aa[interface.get_element(iter)]);
348  return true;
349 }
350 
351 template <class TLayout, class TAttachment>
353 extract(BinaryBuffer& buff, const Interface& interface)
354 {
355  switch(m_op){
356  case MAX: return extract_max(buff, interface);
357  case MIN: return extract_min(buff, interface);
358  case SUM: return extract_sum(buff, interface);
359  case PROD: return extract_prod(buff, interface);
360  case LAND: return extract_land(buff, interface);
361  case BAND: return extract_band(buff, interface);
362  case LOR: return extract_lor(buff, interface);
363  case BOR: return extract_bor(buff, interface);
364  default:
365  UG_THROW("Unsupported reduce operation in ComPol_AttachmentReduce::extract:"
366  << m_op);
367  break;
368  }
369  return false;
370 }
371 
372 template <class TLayout, class TAttachment>
374 extract_max(BinaryBuffer& buff, const Interface& interface)
375 {
376  using std::max;
377  Value v;
378  for(iiter_t iter = interface.begin(); iter != interface.end(); ++iter){
379  Deserialize(buff, v);
380  Element e = interface.get_element(iter);
381  m_aa[e] = art::max(v, m_aa[e]);
382  }
383  return true;
384 }
385 
386 template <class TLayout, class TAttachment>
388 extract_min(BinaryBuffer& buff, const Interface& interface)
389 {
390  using std::min;
391  Value v;
392  for(iiter_t iter = interface.begin(); iter != interface.end(); ++iter){
393  Deserialize(buff, v);
394  Element e = interface.get_element(iter);
395  m_aa[e] = art::min(v, m_aa[e]);
396  }
397  return true;
398 }
399 
400 template <class TLayout, class TAttachment>
402 extract_sum(BinaryBuffer& buff, const Interface& interface)
403 {
404  Value v;
405  for(iiter_t iter = interface.begin(); iter != interface.end(); ++iter){
406  Deserialize(buff, v);
407  Element e = interface.get_element(iter);
408  m_aa[e] = art::sum(v, m_aa[e]);
409  }
410  return true;
411 }
412 
413 template <class TLayout, class TAttachment>
415 extract_prod(BinaryBuffer& buff, const Interface& interface)
416 {
417  Value v;
418  for(iiter_t iter = interface.begin(); iter != interface.end(); ++iter){
419  Deserialize(buff, v);
420  Element e = interface.get_element(iter);
421  m_aa[e] = art::prod(v, m_aa[e]);
422  }
423  return true;
424 }
425 
426 template <class TLayout, class TAttachment>
428 extract_land(BinaryBuffer& buff, const Interface& interface)
429 {
430  Value v;
431  for(iiter_t iter = interface.begin(); iter != interface.end(); ++iter){
432  Deserialize(buff, v);
433  Element e = interface.get_element(iter);
434  m_aa[e] = art::land(v, m_aa[e]);
435  }
436  return true;
437 }
438 
439 template <class TLayout, class TAttachment>
441 extract_band(BinaryBuffer& buff, const Interface& interface)
442 {
443  Value v;
444  for(iiter_t iter = interface.begin(); iter != interface.end(); ++iter){
445  Deserialize(buff, v);
446  Element e = interface.get_element(iter);
447  m_aa[e] = art::band(v, m_aa[e]);
448  }
449  return true;
450 }
451 
452 template <class TLayout, class TAttachment>
454 extract_lor(BinaryBuffer& buff, const Interface& interface)
455 {
456  Value v;
457  for(iiter_t iter = interface.begin(); iter != interface.end(); ++iter){
458  Deserialize(buff, v);
459  Element e = interface.get_element(iter);
460  m_aa[e] = art::lor(v, m_aa[e]);
461  }
462  return true;
463 }
464 
465 template <class TLayout, class TAttachment>
467 extract_bor(BinaryBuffer& buff, const Interface& interface)
468 {
469  Value v;
470  for(iiter_t iter = interface.begin(); iter != interface.end(); ++iter){
471  Deserialize(buff, v);
472  Element e = interface.get_element(iter);
473  m_aa[e] = art::bor(v, m_aa[e]);
474  }
475  return true;
476 }
477 
478 }// end of namespace
479 
480 #endif
specializations are responsible to pack and unpack interface data during communication.
Definition: pcl_communication_structs.h:790
A Buffer for binary data.
Definition: binary_buffer.h:56
Performs reduce operations on the specified attachment.
Definition: compol_attachment_reduce.h:274
bool collect(BinaryBuffer &buff, const Interface &interface)
writes the data for the given interface to the buffer.
Definition: compol_attachment_reduce.h:344
ComPol_AttachmentReduce(Grid &grid, TAttachment &attachment, pcl::ReduceOperation op)
Definition: compol_attachment_reduce.h:296
Grid & m_grid
Definition: compol_attachment_reduce.h:336
bool extract(BinaryBuffer &buff, const Interface &interface)
reads the data from the buffer to the given interface .
Definition: compol_attachment_reduce.h:353
bool extract_lor(BinaryBuffer &buff, const Interface &interface)
Definition: compol_attachment_reduce.h:454
ComPol_AttachmentReduce(Grid &grid, TAttachment &attachment, ReduceOperation op)
Definition: compol_attachment_reduce.h:312
ReduceOperation
Definition: compol_attachment_reduce.h:284
@ LAND
Definition: compol_attachment_reduce.h:290
@ SUM
Definition: compol_attachment_reduce.h:288
@ MIN
Definition: compol_attachment_reduce.h:287
@ PROD
Definition: compol_attachment_reduce.h:289
@ BAND
Definition: compol_attachment_reduce.h:291
@ NONE
Definition: compol_attachment_reduce.h:285
@ LOR
Definition: compol_attachment_reduce.h:292
@ MAX
Definition: compol_attachment_reduce.h:286
@ BOR
Definition: compol_attachment_reduce.h:293
Layout::Type GeomObj
Definition: compol_attachment_reduce.h:277
attachment_reduce_traits< Value > art
Definition: compol_attachment_reduce.h:282
bool extract_prod(BinaryBuffer &buff, const Interface &interface)
Definition: compol_attachment_reduce.h:415
bool extract_sum(BinaryBuffer &buff, const Interface &interface)
Definition: compol_attachment_reduce.h:402
TLayout Layout
Definition: compol_attachment_reduce.h:276
TAttachment::ValueType Value
Definition: compol_attachment_reduce.h:280
bool extract_band(BinaryBuffer &buff, const Interface &interface)
Definition: compol_attachment_reduce.h:441
Grid::AttachmentAccessor< GeomObj, TAttachment > m_aa
Definition: compol_attachment_reduce.h:337
Layout::Interface Interface
Definition: compol_attachment_reduce.h:279
bool extract_bor(BinaryBuffer &buff, const Interface &interface)
Definition: compol_attachment_reduce.h:467
bool extract_max(BinaryBuffer &buff, const Interface &interface)
Definition: compol_attachment_reduce.h:374
Interface::const_iterator iiter_t
Definition: compol_attachment_reduce.h:281
ReduceOperation m_op
Definition: compol_attachment_reduce.h:338
bool extract_min(BinaryBuffer &buff, const Interface &interface)
Definition: compol_attachment_reduce.h:388
Layout::Element Element
Definition: compol_attachment_reduce.h:278
bool extract_land(BinaryBuffer &buff, const Interface &interface)
Definition: compol_attachment_reduce.h:428
Manages the elements of a grid and their interconnection.
Definition: grid.h:132
#define PCL_RO_SUM
Definition: pcl_methods.h:63
#define PCL_RO_MAX
Definition: pcl_methods.h:61
#define PCL_RO_MIN
Definition: pcl_methods.h:62
MPI_Op ReduceOperation
Definition: pcl_methods.h:74
#define PCL_RO_BOR
Definition: pcl_methods.h:68
#define PCL_RO_PROD
Definition: pcl_methods.h:64
#define PCL_RO_LAND
Definition: pcl_methods.h:65
#define PCL_RO_BAND
Definition: pcl_methods.h:66
#define PCL_RO_LOR
Definition: pcl_methods.h:67
static const int dim
#define UG_THROW(msg)
Definition: error.h:57
Definition: smart_pointer.h:814
the ug namespace
void Deserialize(TIStream &buf, ParallelVector< T > &v)
Deerialize for ParallelVector<T>
Definition: restart_bridge.cpp:112
void Serialize(TOStream &buf, const ParallelVector< T > &v)
Serialize for ParallelVector<T>
Definition: restart_bridge.cpp:103
static value_t land(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:82
static value_t prod(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:81
static value_t band(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:83
static value_t sum(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:80
static value_t min(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:78
double value_t
Definition: compol_attachment_reduce.h:77
static value_t max(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:79
static value_t bor(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:85
static value_t lor(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:84
static value_t band(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:68
float value_t
Definition: compol_attachment_reduce.h:62
static value_t min(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:63
static value_t bor(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:70
static value_t sum(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:65
static value_t max(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:64
static value_t land(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:67
static value_t prod(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:66
static value_t lor(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:69
methods defined in those traits are used by ComPol_AttachmentReduce
Definition: compol_attachment_reduce.h:46
static value_t lor(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:54
static value_t sum(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:50
static value_t max(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:49
TValue value_t
Definition: compol_attachment_reduce.h:47
static value_t prod(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:51
static value_t land(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:52
static value_t min(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:48
static value_t bor(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:55
static value_t band(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:53
Definition: compol_attachment_reduce.h:156
static value_t min(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:175
static value_t prod(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:205
std::vector< number > value_t
Definition: compol_attachment_reduce.h:157
static value_t land(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:215
static value_t sum(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:195
static void check_length(value_t &v1, value_t &v2)
Definition: compol_attachment_reduce.h:162
static value_t bor(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:240
static value_t band(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:225
static value_t lor(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:230
static value_t max(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:185
Definition: compol_attachment_reduce.h:90
static value_t min(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:92
static value_t lor(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:127
static value_t sum(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:106
static value_t land(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:119
static value_t max(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:99
MathVector< dim > value_t
Definition: compol_attachment_reduce.h:91
static value_t band(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:126
static value_t bor(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:134
static value_t prod(value_t v1, value_t v2)
Definition: compol_attachment_reduce.h:112