OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
Packer_util.hpp
1 /*
2  * Packer_util.hpp
3  *
4  * Created on: Jan 25, 2016
5  * Author: Yaroslav Zaluzhnyi
6  */
7 
8 #ifndef PACKER_UTIL_HPP_
9 #define PACKER_UTIL_HPP_
10 
11 #include "prp_all_zero.hpp"
12 #include "Packer_Unpacker/Pack_selector.hpp"
13 #include "memory/HeapMemory.hpp"
14 #include "Vector/map_vector_grow_p.hpp"
15 #include "Vector/vect_isel.hpp"
16 #include "memory_ly/memory_conf.hpp"
17 #include "util/Pack_stat.hpp"
18 
19 template<typename T, typename Mem, int pack_type=Pack_selector<T>::value > class Packer;
20 template<typename T, typename Mem, int pack_type=Pack_selector<T>::value > class Unpacker;
21 
22 #include "Vector/vector_def.hpp"
23 
24 
28 
29 
30 //A functor for call_aggregatePackRequest
31 template<typename encap, typename Mem>
33 {
35  encap & obj;
36 
38  size_t & req;
39 
40  /* \brief Constructor
41  *
42  * \param obj object to pack/serialize
43  * \param req byte to pack/serialize
44  *
45  */
46  call_packRequest_enc_functor(encap & obj, size_t & req)
47  :obj(obj),req(req)
48  {}
49 
51  template<typename T>
52  inline void operator()(T& t)
53  {
54  typedef typename boost::mpl::at<typename encap::T_type,T>::type obj_type;
55 
56  Packer<obj_type,Mem>::packRequest(obj.template get<T::value>(),req);
57  }
58 };
59 
60 
61 //Calls a pack request in nested way
62 template<typename encap, typename Mem, int ... prp>
64 {
71  static inline void call_packRequest(encap & obj, size_t & req)
72  {
73  //Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
74  typedef typename prp_all_zero<encap,sizeof...(prp) == 0,prp...>::type b_prp;
75 
77 
78  //Apply functor for each property
79  boost::mpl::for_each_ref<b_prp>(functor);
80  }
81 };
82 
83 //A functor for call_aggregatePack
84 template<typename encap, typename Mem>
86 {
89 
91  const encap & obj;
92 
95 
97  :mem(mem), obj(obj), sts(sts)
98  {
99  }
100 
102  template<typename T>
103  inline void operator()(T& t)
104  {
105  typedef typename boost::mpl::at<typename encap::type,T>::type obj_type;
106 
107  Packer<obj_type,Mem>::pack(mem,obj.template get<T::value>(),sts);
108  }
109 };
110 
111 template<typename Timp>
113 {
114  template<typename T, typename Mem, typename encap>
115  static inline void call(ExtPreAlloc<Mem> & mem,const encap & obj, size_t sub_id, Pack_stat & sts)
116  {
117  Packer<typename Timp::value_type,Mem>::pack(mem,obj.template get<T::value>()[sub_id],sts);
118  }
119 };
120 
121 template<unsigned int N1, typename Timp>
123 {
124  template<typename T, typename Mem, typename encap>
125  static inline void call(ExtPreAlloc<Mem> & mem,const encap & obj, size_t sub_id, Pack_stat & sts)
126  {
127  for (int i = 0 ; i < N1 ; i++)
128  {Packer<typename Timp::value_type,Mem>::pack(mem,obj.template get<T::value>()[i][sub_id],sts);}
129  }
130 };
131 
132 //A functor for call_aggregatePack
133 template<typename encap, typename Mem>
135 {
138 
140  const encap & obj;
141 
143  size_t sub_id;
144 
147 
149  :mem(mem), obj(obj),sub_id(sub_id),sts(sts)
150  {
151  }
152 
154  template<typename T>
155  inline void operator()(T& t)
156  {
157 // typedef typename boost::mpl::at<typename encap::type,T>::type::value_type obj_type;
158 
160 
161 // Packer<obj_type,Mem>::pack(mem,obj.template get<T::value>()[sub_id],sts);
162  }
163 };
164 
165 //Calls a packer in nested way
166 template<typename encap, typename Mem, int ... prp>
168 {
169  static inline void call_pack(const encap & obj, ExtPreAlloc<Mem> & mem, Pack_stat & sts)
170  {
171  //Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
172  typedef typename prp_all_zero<typename encap::T_type,sizeof...(prp) == 0,prp...>::type b_prp;
173 
174  call_pack_enc_functor<encap,Mem> functor(mem,obj,sts);
175 
176  //Apply functor for each property
177  boost::mpl::for_each_ref<b_prp>(functor);
178  }
179 };
180 
181 //Calls a packer in nested way
182 template<typename encap, typename Mem, int ... prp>
184 {
185  static inline void call_pack(const encap & obj, size_t sub_id, ExtPreAlloc<Mem> & mem, Pack_stat & sts)
186  {
187  //Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
188  typedef typename prp_all_zero<typename encap::T_type,sizeof...(prp) == 0,prp...>::type b_prp;
189 
190  call_pack_enc_functor_chunking<encap,Mem> functor(mem,obj,sub_id,sts);
191 
192  //Apply functor for each property
193  boost::mpl::for_each_ref<b_prp>(functor);
194  }
195 };
196 
197 //A functor for call_aggregateUnpack
198 template<typename encap, typename Mem, int ... prp>
200 {
201  ExtPreAlloc<Mem> & mem;
202  const encap & obj;
203  Unpack_stat & ps;
204 
205  call_unpack_encap_functor(ExtPreAlloc<Mem> & mem, const encap & obj, Unpack_stat & ps)
206  :mem(mem), obj(obj), ps(ps)
207  {
208  }
209 
211  template<typename T>
212  inline void operator()(T& t)
213  {
214  typedef typename boost::mpl::at<typename encap::type,T>::type obj_type;
215 
216  Unpacker<obj_type,Mem>::unpack(mem,obj.template get<T::value>(),ps);
217  }
218 };
219 
220 template<typename T>
222 {
223  template<typename T_value, typename Mem, typename encap>
224  static inline void unpack(ExtPreAlloc<Mem> & mem, const encap & obj, unsigned int sub_id, Unpack_stat & ps)
225  {
226  typedef typename boost::mpl::at<typename encap::type,T_value>::type::value_type obj_type;
227 
228  Unpacker<obj_type,Mem>::unpack(mem,obj.template get<T_value::value>()[sub_id],ps);
229  }
230 };
231 
232 template<typename T, unsigned int N1>
234 {
235  template<typename T_value, typename Mem, typename encap>
236  static inline void unpack(ExtPreAlloc<Mem> & mem, const encap & obj, unsigned int sub_id, Unpack_stat & ps)
237  {
238  for (int i = 0 ; i < N1 ; i++)
239  {
240  typedef typename std::remove_all_extents< typename boost::mpl::at<typename encap::type,T_value>::type >::type::value_type obj_type;
241 
242  Unpacker<obj_type,Mem>::unpack(mem,obj.template get<T_value::value>()[i][sub_id],ps);
243  }
244  }
245 };
246 
247 //A functor for call_aggregateUnpack
248 template<typename encap, typename Mem, int ... prp>
250 {
251  ExtPreAlloc<Mem> & mem;
252  const encap & obj;
253  Unpack_stat & ps;
254 
255  size_t sub_id;
256 
257  call_unpack_encap_functor_chunking(ExtPreAlloc<Mem> & mem, const encap & obj, size_t sub_id, Unpack_stat & ps)
258  :mem(mem), obj(obj), ps(ps),sub_id(sub_id)
259  {
260  }
261 
263  template<typename T>
264  inline void operator()(T& t)
265  {
266  typedef typename boost::mpl::at<typename encap::type,T>::type obj_type;
267 
268 // Unpacker<obj_type,Mem>::unpack(mem,obj.template get<T::value>()[sub_id],ps);
269 
271  }
272 };
273 
274 //Calls an unpacker in nested way
275 template<typename encap, typename Mem, int ... prp>
277 {
278  static inline void call_unpack(encap & obj, ExtPreAlloc<Mem> & mem, Unpack_stat & ps)
279  {
280  //Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
281  typedef typename prp_all_zero<encap,sizeof...(prp) == 0,prp...>::type b_prp;
282 
284 
285  //Apply functor for each property
286  boost::mpl::for_each_ref<b_prp>(functor);
287  }
288 };
289 
290 //Calls an unpacker in nested way
291 template<typename encap, typename Mem, int ... prp>
293 {
294  static inline void call_unpack(encap & obj, size_t sub_id, ExtPreAlloc<Mem> & mem, Unpack_stat & ps)
295  {
296  //Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
297  typedef typename prp_all_zero<encap,sizeof...(prp) == 0,prp...>::type b_prp;
298 
300 
301  //Apply functor for each property
302  boost::mpl::for_each_ref<b_prp>(functor);
303  }
304 };
305 
306 
310 
311 
312 template<bool inte_layout, typename obj_type, typename Mem>
314 {
315  template<unsigned int p> static inline void call_pack_req(const obj_type & obj,size_t & req)
316  {
317  typedef typename boost::mpl::at<typename obj_type::type,boost::mpl::int_<p>>::type obj_t;
318 
319  Packer<obj_t,Mem>::packRequest(obj.template get<p>(),req);
320  }
321 };
322 
323 template<typename obj_type, typename Mem>
324 struct pack_request_op<true,obj_type,Mem>
325 {
326  template<unsigned int p> static inline void call_pack_req(const obj_type & obj,size_t & req)
327  {
328  typedef typename boost::mpl::at<typename obj_type::type,boost::mpl::int_<p>>::type obj_t;
329 
330  Packer<obj_t,Mem>::packRequest(obj.template get<p>(),req);
331  }
332 };
333 
334 template<typename> struct Debug;
335 
336 //A functor for call_aggregatePackRequest
337 
338 template<typename obj_type, typename Mem>
340 {
342  const obj_type & obj;
343 
345  size_t & req;
346 
347  call_packRequest_agg_functor(const obj_type & obj, size_t & req)
348  :obj(obj),req(req)
349  {}
350 
352  template<typename T>
353  inline void operator()(T& t)
354  {
355  pack_request_op<openfpm::is_multi_array<decltype(obj.template get<T::value>())>::value,obj_type,Mem>::template call_pack_req<T::value>(obj,req);
356  }
357 };
358 
359 template<typename obj_type, typename Mem>
361 {
363  const obj_type & obj;
364 
366  size_t & req;
367 
369  size_t sub_id;
370 
371  call_packRequest_agg_functor_cnk(const obj_type & obj, size_t sub_id ,size_t & req)
372  :obj(obj),req(req),sub_id(sub_id)
373  {}
374 
376  template<typename T>
377  inline void operator()(T& t)
378  {
379  typedef decltype(obj.template get<T::value>()[sub_id]) obj_t;
380 
381  //for (size_t i = 0; i < obj_type::max_prop ; i++)
382  Packer<obj_t,Mem>::packRequest(obj.template get<T::value>()[sub_id],req);
383  }
384 };
385 
386 //Calls a pack request in nested way
387 template<typename obj_type, typename Mem, int ... prp>
389 {
396  static inline void call_packRequest(const obj_type & obj, size_t & req)
397  {
398  //Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
399  typedef typename prp_all_zero<obj_type,sizeof...(prp) == 0,prp...>::type b_prp;
400 
402 
403  //Apply functor for each property
404  boost::mpl::for_each_ref<b_prp>(functor);
405  }
406 };
407 
408 template<bool inte_layout, typename obj_type, typename Mem>
410 {
411  template<unsigned int p> static inline void call_pack_pack(ExtPreAlloc<Mem> & mem, const obj_type & obj,Pack_stat & sts)
412  {
413  typedef typename boost::mpl::at<typename obj_type::type,boost::mpl::int_<p>>::type obj_t;
414 
415  Packer<obj_t,Mem>::pack(mem,obj.template get<p>(),sts);
416  }
417 };
418 
419 //Calls a pack request in nested way
420 template<typename obj_type, typename Mem, int ... prp>
422 {
429  static inline void call_packRequest(const obj_type & obj, size_t sub_id, size_t & req)
430  {
431  //Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
432  typedef typename prp_all_zero<obj_type,sizeof...(prp) == 0,prp...>::type b_prp;
433 
434  call_packRequest_agg_functor_cnk<obj_type,Mem> functor(obj,sub_id,req);
435 
436  //Apply functor for each property
437  boost::mpl::for_each_ref<b_prp>(functor);
438  }
439 };
440 
441 //A functor for call_aggregatePack
442 template<typename obj_type, typename Mem>
444 {
445  ExtPreAlloc<Mem> & mem;
446  const obj_type & obj;
447  Pack_stat & sts;
448 
449  call_pack_agg_functor(ExtPreAlloc<Mem> & mem, const obj_type & obj, Pack_stat & sts)
450  :mem(mem), obj(obj), sts(sts)
451  {
452  }
453 
455  template<typename T>
456  inline void operator()(T& t)
457  {
458 // typedef typename boost::mpl::at<typename obj_type::type,T>::type obj_t;
459 
460  pack_pack_op<openfpm::is_multi_array<decltype(obj.template get<T::value>())>::value,obj_type,Mem>::template call_pack_pack<T::value>(mem,obj,sts);
461 
462 // Packer<obj_t,Mem>::pack(mem,obj.template get<T::value>(),sts);
463  }
464 };
465 
467 template<typename obj_type, typename Mem, int ... prp>
469 {
477  static inline void call_pack(const obj_type & obj, ExtPreAlloc<Mem> & mem, Pack_stat & sts)
478  {
479  //Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
480  typedef typename prp_all_zero<obj_type,sizeof...(prp) == 0,prp...>::type b_prp;
481 
482  call_pack_agg_functor<obj_type,Mem> functor(mem,obj,sts);
483 
484  //Apply functor for each property
485  boost::mpl::for_each_ref<b_prp>(functor);
486  }
487 };
488 
489 
490 //A functor for call_aggregateUnpack
491 template<typename obj_type, typename Mem>
493 {
496 
498  obj_type & obj;
499 
502 
511  :mem(mem), obj(obj), ps(ps)
512  {
513  }
514 
516  template<typename T>
517  inline void operator()(T& t)
518  {
519  typedef typename boost::mpl::at<typename obj_type::type,T>::type obj_t;
520 
521  Unpacker<obj_t,Mem>::unpack(mem,obj.template get<T::value>(),ps);
522  }
523 };
524 
526 template<typename obj_type, typename Mem, int ... prp>
528 {
536  static inline void call_unpack(obj_type && obj, ExtPreAlloc<Mem> & mem, Unpack_stat & ps)
537  {
538  //Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
539  typedef typename prp_all_zero<obj_type,sizeof...(prp) == 0,prp...>::type b_prp;
540 
541  call_unpack_agg_functor<obj_type,Mem> functor(mem,obj,ps);
542 
543  //Apply functor for each property
544  boost::mpl::for_each_ref<b_prp>(functor);
545  }
546 };
547 
548 #endif /* PACKER_UTIL_HPP_ */
void operator()(T &t)
It calls the packer for each property.
Unpack_stat & ps
statistic about packing
Unpacker class.
Definition: Packer_util.hpp:20
void operator()(T &t)
It calls the unpacker for each property.
call_unpack_agg_functor(ExtPreAlloc< Mem > &mem, obj_type &obj, Unpack_stat &ps)
constructor
const encap & obj
Object to serialize.
Definition: Packer_util.hpp:91
size_t & req
offset of the packed memory
size_t & req
offset of the packed memory
ExtPreAlloc< Mem > & mem
Memory that pack/serialize the object.
Definition: Packer_util.hpp:88
void operator()(T &t)
It calls the pack request for each property.
Definition: Packer_util.hpp:52
encap & obj
encap object to pack (serialize)
Definition: Packer_util.hpp:35
void operator()(T &t)
It calls the unpacker for each property.
void operator()(T &t)
It calls the packer for each property.
void operator()(T &t)
It calls the pack request for each property.
Definition: Packer.hpp:27
const encap & obj
Object to serialize.
void operator()(T &t)
It calls the pack request for each property.
static void call_pack(const obj_type &obj, ExtPreAlloc< Mem > &mem, Pack_stat &sts)
Call the packer.
static size_t packRequest(const T &obj, size_t &req)
Error, no implementation.
Definition: Packer.hpp:66
Packing class.
Definition: Packer.hpp:49
Pack_stat & sts
serialization status
Definition: Packer_util.hpp:94
Unpacking status object.
Definition: Pack_stat.hpp:15
void operator()(T &t)
It calls the unpacker for each property.
Pack_stat & sts
serialization status
obj_type & obj
object to pack
static void unpack(ExtPreAlloc< Mem >, T &obj)
Error, no implementation.
Definition: Unpacker.hpp:40
void operator()(T &t)
It calls the packer for each property.
const obj_type & obj
object to pack
const obj_type & obj
object to pack
static void call_unpack(obj_type &&obj, ExtPreAlloc< Mem > &mem, Unpack_stat &ps)
constructor
Calls a packer in nested way.
Structure to convert a variadic template into boost::mpl::vector.
static void call_packRequest(const obj_type &obj, size_t &req)
Pack the object.
Calls an unpacker in nested way.
ExtPreAlloc< Mem > & mem
Memory that pack/serialize the object.
Packing status object.
Definition: Pack_stat.hpp:60
static void pack(ExtPreAlloc< Mem >, const T &obj)
Error, no implementation.
Definition: Packer.hpp:56
ExtPreAlloc< Mem > & mem
Memory where to pack.
size_t & req
byte counter
Definition: Packer_util.hpp:38
static void call_packRequest(const obj_type &obj, size_t sub_id, size_t &req)
Pack the object.
static void call_packRequest(encap &obj, size_t &req)
pack/serialize
Definition: Packer_util.hpp:71