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 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();\ 84 template<typename exp1, typename exp2_, unsigned int op1>\ 85 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2_,op1>,void,OP_ID>\ 86 fun_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);\ 94 template<unsigned int prp1, typename v1>\ 95 inline vector_dist_expression_op<vector_dist_expression<prp1,v1>,void,OP_ID>\ 96 fun_name(const vector_dist_expression<prp1,v1> & va)\ 98 vector_dist_expression_op<vector_dist_expression<prp1,v1>,void,OP_ID> exp_sum(va);\ 103 CREATE_VDIST_ARG_FUNC(norm,norm,VECT_NORM)
104 CREATE_VDIST_ARG_FUNC(norm2,norm2,VECT_NORM2)
105 CREATE_VDIST_ARG_FUNC(abs,abs,POINT_ABS)
106 CREATE_VDIST_ARG_FUNC(exp,exp,POINT_EXP)
107 CREATE_VDIST_ARG_FUNC(exp2,exp2,POINT_EXP2)
108 CREATE_VDIST_ARG_FUNC(expm1,expm1,POINT_EXPM1)
109 CREATE_VDIST_ARG_FUNC(log,log,POINT_LOG)
110 CREATE_VDIST_ARG_FUNC(log10,log10,POINT_LOG10)
111 CREATE_VDIST_ARG_FUNC(log2,log2,POINT_LOG2)
112 CREATE_VDIST_ARG_FUNC(log1p,log1p,POINT_LOG1P)
113 CREATE_VDIST_ARG_FUNC(sqrt,sqrt,POINT_SQRT)
114 CREATE_VDIST_ARG_FUNC(cbrt,cbrt,POINT_CBRT)
115 CREATE_VDIST_ARG_FUNC(sin,sin,POINT_SIN)
116 CREATE_VDIST_ARG_FUNC(cos,cos,POINT_COS)
117 CREATE_VDIST_ARG_FUNC(tan,tan,POINT_TAN)
118 CREATE_VDIST_ARG_FUNC(asin,asin,POINT_ASIN)
119 CREATE_VDIST_ARG_FUNC(acos,acos,POINT_ACOS)
120 CREATE_VDIST_ARG_FUNC(atan,atan,POINT_ATAN)
121 CREATE_VDIST_ARG_FUNC(sinh,sinh,POINT_SINH)
122 CREATE_VDIST_ARG_FUNC(cosh,cosh,POINT_COSH)
123 CREATE_VDIST_ARG_FUNC(tanh,tanh,POINT_TANH)
124 CREATE_VDIST_ARG_FUNC(asinh,asinh,POINT_ASINH)
125 CREATE_VDIST_ARG_FUNC(acosh,acosh,POINT_ACOSH)
126 CREATE_VDIST_ARG_FUNC(atanh,atanh,POINT_ATANH)
127 CREATE_VDIST_ARG_FUNC(erf,erf,POINT_ERF)
128 CREATE_VDIST_ARG_FUNC(erfc,erfc,POINT_ERFC)
129 CREATE_VDIST_ARG_FUNC(tgamma,tgamma,POINT_TGAMMA)
130 CREATE_VDIST_ARG_FUNC(lgamma,lgamma,POINT_LGAMMA)
131 CREATE_VDIST_ARG_FUNC(ceil,ceil,POINT_CEIL)
132 CREATE_VDIST_ARG_FUNC(floor,floor,POINT_FLOOR)
133 CREATE_VDIST_ARG_FUNC(trunc,trunc,POINT_TRUNC)
134 CREATE_VDIST_ARG_FUNC(round,round,POINT_ROUND)
135 CREATE_VDIST_ARG_FUNC(nearbyint,nearbyint,POINT_NEARBYINT)
136 CREATE_VDIST_ARG_FUNC(rint,rint,POINT_RINT)
145 #define CREATE_VDIST_ARG2_FUNC(fun_base,fun_name,OP_ID) \ 148 template <typename exp1,typename exp2>\ 149 class 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));\ 196 template<unsigned int p1, unsigned int p2, typename v1, typename v2>\ 197 inline vector_dist_expression_op<vector_dist_expression<p1,v1>,vector_dist_expression<p2,v2>,OP_ID>\ 198 fun_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);\ 205 template<typename exp1 , typename exp2, unsigned int op1, unsigned int prp1, typename v1>\ 206 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression<prp1,v1>,OP_ID>\ 207 fun_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);\ 214 template<typename exp1 , typename exp2, unsigned int op1, unsigned int prp1, typename v1>\ 215 inline vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_dist_expression_op<exp1,exp2,op1>,OP_ID>\ 216 fun_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);\ 223 template<typename exp1 , typename exp2, unsigned int op1, typename exp3 , typename exp4, unsigned int op2>\ 224 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression_op<exp3,exp4,op2>,OP_ID>\ 225 fun_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);\ 232 template<unsigned int prp1 , typename v1>\ 233 inline vector_dist_expression_op<vector_dist_expression<prp1,v1>,vector_dist_expression<0,double>,OP_ID>\ 234 fun_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));\ 241 template<unsigned int prp1 , typename v1>\ 242 inline vector_dist_expression_op<vector_dist_expression<0,double>,vector_dist_expression<prp1,v1>,OP_ID>\ 243 fun_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);\ 250 template<typename exp1 , typename exp2, unsigned int op1>\ 251 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,vector_dist_expression<0,double>,OP_ID>\ 252 fun_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));\ 259 template<typename exp1 , typename exp2, unsigned int op1>\ 260 inline vector_dist_expression_op<vector_dist_expression<0,double>,vector_dist_expression_op<exp1,exp2,op1>,OP_ID>\ 261 fun_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);\ 268 CREATE_VDIST_ARG2_FUNC(pmul,pmul,VECT_PMUL)
272 template<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()),
mgpu::plus_t<val_type>(), v_cl.getmgpuContext());
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;
300 template<
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.getmgpuContext());
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;
345 template<
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);
407 template <
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();
496 template<
typename exp1,
typename exp2_,
unsigned int op1>
506 template<
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);
534 template <
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();
672 template<
typename exp1,
typename exp2_,
unsigned int op1>
682 template<
unsigned int prp1,
typename v1>
convert a type into constant type
apply_kernel_rtype< decltype(o1.value(vect_dist_key_dx()))>::rtype rtype
return type of this expression
const vtype & getVector() const
Return the vector on which is acting.
r_type value(const vect_dist_key_dx &key) const
it return the result of the expression (precalculated before)
Grid key for a distributed grid.
vtype & getVector()
Return the vector on which is acting.
exp1::is_ker is_ker
Indicate if it is an in kernel expression.
const vtype & getVector() const
Return the vector on which is acting.
r_type getReduction() const
it return the result of the expression for ODEINT
std::remove_reference< rtype >::type val
return type of the calculated value (without reference)
void init() const
sum reduction require initialization where we calculate the reduction
vector_result< typename exp1::vtype, void >::type vtype
return the vector type on which this expression operate
Transform the boost::fusion::vector into memory specification (memory_traits)
apply_kernel_rtype< decltype(o1.value(vect_dist_key_dx()))>::rtype rtype
return type of this expression
__device__ __host__ r_type value(const vect_dist_key_dx &key) const
it return the result of the expression (precalculated before)
nn_type_result< typename exp1::NN_type, void >::type NN_type
NN_type.
std::remove_reference< rtype >::type get()
it return the result of the expression
const exp1 o1
expression on which apply the reduction
nn_type_result< typename exp1::NN_type, void >::type NN_type
NN_type.
Grow policy define how the vector should grow every time we exceed the size.
Unknown operation specialization.
vector_result< typename exp1::vtype, void >::type vtype
return the vector type on which this expression operate
std::remove_reference< rtype >::type val
return type of the calculated value (without reference)
Main class that encapsulate a vector properties operand to be used for expressions construction.
vector_is_sort_result< exp1::is_sort::value, false >::type is_sort
result for is sort
vtype & getVector()
Return the vector on which is acting.
vector_is_sort_result< exp1::is_sort::value, false >::type is_sort
result for is sort
void init() const
sum reduction require initialization where we calculate the reduction
const exp1 o1
expression on which apply the reduction
NN_type * getNN() const
get the NN object
std::remove_reference< rtype >::type get()
it return the result of the expression
NN_type * getNN() const
get the NN object
auto distance(T exp1, P exp2) -> decltype(norm(exp1 - exp2))
General distance formula.
It give the return type of the expression if applicable.
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
vector_dist_expression_op(const exp1 &o1)
constructor from an epxression exp1 and a vector vd
temporal buffer for reductions
OutputIteratorT OffsetT ReductionOpT OuputT init
< [in] The initial value of the reduction
Test structure used for several test.
exp1::is_ker is_ker
Indicate if it is an in kernel expression.
Implementation of 1-D std::vector like structure.
vector_dist_expression_op(const exp1 &o1)
constructor from an epxression exp1 and a vector vd