OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
Packer_unit_tests.hpp
1 /*
2  * Packer_unit_tests.hpp
3  *
4  * Created on: Jul 15, 2015
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef SRC_PACKER_UNIT_TESTS_HPP_
9 #define SRC_PACKER_UNIT_TESTS_HPP_
10 
11 #include "Pack_selector.hpp"
12 #include "Packer.hpp"
13 #include "Unpacker.hpp"
14 #include "Grid/grid_util_test.hpp"
15 #include <iostream>
16 #include "Vector/vector_test_util.hpp"
17 #include "data_type/aggregate.hpp"
18 
19 BOOST_AUTO_TEST_SUITE( packer_unpacker )
20 
21 BOOST_AUTO_TEST_CASE ( packer_unpacker_test )
22 {
24 
26  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
28  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
30  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
32  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
34  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
36  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
38  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
40  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
42  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
44  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
45 
46  val = Pack_selector<Point_test<float>>::value;
47  BOOST_REQUIRE_EQUAL(val,PACKER_OBJECTS_WITH_POINTER_CHECK);
48 
49 
51  BOOST_REQUIRE_EQUAL(val,PACKER_GENERAL);
53  BOOST_REQUIRE_EQUAL(val,PACKER_GRID);
55  BOOST_REQUIRE_EQUAL(val,PACKER_ENCAP_OBJECTS);
56 
57  struct test_s
58  {
59  float a;
60  float b;
61 
62  static bool noPointers() {return true;}
63  };
64 
66  BOOST_REQUIRE_EQUAL(val,PACKER_OBJECTS_WITH_POINTER_CHECK);
67 
69 
70  {
71 
73 
74  typedef Point_test<float> pt;
75 
76  // Create all the objects we want to pack
77  unsigned char uc = 1;
78  char c = 2;
79  short s = 3;
80  unsigned short us = 4;
81  int i = 5;
82  unsigned int ui = 6;
83  long int li = 7;
84  unsigned long int uli = 8;
85  float f = 9;
86  double d = 10;
87 
88  openfpm::vector<Point_test<float>> v = allocate_openfpm<openfpm::vector<Point_test<float>>>(1024);
89 
91  p.fill();
92 
93  size_t sz[] = {16,16,16};
95  g.setMemory();
96  fill_grid<3>(g);
97 
98  grid_key_dx_iterator_sub<3> sub(g.getGrid(),{1,2,3},{5,6,7});
99 
100  // Here we start to push all the allocations required to pack all the data
101 
102  //std::vector<size_t> pap_prp;
103 
104  size_t size_total = 0;
105  size_t size_total_old = 0;
106 
108  BOOST_REQUIRE_EQUAL(size_total,sizeof(unsigned char));
109  size_total_old = size_total;
110 
112  BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(char));
113  size_total_old = size_total;
114 
116  BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(short));
117  size_total_old = size_total;
118 
120  BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(unsigned short));
121  size_total_old = size_total;
122 
124  BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(int));
125  size_total_old = size_total;
126 
128  BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(unsigned int));
129  size_total_old = size_total;
130 
132  BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(long int));
133  size_total_old = size_total;
134 
136  BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(long unsigned int));
137  size_total_old = size_total;
138 
140  BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(float));
141  size_total_old = size_total;
142 
144  BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(double));
145  size_total_old = size_total;
146 
147  Packer<Point_test<float>,HeapMemory>::packRequest(p,size_total);
148 
149 #ifndef SE_CLASS3
150  BOOST_REQUIRE_EQUAL(size_total - size_total_old,(sizeof(float)*4 + sizeof(float[3]) + sizeof(float[3][3])));
151 #endif
152  size_total_old = size_total;
153 
154  Packer<openfpm::vector<Point_test<float>>,HeapMemory>::packRequest<pt::x,pt::v>(v,size_total);
155 #ifndef SE_CLASS3
156  BOOST_REQUIRE_EQUAL(size_total - size_total_old,(sizeof(float) + sizeof(float[3])) * v.size() + sizeof(v.size()));
157 #endif
158  size_total_old = size_total;
159 
160  Packer<grid_cpu<3,Point_test<float>>,HeapMemory>::packRequest<pt::x,pt::v>(g,sub,size_total);
161 #ifndef SE_CLASS3
162  BOOST_REQUIRE_EQUAL(size_total - size_total_old,(sizeof(float) + sizeof(float[3])) * sub.getVolume());
163 #endif
164  // Calculate how much preallocated memory we need to pack all the objects
165  //size_t req = ExtPreAlloc<HeapMemory>::calculateMem(pap_prp);
166 
167  // allocate the memory
168  HeapMemory pmem;
169  pmem.allocate(size_total);
170  ExtPreAlloc<HeapMemory> & mem = *(new ExtPreAlloc<HeapMemory>(size_total,pmem));
171  mem.incRef();
172 
173  Pack_stat sts;
174 
175  // try to pack
180  Packer<int,HeapMemory>::pack(mem,5,sts);
186  Packer<Point_test<float>,HeapMemory>::pack(mem,p,sts);
187  Packer<openfpm::vector<Point_test<float>>,HeapMemory>::pack<pt::x,pt::v>(mem,v,sts);
188  Packer<grid_cpu<3,Point_test<float>>,HeapMemory>::pack<pt::x,pt::v>(mem,g,sub,sts);
189 
191 
193 
194  Unpack_stat ps;
195 
196  unsigned char uc2;
198  char c2;
200  short s2;
202  unsigned short us2;
204  int i2;
206  unsigned int ui2;
208  long int li2;
210  unsigned long int uli2;
212  float f2;
214  double d2;
216 
217  // Unpack the point and check
218  Point_test<float> p_test;
219  Unpacker<Point_test<float>,HeapMemory>::unpack(mem,p_test,ps);
220 
221  // Unpack the vector and check
223  v_test.resize(v.size());
224  Unpacker<openfpm::vector<Point_test<float>>,HeapMemory>::unpack<pt::x,pt::v>(mem,v_test,ps);
225 
227 
228  BOOST_REQUIRE_EQUAL(uc2,uc);
229  BOOST_REQUIRE_EQUAL(c2,c);
230  BOOST_REQUIRE_EQUAL(s2,s);
231  BOOST_REQUIRE_EQUAL(us2,us);
232  BOOST_REQUIRE_EQUAL(i2,i);
233  BOOST_REQUIRE_EQUAL(ui2,ui);
234  BOOST_REQUIRE_EQUAL(li2,li);
235  BOOST_REQUIRE_EQUAL(uli2,uli);
236  BOOST_REQUIRE_EQUAL(f2,f);
237  BOOST_REQUIRE_EQUAL(d2,d);
238 
239  bool val = (p_test == p);
240  BOOST_REQUIRE_EQUAL(true,val);
241 
242  auto it = v_test.getIterator();
243 
244  while (it.isNext())
245  {
246  float f1 = v_test.template get<pt::x>(it.get());
247  float f2 = v.template get<pt::x>(it.get());
248 
249  BOOST_REQUIRE_EQUAL(f1,f2);
250 
251  for (size_t i = 0 ; i < 3 ; i++)
252  {
253  f1 = v_test.template get<pt::v>(it.get())[i];
254  f2 = v.template get<pt::v>(it.get())[i];
255 
256  BOOST_REQUIRE_EQUAL(f1,f2);
257  }
258 
259  ++it;
260  }
261 
262  // Unpack the grid and check
263 
264  size_t sz2[] = {16,16,16};
265  grid_cpu<3,Point_test<float>> g_test(sz2);
266  g_test.setMemory();
267 
268  grid_key_dx_iterator_sub<3> sub2(g_test.getGrid(),{1,2,3},{5,6,7});
269 
270  Unpacker<grid_cpu<3,Point_test<float>>,HeapMemory>::unpack<pt::x,pt::v>(mem,sub2,g_test,ps);
271 
272  // Check the unpacked grid
273  sub2.reset();
274 
275  while (sub2.isNext())
276  {
277  float f1 = g_test.template get<pt::x>(sub2.get());
278  float f2 = g.template get<pt::x>(sub2.get());
279 
280  BOOST_REQUIRE_EQUAL(f1,f2);
281 
282  for (size_t i = 0 ; i < 3 ; i++)
283  {
284  f1 = g_test.template get<pt::v>(sub2.get())[i];
285  f2 = g.template get<pt::v>(sub2.get())[i];
286 
287  BOOST_REQUIRE_EQUAL(f1,f2);
288  }
289 
290  ++sub2;
291  }
292 
293  // destroy the packed memory
294  mem.decRef();
295  delete &mem;
296 
298 
299  }
300 }
301 
302 BOOST_AUTO_TEST_SUITE_END()
303 
304 
305 
306 #endif /* SRC_PACKER_UNIT_TESTS_HPP_ */
Transform the boost::fusion::vector into memory specification (memory_traits)
Definition: memory_conf.hpp:93
Unpacker class.
Definition: Packer_util.hpp:20
Pack selector.
This class allocate, and destroy CPU memory.
Definition: HeapMemory.hpp:39
void fill()
fill
Definition: Point_test.hpp:366
static size_t packRequest(const T &obj, size_t &req)
Error, no implementation.
Definition: Packer.hpp:59
Packing class.
Definition: Packer.hpp:44
virtual void incRef()
Increment the reference counter.
Definition: ExtPreAlloc.hpp:69
Unpacking status object.
Definition: Pack_stat.hpp:15
grid_key_dx< dim > get() const
Return the actual grid key iterator.
static void unpack(ExtPreAlloc< Mem >, T &obj)
Error, no implementation.
Definition: Unpacker.hpp:36
Declaration grid_key_dx_iterator_sub.
Definition: grid_sm.hpp:77
Test structure used for several test.
Definition: Point_test.hpp:105
void reset()
Reset the iterator (it restart from the beginning)
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:61
Packing status object.
Definition: Pack_stat.hpp:51
bool isNext()
Check if there is the next element.
static void pack(ExtPreAlloc< Mem >, const T &obj)
Error, no implementation.
Definition: Packer.hpp:51