OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
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 "util/cudify/cudify.hpp"
12#include "Pack_selector.hpp"
13#include "Packer.hpp"
14#include "Unpacker.hpp"
15#include "Grid/grid_util_test.hpp"
16#include <iostream>
17#include "Vector/vector_test_util.hpp"
18#include "data_type/aggregate.hpp"
19
20BOOST_AUTO_TEST_SUITE( packer_unpacker )
21
22BOOST_AUTO_TEST_CASE ( packer_unpacker_test )
23{
25
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);
45 BOOST_REQUIRE_EQUAL(val,PACKER_PRIMITIVE);
46
48 BOOST_REQUIRE_EQUAL(val,PACKER_OBJECTS_WITH_POINTER_CHECK);
49
50
52 BOOST_REQUIRE_EQUAL(val,PACKER_GENERAL);
54 BOOST_REQUIRE_EQUAL(val,PACKER_GRID);
56 BOOST_REQUIRE_EQUAL(val,PACKER_ENCAP_OBJECTS);
57
58 struct test_s
59 {
60 float a;
61 float b;
62
63 static bool noPointers() {return true;}
64 };
65
67 BOOST_REQUIRE_EQUAL(val,PACKER_OBJECTS_WITH_POINTER_CHECK);
68
70
71 {
72
74
75 typedef Point_test<float> pt;
76
77 // Create all the objects we want to pack
78 unsigned char uc = 1;
79 char c = 2;
80 short s = 3;
81 unsigned short us = 4;
82 int i = 5;
83 unsigned int ui = 6;
84 long int li = 7;
85 unsigned long int uli = 8;
86 float f = 9;
87 double d = 10;
88
89 openfpm::vector<Point_test<float>> v = allocate_openfpm<openfpm::vector<Point_test<float>>>(1024);
90
92 p.fill();
93
94 size_t sz[] = {16,16,16};
96 g.setMemory();
97 fill_grid<3>(g);
98
99 grid_key_dx_iterator_sub<3> sub(g.getGrid(),{1,2,3},{5,6,7});
100
101 // Here we start to push all the allocations required to pack all the data
102
103 //std::vector<size_t> pap_prp;
104
105 size_t size_total = 0;
106 size_t size_total_old = 0;
107
109 BOOST_REQUIRE_EQUAL(size_total,sizeof(unsigned char));
110 size_total_old = size_total;
111
113 BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(char));
114 size_total_old = size_total;
115
117 BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(short));
118 size_total_old = size_total;
119
121 BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(unsigned short));
122 size_total_old = size_total;
123
125 BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(int));
126 size_total_old = size_total;
127
129 BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(unsigned int));
130 size_total_old = size_total;
131
133 BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(long int));
134 size_total_old = size_total;
135
137 BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(long unsigned int));
138 size_total_old = size_total;
139
141 BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(float));
142 size_total_old = size_total;
143
145 BOOST_REQUIRE_EQUAL(size_total - size_total_old,sizeof(double));
146 size_total_old = size_total;
147
148 Packer<Point_test<float>,HeapMemory>::packRequest(p,size_total);
149
150#ifndef SE_CLASS3
151 BOOST_REQUIRE_EQUAL(size_total - size_total_old,(sizeof(float)*4 + sizeof(float[3]) + sizeof(float[3][3])));
152#endif
153 size_total_old = size_total;
154
155 Packer<openfpm::vector<Point_test<float>>,HeapMemory>::packRequest<pt::x,pt::v>(v,size_total);
156#ifndef SE_CLASS3
157 BOOST_REQUIRE_EQUAL(size_total - size_total_old,(sizeof(float) + sizeof(float[3])) * v.size() + sizeof(v.size()));
158#endif
159 size_total_old = size_total;
160
161 Packer<grid_cpu<3,Point_test<float>>,HeapMemory>::packRequest<decltype(sub),pt::x,pt::v>(g,sub,size_total);
162#ifndef SE_CLASS3
163 BOOST_REQUIRE_EQUAL(size_total - size_total_old,(sizeof(float) + sizeof(float[3])) * sub.getVolume());
164#endif
165 // Calculate how much preallocated memory we need to pack all the objects
166 //size_t req = ExtPreAlloc<HeapMemory>::calculateMem(pap_prp);
167
168 // allocate the memory
169 HeapMemory pmem;
170 pmem.allocate(size_total);
171 ExtPreAlloc<HeapMemory> & mem = *(new ExtPreAlloc<HeapMemory>(size_total,pmem));
172 mem.incRef();
173
174 Pack_stat sts;
175
176 // try to pack
187 Packer<Point_test<float>,HeapMemory>::pack(mem,p,sts);
188 Packer<openfpm::vector<Point_test<float>>,HeapMemory>::pack<pt::x,pt::v>(mem,v,sts);
189 Packer<grid_cpu<3,Point_test<float>>,HeapMemory>::pack<decltype(sub),pt::x,pt::v>(mem,g,sub,sts);
190
192
194
195 Unpack_stat ps;
196
197 unsigned char uc2;
199 char c2;
201 short s2;
203 unsigned short us2;
205 int i2;
207 unsigned int ui2;
209 long int li2;
211 unsigned long int uli2;
213 float f2;
215 double d2;
217
218 // Unpack the point and check
219 Point_test<float> p_test;
220 Unpacker<Point_test<float>,HeapMemory>::unpack(mem,p_test,ps);
221
222 // Unpack the vector and check
224 v_test.resize(v.size());
225 Unpacker<openfpm::vector<Point_test<float>>,HeapMemory>::unpack<pt::x,pt::v>(mem,v_test,ps);
226
228
229 BOOST_REQUIRE_EQUAL(uc2,uc);
230 BOOST_REQUIRE_EQUAL(c2,c);
231 BOOST_REQUIRE_EQUAL(s2,s);
232 BOOST_REQUIRE_EQUAL(us2,us);
233 BOOST_REQUIRE_EQUAL(i2,i);
234 BOOST_REQUIRE_EQUAL(ui2,ui);
235 BOOST_REQUIRE_EQUAL(li2,li);
236 BOOST_REQUIRE_EQUAL(uli2,uli);
237 BOOST_REQUIRE_EQUAL(f2,f);
238 BOOST_REQUIRE_EQUAL(d2,d);
239
240 bool val = (p_test == p);
241 BOOST_REQUIRE_EQUAL(true,val);
242
243 auto it = v_test.getIterator();
244
245 while (it.isNext())
246 {
247 float f1 = v_test.template get<pt::x>(it.get());
248 float f2 = v.template get<pt::x>(it.get());
249
250 BOOST_REQUIRE_EQUAL(f1,f2);
251
252 for (size_t i = 0 ; i < 3 ; i++)
253 {
254 f1 = v_test.template get<pt::v>(it.get())[i];
255 f2 = v.template get<pt::v>(it.get())[i];
256
257 BOOST_REQUIRE_EQUAL(f1,f2);
258 }
259
260 ++it;
261 }
262
263 // Unpack the grid and check
264
265 size_t sz2[] = {16,16,16};
266 grid_cpu<3,Point_test<float>> g_test(sz2);
267 g_test.setMemory();
268
269 grid_key_dx_iterator_sub<3> sub2(g_test.getGrid(),{1,2,3},{5,6,7});
270
271 int ctx = 0;
272 Unpacker<grid_cpu<3,Point_test<float>>,HeapMemory>::unpack<decltype(sub2),int,pt::x,pt::v>(mem,sub2,g_test,ps,ctx,rem_copy_opt::NONE_OPT);
273
274 // Check the unpacked grid
275 sub2.reset();
276
277 while (sub2.isNext())
278 {
279 float f1 = g_test.template get<pt::x>(sub2.get());
280 float f2 = g.template get<pt::x>(sub2.get());
281
282 BOOST_REQUIRE_EQUAL(f1,f2);
283
284 for (size_t i = 0 ; i < 3 ; i++)
285 {
286 f1 = g_test.template get<pt::v>(sub2.get())[i];
287 f2 = g.template get<pt::v>(sub2.get())[i];
288
289 BOOST_REQUIRE_EQUAL(f1,f2);
290 }
291
292 ++sub2;
293 }
294
295 // destroy the packed memory
296 mem.decRef();
297 delete &mem;
298
300
301 }
302}
303
304BOOST_AUTO_TEST_CASE ( packer_selector_test )
305{
306 BOOST_REQUIRE_EQUAL(Pack_selector<int>::value,PACKER_PRIMITIVE);
307 BOOST_REQUIRE_EQUAL(Pack_selector<float>::value,PACKER_PRIMITIVE);
308 BOOST_REQUIRE_EQUAL(Pack_selector<double>::value,PACKER_PRIMITIVE);
309 BOOST_REQUIRE_EQUAL(Pack_selector<long int>::value,PACKER_PRIMITIVE);
310
311 BOOST_REQUIRE_EQUAL(Pack_selector<int[3]>::value,PACKER_ARRAY_CP_PRIMITIVE);
312 BOOST_REQUIRE_EQUAL(Pack_selector<float[3][3]>::value,PACKER_ARRAY_CP_PRIMITIVE);
313 BOOST_REQUIRE_EQUAL(Pack_selector<double[5][6]>::value,PACKER_ARRAY_CP_PRIMITIVE);
314 BOOST_REQUIRE_EQUAL(Pack_selector<long int[5][8][9]>::value,PACKER_ARRAY_CP_PRIMITIVE);
315
316 BOOST_REQUIRE_EQUAL(Pack_selector<openfpm::vector<float>>::value,PACKER_GENERAL);
318 BOOST_REQUIRE_EQUAL(aa,PACKER_GENERAL);
320 BOOST_REQUIRE_EQUAL(aa,PACKER_GENERAL);
321
323 BOOST_REQUIRE_EQUAL(aa,PACKER_GRID);
324
325
327 aa = Pack_selector<decltype(vd.get(0))>::value;
328 BOOST_REQUIRE_EQUAL(aa,PACKER_ENCAP_OBJECTS);
329
330 size_t sz[3] = {6,6,6};
332 aa = Pack_selector<decltype(gd.get_o(grid_key_dx<3>({0,0,0})))>::value;
333 BOOST_REQUIRE_EQUAL(aa,PACKER_ENCAP_OBJECTS);
334
335 struct a_test
336 {
337 int a;
338 int b;
339 int c;
340 };
341
342 BOOST_REQUIRE_EQUAL(Pack_selector<a_test>::value,PACKER_OBJECTS_WITH_WARNING_POINTERS);
343
344 struct b_test
345 {
346 int a;
347 int b;
348 int c;
349
350 static bool noPointers() {return true;}
351 };
352
353 BOOST_REQUIRE_EQUAL(Pack_selector<b_test>::value,PACKER_OBJECTS_WITH_POINTER_CHECK);
354}
355
356BOOST_AUTO_TEST_CASE ( packer_memory_traits_inte )
357{
358
359}
360
361BOOST_AUTO_TEST_SUITE_END()
362
363
364
365#endif /* SRC_PACKER_UNIT_TESTS_HPP_ */
virtual void decRef()
Decrement the reference counter.
virtual void incRef()
Increment the reference counter.
This class allocate, and destroy CPU memory.
virtual bool allocate(size_t sz)
allocate memory
Packing status object.
Definition Pack_stat.hpp:61
Packing class.
Definition Packer.hpp:50
static void pack(ExtPreAlloc< Mem >, const T &obj)
Error, no implementation.
Definition Packer.hpp:56
static size_t packRequest(const T &obj, size_t &req)
Error, no implementation.
Definition Packer.hpp:66
Test structure used for several test.
void fill()
fill
Unpacking status object.
Definition Pack_stat.hpp:16
Unpacker class.
Definition Unpacker.hpp:34
static void unpack(ExtPreAlloc< Mem >, T &obj)
Error, no implementation.
Definition Unpacker.hpp:40
Declaration grid_key_dx_iterator_sub.
grid_key_dx is the key to access any element in the grid
Definition grid_key.hpp:19
Implementation of 1-D std::vector like structure.
Pack selector.
Transform the boost::fusion::vector into memory specification (memory_traits)