OpenFPM_data  0.1.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Namespaces Functions Variables Typedefs Friends
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 
18 BOOST_AUTO_TEST_SUITE( packer_unpacker )
19 
20 BOOST_AUTO_TEST_CASE ( packer_unpacker_test )
21 {
23 
25  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
27  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
29  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
31  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
33  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
35  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
37  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
39  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
41  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
43  BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
44 
45  val = Pack_selector<Point_test<float>>::value;
46  BOOST_REQUIRE_EQUAL(val,PACKER_OBJECTS_WITH_POINTER_CHECK);
47 
48 
50  BOOST_REQUIRE_EQUAL(val,PACKER_VECTOR);
52  BOOST_REQUIRE_EQUAL(val,PACKER_GRID);
54  BOOST_REQUIRE_EQUAL(val,PACKER_ENCAP_OBJECTS);
55 
56  struct test_s
57  {
58  float a;
59  float b;
60 
61  static bool noPointers() {return true;}
62  };
63 
65  BOOST_REQUIRE_EQUAL(val,PACKER_OBJECTS_WITH_POINTER_CHECK);
66 
68 
69  {
70 
72 
73  typedef Point_test<float> pt;
74 
75  // Create all the objects we want to pack
76  unsigned char uc = 1;
77  char c = 2;
78  short s = 3;
79  unsigned short us = 4;
80  int i = 5;
81  unsigned int ui = 6;
82  long int li = 7;
83  unsigned long int uli = 8;
84  float f = 9;
85  double d = 10;
86 
87  openfpm::vector<Point_test<float>> v = allocate_openfpm(1024);
88 
90  p.fill();
91 
92  size_t sz[] = {16,16,16};
94  g.setMemory();
95  fill_grid<3>(g);
96  grid_key_dx_iterator_sub<3> sub(g.getGrid(),{1,2,3},{5,6,7});
97 
98  // Here we start to push all the allocations required to pack all the data
99 
100  std::vector<size_t> pap_prp;
101 
103  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],sizeof(unsigned char));
105  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],sizeof(char));
107  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],sizeof(short));
109  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],sizeof(unsigned short));
111  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],sizeof(int));
113  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],sizeof(unsigned int));
115  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],sizeof(long int));
117  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],sizeof(long unsigned int));
119  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],sizeof(float));
121  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],sizeof(double));
122  Packer<Point_test<float>,HeapMemory>::packRequest(pap_prp);
123  Packer<openfpm::vector<Point_test<float>>,HeapMemory>::packRequest<pt::x,pt::v>(v,pap_prp);
124  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],(sizeof(float) + sizeof(float[3])) * v.size());
125  Packer<grid_cpu<3,Point_test<float>>,HeapMemory>::packRequest<pt::x,pt::v>(g,sub,pap_prp);
126  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],(sizeof(float) + sizeof(float[3])) * sub.getVolume());
127 
128  // Calculate how much preallocated memory we need to pack all the objects
129  size_t req = ExtPreAlloc<HeapMemory>::calculateMem(pap_prp);
130 
131  // allocate the memory
132  HeapMemory pmem;
133  pmem.allocate(req);
134  ExtPreAlloc<HeapMemory> & mem = *(new ExtPreAlloc<HeapMemory>(pap_prp,pmem));
135  mem.incRef();
136 
137  Pack_stat sts;
138 
139  // try to pack
144  Packer<int,HeapMemory>::pack(mem,5,sts);
150  Packer<Point_test<float>,HeapMemory>::pack(mem,p,sts);
151  Packer<openfpm::vector<Point_test<float>>,HeapMemory>::pack<pt::x,pt::v>(mem,v,sts);
152  Packer<grid_cpu<3,Point_test<float>>,HeapMemory>::pack<pt::x,pt::v>(mem,g,sub,sts);
153 
155 
157 
158  Unpack_stat ps;
159 
160  unsigned char uc2;
162  char c2;
164  short s2;
166  unsigned short us2;
168  int i2;
170  unsigned int ui2;
172  long int li2;
174  unsigned long int uli2;
176  float f2;
178  double d2;
180 
181  // Unpack the point and check
182  Point_test<float> p_test;
183  Unpacker<Point_test<float>,HeapMemory>::unpack(mem,p_test,ps);
184 
185  // Unpack the vector and check
187  v_test.resize(v.size());
188  Unpacker<openfpm::vector<Point_test<float>>,HeapMemory>::unpack<pt::x,pt::v>(mem,v_test,ps);
189 
191 
192  BOOST_REQUIRE_EQUAL(uc2,uc);
193  BOOST_REQUIRE_EQUAL(c2,c);
194  BOOST_REQUIRE_EQUAL(s2,s);
195  BOOST_REQUIRE_EQUAL(us2,us);
196  BOOST_REQUIRE_EQUAL(i2,i);
197  BOOST_REQUIRE_EQUAL(ui2,ui);
198  BOOST_REQUIRE_EQUAL(li2,li);
199  BOOST_REQUIRE_EQUAL(uli2,uli);
200  BOOST_REQUIRE_EQUAL(f2,f);
201  BOOST_REQUIRE_EQUAL(d2,d);
202 
203  bool val = (p_test == p);
204  BOOST_REQUIRE_EQUAL(true,val);
205 
206  auto it = v_test.getIterator();
207 
208  while (it.isNext())
209  {
210  float f1 = v_test.template get<pt::x>(it.get());
211  float f2 = v.template get<pt::x>(it.get());
212 
213  BOOST_REQUIRE_EQUAL(f1,f2);
214 
215  for (size_t i = 0 ; i < 3 ; i++)
216  {
217  f1 = v_test.template get<pt::v>(it.get())[i];
218  f2 = v.template get<pt::v>(it.get())[i];
219 
220  BOOST_REQUIRE_EQUAL(f1,f2);
221  }
222 
223  ++it;
224  }
225 
226  // Unpack the grid and check
227 
228  size_t sz2[] = {16,16,16};
229  grid_cpu<3,Point_test<float>> g_test(sz2);
230  g_test.setMemory();
231  grid_key_dx_iterator_sub<3> sub2(g_test.getGrid(),{1,2,3},{5,6,7});
232 
233  Unpacker<grid_cpu<3,Point_test<float>>,HeapMemory>::unpack<pt::x,pt::v>(mem,sub2,g_test,ps);
234 
235  // Check the unpacked grid
236  sub2.reset();
237 
238  while (sub2.isNext())
239  {
240  float f1 = g_test.template get<pt::x>(sub2.get());
241  float f2 = g.template get<pt::x>(sub2.get());
242 
243  BOOST_REQUIRE_EQUAL(f1,f2);
244 
245  for (size_t i = 0 ; i < 3 ; i++)
246  {
247  f1 = g_test.template get<pt::v>(sub2.get())[i];
248  f2 = g.template get<pt::v>(sub2.get())[i];
249 
250  BOOST_REQUIRE_EQUAL(f1,f2);
251  }
252 
253  ++sub2;
254  }
255 
256  // destroy the packed memory
257  mem.decRef();
258  delete &mem;
259 
261 
262  }
263 }
264 
265 BOOST_AUTO_TEST_SUITE_END()
266 
267 
268 
269 #endif /* SRC_PACKER_UNIT_TESTS_HPP_ */
Transform the boost::fusion::vector into memory specification (memory_traits)
Definition: memory_conf.hpp:71
Unpacker class.
Definition: Unpacker.hpp:28
Pack selector.
bool isNext()
Check if there is the next element.
void fill()
fill
Definition: Point_test.hpp:227
static size_t packRequest(std::vector< size_t > &req)
Error, no implementation.
Definition: Packer.hpp:61
grid_key_dx< dim > get()
Return the actual grid key iterator.
Packing class.
Definition: Packer.hpp:46
Unpacking status object.
Definition: Pack_stat.hpp:15
void reset()
Reset the iterator (it restart from the beginning)
static void unpack(ExtPreAlloc< Mem >, T &obj)
Error, no implementation.
Definition: Unpacker.hpp:35
Test structure used for several test.
Definition: Point_test.hpp:72
Implementation of 1-D std::vector like structure.
Definition: map_grid.hpp:94
Packing status object.
Definition: Pack_stat.hpp:49
static void pack(ExtPreAlloc< Mem >, const T &obj)
Error, no implementation.
Definition: Packer.hpp:53