OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
Point_operators_functions.hpp
1 /*
2  * Point_operators_functions.hpp
3  *
4  * Created on: Jun 20, 2016
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_SPACE_SHAPE_POINT_OPERATORS_FUNCTIONS_HPP_
9 #define OPENFPM_DATA_SRC_SPACE_SHAPE_POINT_OPERATORS_FUNCTIONS_HPP_
10 
11 #define CREATE_ARG_FUNC_CLASS(fun_base,fun_name,OP_ID) \
12 \
13 template <typename orig, typename exp1, typename exp2>\
14 class point_expression_op<orig,exp1,exp2, OP_ID >\
15 {\
16  const exp1 o1;\
17 \
18  mutable typename std::remove_const<typename orig::coord_type>::type scal;\
19 \
20 public:\
21 \
22  typedef orig orig_type;\
23 \
24  typedef int is_expression;\
25 \
26  typedef int has_init;\
27 \
28  typedef typename orig::coord_type return_type;\
29 \
30  static const unsigned int nvals = exp1::nvals;\
31  \
32  typedef typename first_or_second_pt<has_coordtype<exp1>::value,exp1,exp2>::coord_type coord_type;\
33  \
34 \
35  __device__ __host__ inline explicit point_expression_op(const exp1 & o1)\
36  :o1(o1),scal(0)\
37  {}\
38 \
39  __device__ __host__ inline void init() const\
40  {\
41  o1.init();\
42  }\
43 \
44  template<typename r_type=typename std::remove_reference<decltype(o1.value(0))>::type > __device__ __host__ inline r_type value(size_t k) const\
45  {\
46  return fun_base(o1.value(k));\
47  }\
48 \
49  template <typename T> __device__ __host__ operator T() const\
50  {\
51  init();\
52  return fun_base(o1.value(0));\
53  }\
54 };
55 
62 #define CREATE_ARG_FUNC(fun_base,fun_name,OP_ID) \
63 \
64 template<typename orig,typename exp1, typename exp_2, unsigned int op1>\
65 __device__ __host__ inline point_expression_op<orig,point_expression_op<orig,exp1,exp_2,op1>,void, OP_ID >\
66 fun_name(const point_expression_op<orig,exp1,exp_2,op1> & va)\
67 {\
68  point_expression_op<orig,point_expression_op<orig,exp1,exp_2,op1>,void,OP_ID> exp_sum(va);\
69 \
70  return exp_sum;\
71 }\
72 \
73 \
74 template<unsigned int dim, typename T>\
75 __device__ __host__ inline point_expression_op<Point<dim,T>,Point<dim,T>,void, OP_ID >\
76 fun_name(const Point<dim,T> & va)\
77 {\
78  point_expression_op<Point<dim,T>,Point<dim,T>,void,OP_ID> exp_sum(va);\
79 \
80  return exp_sum;\
81 }
82 
91 template <typename orig, typename exp1, typename exp2>
92 class point_expression_op<orig,exp1,exp2,POINT_NORM_INF>
93 {
94  const exp1 o1;
95 
97  mutable typename std::remove_const<typename orig::coord_type>::type scal;
98 
99 public:
100 
102  typedef orig orig_type;
103 
105  typedef int is_expression;
106 
108  typedef int has_init;
109 
111  typedef typename orig::coord_type return_type;
112 
114  static const unsigned int nvals = 1;
115 
116  typedef typename exp1::coord_type coord_type;
117 
119  __device__ __host__ inline explicit point_expression_op(const exp1 & o1)
120  :o1(o1),scal(0.0)
121  {}
122 
128  __device__ __host__ inline void init() const
129  {
130  scal = 0.0;
131 
132  for (size_t i = 0 ; i < orig::dims ; i++) {
133  if (fabs(o1.value(i)) > scal) {
134  scal=fabs(o1.value(i));
135  }
136  }
137  //scal += o1.value(i) * o1.value(i);
138 
139  //scal = sqrt(scal);
140 
141  o1.init();
142  }
143 
149  template<typename r_type=typename std::remove_reference<decltype(o1.value(0))>::type > __device__ __host__ inline r_type value(size_t k) const
150  {
151  return scal;
152  }
153 
154  template <typename T>__device__ __host__ operator T() const
155  {
156  init();
157  return scal;
158  }
159 };
160 
161 
170 template <typename orig, typename exp1, typename exp2>
172 {
173  const exp1 o1;
174 
176  mutable typename std::remove_const<typename orig::coord_type>::type scal;
177 
178 public:
179 
181  typedef orig orig_type;
182 
184  typedef int is_expression;
185 
187  typedef int has_init;
188 
190  typedef typename orig::coord_type return_type;
191 
193  static const unsigned int nvals = 1;
194 
195  typedef typename exp1::coord_type coord_type;
196 
198  __device__ __host__ inline explicit point_expression_op(const exp1 & o1)
199  :o1(o1),scal(0.0)
200  {}
201 
207  __device__ __host__ inline void init() const
208  {
209  scal = 0.0;
210 
211  for (size_t i = 0 ; i < orig::dims ; i++)
212  scal += o1.value(i) * o1.value(i);
213 
214  scal = sqrt(scal);
215 
216  o1.init();
217  }
218 
224  template<typename r_type=typename std::remove_reference<decltype(o1.value(0))>::type > __device__ __host__ inline r_type value(size_t k) const
225  {
226  return scal;
227  }
228 
229  template <typename T>__device__ __host__ operator T() const
230  {
231  init();
232  return scal;
233  }
234 };
235 
244 template <typename orig, typename exp1, typename exp2>
246 {
248  const exp1 o1;
249 
251  mutable typename std::remove_const<typename orig::coord_type>::type scal;
252 
253 public:
254 
255  typedef orig orig_type;
256 
258  typedef int has_init;
259 
261  typedef int is_expression;
262 
264  typedef typename orig::coord_type return_type;
265 
267  static const unsigned int nvals = 1;
268 
269  typedef typename exp1::coord_type coord_type;
270 
272  __device__ __host__ inline explicit point_expression_op(const exp1 & o1)
273  :o1(o1),scal(0.0)
274  {}
275 
281  __device__ __host__ inline void init() const
282  {
283  scal = 0.0;
284 
285  for (size_t i = 0 ; i < orig::dims ; i++)
286  scal += o1.value(i) * o1.value(i);
287 
288  o1.init();
289  }
290 
296  template<typename r_type=typename std::remove_reference<decltype(o1.value(0))>::type > __device__ __host__ inline r_type value(size_t k) const
297  {
298  return scal;
299  }
300 
302  template <typename T> __device__ __host__ operator T() const
303  {
304  init();
305  return scal;
306  }
307 };
308 
309 
310 CREATE_ARG_FUNC_CLASS(std::abs,abs,POINT_ABS)
311 CREATE_ARG_FUNC_CLASS(std::exp,exp,POINT_EXP)
312 CREATE_ARG_FUNC_CLASS(std::exp2,exp2,POINT_EXP2)
313 CREATE_ARG_FUNC_CLASS(std::expm1,expm1,POINT_EXPM1)
314 CREATE_ARG_FUNC_CLASS(std::log,log,POINT_LOG)
315 CREATE_ARG_FUNC_CLASS(std::log10,log10,POINT_LOG10)
316 CREATE_ARG_FUNC_CLASS(std::log2,log2,POINT_LOG2)
317 CREATE_ARG_FUNC_CLASS(std::log1p,log1p,POINT_LOG1P)
318 CREATE_ARG_FUNC_CLASS(std::sqrt,sqrt,POINT_SQRT)
319 CREATE_ARG_FUNC_CLASS(std::cbrt,cbrt,POINT_CBRT)
320 CREATE_ARG_FUNC_CLASS(std::sin,sin,POINT_SIN)
321 CREATE_ARG_FUNC_CLASS(std::cos,cos,POINT_COS)
322 CREATE_ARG_FUNC_CLASS(std::tan,tan,POINT_TAN)
323 CREATE_ARG_FUNC_CLASS(std::asin,asin,POINT_ASIN)
324 CREATE_ARG_FUNC_CLASS(std::acos,acos,POINT_ACOS)
325 CREATE_ARG_FUNC_CLASS(std::atan,atan,POINT_ATAN)
326 CREATE_ARG_FUNC_CLASS(std::sinh,sinh,POINT_SINH)
327 CREATE_ARG_FUNC_CLASS(std::cosh,cosh,POINT_COSH)
328 CREATE_ARG_FUNC_CLASS(std::tanh,tanh,POINT_TANH)
329 CREATE_ARG_FUNC_CLASS(std::asinh,asinh,POINT_ASINH)
330 CREATE_ARG_FUNC_CLASS(std::acosh,acosh,POINT_ACOSH)
331 CREATE_ARG_FUNC_CLASS(std::atanh,atanh,POINT_ATANH)
332 CREATE_ARG_FUNC_CLASS(std::erf,erf,POINT_ERF)
333 CREATE_ARG_FUNC_CLASS(std::erfc,erfc,POINT_ERFC)
334 CREATE_ARG_FUNC_CLASS(std::tgamma,tgamma,POINT_TGAMMA)
335 CREATE_ARG_FUNC_CLASS(std::lgamma,lgamma,POINT_LGAMMA)
336 CREATE_ARG_FUNC_CLASS(std::ceil,ceil,POINT_CEIL)
337 CREATE_ARG_FUNC_CLASS(std::floor,floor,POINT_FLOOR)
338 CREATE_ARG_FUNC_CLASS(std::trunc,trunc,POINT_TRUNC)
339 CREATE_ARG_FUNC_CLASS(std::round,round,POINT_ROUND)
340 CREATE_ARG_FUNC_CLASS(std::nearbyint,nearbyint,POINT_NEARBYINT)
341 CREATE_ARG_FUNC_CLASS(std::rint,rint,POINT_RINT)
342 
343 
345 /* \brief Calculate the norm of the point
346  *
347  * \param va point expression one
348  *
349  * \return an object that encapsulate the expression
350  *
351  */
352 template<typename orig,typename exp1, typename exp2, unsigned int op1>
353 __device__ __host__ inline point_expression_op<orig,point_expression_op<orig,exp1,exp2,op1>,void,POINT_NORM>
355 {
357 
358  return exp_sum;
359 }
360 
361 
362 /* \brief norm of a scalar
363  *
364  * \param d scalar double or float
365  *
366  * \return d
367  *
368  */
369 template <typename T>
370 __device__ __host__ T norm(T d)
371 {
372  return d;
373 }
374 
375 /* \brief Calculate the norm of the point
376  *
377  * \param va point expression one
378  *
379  * \return an object that encapsulate the expression
380  *
381  */
382 template<typename T, typename vmpl>
383 __device__ __host__ inline point_expression_op<Point<subar_dim<vmpl>::type::value,T>,
385  void,
386  POINT_NORM>
388 {
391  void,
393 
394  return exp_sum;
395 }
396 
397 /* \brief Calculate the norm of the point
398  *
399  * \param va point expression one
400  *
401  * \return an object that encapsulate the expression
402  *
403  */
404 template<unsigned int dim, typename T>
405 __device__ __host__ inline point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM>
406 norm(const point_expression<T[dim]> & d)
407 {
408  point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM> exp_sum( (point_expression<T[dim]>(d)) );
409 
410  return exp_sum;
411 }
412 
413 
414 /* \brief Divide two points expression
415  *
416  * \param va point expression one
417  * \param vb point expression two
418  *
419  * \return an object that encapsulate the expression
420  *
421  */
422 template<unsigned int dim, typename T>
423 __device__ __host__ inline point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM>
424 norm(const Point<dim,T> & va)
425 {
426  point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM> exp_sum(va);
427 
428  return exp_sum;
429 }
430 
431 
433 
434 /* \brief Calculate the norm of the point
435  *
436  * \param va point expression one
437  *
438  * \return an object that encapsulate the expression
439  *
440  */
441 template<typename orig,typename exp1, typename exp2, unsigned int op1>
442 __device__ __host__ inline point_expression_op<orig,point_expression_op<orig,exp1,exp2,op1>,void,POINT_NORM2>
444 {
446 
447  return exp_sum;
448 }
449 
450 
451 /* \brief norm of a scalar
452  *
453  * \param d scalar double or float
454  *
455  * \return d
456  *
457  */
458 template <typename T>__device__ __host__ T norm2(T d)
459 {
460  return d*d;
461 }
462 
463 
464 /* \brief Calculate the norm of the point
465  *
466  * \param va point expression one
467  *
468  * \return an object that encapsulate the expression
469  *
470  */
471 template<unsigned int dim, typename T>
472 __device__ __host__ inline point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM2>
473 norm2(const point_expression<T[dim]> & d)
474 {
475  point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM2> exp_sum( (point_expression<T[dim]>(d)) );
476 
477  return exp_sum;
478 }
479 
480 
481 /* \brief Divide two points expression
482  *
483  * \param va point expression one
484  * \param vb point expression two
485  *
486  * \return an object that encapsulate the expression
487  *
488  */
489 template<unsigned int dim, typename T>
490 __device__ __host__ inline point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM2>
491 norm2(const Point<dim,T> & va)
492 {
493  point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM2> exp_sum(va);
494 
495  return exp_sum;
496 }
497 
498 
503 template <typename T, typename P> __device__ __host__ auto distance(T exp1, P exp2) -> decltype(norm(exp1 - exp2))
504 {
505  return norm(exp1 - exp2);
506 }
507 
508 
509 
511 
512 /* \brief Calculate the norm of the point
513  *
514  * \param va point expression one
515  *
516  * \return an object that encapsulate the expression
517  *
518  */
519 template<typename orig,typename exp1, typename exp2, unsigned int op1>
520 __device__ __host__ inline point_expression_op<orig,point_expression_op<orig,exp1,exp2,op1>,void,POINT_NORM_INF>
521 norm_inf(const point_expression_op<orig,exp1,exp2,op1> & va)
522 {
524 
525  return exp_sum;
526 }
527 
528 
529 /* \brief norm of a scalar
530  *
531  * \param d scalar double or float
532  *
533  * \return d
534  *
535  */
536 template <typename T>__device__ __host__ T norm_inf(T d)
537 {
538  return abs(d);
539 }
540 
541 
542 /* \brief Calculate the norm of the point
543  *
544  * \param va point expression one
545  *
546  * \return an object that encapsulate the expression
547  *
548  */
549 template<unsigned int dim, typename T>
550 __device__ __host__ inline point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM_INF>
551 norm_inf(const point_expression<T[dim]> & d)
552 {
553  point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM_INF> exp_sum( (point_expression<T[dim]>(d)) );
554 
555  return exp_sum;
556 }
557 
558 
559 /* \brief Divide two points expression
560  *
561  * \param va point expression one
562  * \param vb point expression two
563  *
564  * \return an object that encapsulate the expression
565  *
566  */
567 template<unsigned int dim, typename T>
568 __device__ __host__ inline point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM_INF>
569 norm_inf(const Point<dim,T> & va)
570 {
571  point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM_INF> exp_sum(va);
572 
573  return exp_sum;
574 }
575 
576 
581 template <typename T, typename P> __device__ __host__ auto distance_inf(T exp1, P exp2) -> decltype(norm_inf(exp1 - exp2))
582 {
583  return norm_inf(exp1 - exp2);
584 }
585 
586 
588 
589 CREATE_ARG_FUNC(std::abs,abs,POINT_ABS)
590 CREATE_ARG_FUNC(std::exp,exp,POINT_EXP)
591 CREATE_ARG_FUNC(std::exp2,exp2,POINT_EXP2)
592 CREATE_ARG_FUNC(std::expm1,expm1,POINT_EXPM1)
593 CREATE_ARG_FUNC(std::log,log,POINT_LOG)
594 CREATE_ARG_FUNC(std::log10,log10,POINT_LOG10)
595 CREATE_ARG_FUNC(std::log2,log2,POINT_LOG2)
596 CREATE_ARG_FUNC(std::log1p,log1p,POINT_LOG1P)
597 CREATE_ARG_FUNC(std::sqrt,sqrt,POINT_SQRT)
598 CREATE_ARG_FUNC(std::cbrt,cbrt,POINT_CBRT)
599 CREATE_ARG_FUNC(std::sin,sin,POINT_SIN)
600 CREATE_ARG_FUNC(std::cos,cos,POINT_COS)
601 CREATE_ARG_FUNC(std::tan,tan,POINT_TAN)
602 CREATE_ARG_FUNC(std::asin,asin,POINT_ASIN)
603 CREATE_ARG_FUNC(std::acos,acos,POINT_ACOS)
604 CREATE_ARG_FUNC(std::atan,atan,POINT_ATAN)
605 CREATE_ARG_FUNC(std::sinh,sinh,POINT_SINH)
606 CREATE_ARG_FUNC(std::cosh,cosh,POINT_COSH)
607 CREATE_ARG_FUNC(std::tanh,tanh,POINT_TANH)
608 CREATE_ARG_FUNC(std::asinh,asinh,POINT_ASINH)
609 CREATE_ARG_FUNC(std::acosh,acosh,POINT_ACOSH)
610 CREATE_ARG_FUNC(std::atanh,atanh,POINT_ATANH)
611 CREATE_ARG_FUNC(std::erf,erf,POINT_ERF)
612 CREATE_ARG_FUNC(std::erfc,erfc,POINT_ERFC)
613 CREATE_ARG_FUNC(std::tgamma,tgamma,POINT_TGAMMA)
614 CREATE_ARG_FUNC(std::lgamma,lgamma,POINT_LGAMMA)
615 CREATE_ARG_FUNC(std::ceil,ceil,POINT_CEIL)
616 CREATE_ARG_FUNC(std::floor,floor,POINT_FLOOR)
617 CREATE_ARG_FUNC(std::trunc,trunc,POINT_TRUNC)
618 CREATE_ARG_FUNC(std::round,round,POINT_ROUND)
619 CREATE_ARG_FUNC(std::nearbyint,nearbyint,POINT_NEARBYINT)
620 CREATE_ARG_FUNC(std::rint,rint,POINT_RINT)
621 
622 
623 #endif /* OPENFPM_DATA_SRC_SPACE_SHAPE_POINT_OPERATORS_FUNCTIONS_HPP_ */
__device__ __host__ point_expression_op(const exp1 &o1)
Constructor from expression.
int is_expression
indicate that this class encapsulate an expression
orig::coord_type return_type
return type of the expression
Specialization for an array of dimension dim as expression.
orig::coord_type return_type
return type of the expression
__device__ __host__ void init() const
This function must be called before value.
int has_init
indicate that init must be called before value
Unknown operation specialization.
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:27
Main class that encapsulate a constant number used in a point expression.
int has_init
indicate that init must be called before value
int is_expression
indicate that this class encapsulate an expression
int is_expression
indicate that this class encapsulate an expression
__device__ __host__ point_expression_op(const exp1 &o1)
Constructor from expression.
std::remove_const< typename orig::coord_type >::type scal
result of the expression
__device__ __host__ void init() const
This function must be called before value.
__device__ __host__ r_type value(size_t k) const
Evaluate the expression.
std::remove_const< typename orig::coord_type >::type scal
Scalar value.
__device__ __host__ r_type value(size_t k) const
Evaluate the expression.
__device__ __host__ void init() const
This function must be called before value.
orig::coord_type return_type
return type of the expression
int has_init
indicate that init must be called before value
OutputIteratorT OffsetT ReductionOpT OuputT init
< [in] The initial value of the reduction
Test structure used for several test.
Definition: Point_test.hpp:105
__device__ __host__ r_type value(size_t k) const
Evaluate the expression.
std::remove_const< typename orig::coord_type >::type scal
Scalar value.
__device__ __host__ point_expression_op(const exp1 &o1)
constructor from an expression