1 | #ifndef GRID_UNIT_TEST_HPP |
2 | #define GRID_UNIT_TEST_HPP |
3 | |
4 | #include "config.h" |
5 | #include "map_grid.hpp" |
6 | #include "Point_test.hpp" |
7 | #include "Space/Shape/HyperCube.hpp" |
8 | #include "timer.hpp" |
9 | #include "grid_util_test.hpp" |
10 | #include "grid_test_utils.hpp" |
11 | |
12 | #ifdef TEST_COVERAGE_MODE |
13 | constexpr int GS_SIZE = 8; |
14 | #else |
15 | constexpr int GS_SIZE = 128; |
16 | #endif |
17 | |
18 | template<unsigned int dim, typename g> void test_layout_gridNd(g & c3, size_t sz); |
19 | template<unsigned int dim, typename g> void test_layout_gridObjNd(g & c3, size_t sz); |
20 | template<typename g> void test_layout_grid3d(g & c3, size_t sz); |
21 | |
22 | /*! \brief Test all grid with dimensionality dim and size sz on all dimensions |
23 | * |
24 | * Test all grid with dimensionality dim and size sz on all dimensions |
25 | * |
26 | */ |
27 | template<unsigned int dim> void test_all_grid(size_t sz) |
28 | { |
29 | size_t szz[dim]; |
30 | |
31 | for (size_t i = 0 ; i < dim ; i++) |
32 | {szz[i] = sz;} |
33 | |
34 | {grid_cpu<dim, Point_test<float> > c3(szz); |
35 | c3.setMemory(); |
36 | test_layout_gridNd<dim>(c3,sz);} |
37 | |
38 | {grid_cpu<dim, Point_test<float> > c3(szz); |
39 | c3.setMemory(); |
40 | test_layout_gridObjNd<dim>(c3,sz);} |
41 | |
42 | #ifdef CUDA_GPU |
43 | |
44 | {grid_gpu<dim, Point_test<float> > c3(szz); |
45 | c3.setMemory(); |
46 | test_layout_gridNd<dim>(c3,sz);} |
47 | |
48 | {grid_gpu<dim, Point_test<float> > c3(szz); |
49 | c3.setMemory(); |
50 | test_layout_gridObjNd<dim>(c3,sz);} |
51 | |
52 | #endif |
53 | |
54 | {grid_cpu<dim, Point_test<float> > c3(szz); |
55 | c3.setMemory(); |
56 | test_layout_gridObjNd<dim>(c3,sz);} |
57 | |
58 | {grid_cpu<dim, Point_test<float> > c3(szz); |
59 | c3.setMemory(); |
60 | test_layout_gridNd<dim>(c3,sz);} |
61 | |
62 | #ifdef CUDA_GPU |
63 | |
64 | {grid_gpu<dim, Point_test<float> > c3(szz); |
65 | c3.setMemory(); |
66 | test_layout_gridNd<dim>(c3,sz);} |
67 | |
68 | {grid_gpu<dim, Point_test<float> > c3(szz); |
69 | c3.setMemory(); |
70 | test_layout_gridObjNd<dim>(c3,sz);} |
71 | |
72 | #endif |
73 | } |
74 | |
75 | |
76 | |
77 | template<typename g> void test_layout_grid3d(g & c3, size_t sz) |
78 | { |
79 | |
80 | #ifdef VERBOSE_TEST |
81 | std::cout << "3D Array with grid_key (without redundant dimension): " << "\n" ; |
82 | #endif |
83 | |
84 | //! [Access a grid c3 of size sz on each direction] |
85 | typedef Point_test<float> P; |
86 | |
87 | timer t; |
88 | t.start(); |
89 | |
90 | grid_key_dx<3> kk; |
91 | |
92 | for (size_t i = 0 ; i < sz ; i++) |
93 | { |
94 | for (size_t j = 0 ; j < sz ; j++) |
95 | { |
96 | for (size_t k = 0 ; k < sz ; k++) |
97 | { |
98 | |
99 | kk.set(i,j,k); |
100 | |
101 | c3.template get<P::x>(kk) = 1.1f; |
102 | c3.template get<P::y>(kk) = 1.2f; |
103 | c3.template get<P::z>(kk) = 1.3f; |
104 | c3.template get<P::s>(kk) = 1.0f; |
105 | |
106 | c3.template get<P::v>(kk)[0] = 1.0f; |
107 | c3.template get<P::v>(kk)[1] = 2.0f; |
108 | c3.template get<P::v>(kk)[2] = 3.0f; |
109 | |
110 | c3.template get<P::t>(kk)[0][0] = 1.0f; |
111 | c3.template get<P::t>(kk)[0][1] = 2.0f; |
112 | c3.template get<P::t>(kk)[0][2] = 3.0f; |
113 | c3.template get<P::t>(kk)[1][0] = 4.0f; |
114 | c3.template get<P::t>(kk)[1][1] = 5.0f; |
115 | c3.template get<P::t>(kk)[1][2] = 6.0f; |
116 | c3.template get<P::t>(kk)[2][0] = 7.0f; |
117 | c3.template get<P::t>(kk)[2][1] = 8.0f; |
118 | c3.template get<P::t>(kk)[2][2] = 9.0f; |
119 | |
120 | } |
121 | } |
122 | } |
123 | |
124 | //! [Access a grid c3 of size sz on each direction] |
125 | |
126 | #ifdef VERBOSE_TEST |
127 | t.stop(); |
128 | |
129 | std::cout << "End : " << sz*sz*sz*16*4 << " Byte " << " Bandwidth: " << sz*sz*sz*16*4/1024/1024/t.getwct() << " MB/s " ; |
130 | #endif |
131 | |
132 | /////////////////////////////////// MEM CHECK //////////////////////////////////////////////////////// |
133 | |
134 | bool passed = true; |
135 | |
136 | for (size_t i = 0 ; i < sz ; i++) |
137 | { |
138 | for (size_t j = 0 ; j < sz ; j++) |
139 | { |
140 | for (size_t k = 0 ; k < sz ; k++) |
141 | { |
142 | kk.set(i,j,k); |
143 | |
144 | c3.template get<P::x>(kk) = i; |
145 | c3.template get<P::y>(kk) = j; |
146 | c3.template get<P::z>(kk) = k; |
147 | c3.template get<P::s>(kk) = i+j+k; |
148 | |
149 | c3.template get<P::v>(kk)[0] = i; |
150 | c3.template get<P::v>(kk)[1] = j; |
151 | c3.template get<P::v>(kk)[2] = k; |
152 | |
153 | c3.template get<P::t>(kk)[0][0] = i+i; |
154 | c3.template get<P::t>(kk)[0][1] = i+j; |
155 | c3.template get<P::t>(kk)[0][2] = i+k; |
156 | c3.template get<P::t>(kk)[1][0] = j+i; |
157 | c3.template get<P::t>(kk)[1][1] = j+j; |
158 | c3.template get<P::t>(kk)[1][2] = j+k; |
159 | c3.template get<P::t>(kk)[2][0] = k+i; |
160 | c3.template get<P::t>(kk)[2][1] = k+j; |
161 | c3.template get<P::t>(kk)[2][2] = k+k; |
162 | } |
163 | } |
164 | } |
165 | |
166 | for (size_t i = 0 ; i < sz ; i++) |
167 | { |
168 | for (size_t j = 0 ; j < sz ; j++) |
169 | { |
170 | for (size_t k = 0 ; k < sz ; k++) |
171 | { |
172 | kk.set(i,j,k); |
173 | |
174 | if (c3.template get<P::x>(kk) != i) passed = false; |
175 | if (c3.template get<P::y>(kk) != j) passed = false; |
176 | if (c3.template get<P::z>(kk) != k) passed = false; |
177 | if (c3.template get<P::s>(kk) != i+j+k) passed = false; |
178 | |
179 | if (c3.template get<P::v>(kk)[0] != i) passed = false; |
180 | if (c3.template get<P::v>(kk)[1] != j) passed = false; |
181 | if (c3.template get<P::v>(kk)[2] != k) passed = false; |
182 | |
183 | if (c3.template get<P::t>(kk)[0][0] != i+i) passed = false; |
184 | if (c3.template get<P::t>(kk)[0][1] != i+j) passed = false; |
185 | if (c3.template get<P::t>(kk)[0][2] != i+k) passed = false; |
186 | if (c3.template get<P::t>(kk)[1][0] != j+i) passed = false; |
187 | if (c3.template get<P::t>(kk)[1][1] != j+j) passed = false; |
188 | if (c3.template get<P::t>(kk)[1][2] != j+k) passed = false; |
189 | if (c3.template get<P::t>(kk)[2][0] != k+i) passed = false; |
190 | if (c3.template get<P::t>(kk)[2][1] != k+j) passed = false; |
191 | if (c3.template get<P::t>(kk)[2][2] != k+k) passed = false; |
192 | } |
193 | } |
194 | } |
195 | |
196 | BOOST_REQUIRE_EQUAL(passed,true); |
197 | } |
198 | |
199 | template<unsigned int dim, typename g> void test_layout_gridObjNd(g & c3, size_t sz) |
200 | { |
201 | #ifdef VERBOSE_TEST |
202 | std::cout << dim << "D Array with grid_key (without redundant dimension): " << "\n" ; |
203 | |
204 | timer t; |
205 | t.start(); |
206 | #endif |
207 | |
208 | //! [Get the object in an N-dimensional grid with an iterator] |
209 | |
210 | typedef Point_test<float> P; |
211 | |
212 | grid_key_dx_iterator<dim> key_it = c3.getIterator(); |
213 | |
214 | while (key_it.isNext()) |
215 | { |
216 | grid_key_dx<dim> kk = key_it.get(); |
217 | |
218 | // Here we get a reference to the object, in reality we get an encapsulated object reference encapc |
219 | auto v = c3.get_o(kk); |
220 | |
221 | // An encapsulated object can be accessed like that |
222 | // (this will change the value in the grid) |
223 | v.template get<P::x>() = 1.1f; |
224 | v.template get<P::y>() = 1.2f; |
225 | v.template get<P::z>() = 1.3f; |
226 | v.template get<P::s>() = 1.0f; |
227 | |
228 | v.template get<P::v>()[0] = 1.0f; |
229 | v.template get<P::v>()[1] = 2.0f; |
230 | v.template get<P::v>()[2] = 3.0f; |
231 | |
232 | v.template get<P::t>()[0][0] = 1.0f; |
233 | v.template get<P::t>()[0][1] = 2.0f; |
234 | v.template get<P::t>()[0][2] = 3.0f; |
235 | v.template get<P::t>()[1][0] = 4.0f; |
236 | v.template get<P::t>()[1][1] = 5.0f; |
237 | v.template get<P::t>()[1][2] = 6.0f; |
238 | v.template get<P::t>()[2][0] = 7.0f; |
239 | v.template get<P::t>()[2][1] = 8.0f; |
240 | v.template get<P::t>()[2][2] = 9.0f; |
241 | |
242 | // From an encapsulated reference object you can create |
243 | // an object |
244 | Point_test<float> obj = c3.get_o(kk); |
245 | |
246 | // And do some operation |
247 | obj.fill(); |
248 | |
249 | // Note change obj does not change the grid |
250 | |
251 | ++key_it; |
252 | |
253 | } |
254 | |
255 | //! [Get the object in an N-dimensional grid with an iterator] |
256 | |
257 | #ifdef VERBOSE_TEST |
258 | t.stop(); |
259 | |
260 | std::cout << "End : " << pow(sz,dim)*16*4/1024/1024 << " MB " << " Bandwidth: " << pow(sz,dim)*16*4/1024/1024/t.getect() << " MB/s " << "\n" ; |
261 | #endif |
262 | |
263 | /////////////////////////////////// MEM CHECK //////////////////////////////////////////////////////// |
264 | |
265 | bool passed = true; |
266 | |
267 | //! Fill the grid with some data |
268 | fill_grid<dim>(c3); |
269 | |
270 | key_it = c3.getIterator(); |
271 | |
272 | while (key_it.isNext()) |
273 | { |
274 | grid_key_dx<dim> kk = key_it.get(); |
275 | |
276 | if (c3.template get<P::x>(kk) != c3.getGrid().LinId(kk)) passed = false; |
277 | if (c3.template get<P::y>(kk) != c3.getGrid().LinId(kk)+1) passed = false; |
278 | if (c3.template get<P::z>(kk) != c3.getGrid().LinId(kk)+2) passed = false; |
279 | if (c3.template get<P::s>(kk) != c3.getGrid().LinId(kk)+3) passed = false; |
280 | |
281 | if (c3.template get<P::v>(kk)[0] != c3.getGrid().LinId(kk)+123) passed = false; |
282 | if (c3.template get<P::v>(kk)[1] != c3.getGrid().LinId(kk)+124) passed = false; |
283 | if (c3.template get<P::v>(kk)[2] != c3.getGrid().LinId(kk)+125) passed = false; |
284 | |
285 | if (c3.template get<P::t>(kk)[0][0] != c3.getGrid().LinId(kk)+567) passed = false; |
286 | if (c3.template get<P::t>(kk)[0][1] != c3.getGrid().LinId(kk)+568) passed = false; |
287 | if (c3.template get<P::t>(kk)[0][2] != c3.getGrid().LinId(kk)+569) passed = false; |
288 | if (c3.template get<P::t>(kk)[1][0] != c3.getGrid().LinId(kk)+570) passed = false; |
289 | if (c3.template get<P::t>(kk)[1][1] != c3.getGrid().LinId(kk)+571) passed = false; |
290 | if (c3.template get<P::t>(kk)[1][2] != c3.getGrid().LinId(kk)+572) passed = false; |
291 | if (c3.template get<P::t>(kk)[2][0] != c3.getGrid().LinId(kk)+573) passed = false; |
292 | if (c3.template get<P::t>(kk)[2][1] != c3.getGrid().LinId(kk)+574) passed = false; |
293 | if (c3.template get<P::t>(kk)[2][2] != c3.getGrid().LinId(kk)+575) passed = false; |
294 | |
295 | ++key_it; |
296 | } |
297 | |
298 | BOOST_REQUIRE_EQUAL(passed,true); |
299 | } |
300 | |
301 | |
302 | BOOST_AUTO_TEST_SUITE( grid_test ) |
303 | |
304 | BOOST_AUTO_TEST_CASE( grid_safety_check ) |
305 | { |
306 | #if defined(SE_CLASS1) && defined (THROW_ON_ERROR) |
307 | |
308 | bool error = false; |
309 | |
310 | typedef Point_test<float> p; |
311 | |
312 | size_t sz[] = {16,16,16}; |
313 | |
314 | // Create a grid |
315 | |
316 | grid_cpu<3,Point_test<float>> g(sz); |
317 | grid_cpu<3,Point_test<float>> g2(sz); |
318 | |
319 | |
320 | |
321 | // try to access uninitialized grid |
322 | grid_key_dx<3> keyOut(23,1,1); |
323 | grid_key_dx<3> keyGood(15,1,1); |
324 | grid_key_dx<3> keyNeg(-1,0,0); |
325 | |
326 | error = false; |
327 | try |
328 | {g.get<p::x>(keyOut);} |
329 | catch (std::exception & e) |
330 | { |
331 | error = true; |
332 | BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error" ); |
333 | } |
334 | BOOST_REQUIRE_EQUAL(error,true); |
335 | |
336 | error = false; |
337 | g.setMemory(); |
338 | try |
339 | {g.get<p::x>(keyOut);} |
340 | catch (std::exception & e) |
341 | { |
342 | error = true; |
343 | BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error" ); |
344 | } |
345 | BOOST_REQUIRE_EQUAL(error,true); |
346 | |
347 | error = false; |
348 | Point_test<float> t; |
349 | try |
350 | {g.set(keyOut,t);} |
351 | catch (std::exception & e) |
352 | { |
353 | error = true; |
354 | BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error" ); |
355 | } |
356 | BOOST_REQUIRE_EQUAL(error,true); |
357 | |
358 | error = false; |
359 | try |
360 | {g.set(keyGood,g2,keyOut);} |
361 | catch (std::exception & e) |
362 | { |
363 | error = true; |
364 | BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error" ); |
365 | } |
366 | BOOST_REQUIRE_EQUAL(error,true); |
367 | |
368 | //// Negative key |
369 | |
370 | error = false; |
371 | try |
372 | {g.get<p::x>(keyNeg);} |
373 | catch (std::exception & e) |
374 | { |
375 | error = true; |
376 | BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error" ); |
377 | } |
378 | BOOST_REQUIRE_EQUAL(error,true); |
379 | |
380 | error = false; |
381 | Point_test<float> t2; |
382 | try |
383 | {g.set(keyNeg,t2);} |
384 | catch (std::exception & e) |
385 | { |
386 | error = true; |
387 | BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error" ); |
388 | } |
389 | BOOST_REQUIRE_EQUAL(error,true); |
390 | |
391 | error = false; |
392 | try |
393 | {g.set(keyGood,g2,keyNeg);} |
394 | catch (std::exception & e) |
395 | { |
396 | error = true; |
397 | BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error" ); |
398 | } |
399 | BOOST_REQUIRE_EQUAL(error,true); |
400 | |
401 | #endif |
402 | } |
403 | |
404 | BOOST_AUTO_TEST_CASE( grid_set_prp_check ) |
405 | { |
406 | size_t szz[2] = {8,8}; |
407 | |
408 | grid_cpu<2, Point_test<float> > c2(szz); |
409 | grid_cpu<2, Point_test<float> > c1(szz); |
410 | |
411 | c1.setMemory(); |
412 | c2.setMemory(); |
413 | |
414 | grid_key_dx<2> k1({0,0}); |
415 | |
416 | c2.template get<1>(k1) = 5.0; |
417 | |
418 | c1.template set<1>(k1,c2,k1); |
419 | |
420 | BOOST_REQUIRE_EQUAL(c1.template get<1>(k1),5.0); |
421 | } |
422 | |
423 | BOOST_AUTO_TEST_CASE( grid_use) |
424 | { |
425 | /* tensor<int,3,3,3> c; |
426 | tensor<tensor<int,3,3,3>,3,3,3> c2;*/ |
427 | |
428 | std::cout << "Grid unit test start" << "\n" ; |
429 | |
430 | size_t sz[3] = {GS_SIZE,GS_SIZE,GS_SIZE}; |
431 | |
432 | // test the grid from dimensionality 1 to 8 with several size non multiple of two |
433 | // Dimension 8-1 |
434 | |
435 | // With test coverage reduce the test size |
436 | |
437 | #ifndef TEST_COVERAGE_MODE |
438 | test_all_grid<8>(4); |
439 | test_all_grid<7>(8); |
440 | test_all_grid<6>(9); |
441 | test_all_grid<5>(18); |
442 | test_all_grid<4>(37); |
443 | test_all_grid<3>(126); |
444 | test_all_grid<2>(1414); |
445 | test_all_grid<1>(2000000); |
446 | |
447 | |
448 | // Special case grid of size 0 |
449 | test_all_grid<8>(0); |
450 | test_all_grid<7>(0); |
451 | test_all_grid<6>(0); |
452 | test_all_grid<5>(0); |
453 | test_all_grid<4>(0); |
454 | test_all_grid<3>(0); |
455 | test_all_grid<2>(0); |
456 | test_all_grid<1>(0); |
457 | #else |
458 | test_all_grid<4>(4); |
459 | test_all_grid<3>(8); |
460 | test_all_grid<2>(16); |
461 | test_all_grid<1>(256); |
462 | #endif |
463 | |
464 | // Test the 3d gpu grid with CudaMemory and HeapMemory with different size |
465 | |
466 | for (int i = 0 ; i <= GS_SIZE ; i++) |
467 | { |
468 | #ifdef CUDA_GPU |
469 | |
470 | { |
471 | //! [Definition and allocation of a 3D grid on GPU memory] |
472 | grid_gpu<3, Point_test<float> > c3(sz); |
473 | c3.setMemory(); |
474 | //! [Definition and allocation of a 3D grid on GPU memory] |
475 | test_layout_grid3d(c3,i); |
476 | } |
477 | |
478 | #endif |
479 | |
480 | {grid_cpu<3, Point_test<float> > c3(sz); |
481 | c3.setMemory(); |
482 | test_layout_grid3d(c3,i);} |
483 | |
484 | { |
485 | //! [Definition and allocation of a 3D grid on CPU memory] |
486 | grid_cpu<3, Point_test<float> > c3(sz); |
487 | c3.setMemory(); |
488 | //! [Definition and allocation of a 3D grid on CPU memory] |
489 | test_layout_grid3d(c3,i);} |
490 | |
491 | // Test the 3d cpu grid with Cudamemory and HeapMemory |
492 | |
493 | {grid_cpu<3, Point_test<float> > c3(sz); |
494 | c3.setMemory(); |
495 | test_layout_grid3d(c3,i);} |
496 | |
497 | } |
498 | |
499 | std::cout << "Grid unit test end" << "\n" ; |
500 | } |
501 | |
502 | BOOST_AUTO_TEST_CASE( grid_use_smb) |
503 | { |
504 | /* tensor<int,3,3,3> c; |
505 | tensor<tensor<int,3,3,3>,3,3,3> c2;*/ |
506 | |
507 | std::cout << "Grid smb unit test start" << "\n" ; |
508 | |
509 | size_t sz[3] = {GS_SIZE,GS_SIZE,GS_SIZE}; |
510 | |
511 | |
512 | // Test the 3d gpu grid with CudaMemory and HeapMemory with different size |
513 | |
514 | for (int i = 0 ; i <= GS_SIZE ; i+=4) |
515 | { |
516 | #ifdef CUDA_GPU |
517 | |
518 | { |
519 | //! [Definition and allocation of a 3D grid on GPU memory] |
520 | grid_gpu<3, Point_test<float>, grid_smb<3,4> > c3(sz); |
521 | c3.setMemory(); |
522 | //! [Definition and allocation of a 3D grid on GPU memory] |
523 | test_layout_grid3d(c3,i); |
524 | } |
525 | |
526 | #endif |
527 | |
528 | {grid_cpu<3, Point_test<float>, grid_smb<3,4> > c3(sz); |
529 | c3.setMemory(); |
530 | test_layout_grid3d(c3,i);} |
531 | |
532 | { |
533 | //! [Definition and allocation of a 3D grid on CPU memory] |
534 | grid_cpu<3, Point_test<float>, grid_smb<3,4> > c3(sz); |
535 | c3.setMemory(); |
536 | //! [Definition and allocation of a 3D grid on CPU memory] |
537 | test_layout_grid3d(c3,i);} |
538 | |
539 | // Test the 3d cpu grid with Cudamemory and HeapMemory |
540 | |
541 | {grid_cpu<3, Point_test<float>, grid_smb<3,4> > c3(sz); |
542 | c3.setMemory(); |
543 | test_layout_grid3d(c3,i);} |
544 | |
545 | } |
546 | |
547 | std::cout << "Grid unit test end" << "\n" ; |
548 | } |
549 | |
550 | BOOST_AUTO_TEST_CASE( grid_use_zmb) |
551 | { |
552 | /* tensor<int,3,3,3> c; |
553 | tensor<tensor<int,3,3,3>,3,3,3> c2;*/ |
554 | |
555 | std::cout << "Grid zmb unit test start" << "\n" ; |
556 | |
557 | size_t sz[3] = {GS_SIZE,GS_SIZE,GS_SIZE}; |
558 | |
559 | // Test the 3d gpu grid with CudaMemory and HeapMemory with different size |
560 | |
561 | for (int i = 4 ; i <= GS_SIZE ; i*=2) |
562 | { |
563 | #ifdef CUDA_GPU |
564 | |
565 | { |
566 | //! [Definition and allocation of a 3D grid on GPU memory] |
567 | grid_gpu<3, Point_test<float>, grid_zmb<3,4, long int> > c3(sz); |
568 | c3.setMemory(); |
569 | //! [Definition and allocation of a 3D grid on GPU memory] |
570 | test_layout_grid3d(c3,i); |
571 | } |
572 | |
573 | #endif |
574 | |
575 | {grid_cpu<3, Point_test<float>, grid_zmb<3,4, long int> > c3(sz); |
576 | c3.setMemory(); |
577 | test_layout_grid3d(c3,i);} |
578 | |
579 | { |
580 | //! [Definition and allocation of a 3D grid on CPU memory] |
581 | grid_cpu<3, Point_test<float>, grid_zmb<3,4, long int> > c3(sz); |
582 | c3.setMemory(); |
583 | //! [Definition and allocation of a 3D grid on CPU memory] |
584 | test_layout_grid3d(c3,i);} |
585 | |
586 | // Test the 3d cpu grid with Cudamemory and HeapMemory |
587 | |
588 | {grid_cpu<3, Point_test<float>, grid_zmb<3,4, long int> > c3(sz); |
589 | c3.setMemory(); |
590 | test_layout_grid3d(c3,i);} |
591 | |
592 | } |
593 | |
594 | std::cout << "Grid unit test end" << "\n" ; |
595 | } |
596 | |
597 | /* \brief This is an ordinary test simple 3D with plain C array |
598 | * |
599 | * This is an ordinary test simple 3D with plain C array |
600 | * |
601 | */ |
602 | |
603 | BOOST_AUTO_TEST_CASE( C_array_test ) |
604 | { |
605 | // Testing the grids |
606 | |
607 | #ifdef VERBOSE_TEST |
608 | std::cout << "Grid size known at runtime" << "\n" ; |
609 | std::cout << "1D Array with index calculation: " << "\n" ; |
610 | #endif |
611 | |
612 | Point_orig<float> * pA = new Point_orig<float>[GS_SIZE*GS_SIZE*GS_SIZE]; |
613 | |
614 | int gs_sq = GS_SIZE*GS_SIZE; |
615 | int gs = GS_SIZE; |
616 | |
617 | #ifdef VERBOSE_TEST |
618 | timer t; |
619 | t.start(); |
620 | #endif |
621 | |
622 | for (int i = 0 ; i < GS_SIZE ; i++) |
623 | { |
624 | for (int j = 0 ; j < GS_SIZE ; j++) |
625 | { |
626 | for (int k = 0 ; k < GS_SIZE ; k++) |
627 | { |
628 | pA[i*gs_sq+j*gs+k].x = 1.1f; |
629 | pA[i*gs_sq+j*gs+k].y = 1.2f; |
630 | pA[i*gs_sq+j*gs+k].z = 1.3f; |
631 | pA[i*gs_sq+j*gs+k].s = 1.0f; |
632 | |
633 | pA[i*gs_sq+j*gs+k].v[0] = 1.0f; |
634 | pA[i*gs_sq+j*gs+k].v[1] = 2.0f; |
635 | pA[i*gs_sq+j*gs+k].v[2] = 3.0f; |
636 | |
637 | pA[i*gs_sq+j*gs+k].t[0][0] = 1.0f; |
638 | pA[i*gs_sq+j*gs+k].t[0][1] = 2.0f; |
639 | pA[i*gs_sq+j*gs+k].t[0][2] = 3.0f; |
640 | pA[i*gs_sq+j*gs+k].t[1][0] = 4.0f; |
641 | pA[i*gs_sq+j*gs+k].t[1][1] = 5.0f; |
642 | pA[i*gs_sq+j*gs+k].t[1][2] = 6.0f; |
643 | pA[i*gs_sq+j*gs+k].t[2][0] = 7.0f; |
644 | pA[i*gs_sq+j*gs+k].t[2][1] = 8.0f; |
645 | pA[i*gs_sq+j*gs+k].t[2][2] = 9.0f; |
646 | } |
647 | } |
648 | } |
649 | |
650 | delete [] pA; |
651 | |
652 | #ifdef VERBOSE_TEST |
653 | t.stop(); |
654 | |
655 | std::cout << "End : " << GS_SIZE*GS_SIZE*GS_SIZE*16*4/1024/1024 << " MB " << " Bandwidth: " << GS_SIZE*GS_SIZE*GS_SIZE*16*4/1024/1024/t.getcputime() << " MB/s \n" ; |
656 | #endif |
657 | } |
658 | |
659 | BOOST_AUTO_TEST_CASE(grid_operator_equal) |
660 | { |
661 | //! [Create a grid g1 and copy into another g2] |
662 | |
663 | size_t sz[] = {16,16}; |
664 | |
665 | typedef Box<2,float> b; |
666 | |
667 | grid_cpu<2,Box<2,float>> g1(sz); |
668 | g1.setMemory(); |
669 | |
670 | auto it = g1.getIterator(); |
671 | |
672 | while (it.isNext()) |
673 | { |
674 | auto key = it.get(); |
675 | |
676 | g1.get<b::p1>(key)[0] = key.get(0); |
677 | g1.get<b::p2>(key)[1] = key.get(1); |
678 | |
679 | ++it; |
680 | } |
681 | |
682 | grid_cpu<2,Box<2,float>> g2; |
683 | g2 = g1; |
684 | |
685 | //! [Create a grid g1 and copy into another g2] |
686 | |
687 | bool ret = (g2 == g1); |
688 | |
689 | BOOST_REQUIRE_EQUAL(ret,true); |
690 | } |
691 | |
692 | /*! \brief Fill twi grid of boxes with data |
693 | * |
694 | * \param g1 Grid1 |
695 | * \param g2 Grid2 |
696 | * |
697 | */ |
698 | |
699 | void fill_2_grid_data(grid_cpu<2,Box<2,float>> & g1, grid_cpu<2,Box<2,float>> & g2) |
700 | { |
701 | typedef Box<2,float> b; |
702 | |
703 | auto it1 = g1.getIterator(); |
704 | |
705 | while (it1.isNext()) |
706 | { |
707 | auto key = it1.get(); |
708 | |
709 | g1.get<b::p1>(key)[0] = key.get(0); |
710 | g1.get<b::p2>(key)[1] = key.get(1); |
711 | |
712 | ++it1; |
713 | } |
714 | |
715 | auto it2 = g2.getIterator(); |
716 | |
717 | while (it2.isNext()) |
718 | { |
719 | auto key = it2.get(); |
720 | |
721 | g2.get<b::p1>(key)[0] = key.get(0); |
722 | g2.get<b::p2>(key)[1] = key.get(1); |
723 | |
724 | ++it2; |
725 | } |
726 | |
727 | |
728 | } |
729 | |
730 | BOOST_AUTO_TEST_CASE(grid_operator_swap) |
731 | { |
732 | size_t sz1[] = {16,16}; |
733 | size_t sz2[] = {5,5}; |
734 | |
735 | grid_cpu<2,Box<2,float>> g1_old(sz1); |
736 | g1_old.setMemory(); |
737 | grid_cpu<2,Box<2,float>> g2_old(sz2); |
738 | g2_old.setMemory(); |
739 | grid_cpu<2,Box<2,float>> g1(sz1); |
740 | g1.setMemory(); |
741 | grid_cpu<2,Box<2,float>> g2(sz2); |
742 | g2.setMemory(); |
743 | |
744 | fill_2_grid_data(g1_old,g2_old); |
745 | fill_2_grid_data(g1,g2); |
746 | |
747 | //! [swap the memory of two grids] |
748 | |
749 | g2.swap(g1); |
750 | |
751 | //! [swap the memory of two grids] |
752 | |
753 | bool ret = (g2 == g1_old); |
754 | BOOST_REQUIRE_EQUAL(ret,true); |
755 | ret = (g1 == g2_old); |
756 | BOOST_REQUIRE_EQUAL(ret,true); |
757 | } |
758 | |
759 | BOOST_AUTO_TEST_CASE(grid_resize_less) |
760 | { |
761 | size_t sz1[] = {256,256}; |
762 | size_t sz2[] = {5,5}; |
763 | |
764 | grid_cpu<2,Box<2,float>> g1(sz1); |
765 | g1.setMemory(); |
766 | |
767 | fill_2_grid_data(g1,g1); |
768 | |
769 | |
770 | g1.resize(sz2); |
771 | |
772 | BOOST_REQUIRE_EQUAL(g1.size(),25ul); |
773 | } |
774 | |
775 | BOOST_AUTO_TEST_CASE(copy_encap_vector_fusion_test) |
776 | { |
777 | size_t sz2[] = {5,5}; |
778 | grid_cpu<2,aggregate<float,float[3],float[3][3]>> g(sz2); |
779 | g.setMemory(); |
780 | |
781 | aggregate<float,float[3],float[3][3]>::type tmp; |
782 | |
783 | grid_key_dx<2> key({0,0}); |
784 | grid_key_dx<2> key1({1,1}); |
785 | |
786 | g.template get<0>(key) = 1.0; |
787 | |
788 | g.template get<1>(key)[0] = 2.0; |
789 | g.template get<1>(key)[1] = 3.0; |
790 | g.template get<1>(key)[2] = 4.0; |
791 | |
792 | g.template get<2>(key)[0][0] = 5.0; |
793 | g.template get<2>(key)[0][1] = 6.0; |
794 | g.template get<2>(key)[0][2] = 7.0; |
795 | g.template get<2>(key)[1][0] = 8.0; |
796 | g.template get<2>(key)[1][1] = 9.0; |
797 | g.template get<2>(key)[1][2] = 10.0; |
798 | g.template get<2>(key)[2][0] = 11.0; |
799 | g.template get<2>(key)[2][1] = 12.0; |
800 | g.template get<2>(key)[2][2] = 13.0; |
801 | |
802 | auto ge = g.get_o(key); |
803 | copy_encap_vector_fusion<decltype(g.get_o(key)),typename aggregate<float,float[3],float[3][3]>::type> cp(ge,tmp); |
804 | boost::mpl::for_each_ref< boost::mpl::range_c<int,0,aggregate<float,float[3],float[3][3]>::max_prop> >(cp); |
805 | |
806 | g.get_o(key1) = tmp; |
807 | |
808 | BOOST_REQUIRE_EQUAL(g.template get<0>(key),g.template get<0>(key1)); |
809 | |
810 | BOOST_REQUIRE_EQUAL(g.template get<1>(key)[0],g.template get<1>(key1)[0]); |
811 | BOOST_REQUIRE_EQUAL(g.template get<1>(key)[1],g.template get<1>(key1)[1]); |
812 | BOOST_REQUIRE_EQUAL(g.template get<1>(key)[2],g.template get<1>(key1)[2]); |
813 | |
814 | BOOST_REQUIRE_EQUAL(g.template get<2>(key)[0][0],g.template get<2>(key1)[0][0]); |
815 | BOOST_REQUIRE_EQUAL(g.template get<2>(key)[0][1],g.template get<2>(key1)[0][1]); |
816 | BOOST_REQUIRE_EQUAL(g.template get<2>(key)[0][2],g.template get<2>(key1)[0][2]); |
817 | BOOST_REQUIRE_EQUAL(g.template get<2>(key)[1][0],g.template get<2>(key1)[1][0]); |
818 | BOOST_REQUIRE_EQUAL(g.template get<2>(key)[1][1],g.template get<2>(key1)[1][1]); |
819 | BOOST_REQUIRE_EQUAL(g.template get<2>(key)[1][2],g.template get<2>(key1)[1][2]); |
820 | BOOST_REQUIRE_EQUAL(g.template get<2>(key)[2][0],g.template get<2>(key1)[2][0]); |
821 | BOOST_REQUIRE_EQUAL(g.template get<2>(key)[2][1],g.template get<2>(key1)[2][1]); |
822 | BOOST_REQUIRE_EQUAL(g.template get<2>(key)[2][2],g.template get<2>(key1)[2][2]); |
823 | } |
824 | |
825 | |
826 | BOOST_AUTO_TEST_CASE(grid_test_copy_to) |
827 | { |
828 | size_t sz_dst[] = {5,5}; |
829 | size_t sz_src[] = {3,2}; |
830 | grid_cpu<2,aggregate<float,float[3],float[3][3]>> g_dst(sz_dst); |
831 | grid_cpu<2,aggregate<float,float[3],float[3][3]>> g_src(sz_src); |
832 | |
833 | Box<2,size_t> box_dst({1,2},{2,3}); |
834 | Box<2,size_t> box_src({1,0},{2,1}); |
835 | |
836 | copy_test(g_src,g_dst,box_src,box_dst); |
837 | } |
838 | |
839 | |
840 | BOOST_AUTO_TEST_SUITE_END() |
841 | |
842 | #endif |
843 | |