OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
Encap.hpp
1 /*
2  * Encap.hpp
3  *
4  * Created on: Feb 2, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef ENCAP_HPP_
9 #define ENCAP_HPP_
10 
11 #include "util/for_each_ref.hpp"
12 #include "util/copy_compare/meta_copy.hpp"
13 #include "boost/mpl/range_c.hpp"
14 #include <boost/fusion/container/vector.hpp>
15 #ifdef SE_CLASS2
16 #include "Memleak_check.hpp"
17 #endif
18 #include "util/se_util.hpp"
19 #include "util/copy_compare/copy_fusion_vector.hpp"
20 #include "util/copy_compare/compare_fusion_vector.hpp"
21 #include "memory_ly/memory_conf.hpp"
22 
34 template<typename e_src, typename e_dst>
36 {
38  const e_src & src;
40  e_dst & dst;
41 
42 
49  inline copy_cpu_encap_encap(const e_src & src, e_dst & dst)
50  :src(src),dst(dst)
51  {
52 #ifdef SE_CLASS1
53  // e_src and e_dst must have the same number of properties
54 
55  if (e_src::T_type::max_prop != e_dst::T_type::max_prop)
56  std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " the number of properties between src and dst must match";
57 #endif
58  };
59 
60 
61 #ifdef SE_CLASS1
62 
68  inline copy_cpu_encap_encap(const e_src && src, const e_dst && dst)
69  :src(src),dst(dst)
70  {std::cerr << "Error: " <<__FILE__ << ":" << __LINE__ << " Passing a temporal object";};
71 #endif
72 
74  template<typename T>
75  inline void operator()(T& t) const
76  {
77  // Remove the reference from the type to copy
78  typedef typename boost::remove_reference<decltype(dst.template get<T::value>())>::type copy_rtype;
79 
80  meta_copy<copy_rtype>::meta_copy_(src.template get<T::value>(),dst.template get<T::value>());
81  }
82 };
83 
85 
87 template<template<typename,typename> class op,typename e_src, typename e_dst, int ... prp>
89 {
91  const e_src & src;
93  e_dst & dst;
94 
95 
103  inline copy_cpu_encap_encap_op_prp(const e_src & src, e_dst & dst)
104  :src(src),dst(dst)
105  {
106 #ifdef SE_CLASS1
107  // e_src and e_dst must have the same number of properties
108 
109  if (e_src::T_type::max_prop != e_dst::T_type::max_prop)
110  std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " the number of properties between src and dst must match";
111 #endif
112  };
113 
114 
115 #ifdef SE_CLASS1
116 
122  inline copy_cpu_encap_encap_op_prp(const e_src && src, const e_dst && dst)
123  :src(src),dst(dst)
124  {std::cerr << "Error: " <<__FILE__ << ":" << __LINE__ << " Passing a temporal object";};
125 #endif
126 
128  template<typename T>
129  inline void operator()(T& t) const
130  {
131  // Convert variadic to boost::vector
132  typedef typename boost::mpl::vector_c<unsigned int,prp...> prpv;
133 
134  // element id to copy applying an operation
135  typedef typename boost::mpl::at<prpv,T>::type ele_cop;
136 
137  // Remove the reference from the type to copy
138  typedef typename boost::remove_reference<decltype(src.template get< ele_cop::value >())>::type copy_rtype;
139 
140  meta_copy_op<op,copy_rtype>::meta_copy_op_(src.template get< ele_cop::value >(),dst.template get< ele_cop::value >());
141  }
142 };
143 
144 
145 
147 
159 template<typename e_src, typename e_dst>
161 {
163  const e_src & src;
165  const e_dst & dst;
166 
167 
175  inline compare_cpu_encap_encap(const e_src & src, const e_dst & dst)
176  :src(src),dst(dst)
177  {
178 #ifdef SE_CLASS1
179  // e_src and e_dst must have the same number of properties
180 
181  if (e_src::max_prop != e_dst::max_prop)
182  std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " the number of properties between src and dst must match";
183 #endif
184  };
185 
186 
187 #ifdef SE_CLASS1
188 
198  inline compare_cpu_encap_encap(const e_src && src, const e_dst && dst)
199  :src(src),dst(dst)
200  {std::cerr << "Error: " <<__FILE__ << ":" << __LINE__ << " Passing a temporal object";};
201 #endif
202 
208  template<typename T>
209  inline void operator()(T& t) const
210  {
212  typedef typename boost::fusion::result_of::at_c<typename e_src::type,T::value>::type copy_type;
213 
215  typedef typename boost::remove_reference<copy_type>::type copy_rtype;
216 
217  meta_compare<copy_rtype> cp(src.template get<T::value>(),dst.template get<T::value>());
218  }
219 };
220 
230 template<unsigned int p,typename Mem>
232 {
234  typedef Mem vtype;
236  typedef typename boost::fusion::result_of::at< vtype,boost::mpl::int_<p> >::type rtype;
238  typedef typename boost::remove_reference<rtype>::type mtype;
240  typedef typename mtype::type type;
241 };
242 
250 template<unsigned int dim,typename T,typename layout>
251 class encapc
252 {
253 
254 };
255 
272 template<unsigned int dim,typename T>
273 class encapc<dim,T,typename memory_traits_lin<T>::type >
274 {
275 public:
276 
278  typedef typename T::type type;
279 
280 private:
281 
284 
286  typedef typename memory_traits_lin<T>::type Mem;
287 
288 public:
289 
291  typedef int yes_i_am_encap;
292 
294  typedef T T_type;
295 
297  static const int max_prop = T::max_prop;
298 
300  inline encapc(type & data_c)
301  :data_c(data_c)
302  {}
303 
309  inline type * operator&()
310  {
311  return &data_c;
312  }
313 
319  template <unsigned int p, typename r_type=decltype(boost::fusion::at_c<p>(data_c))>
320  inline r_type get()
321  {
322 #ifdef SE_CLASS2
323  check_valid(&boost::fusion::at_c<p>(data_c),sizeof(typename boost::mpl::at<type,boost::mpl::int_<p>>::type));
324 #endif
325  return boost::fusion::at_c<p>(data_c);
326  }
327 
333  template <unsigned int p, typename r_type=decltype(boost::fusion::at_c<p>(data_c))>
334  inline const r_type get() const
335  {
336 #ifdef SE_CLASS2
337  check_valid(&boost::fusion::at_c<p>(data_c),sizeof(typename boost::mpl::at<type,boost::mpl::int_<p>>::type));
338 #endif
339  return boost::fusion::at_c<p>(data_c);
340  }
341 
349  template <unsigned int p> inline void set(decltype(boost::fusion::at_c<p>(data_c)) & ele)
350  {
351 #ifdef SE_CLASS2
352  check_valid(&boost::fusion::at_c<p>(data_c),sizeof(typename boost::mpl::at<type,boost::mpl::int_<p>>::type));
353 #endif
354  return boost::fusion::at_c<p>(data_c) = ele;
355  }
356 
367  template<unsigned int dim2> inline encapc<dim,T,Mem> & set(const encapc<dim2,T,Mem> & ec)
368  {
370 
371  boost::mpl::for_each_ref< boost::mpl::range_c<int,0,T::max_prop> >(cp);
372 
373  return *this;
374  }
375 
384  {
386 
387  boost::mpl::for_each_ref< boost::mpl::range_c<int,0,T::max_prop> >(cp);
388 
389  return *this;
390  }
391 
399  inline encapc<dim,T,Mem> & operator=(const T & obj)
400  {
401  copy_fusion_vector<typename T::type> cp(obj.data,data_c);
402 
403  boost::mpl::for_each_ref< boost::mpl::range_c<int,0,T::max_prop> >(cp);
404 
405  return *this;
406  }
407 
415  inline bool operator==(const encapc<dim,T,Mem> & ec) const
416  {
417  compare_fusion_vector<typename T::type> cp(ec.data_c,data_c);
418 
419  boost::mpl::for_each_ref< boost::mpl::range_c<int,0,T::max_prop> >(cp);
420 
421  return cp.result();
422  }
423 
431  inline bool operator!=(const encapc<dim,T,Mem> & ec) const
432  {
433  return ! this->operator==(ec);
434  }
435 };
436 
444 template<unsigned int dim,typename Mem>
445 class encapc<dim,void,Mem>
446 {
447 public:
448 
449  // constructor require a key and a memory data
450  encapc()
451  {}
452 
454  template <unsigned int p> void get()
455  {}
456 
458  template <unsigned int p, typename S> void set(S & ele)
459  {}
460 };
461 
476 template<unsigned int dim,typename T>
477 class encapc<dim,T,typename memory_traits_inte<T>::type>
478 {
481 
484 
486  size_t k;
487 
488 public:
489 
491  typedef typename T::type type;
492 
494  typedef int yes_i_am_encap;
495 
497  typedef T T_type;
498 
500  encapc(typename memory_traits_inte<T>::type & data, size_t k)
501  :data(data),k(k)
502  {}
503 
511  template <unsigned int p> typename type_gpu_prop<p,typename memory_traits_inte<T>::type>::type::reference get()
512  {
513  return boost::fusion::at_c<p>(data).mem_r->operator[](k);
514  }
515 
523  template <unsigned int p> typename type_gpu_prop<p,typename memory_traits_inte<T>::type>::type::reference get() const
524  {
525  return boost::fusion::at_c<p>(data).mem_r->operator[](k);
526  }
527 
536  {
538 
539  boost::mpl::for_each_ref< boost::mpl::range_c<int,0,T::max_prop> >(cp);
540 
541  return *this;
542  }
543 
551  inline encapc<dim,T,Mem> & operator=(const T & obj)
552  {
554 
555  boost::mpl::for_each_ref< boost::mpl::range_c<int,0,T::max_prop> >(cp);
556 
557  return *this;
558  }
559 };
560 
561 #include "util/common.hpp"
562 
563 template<typename T, typename Sfinae = void>
564 struct is_encap: std::false_type {};
565 
566 
576 template<typename T>
577 struct is_encap<T, typename Void< typename T::yes_i_am_encap>::type> : std::true_type
578 {};
579 
580 #endif /* ENCAP_HPP_ */
encapc(type &data_c)
constructor from a reference object
Definition: Encap.hpp:300
int yes_i_am_encap
indicate the it is an encapsulated object
Definition: Encap.hpp:291
e_dst & dst
encapsulated destination object
Definition: Encap.hpp:40
int yes_i_am_encap
indicate it is an encapsulated object
Definition: Encap.hpp:494
Transform the boost::fusion::vector into memory specification (memory_traits)
Definition: memory_conf.hpp:93
encapc< dim, T, Mem > & set(const encapc< dim2, T, Mem > &ec)
Set one property of the encapsulated object.
Definition: Encap.hpp:367
void operator()(T &t) const
It call the copy function for each property.
Definition: Encap.hpp:75
static void meta_copy_(const T &src, T &dst)
copy and object from src to dst
Definition: meta_copy.hpp:37
static void meta_copy_op_(const T &src, T &dst)
Meta-copy applying an operation.
Definition: meta_copy.hpp:569
this class is a functor for "for_each" algorithm
Definition: Encap.hpp:160
void set(decltype(boost::fusion::at_c< p >(data_c))&ele)
Set one property of the encapsulated object.
Definition: Encap.hpp:349
this class is a functor for "for_each" algorithm
This class is an helper to get the return type of get for each property.
Definition: Encap.hpp:231
void operator()(T &t) const
It call the copy function for each property.
Definition: Encap.hpp:209
Mem vtype
return a boost::fusion::vector<memory_c<....>....>
Definition: Encap.hpp:234
Mem & data
reference to the encapsulated object
Definition: Encap.hpp:483
memory_traits_lin< T >::type Mem
layout of the encapsulated object
Definition: Encap.hpp:286
Transform the boost::fusion::vector into memory specification (memory_traits)
Definition: memory_conf.hpp:52
boost::remove_reference< rtype >::type mtype
remove the reference
Definition: Encap.hpp:238
void set(S &ele)
set the data
Definition: Encap.hpp:458
bool operator==(const encapc< dim, T, Mem > &ec) const
Compare.
Definition: Encap.hpp:415
const e_src & src
object 1 we have to compare
Definition: Encap.hpp:163
type & data_c
reference to the encapsulated object
Definition: Encap.hpp:283
inter_memc< typename T::type >::type type
for each element in the vector interleave memory_c
Definition: memory_conf.hpp:55
void operator()(T &t) const
It call the copy function for each property.
Definition: Encap.hpp:129
memory_traits_inte< T >::type Mem
type of layout
Definition: Encap.hpp:480
type * operator&()
Return the address of the base.
Definition: Encap.hpp:309
encapc(typename memory_traits_inte< T >::type &data, size_t k)
constructor require a key and a memory data
Definition: Encap.hpp:500
encapc< dim, T, Mem > & operator=(const encapc< dim, T, Mem > &ec)
Assignment.
Definition: Encap.hpp:535
encapc< dim, T, Mem > & operator=(const encapc< dim, T, Mem > &ec)
Assignment.
Definition: Encap.hpp:383
this class is a functor for "for_each" algorithm
bool operator!=(const encapc< dim, T, Mem > &ec) const
Compare.
Definition: Encap.hpp:431
bool result()
Returh the result of the comparison.
Void structure.
Definition: common.hpp:63
This class compare general objects.
mtype::type type
get the base type that the buffer is storing
Definition: Encap.hpp:240
compare_cpu_encap_encap(const e_src &src, const e_dst &dst)
constructor
Definition: Encap.hpp:175
this class is a functor for "for_each" algorithm
Definition: Encap.hpp:35
It copy two encap object.
Definition: Encap.hpp:88
e_dst & dst
encapsulated object destination
Definition: Encap.hpp:93
encapc< dim, T, Mem > & operator=(const T &obj)
Assignment.
Definition: Encap.hpp:551
copy_cpu_encap_encap_op_prp(const e_src &src, e_dst &dst)
constructor
Definition: Encap.hpp:103
encapc< dim, T, Mem > & operator=(const T &obj)
Assignment.
Definition: Encap.hpp:399
This class is a container for the memory interface like HeapMemory CudaMemory.
Definition: memory_c.hpp:31
const e_dst & dst
object 2 we have to compare
Definition: Encap.hpp:165
boost::fusion::result_of::at< vtype, boost::mpl::int_< p > >::type rtype
return a memory_c<...>
Definition: Encap.hpp:236
const e_src & src
encapsulated object source
Definition: Encap.hpp:91
copy_cpu_encap_encap(const e_src &src, e_dst &dst)
constructor
Definition: Encap.hpp:49
this class is a functor for "for_each" algorithm
const e_src & src
encapsulated source object
Definition: Encap.hpp:38
T::type type
object type the encap object encapsulate
Definition: Encap.hpp:278