ug4
pcl_reduce_traits.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017: 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__PCL_reduce_traits
34 #define __H__PCL_reduce_traits
35 
36 #include <algorithm>
37 
38 namespace pcl {
39 
41 
42 template <class TValue>
44 {
45  typedef TValue value_t;
46  static inline value_t min(value_t v1, value_t v2) {return std::min(v1, v2);}
47  static inline value_t max(value_t v1, value_t v2) {return std::max(v1, v2);}
48  static inline value_t sum(value_t v1, value_t v2) {return v1 + v2;}
49  static inline value_t prod(value_t v1, value_t v2) {return v1 * v2;}
50  static inline value_t land(value_t v1, value_t v2) {return v1 && v2;}
51  static inline value_t band(value_t v1, value_t v2) {return v1 & v2;}
52  static inline value_t lor(value_t v1, value_t v2) {return v1 || v2;}
53  static inline value_t bor(value_t v1, value_t v2) {return v1 | v2;}
54 };
55 
57 template <>
58 struct reduce_traits<float>
59 {
60  typedef float value_t;
61  static inline value_t min(value_t v1, value_t v2) {return std::min(v1, v2);}
62  static inline value_t max(value_t v1, value_t v2) {return std::max(v1, v2);}
63  static inline value_t sum(value_t v1, value_t v2) {return v1 + v2;}
64  static inline value_t prod(value_t v1, value_t v2) {return v1 * v2;}
65  static inline value_t land(value_t v1, value_t v2) {return v1 && v2;}
66  static inline value_t band(value_t v1, value_t v2) {UG_THROW("floats do not support a binary and operation.");}
67  static inline value_t lor(value_t v1, value_t v2) {return v1 || v2;}
68  static inline value_t bor(value_t v1, value_t v2) {UG_THROW("floats do not support a binary or operation.");}
69 };
70 
72 template <>
73 struct reduce_traits<double>
74 {
75  typedef double value_t;
76  static inline value_t min(value_t v1, value_t v2) {return std::min(v1, v2);}
77  static inline value_t max(value_t v1, value_t v2) {return std::max(v1, v2);}
78  static inline value_t sum(value_t v1, value_t v2) {return v1 + v2;}
79  static inline value_t prod(value_t v1, value_t v2) {return v1 * v2;}
80  static inline value_t land(value_t v1, value_t v2) {return v1 && v2;}
81  static inline value_t band(value_t v1, value_t v2) {UG_THROW("doubles do not support a binary and operation.");}
82  static inline value_t lor(value_t v1, value_t v2) {return v1 || v2;}
83  static inline value_t bor(value_t v1, value_t v2) {UG_THROW("doubles do not support a binary or operation.");}
84 };
85 
86 
87 template <class T>
88 class Reducer {
89 public:
91  {
92  if(rop == MPI_MIN) m_op = &reduce_traits<T>::min;
93  else if(rop == MPI_MAX) m_op = &reduce_traits<T>::max;
94  else if(rop == MPI_SUM) m_op = &reduce_traits<T>::sum;
95  else if(rop == MPI_PROD) m_op = &reduce_traits<T>::prod;
96  else if(rop == MPI_LAND) m_op = &reduce_traits<T>::land;
97  else if(rop == MPI_BAND) m_op = &reduce_traits<T>::band;
98  else if(rop == MPI_LOR) m_op = &reduce_traits<T>::lor;
99  else if(rop == MPI_BOR) m_op = &reduce_traits<T>::bor;
100  else {UG_THROW ("Unsupported reduce operation: " << rop)};
101  }
102 
103 
104  T operator () (T v1, T v2) {return m_op (v1, v2);}
105 
106  T reduce (T v1, T v2) {return m_op (v1, v2);}
107 
108 private:
109  T (*m_op) (T, T);
110 };
111 
112 
113 
114 }// end of namespace
115 
116 #endif //__H__PCL_reduce_traits
Definition: pcl_reduce_traits.h:88
T operator()(T v1, T v2)
Definition: pcl_reduce_traits.h:104
Reducer(ReduceOperation rop)
Definition: pcl_reduce_traits.h:90
T reduce(T v1, T v2)
Definition: pcl_reduce_traits.h:106
T(* m_op)(T, T)
Definition: pcl_reduce_traits.h:109
MPI_Op ReduceOperation
Definition: pcl_methods.h:74
#define UG_THROW(msg)
Definition: error.h:57
Definition: parallel_grid_layout.h:46
static value_t band(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:81
double value_t
Definition: pcl_reduce_traits.h:75
static value_t prod(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:79
static value_t bor(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:83
static value_t min(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:76
static value_t sum(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:78
static value_t land(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:80
static value_t lor(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:82
static value_t max(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:77
static value_t lor(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:67
static value_t land(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:65
static value_t max(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:62
static value_t prod(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:64
static value_t min(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:61
static value_t band(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:66
static value_t sum(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:63
float value_t
Definition: pcl_reduce_traits.h:60
static value_t bor(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:68
methods defined in those traits are used by ComPol_AttachmentReduce
Definition: pcl_reduce_traits.h:44
static value_t max(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:47
static value_t land(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:50
static value_t min(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:46
static value_t bor(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:53
TValue value_t
Definition: pcl_reduce_traits.h:45
static value_t prod(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:49
static value_t band(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:51
static value_t sum(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:48
static value_t lor(value_t v1, value_t v2)
Definition: pcl_reduce_traits.h:52