8#ifndef OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_FUNCTIONS_HPP_
9#define OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_FUNCTIONS_HPP_
12#include "cuda/vector_dist_operators_cuda.cuh"
22#define CREATE_VDIST_ARG_FUNC(fun_base,fun_name,OP_ID) \
25template <typename exp1>\
26class vector_dist_expression_op<exp1,void,OP_ID>\
32 typedef typename exp1::is_ker is_ker;\
34 typedef typename vector_result<typename exp1::vtype,void>::type vtype;\
36 typedef typename vector_is_sort_result<exp1::is_sort::value,false>::type is_sort;\
38 typedef typename nn_type_result<typename exp1::NN_type,void>::type NN_type;\
40 vector_dist_expression_op(const exp1 & o1)\
44 inline NN_type * getNN()\
46 return nn_type_result<typename exp1::NN_type,void>::getNN(o1);\
49 const vtype & getVector()\
51 return vector_result<typename exp1::vtype,void>::getVector(o1);\
54 inline const exp1 & getExpr() const\
59 inline void init() const\
64 template<typename r_type=typename std::remove_reference<decltype(fun_base(o1.value(vect_dist_key_dx(0))))>::type > \
65 __device__ __host__ inline r_type value(const vect_dist_key_dx & key) const\
67 return fun_base(o1.value(key));\
70 template<typename r_type=typename std::remove_reference<decltype(fun_base(o1.value(vect_dist_key_dx(0))))>::type > \
71 __device__ __host__ inline r_type value(const unsigned int & key) const\
73 return fun_base(o1.value(key));\
76 const vtype & getVector() const\
78 return o1.getVector();\
84template<typename exp1, typename exp2_, unsigned int op1>\
85inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2_,op1>,void,OP_ID>\
86fun_name(const vector_dist_expression_op<exp1,exp2_,op1> & va)\
88 vector_dist_expression_op<vector_dist_expression_op<exp1,exp2_,op1>,void,OP_ID> exp_sum(va);\
94template<unsigned int prp1, typename v1>\
95inline vector_dist_expression_op<vector_dist_expression<prp1,v1>,void,OP_ID>\
96fun_name(const vector_dist_expression<prp1,v1> & va)\
98 vector_dist_expression_op<vector_dist_expression<prp1,v1>,void,OP_ID> exp_sum(va);\
103CREATE_VDIST_ARG_FUNC(norm,norm,VECT_NORM)
104CREATE_VDIST_ARG_FUNC(norm2,norm2,VECT_NORM2)
105CREATE_VDIST_ARG_FUNC(abs,abs,POINT_ABS)
106CREATE_VDIST_ARG_FUNC(exp,exp,POINT_EXP)
107CREATE_VDIST_ARG_FUNC(exp2,exp2,POINT_EXP2)
108CREATE_VDIST_ARG_FUNC(expm1,expm1,POINT_EXPM1)
109CREATE_VDIST_ARG_FUNC(log,log,POINT_LOG)
110CREATE_VDIST_ARG_FUNC(log10,log10,POINT_LOG10)
111CREATE_VDIST_ARG_FUNC(log2,log2,POINT_LOG2)
112CREATE_VDIST_ARG_FUNC(log1p,log1p,POINT_LOG1P)
113CREATE_VDIST_ARG_FUNC(sqrt,sqrt,POINT_SQRT)
114CREATE_VDIST_ARG_FUNC(cbrt,cbrt,POINT_CBRT)
115CREATE_VDIST_ARG_FUNC(sin,sin,POINT_SIN)
116CREATE_VDIST_ARG_FUNC(cos,cos,POINT_COS)
117CREATE_VDIST_ARG_FUNC(tan,tan,POINT_TAN)
118CREATE_VDIST_ARG_FUNC(asin,asin,POINT_ASIN)
119CREATE_VDIST_ARG_FUNC(acos,acos,POINT_ACOS)
120CREATE_VDIST_ARG_FUNC(atan,atan,POINT_ATAN)
121CREATE_VDIST_ARG_FUNC(sinh,sinh,POINT_SINH)
122CREATE_VDIST_ARG_FUNC(cosh,cosh,POINT_COSH)
123CREATE_VDIST_ARG_FUNC(tanh,tanh,POINT_TANH)
124CREATE_VDIST_ARG_FUNC(asinh,asinh,POINT_ASINH)
125CREATE_VDIST_ARG_FUNC(acosh,acosh,POINT_ACOSH)
126CREATE_VDIST_ARG_FUNC(atanh,atanh,POINT_ATANH)
127CREATE_VDIST_ARG_FUNC(erf,erf,POINT_ERF)
128CREATE_VDIST_ARG_FUNC(erfc,erfc,POINT_ERFC)
129CREATE_VDIST_ARG_FUNC(tgamma,tgamma,POINT_TGAMMA)
130CREATE_VDIST_ARG_FUNC(lgamma,lgamma,POINT_LGAMMA)
131CREATE_VDIST_ARG_FUNC(ceil,ceil,POINT_CEIL)
132CREATE_VDIST_ARG_FUNC(floor,floor,POINT_FLOOR)
133CREATE_VDIST_ARG_FUNC(trunc,trunc,POINT_TRUNC)
134CREATE_VDIST_ARG_FUNC(round,round,POINT_ROUND)
135CREATE_VDIST_ARG_FUNC(nearbyint,nearbyint,POINT_NEARBYINT)
136CREATE_VDIST_ARG_FUNC(rint,rint,POINT_RINT)
145#define CREATE_VDIST_ARG2_FUNC(fun_base,fun_name,OP_ID) \
148template <typename exp1,typename exp2>\
149class vector_dist_expression_op<exp1,exp2,OP_ID>\
156 typedef std::integral_constant<bool,exp1::is_ker::value || exp1::is_ker::value> is_ker;\
158 typedef typename vector_result<typename exp1::vtype,typename exp2::vtype>::type vtype;\
160 typedef typename vector_is_sort_result<exp1::is_sort::value,exp2::is_sort::value>::type is_sort;\
162 typedef typename nn_type_result<typename exp1::NN_type,typename exp2::NN_type>::type NN_type;\
164 inline NN_type * getNN()\
166 return nn_type_result<typename exp1::NN_type,typename exp2::NN_type>::getNN(o1,o2);\
171 return vector_result<typename exp1::vtype,typename exp2::vtype>::getVector(o1,o2);\
173 const vtype & getVector() const\
175 return vector_result<typename exp1::vtype,typename exp2::vtype>::getVector(o1,o2);\
179 vector_dist_expression_op(const exp1 & o1, const exp2 & o2)\
183 inline void init() const\
189 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\
191 return fun_base(o1.value(key),o2.value(key));\
196template<unsigned int p1, unsigned int p2, typename v1, typename v2>\
197inline vector_dist_expression_op<vector_dist_expression<p1,v1>,vector_dist_expression<p2,v2>,OP_ID>\
198fun_name(const vector_dist_expression<p1,v1> & va, const vector_dist_expression<p2,v2> & vb)\
200 vector_dist_expression_op<vector_dist_expression<p1,v1>,vector_dist_expression<p2,v2>,OP_ID> exp_sum(va,vb);\
205template<typename exp1 , typename exp2, unsigned int op1, unsigned int prp1, typename v1>\
206inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression<prp1,v1>,OP_ID>\
207fun_name(const vector_dist_expression_op<exp1,exp2,op1> & va, const vector_dist_expression<prp1,v1> & vb)\
209 vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression<prp1,v1>,OP_ID> exp_sum(va,vb);\
214template<typename exp1 , typename exp2, unsigned int op1, unsigned int prp1, typename v1>\
215inline vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_dist_expression_op<exp1,exp2,op1>,OP_ID>\
216fun_name(const vector_dist_expression<prp1,v1> & va, const vector_dist_expression_op<exp1,exp2,op1> & vb)\
218 vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_dist_expression_op<exp1,exp2,op1>,OP_ID> exp_sum(va,vb);\
223template<typename exp1 , typename exp2, unsigned int op1, typename exp3 , typename exp4, unsigned int op2>\
224inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression_op<exp3,exp4,op2>,OP_ID>\
225fun_name(const vector_dist_expression_op<exp1,exp2,op1> & va, const vector_dist_expression_op<exp3,exp4,op2> & vb)\
227 vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression_op<exp3,exp4,op2>,OP_ID> exp_sum(va,vb);\
232template<unsigned int prp1 , typename v1>\
233inline vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_dist_expression<0,double>,OP_ID>\
234fun_name(const vector_dist_expression<prp1,v1> & va, double d)\
236 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));\
241template<unsigned int prp1 , typename v1>\
242inline vector_dist_expression_op<vector_dist_expression<0,double>,vector_dist_expression<prp1,v1>,OP_ID>\
243fun_name(double d, const vector_dist_expression<prp1,v1> & vb)\
245 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);\
250template<typename exp1 , typename exp2, unsigned int op1>\
251inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression<0,double>,OP_ID>\
252fun_name(const vector_dist_expression_op<exp1,exp2,op1> & va, double d)\
254 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));\
259template<typename exp1 , typename exp2, unsigned int op1>\
260inline vector_dist_expression_op<vector_dist_expression<0,double>,vector_dist_expression_op<exp1,exp2,op1>,OP_ID>\
261fun_name(double d, const vector_dist_expression_op<exp1,exp2,op1> & va)\
263 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);\
268CREATE_VDIST_ARG2_FUNC(pmul,pmul,VECT_PMUL)
272template<typename val_type, bool is_sort, bool is_scalar = is_Point<val_type>::type::value>
277 template<
typename vector_type,
typename expression>
278 static void process(val_type & val,
vector_type & ve, expression & o1)
282 auto vek = ve.toKernel();
285 exp_tmp2[0].resize(
sizeof(val_type));
287 auto & v_cl = create_vcluster<CudaMemory>();
289 openfpm::reduce((val_type *)ve.template getDeviceBuffer<0>(), ve.size(), (val_type *)(exp_tmp2[0].getDevicePointer()),
gpu::plus_t<val_type>(), v_cl.getgpuContext());
291 exp_tmp2[0].deviceToHost();
293 val = *(val_type *)(exp_tmp2[0].getPointer());
295 std::cout << __FILE__ <<
":" << __LINE__ <<
" error: to make expression work on GPU the file must be compiled on GPU" << std::endl;
300template<
typename val_type,
bool is_sort>
303 typedef val_type type;
305 template<
typename vector_type,
typename expression>
306 static void process(val_type & val,
vector_type & ve, expression & o1)
314 auto vek = ve.toKernel();
317 exp_tmp2[0].resize(
sizeof(val_type));
321 auto & v_cl = create_vcluster<CudaMemory>();
323 for (
size_t i = 0 ; i < val_type::dims ; i++)
325 openfpm::reduce(&((
typename val_type::coord_type *)ve.template getDeviceBuffer<0>())[offset],
327 (
typename val_type::coord_type *)(exp_tmp2[0].getDevicePointer()),
329 v_cl.getgpuContext());
331 exp_tmp2[0].deviceToHost();
333 val.get(i) = *(
typename val_type::coord_type *)exp_tmp2[0].getPointer();
335 offset += ve.capacity();
339 std::cout << __FILE__ <<
":" << __LINE__ <<
" error: to make expression work on GPU the file must be compiled on GPU" << std::endl;
345template<
bool is_device>
348 template<
typename is_sort,
typename o1_type,
typename val_type>
349 static void red(o1_type & o1, val_type & val)
361 auto & orig_v = o1.getVector();
363 if (exp_tmp.ref() == 0)
366 ve.setMemory(exp_tmp);
367 ve.resize(orig_v.size_local());
371 std::cout << __FILE__ <<
":" << __LINE__ <<
" error, to use expression on GPU you must compile with nvcc compiler " << std::endl;
379 template<
typename is_sort,
typename o1_type,
typename val_type>
380 static void red(o1_type & o1, val_type & val)
382 const auto & orig_v = o1.getVector();
388 auto it = orig_v.getDomainIterator();
394 val += o1.value(key);
407template <
typename exp1>
418 mutable typename std::remove_reference<rtype>::type
val;
426 typedef typename vector_result<typename exp1::vtype,void>::type
vtype;
429 typedef typename vector_is_sort_result<exp1::is_sort::value,false>::type
is_sort;
432 typedef typename nn_type_result<typename exp1::NN_type,void>::type
NN_type;
457 inline typename std::remove_reference<rtype>::type
get()
464 template<typename r_type= typename std::remove_reference<rtype>::type >
479 return o1.getVector();
491 return o1.getVector();
496template<
typename exp1,
typename exp2_,
unsigned int op1>
506template<
unsigned int prp1,
typename v1>
521 template <
typename T,
typename P>
auto distance(T exp1,
P exp2) ->
decltype(norm(exp1 - exp2))
523 return norm(exp1 - exp2);
534template <
typename exp1>
545 mutable typename std::remove_reference<rtype>::type
val;
553 typedef typename vector_result<typename exp1::vtype,void>::type
vtype;
556 typedef typename vector_is_sort_result<exp1::is_sort::value,false>::type
is_sort;
559 typedef typename nn_type_result<typename exp1::NN_type,void>::type
NN_type;
570 if (exp1::is_ker::value ==
true)
574 typedef decltype(val) val_type;
583 auto & orig_v = o1.getVector();
585 if (exp_tmp.ref() == 0)
588 ve.setMemory(exp_tmp);
589 ve.resize(orig_v.size_local());
593 std::cout << __FILE__ <<
":" << __LINE__ <<
" error, to use expression on GPU you must compile with nvcc compiler " << std::endl;
598 const auto & orig_v = o1.getVector();
604 auto it = orig_v.getDomainIterator();
609 if(fabs(o1.value(key))>val) {
610 val = fabs(o1.value(key));
628 inline typename std::remove_reference<rtype>::type
get()
635 template<typename r_type= typename std::remove_reference<rtype>::type >
inline r_type
value(
const vect_dist_key_dx & key)
const
641 template<typename r_type= typename std::remove_reference<rtype>::type >
inline r_type
getReduction()
const
655 return o1.getVector();
667 return o1.getVector();
672template<
typename exp1,
typename exp2_,
unsigned int op1>
682template<
unsigned int prp1,
typename v1>
Test structure used for several test.
Grow policy define how the vector should grow every time we exceed the size.
Implementation of 1-D std::vector like structure.
Grid key for a distributed grid.
vector_result< typenameexp1::vtype, void >::type vtype
return the vector type on which this expression operate
vtype & getVector()
Return the vector on which is acting.
void init() const
sum reduction require initialization where we calculate the reduction
const vtype & getVector() const
Return the vector on which is acting.
r_type getReduction() const
it return the result of the expression for ODEINT
const exp1 o1
expression on which apply the reduction
std::remove_reference< rtype >::type val
return type of the calculated value (without reference)
vector_dist_expression_op(const exp1 &o1)
constructor from an epxression exp1 and a vector vd
exp1::is_ker is_ker
Indicate if it is an in kernel expression.
NN_type * getNN() const
get the NN object
std::remove_reference< rtype >::type get()
it return the result of the expression
r_type value(const vect_dist_key_dx &key) const
it return the result of the expression (precalculated before)
vector_is_sort_result< exp1::is_sort::value, false >::type is_sort
result for is sort
apply_kernel_rtype< decltype(o1.value(vect_dist_key_dx()))>::rtype rtype
return type of this expression
nn_type_result< typenameexp1::NN_type, void >::type NN_type
NN_type.
apply_kernel_rtype< decltype(o1.value(vect_dist_key_dx()))>::rtype rtype
return type of this expression
std::remove_reference< rtype >::type get()
it return the result of the expression
void init() const
sum reduction require initialization where we calculate the reduction
const exp1 o1
expression on which apply the reduction
vector_is_sort_result< exp1::is_sort::value, false >::type is_sort
result for is sort
vector_dist_expression_op(const exp1 &o1)
constructor from an epxression exp1 and a vector vd
nn_type_result< typenameexp1::NN_type, void >::type NN_type
NN_type.
__device__ __host__ r_type value(const vect_dist_key_dx &key) const
it return the result of the expression (precalculated before)
const vtype & getVector() const
Return the vector on which is acting.
vector_result< typenameexp1::vtype, void >::type vtype
return the vector type on which this expression operate
NN_type * getNN() const
get the NN object
std::remove_reference< rtype >::type val
return type of the calculated value (without reference)
vtype & getVector()
Return the vector on which is acting.
exp1::is_ker is_ker
Indicate if it is an in kernel expression.
Unknown operation specialization.
Main class that encapsulate a vector properties operand to be used for expressions construction.
convert a type into constant type
auto distance(T exp1, P exp2) -> decltype(norm(exp1 - exp2))
General distance formula.
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
It give the return type of the expression if applicable.
Transform the boost::fusion::vector into memory specification (memory_traits)
temporal buffer for reductions