OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
vector_dist_operators_functions.hpp
1 /*
2  * vector_dist_operators_functions.hpp
3  *
4  * Created on: Jul 17, 2016
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_FUNCTIONS_HPP_
9 #define OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_FUNCTIONS_HPP_
10 
17 #define CREATE_VDIST_ARG_FUNC(fun_base,fun_name,OP_ID) \
18 \
19 \
20 template <typename exp1>\
21 class vector_dist_expression_op<exp1,void,OP_ID>\
22 {\
23  const exp1 o1;\
24 \
25 public:\
26 \
27  vector_dist_expression_op(const exp1 & o1)\
28  :o1(o1)\
29  {}\
30 \
31  inline void init() const\
32  {\
33  o1.init();\
34  }\
35 \
36  template<typename r_type=typename std::remove_reference<decltype(fun_base(o1.value(vect_dist_key_dx(0))))>::type > inline r_type value(const vect_dist_key_dx & key) const\
37  {\
38  return fun_base(o1.value(key));\
39  }\
40 };\
41 \
42 \
43 template<typename exp1, typename exp2_, unsigned int op1>\
44 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2_,op1>,void,OP_ID>\
45 fun_name(const vector_dist_expression_op<exp1,exp2_,op1> & va)\
46 {\
47  vector_dist_expression_op<vector_dist_expression_op<exp1,exp2_,op1>,void,OP_ID> exp_sum(va);\
48 \
49  return exp_sum;\
50 }\
51 \
52 \
53 template<unsigned int prp1, typename v1>\
54 inline vector_dist_expression_op<vector_dist_expression<prp1,v1>,void,OP_ID>\
55 fun_name(const vector_dist_expression<prp1,v1> & va)\
56 {\
57  vector_dist_expression_op<vector_dist_expression<prp1,v1>,void,OP_ID> exp_sum(va);\
58 \
59  return exp_sum;\
60 }
61 
62 
63 CREATE_VDIST_ARG_FUNC(norm,norm,VECT_NORM)
64 CREATE_VDIST_ARG_FUNC(norm2,norm2,VECT_NORM2)
65 CREATE_VDIST_ARG_FUNC(abs,abs,POINT_ABS)
66 CREATE_VDIST_ARG_FUNC(exp,exp,POINT_EXP)
67 CREATE_VDIST_ARG_FUNC(exp2,exp2,POINT_EXP2)
68 CREATE_VDIST_ARG_FUNC(expm1,expm1,POINT_EXPM1)
69 CREATE_VDIST_ARG_FUNC(log,log,POINT_LOG)
70 CREATE_VDIST_ARG_FUNC(log10,log10,POINT_LOG10)
71 CREATE_VDIST_ARG_FUNC(log2,log2,POINT_LOG2)
72 CREATE_VDIST_ARG_FUNC(log1p,log1p,POINT_LOG1P)
73 CREATE_VDIST_ARG_FUNC(sqrt,sqrt,POINT_SQRT)
74 CREATE_VDIST_ARG_FUNC(cbrt,cbrt,POINT_CBRT)
75 CREATE_VDIST_ARG_FUNC(sin,sin,POINT_SIN)
76 CREATE_VDIST_ARG_FUNC(cos,cos,POINT_COS)
77 CREATE_VDIST_ARG_FUNC(tan,tan,POINT_TAN)
78 CREATE_VDIST_ARG_FUNC(asin,asin,POINT_ASIN)
79 CREATE_VDIST_ARG_FUNC(acos,acos,POINT_ACOS)
80 CREATE_VDIST_ARG_FUNC(atan,atan,POINT_ATAN)
81 CREATE_VDIST_ARG_FUNC(sinh,sinh,POINT_SINH)
82 CREATE_VDIST_ARG_FUNC(cosh,cosh,POINT_COSH)
83 CREATE_VDIST_ARG_FUNC(tanh,tanh,POINT_TANH)
84 CREATE_VDIST_ARG_FUNC(asinh,asinh,POINT_ASINH)
85 CREATE_VDIST_ARG_FUNC(acosh,acosh,POINT_ACOSH)
86 CREATE_VDIST_ARG_FUNC(atanh,atanh,POINT_ATANH)
87 CREATE_VDIST_ARG_FUNC(erf,erf,POINT_ERF)
88 CREATE_VDIST_ARG_FUNC(erfc,erfc,POINT_ERFC)
89 CREATE_VDIST_ARG_FUNC(tgamma,tgamma,POINT_TGAMMA)
90 CREATE_VDIST_ARG_FUNC(lgamma,lgamma,POINT_LGAMMA)
91 CREATE_VDIST_ARG_FUNC(ceil,ceil,POINT_CEIL)
92 CREATE_VDIST_ARG_FUNC(floor,floor,POINT_FLOOR)
93 CREATE_VDIST_ARG_FUNC(trunc,trunc,POINT_TRUNC)
94 CREATE_VDIST_ARG_FUNC(round,round,POINT_ROUND)
95 CREATE_VDIST_ARG_FUNC(nearbyint,nearbyint,POINT_NEARBYINT)
96 CREATE_VDIST_ARG_FUNC(rint,rint,POINT_RINT)
97 
98 
105 #define CREATE_VDIST_ARG2_FUNC(fun_base,fun_name,OP_ID) \
106 \
107 \
108 template <typename exp1,typename exp2>\
109 class vector_dist_expression_op<exp1,exp2,OP_ID>\
110 {\
111  const exp1 o1;\
112  const exp2 o2;\
113 \
114 public:\
115 \
116  vector_dist_expression_op(const exp1 & o1, const exp2 & o2)\
117  :o1(o1),o2(o2)\
118  {}\
119 \
120  inline void init() const\
121  {\
122  o1.init();\
123  o2.init();\
124  }\
125 \
126  template<typename r_type=typename std::remove_reference<decltype(fun_base(o1.value(vect_dist_key_dx(0)),o2.value(vect_dist_key_dx(0)) ))>::type > inline r_type value(const vect_dist_key_dx & key) const\
127  {\
128  return fun_base(o1.value(key),o2.value(key));\
129  }\
130 };\
131 \
132 \
133 template<unsigned int p1, unsigned int p2, typename v1, typename v2>\
134 inline vector_dist_expression_op<vector_dist_expression<p1,v1>,vector_dist_expression<p2,v2>,OP_ID>\
135 fun_name(const vector_dist_expression<p1,v1> & va, const vector_dist_expression<p2,v2> & vb)\
136 {\
137  vector_dist_expression_op<vector_dist_expression<p1,v1>,vector_dist_expression<p2,v2>,OP_ID> exp_sum(va,vb);\
138 \
139  return exp_sum;\
140 }\
141 \
142 template<typename exp1 , typename exp2, unsigned int op1, unsigned int prp1, typename v1>\
143 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression<prp1,v1>,OP_ID>\
144 fun_name(const vector_dist_expression_op<exp1,exp2,op1> & va, const vector_dist_expression<prp1,v1> & vb)\
145 {\
146  vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression<prp1,v1>,OP_ID> exp_sum(va,vb);\
147 \
148  return exp_sum;\
149 }\
150 \
151 template<typename exp1 , typename exp2, unsigned int op1, unsigned int prp1, typename v1>\
152 inline vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_dist_expression_op<exp1,exp2,op1>,OP_ID>\
153 fun_name(const vector_dist_expression<prp1,v1> & va, const vector_dist_expression_op<exp1,exp2,op1> & vb)\
154 {\
155  vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_dist_expression_op<exp1,exp2,op1>,OP_ID> exp_sum(va,vb);\
156 \
157  return exp_sum;\
158 }\
159 \
160 template<typename exp1 , typename exp2, unsigned int op1, typename exp3 , typename exp4, unsigned int op2>\
161 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression_op<exp3,exp4,op2>,OP_ID>\
162 fun_name(const vector_dist_expression_op<exp1,exp2,op1> & va, const vector_dist_expression_op<exp3,exp4,op2> & vb)\
163 {\
164  vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression_op<exp3,exp4,op2>,OP_ID> exp_sum(va,vb);\
165 \
166  return exp_sum;\
167 }\
168 \
169 template<unsigned int prp1 , typename v1>\
170 inline vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_dist_expression<0,double>,OP_ID>\
171 fun_name(const vector_dist_expression<prp1,v1> & va, double d)\
172 {\
173  vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_dist_expression<0,double>,OP_ID> exp_sum(va,vector_dist_expression<0,double>(d));\
174 \
175  return exp_sum;\
176 }\
177 \
178 template<unsigned int prp1 , typename v1>\
179 inline vector_dist_expression_op<vector_dist_expression<0,double>,vector_dist_expression<prp1,v1>,OP_ID>\
180 fun_name(double d, const vector_dist_expression<prp1,v1> & vb)\
181 {\
182  vector_dist_expression_op<vector_dist_expression<0,double>,vector_dist_expression<prp1,v1>,OP_ID> exp_sum(vector_dist_expression<0,double>(d),vb);\
183 \
184  return exp_sum;\
185 }\
186 \
187 template<typename exp1 , typename exp2, unsigned int op1>\
188 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression<0,double>,OP_ID>\
189 fun_name(const vector_dist_expression_op<exp1,exp2,op1> & va, double d)\
190 {\
191  vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression<0,double>,OP_ID> exp_sum(va,vector_dist_expression<0,double>(d));\
192 \
193  return exp_sum;\
194 }\
195 \
196 template<typename exp1 , typename exp2, unsigned int op1>\
197 inline vector_dist_expression_op<vector_dist_expression<0,double>,vector_dist_expression_op<exp1,exp2,op1>,OP_ID>\
198 fun_name(double d, const vector_dist_expression_op<exp1,exp2,op1> & va)\
199 {\
200  vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression<0,double>,OP_ID> exp_sum(vector_dist_expression<0,double>(d),va);\
201 \
202  return exp_sum;\
203 }
204 
205 CREATE_VDIST_ARG2_FUNC(pmul,pmul,VECT_PMUL)
206 
207 
209 
210 
216 template <typename exp1, typename vector_type>
217 class vector_dist_expression_op<exp1,vector_type,VECT_SUM_REDUCE>
218 {
219 
221  const exp1 o1;
222 
224  typedef typename apply_kernel_rtype<decltype(o1.value(vect_dist_key_dx()))>::rtype rtype;
225 
227  mutable typename std::remove_reference<rtype>::type val;
228 
230  const vector_type & vd;
231 
232 public:
233 
235  vector_dist_expression_op(const exp1 & o1, const vector_type & vd)
236  :o1(o1),val(0),vd(vd)
237  {}
238 
240  // this produce a cache for the calculated value
241  inline void init() const
242  {
243  o1.init();
244 
245  val = 0.0;
246 
247  auto it = vd.getDomainIterator();
248 
249  while (it.isNext())
250  {
251  auto key = it.get();
252 
253  val += o1.value(key);
254 
255  ++it;
256  }
257  }
258 
260  inline typename std::remove_reference<rtype>::type get()
261  {
262  init();
263  return value(vect_dist_key_dx());
264  }
265 
267  template<typename r_type= typename std::remove_reference<rtype>::type > inline r_type value(const vect_dist_key_dx & key) const
268  {
269  return val;
270  }
271 };
272 
274 template<typename exp1, typename exp2_, unsigned int op1, typename vector_type>
276 rsum(const vector_dist_expression_op<exp1,exp2_,op1> & va, const vector_type & vd)
277 {
278  vector_dist_expression_op<vector_dist_expression_op<exp1,exp2_,op1>,vector_type,VECT_SUM_REDUCE> exp_sum(va,vd);
279 
280  return exp_sum;
281 }
282 
284 template<unsigned int prp1, typename v1, typename vector_type>
285 inline vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_type,VECT_SUM_REDUCE>
286 rsum(const vector_dist_expression<prp1,v1> & va, const vector_type & vd)
287 {
288  vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_type,VECT_SUM_REDUCE> exp_sum(va,vd);
289 
290  return exp_sum;
291 }
292 
293 namespace openfpm
294 {
299  template <typename T, typename P> auto distance(T exp1, P exp2) -> decltype(norm(exp1 - exp2))
300  {
301  return norm(exp1 - exp2);
302  }
303 }
304 
305 
306 #endif /* OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_FUNCTIONS_HPP_ */
vector_dist_expression_op(const exp1 &o1, const vector_type &vd)
constructor from an epxression exp1 and a vector vd
std::remove_reference< rtype >::type val
return type of the calculated value (without reference)
Grid key for a distributed grid.
void init() const
sum reduction require initialization where we calculate the reduction
apply_kernel_rtype< decltype(o1.value(vect_dist_key_dx()))>::rtype rtype
return type of this expression
const vector_type & vd
vector on which we apply the reduction expression
Unknown operation specialization.
Main class that encapsulate a vector properties operand to be used for expressions construction...
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:201
r_type value(const vect_dist_key_dx &key) const
it return the result of the expression (precalculated before)
vector_dist_iterator getDomainIterator() const
Get an iterator that traverse the particles in the domain.
Distributed vector.
auto distance(T exp1, P exp2) -> decltype(norm(exp1-exp2))
General distance formula.
It give the return type of the expression if applicable.
Test structure used for several test.
Definition: Point_test.hpp:105