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) \
25 template <typename exp1>\
26 class 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 nn_type_result<typename exp1::NN_type,void>::type NN_type;\
38 vector_dist_expression_op(const exp1 & o1)\
42 inline NN_type * getNN()\
44 return nn_type_result<typename exp1::NN_type,void>::getNN(o1);\
47 const vtype & getVector()\
49 return vector_result<typename exp1::vtype,void>::getVector(o1);\
52 inline const exp1 & getExpr() const\
57 inline void init() const\
62 template<typename r_type=typename std::remove_reference<decltype(fun_base(o1.value(vect_dist_key_dx(0))))>::type > \
63 __device__ __host__ inline r_type value(const vect_dist_key_dx & key) const\
65 return fun_base(o1.value(key));\
68 template<typename r_type=typename std::remove_reference<decltype(fun_base(o1.value(vect_dist_key_dx(0))))>::type > \
69 __device__ __host__ inline r_type value(const unsigned int & key) const\
71 return fun_base(o1.value(key));\
74 const vtype & getVector() const\
76 return o1.getVector();\
82 template<typename exp1, typename exp2_, unsigned int op1>\
83 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2_,op1>,void,OP_ID>\
84 fun_name(const vector_dist_expression_op<exp1,exp2_,op1> & va)\
86 vector_dist_expression_op<vector_dist_expression_op<exp1,exp2_,op1>,void,OP_ID> exp_sum(va);\
92 template<unsigned int prp1, typename v1>\
93 inline vector_dist_expression_op<vector_dist_expression<prp1,v1>,void,OP_ID>\
94 fun_name(const vector_dist_expression<prp1,v1> & va)\
96 vector_dist_expression_op<vector_dist_expression<prp1,v1>,void,OP_ID> exp_sum(va);\
101 CREATE_VDIST_ARG_FUNC(norm,norm,VECT_NORM)
102 CREATE_VDIST_ARG_FUNC(norm2,norm2,VECT_NORM2)
103 CREATE_VDIST_ARG_FUNC(abs,abs,POINT_ABS)
104 CREATE_VDIST_ARG_FUNC(exp,exp,POINT_EXP)
105 CREATE_VDIST_ARG_FUNC(exp2,exp2,POINT_EXP2)
106 CREATE_VDIST_ARG_FUNC(expm1,expm1,POINT_EXPM1)
107 CREATE_VDIST_ARG_FUNC(log,log,POINT_LOG)
108 CREATE_VDIST_ARG_FUNC(log10,log10,POINT_LOG10)
109 CREATE_VDIST_ARG_FUNC(log2,log2,POINT_LOG2)
110 CREATE_VDIST_ARG_FUNC(log1p,log1p,POINT_LOG1P)
111 CREATE_VDIST_ARG_FUNC(sqrt,sqrt,POINT_SQRT)
112 CREATE_VDIST_ARG_FUNC(cbrt,cbrt,POINT_CBRT)
113 CREATE_VDIST_ARG_FUNC(sin,sin,POINT_SIN)
114 CREATE_VDIST_ARG_FUNC(cos,cos,POINT_COS)
115 CREATE_VDIST_ARG_FUNC(tan,tan,POINT_TAN)
116 CREATE_VDIST_ARG_FUNC(asin,asin,POINT_ASIN)
117 CREATE_VDIST_ARG_FUNC(acos,acos,POINT_ACOS)
118 CREATE_VDIST_ARG_FUNC(atan,atan,POINT_ATAN)
119 CREATE_VDIST_ARG_FUNC(sinh,sinh,POINT_SINH)
120 CREATE_VDIST_ARG_FUNC(cosh,cosh,POINT_COSH)
121 CREATE_VDIST_ARG_FUNC(tanh,tanh,POINT_TANH)
122 CREATE_VDIST_ARG_FUNC(asinh,asinh,POINT_ASINH)
123 CREATE_VDIST_ARG_FUNC(acosh,acosh,POINT_ACOSH)
124 CREATE_VDIST_ARG_FUNC(atanh,atanh,POINT_ATANH)
125 CREATE_VDIST_ARG_FUNC(erf,erf,POINT_ERF)
126 CREATE_VDIST_ARG_FUNC(erfc,erfc,POINT_ERFC)
127 CREATE_VDIST_ARG_FUNC(tgamma,tgamma,POINT_TGAMMA)
128 CREATE_VDIST_ARG_FUNC(lgamma,lgamma,POINT_LGAMMA)
129 CREATE_VDIST_ARG_FUNC(ceil,ceil,POINT_CEIL)
130 CREATE_VDIST_ARG_FUNC(floor,floor,POINT_FLOOR)
131 CREATE_VDIST_ARG_FUNC(trunc,trunc,POINT_TRUNC)
132 CREATE_VDIST_ARG_FUNC(round,round,POINT_ROUND)
133 CREATE_VDIST_ARG_FUNC(nearbyint,nearbyint,POINT_NEARBYINT)
134 CREATE_VDIST_ARG_FUNC(rint,rint,POINT_RINT)
143 #define CREATE_VDIST_ARG2_FUNC(fun_base,fun_name,OP_ID) \
146 template <typename exp1,typename exp2>\
147 class vector_dist_expression_op<exp1,exp2,OP_ID>\
154 typedef std::integral_constant<bool,exp1::is_ker::value || exp1::is_ker::value> is_ker;\
156 typedef typename vector_result<typename exp1::vtype,typename exp2::vtype>::type vtype;\
158 typedef typename nn_type_result<typename exp1::NN_type,typename exp2::NN_type>::type NN_type;\
160 inline NN_type * getNN()\
162 return nn_type_result<typename exp1::NN_type,typename exp2::NN_type>::getNN(o1,o2);\
167 return vector_result<typename exp1::vtype,typename exp2::vtype>::getVector(o1,o2);\
169 const vtype & getVector() const\
171 return vector_result<typename exp1::vtype,typename exp2::vtype>::getVector(o1,o2);\
175 vector_dist_expression_op(const exp1 & o1, const exp2 & o2)\
179 inline void init() const\
185 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\
187 return fun_base(o1.value(key),o2.value(key));\
192 template<unsigned int p1, unsigned int p2, typename v1, typename v2>\
193 inline vector_dist_expression_op<vector_dist_expression<p1,v1>,vector_dist_expression<p2,v2>,OP_ID>\
194 fun_name(const vector_dist_expression<p1,v1> & va, const vector_dist_expression<p2,v2> & vb)\
196 vector_dist_expression_op<vector_dist_expression<p1,v1>,vector_dist_expression<p2,v2>,OP_ID> exp_sum(va,vb);\
201 template<typename exp1 , typename exp2, unsigned int op1, unsigned int prp1, typename v1>\
202 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression<prp1,v1>,OP_ID>\
203 fun_name(const vector_dist_expression_op<exp1,exp2,op1> & va, const vector_dist_expression<prp1,v1> & vb)\
205 vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression<prp1,v1>,OP_ID> exp_sum(va,vb);\
210 template<typename exp1 , typename exp2, unsigned int op1, unsigned int prp1, typename v1>\
211 inline vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_dist_expression_op<exp1,exp2,op1>,OP_ID>\
212 fun_name(const vector_dist_expression<prp1,v1> & va, const vector_dist_expression_op<exp1,exp2,op1> & vb)\
214 vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_dist_expression_op<exp1,exp2,op1>,OP_ID> exp_sum(va,vb);\
219 template<typename exp1 , typename exp2, unsigned int op1, typename exp3 , typename exp4, unsigned int op2>\
220 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression_op<exp3,exp4,op2>,OP_ID>\
221 fun_name(const vector_dist_expression_op<exp1,exp2,op1> & va, const vector_dist_expression_op<exp3,exp4,op2> & vb)\
223 vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression_op<exp3,exp4,op2>,OP_ID> exp_sum(va,vb);\
228 template<unsigned int prp1 , typename v1>\
229 inline vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_dist_expression<0,double>,OP_ID>\
230 fun_name(const vector_dist_expression<prp1,v1> & va, double d)\
232 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));\
237 template<unsigned int prp1 , typename v1>\
238 inline vector_dist_expression_op<vector_dist_expression<0,double>,vector_dist_expression<prp1,v1>,OP_ID>\
239 fun_name(double d, const vector_dist_expression<prp1,v1> & vb)\
241 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);\
246 template<typename exp1 , typename exp2, unsigned int op1>\
247 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression<0,double>,OP_ID>\
248 fun_name(const vector_dist_expression_op<exp1,exp2,op1> & va, double d)\
250 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));\
255 template<typename exp1 , typename exp2, unsigned int op1>\
256 inline vector_dist_expression_op<vector_dist_expression<0,double>,vector_dist_expression_op<exp1,exp2,op1>,OP_ID>\
257 fun_name(double d, const vector_dist_expression_op<exp1,exp2,op1> & va)\
259 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);\
264 CREATE_VDIST_ARG2_FUNC(pmul,pmul,VECT_PMUL)
268 template<typename val_type, bool is_scalar = is_Point<val_type>::type::value>
273 template<
typename vector_type,
typename expression>
274 static void process(val_type & val,
vector_type & ve, expression & o1)
278 auto vek = ve.toKernel();
281 exp_tmp2[0].resize(
sizeof(val_type));
283 auto & v_cl = create_vcluster<CudaMemory>();
285 openfpm::reduce((val_type *)ve.template getDeviceBuffer<0>(), ve.size(), (val_type *)(exp_tmp2[0].getDevicePointer()),
gpu::plus_t<val_type>(), v_cl.getGpuContext());
287 exp_tmp2[0].deviceToHost();
289 val = *(val_type *)(exp_tmp2[0].getPointer());
291 std::cout << __FILE__ <<
":" << __LINE__ <<
" error: to make expression work on GPU the file must be compiled on GPU" << std::endl;
296 template<
typename val_type>
299 typedef val_type type;
301 template<
typename vector_type,
typename expression>
302 static void process(val_type & val,
vector_type & ve, expression & o1)
310 auto vek = ve.toKernel();
313 exp_tmp2[0].resize(
sizeof(val_type));
317 auto & v_cl = create_vcluster<CudaMemory>();
319 for (
size_t i = 0 ; i < val_type::dims ; i++)
321 openfpm::reduce(&((
typename val_type::coord_type *)ve.template getDeviceBuffer<0>())[offset],
323 (
typename val_type::coord_type *)(exp_tmp2[0].getDevicePointer()),
325 v_cl.getGpuContext());
327 exp_tmp2[0].deviceToHost();
329 val.get(i) = *(
typename val_type::coord_type *)exp_tmp2[0].getPointer();
331 offset += ve.capacity();
335 std::cout << __FILE__ <<
":" << __LINE__ <<
" error: to make expression work on GPU the file must be compiled on GPU" << std::endl;
341 template<
bool is_device>
344 template<
typename o1_type,
typename val_type>
345 static void red(o1_type & o1, val_type & val)
357 auto & orig_v = o1.getVector();
359 if (exp_tmp.ref() == 0)
362 ve.setMemory(exp_tmp);
363 ve.resize(orig_v.size_local());
367 std::cout << __FILE__ <<
":" << __LINE__ <<
" error, to use expression on GPU you must compile with nvcc compiler " << std::endl;
375 template<
typename o1_type,
typename val_type>
376 static void red(o1_type & o1, val_type & val)
378 const auto & orig_v = o1.getVector();
384 auto it = orig_v.getDomainIterator();
390 val += o1.value(key);
403 template <
typename exp1>
414 mutable typename std::remove_reference<rtype>::type
val;
422 typedef typename vector_result<typename exp1::vtype,void>::type
vtype;
425 typedef typename nn_type_result<typename exp1::NN_type,void>::type
NN_type;
450 inline typename std::remove_reference<rtype>::type
get()
457 template<typename r_type= typename std::remove_reference<rtype>::type >
472 return o1.getVector();
484 return o1.getVector();
489 template<
typename exp1,
typename exp2_,
unsigned int op1>
499 template<
unsigned int prp1,
typename v1>
514 template <
typename T,
typename P>
auto distance(T exp1,
P exp2) -> decltype(norm(exp1 - exp2))
516 return norm(exp1 - exp2);
527 template <
typename exp1>
538 mutable typename std::remove_reference<rtype>::type
val;
546 typedef typename vector_result<typename exp1::vtype,void>::type
vtype;
549 typedef typename nn_type_result<typename exp1::NN_type,void>::type
NN_type;
560 if (exp1::is_ker::value ==
true)
564 typedef decltype(val) val_type;
573 auto & orig_v = o1.getVector();
575 if (exp_tmp.ref() == 0)
578 ve.setMemory(exp_tmp);
579 ve.resize(orig_v.size_local());
583 std::cout << __FILE__ <<
":" << __LINE__ <<
" error, to use expression on GPU you must compile with nvcc compiler " << std::endl;
588 const auto & orig_v = o1.getVector();
594 auto it = orig_v.getDomainIterator();
599 if(fabs(o1.value(key))>val) {
600 val = fabs(o1.value(key));
618 inline typename std::remove_reference<rtype>::type
get()
625 template<typename r_type= typename std::remove_reference<rtype>::type >
inline r_type
value(
const vect_dist_key_dx & key)
const
631 template<typename r_type= typename std::remove_reference<rtype>::type >
inline r_type
getReduction()
const
645 return o1.getVector();
657 return o1.getVector();
662 template<
typename exp1,
typename exp2_,
unsigned int op1>
672 template<
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.
NN_type * getNN() const
get the NN object
nn_type_result< typename exp1::NN_type, void >::type NN_type
NN_type.
void init() const
sum reduction require initialization where we calculate the reduction
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
vector_result< typename exp1::vtype, void >::type vtype
return the vector type on which this expression operate
const vtype & getVector() const
Return the vector on which is acting.
exp1::is_ker is_ker
Indicate if it is an in kernel expression.
r_type value(const vect_dist_key_dx &key) const
it return the result of the expression (precalculated before)
std::remove_reference< rtype >::type get()
it return the result of the expression
vtype & getVector()
Return the vector on which is acting.
apply_kernel_rtype< decltype(o1.value(vect_dist_key_dx()))>::rtype rtype
return type of this expression
nn_type_result< typename exp1::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
vector_result< typename exp1::vtype, void >::type vtype
return the vector type on which this expression operate
vtype & getVector()
Return the vector on which is acting.
const vtype & getVector() const
Return the vector on which is acting.
void init() const
sum reduction require initialization where we calculate the reduction
const exp1 o1
expression on which apply the reduction
vector_dist_expression_op(const exp1 &o1)
constructor from an epxression exp1 and a vector vd
__device__ __host__ r_type value(const vect_dist_key_dx &key) const
it return the result of the expression (precalculated before)
std::remove_reference< rtype >::type val
return type of the calculated value (without reference)
NN_type * getNN() const
get the NN object
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.
OutputIteratorT OffsetT ReductionOpT OuputT init
< [in] The initial value of the reduction
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