OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
grid_unit_tests.hpp
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 
11 #ifdef TEST_COVERAGE_MODE
12 #define GS_SIZE 8
13 #else
14 #define GS_SIZE 128
15 #endif
16 
17 template<unsigned int dim, typename g> void test_layout_gridNd(g & c3, size_t sz);
18 template<unsigned int dim, typename g> void test_layout_gridObjNd(g & c3, size_t sz);
19 template<typename g> void test_layout_grid3d(g & c3, size_t sz);
20 
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 
35  c3.setMemory();
36  test_layout_gridNd<dim>(c3,sz);}
37 
39  c3.setMemory();
40  test_layout_gridObjNd<dim>(c3,sz);}
41 
42 #ifdef CUDA_GPU
43 
45  c3.template setMemory();
46  test_layout_gridNd<dim>(c3,sz);}
47 
49  c3.template setMemory();
50  test_layout_gridObjNd<dim>(c3,sz);}
51 
52 #endif
53 
55  c3.setMemory();
56  test_layout_gridObjNd<dim>(c3,sz);}
57 
59  c3.setMemory();
60  test_layout_gridNd<dim>(c3,sz);}
61 
62 #ifdef CUDA_GPU
63 
65  c3.setMemory();
66  test_layout_gridNd<dim>(c3,sz);}
67 
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 
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 
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 
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 
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 
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 
264 
265  bool passed = true;
266 
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 template<unsigned int dim, typename g> void test_layout_gridNd(g & c3, size_t sz)
302 {
303 #ifdef VERBOSE_TEST
304  std::cout << dim << "D Array with grid_key (without redundant dimension): " << "\n";
305 
306  timer t;
307  t.start();
308 #endif
309 
311  typedef Point_test<float> P;
312 
313  grid_key_dx_iterator<dim> key_it = c3.getIterator();
314 
315  while (key_it.isNext())
316  {
317  grid_key_dx<dim> kk = key_it.get();
318 
319  c3.template get<P::x>(kk) = 1.1f;
320  c3.template get<P::y>(kk) = 1.2f;
321  c3.template get<P::z>(kk) = 1.3f;
322  c3.template get<P::s>(kk) = 1.0f;
323 
324  c3.template get<P::v>(kk)[0] = 1.0f;
325  c3.template get<P::v>(kk)[1] = 2.0f;
326  c3.template get<P::v>(kk)[2] = 3.0f;
327 
328  c3.template get<P::t>(kk)[0][0] = 1.0f;
329  c3.template get<P::t>(kk)[0][1] = 2.0f;
330  c3.template get<P::t>(kk)[0][2] = 3.0f;
331  c3.template get<P::t>(kk)[1][0] = 4.0f;
332  c3.template get<P::t>(kk)[1][1] = 5.0f;
333  c3.template get<P::t>(kk)[1][2] = 6.0f;
334  c3.template get<P::t>(kk)[2][0] = 7.0f;
335  c3.template get<P::t>(kk)[2][1] = 8.0f;
336  c3.template get<P::t>(kk)[2][2] = 9.0f;
337 
338  ++key_it;
339 
340  }
342 
343 #ifdef VERBOSE_TEST
344  t.stop();
345 
346  std::cout << "End : " << pow(sz,dim)*16*4/1024/1024 << " MB " << " Bandwidth: " << pow(sz,dim)*16*4/1024/1024/t.getwct() << " MB/s " << "\n";
347 #endif
348 
350 
351  bool passed = true;
352 
353  key_it = c3.getIterator();
354 
355  while (key_it.isNext())
356  {
357  grid_key_dx<dim> kk = key_it.get();
358 
359  c3.template get<P::x>(kk) = c3.getGrid().LinId(kk);
360  c3.template get<P::y>(kk) = c3.getGrid().LinId(kk)+1;
361  c3.template get<P::z>(kk) = c3.getGrid().LinId(kk)+2;
362  c3.template get<P::s>(kk) = c3.getGrid().LinId(kk)+3;
363 
364  c3.template get<P::v>(kk)[0] = c3.getGrid().LinId(kk)+123;
365  c3.template get<P::v>(kk)[1] = c3.getGrid().LinId(kk)+124;
366  c3.template get<P::v>(kk)[2] = c3.getGrid().LinId(kk)+125;
367 
368  c3.template get<P::t>(kk)[0][0] = c3.getGrid().LinId(kk)+567;
369  c3.template get<P::t>(kk)[0][1] = c3.getGrid().LinId(kk)+568;
370  c3.template get<P::t>(kk)[0][2] = c3.getGrid().LinId(kk)+569;
371  c3.template get<P::t>(kk)[1][0] = c3.getGrid().LinId(kk)+570;
372  c3.template get<P::t>(kk)[1][1] = c3.getGrid().LinId(kk)+571;
373  c3.template get<P::t>(kk)[1][2] = c3.getGrid().LinId(kk)+572;
374  c3.template get<P::t>(kk)[2][0] = c3.getGrid().LinId(kk)+573;
375  c3.template get<P::t>(kk)[2][1] = c3.getGrid().LinId(kk)+574;
376  c3.template get<P::t>(kk)[2][2] = c3.getGrid().LinId(kk)+575;
377 
378  ++key_it;
379  }
380 
381 
382  key_it = c3.getIterator();
383 
384  while (key_it.isNext())
385  {
386  grid_key_dx<dim> kk = key_it.get();
387 
388  if (c3.template get<P::x>(kk) != c3.getGrid().LinId(kk)) passed = false;
389  if (c3.template get<P::y>(kk) != c3.getGrid().LinId(kk)+1) passed = false;
390  if (c3.template get<P::z>(kk) != c3.getGrid().LinId(kk)+2) passed = false;
391  if (c3.template get<P::s>(kk) != c3.getGrid().LinId(kk)+3) passed = false;
392 
393  if (c3.template get<P::v>(kk)[0] != c3.getGrid().LinId(kk)+123) passed = false;
394  if (c3.template get<P::v>(kk)[1] != c3.getGrid().LinId(kk)+124) passed = false;
395  if (c3.template get<P::v>(kk)[2] != c3.getGrid().LinId(kk)+125) passed = false;
396 
397  if (c3.template get<P::t>(kk)[0][0] != c3.getGrid().LinId(kk)+567) passed = false;
398  if (c3.template get<P::t>(kk)[0][1] != c3.getGrid().LinId(kk)+568) passed = false;
399  if (c3.template get<P::t>(kk)[0][2] != c3.getGrid().LinId(kk)+569) passed = false;
400  if (c3.template get<P::t>(kk)[1][0] != c3.getGrid().LinId(kk)+570) passed = false;
401  if (c3.template get<P::t>(kk)[1][1] != c3.getGrid().LinId(kk)+571) passed = false;
402  if (c3.template get<P::t>(kk)[1][2] != c3.getGrid().LinId(kk)+572) passed = false;
403  if (c3.template get<P::t>(kk)[2][0] != c3.getGrid().LinId(kk)+573) passed = false;
404  if (c3.template get<P::t>(kk)[2][1] != c3.getGrid().LinId(kk)+574) passed = false;
405  if (c3.template get<P::t>(kk)[2][2] != c3.getGrid().LinId(kk)+575) passed = false;
406 
407  ++key_it;
408  }
409 
410  BOOST_REQUIRE_EQUAL(passed,true);
411 
412  // Check sub iterator
413 
414  /*
415  * Basically we first fill the interior part of the grid than the borders
416  * creating sub iterator always of smaller dimension
417  *
418  * Example:
419  *
420  * 2D
421  *
422  * if we have a square grid 16x16 we first will with 1 the interior 14x14 grid
423  *
424  * than the 4 line borders 14x1 with one
425  * than the 4 point borders 1x1
426  *
427  * We check that
428  *
429  * 1) The number of points for each sub-grid correspond
430  * 2) No point is filled more than one time
431  * 3) All point are filled
432  *
433  * we use the property x of c3
434  *
435  */
436 
437  // Erase the property x
438 
439  key_it = c3.getIterator();
440 
441  while (key_it.isNext())
442  {
443  grid_key_dx<dim> kk = key_it.get();
444 
445  c3.template get<P::x>(kk) = 0.0;
446 
447  ++key_it;
448  }
449 
450  for(size_t i = 0 ; i <= dim ; i++)
451  {
452  // get the combination of dimension dim-i
453  std::vector<comb<dim>> combs = HyperCube<dim>::getCombinations_R(dim-i);
454 
455  // For each combination create a sub iterator
456 
457  for (size_t j = 0 ; j < combs.size() ; j++)
458  {
459  // Grid key of the sub-iterator
460 
461  grid_key_dx<dim> start;
462  grid_key_dx<dim> stop;
463 
464  // sub iterator
465 
466  for (size_t k = 0 ; k < dim ; k++)
467  {
468  // if combination is 0 the hyper-cube
469 
470  if (combs[j].c[k] == -1)
471  {
472  start.set_d(k,0);
473  stop.set_d(k,0);
474  }
475  else if (combs[j].c[k] == 1)
476  {
477  start.set_d(k,c3.getGrid().size(k)-1);
478  stop.set_d(k,c3.getGrid().size(k)-1);
479  }
480  else
481  {
482  start.set_d(k,1);
483  stop.set_d(k,c3.getGrid().size(k)-2);
484  }
485  }
486 
487  bool make_test = true;
488 
489 #ifdef SE_CLASS1
490 
491  if (c3.size() == 0)
492  {make_test = false;}
493 
494 #endif
495 
496  if (make_test == true)
497  {
498  auto key_it = c3.getSubIterator(start,stop);
499 
500  while (key_it.isNext())
501  {
502  grid_key_dx<dim> kk = key_it.get();
503 
504  BOOST_REQUIRE_EQUAL(c3.template get<P::x>(kk),0.0);
505 
506  c3.template get<P::x>(kk) = 1.0;
507 
508  ++key_it;
509  }
510  }
511  }
512  }
513 
514  // Check that everything is 1.0
515 
516  key_it = c3.getIterator();
517 
518  while (key_it.isNext())
519  {
520  grid_key_dx<dim> kk = key_it.get();
521 
522  BOOST_REQUIRE_EQUAL(c3.template get<P::x>(kk),1.0);
523 
524  ++key_it;
525  }
526 }
527 
528 BOOST_AUTO_TEST_SUITE( grid_test )
529 
530 
532 #ifdef SE_CLASS2
533 
534 grid_cpu<3,aggregate<float>> & test_error()
535 {
536  size_t sz[] = {16,16,16};
537 
539 
540  return g;
541 }
542 
543 #endif
544 
546 
547 BOOST_AUTO_TEST_CASE( grid_safety_check )
548 {
549 #if defined(SE_CLASS1) && defined (THROW_ON_ERROR)
550 
551  bool error = false;
552 
553  typedef Point_test<float> p;
554 
555  size_t sz[] = {16,16,16};
556 
557  // Create a grid
558 
561 
562  // try to access uninitialized grid
563  grid_key_dx<3> keyOut(23,1,1);
564  grid_key_dx<3> keyGood(15,1,1);
565  grid_key_dx<3> keyNeg(-1,0,0);
566 
567  error = false;
568  try
569  {g.get<p::x>(keyOut);}
570  catch (std::exception & e)
571  {
572  error = true;
573  BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error");
574  }
575  BOOST_REQUIRE_EQUAL(error,true);
576 
577  error = false;
578  g.setMemory();
579  try
580  {g.get<p::x>(keyOut);}
581  catch (std::exception & e)
582  {
583  error = true;
584  BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error");
585  }
586  BOOST_REQUIRE_EQUAL(error,true);
587 
588  error = false;
590  try
591  {g.set(keyOut,t);}
592  catch (std::exception & e)
593  {
594  error = true;
595  BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error");
596  }
597  BOOST_REQUIRE_EQUAL(error,true);
598 
599  error = false;
600  try
601  {g.set(keyGood,g2,keyOut);}
602  catch (std::exception & e)
603  {
604  error = true;
605  BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error");
606  }
607  BOOST_REQUIRE_EQUAL(error,true);
608 
610 
611  error = false;
612  try
613  {g.get<p::x>(keyNeg);}
614  catch (std::exception & e)
615  {
616  error = true;
617  BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error");
618  }
619  BOOST_REQUIRE_EQUAL(error,true);
620 
621  error = false;
623  try
624  {g.set(keyNeg,t2);}
625  catch (std::exception & e)
626  {
627  error = true;
628  BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error");
629  }
630  BOOST_REQUIRE_EQUAL(error,true);
631 
632  error = false;
633  try
634  {g.set(keyGood,g2,keyNeg);}
635  catch (std::exception & e)
636  {
637  error = true;
638  BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error");
639  }
640  BOOST_REQUIRE_EQUAL(error,true);
641 
642  #if defined(SE_CLASS2) && defined (THROW_ON_ERROR)
643 
644  error = false;
645 
646  // Create a grid
647 
649  delete gp;
650 
651  // Try to access the class
652 
653  try
654  {gp->size();}
655  catch (std::exception & e)
656  {
657  error = true;
658  BOOST_REQUIRE_EQUAL(e.what(),"Runtime memory error");
659  }
660  BOOST_REQUIRE_EQUAL(error,true);
661 
662  error = false;
663  try
664  {grid_cpu<3,aggregate<float>> gr = test_error();}
665  catch (std::exception & e)
666  {
667  error = true;
668  BOOST_REQUIRE_EQUAL(e.what(),"Runtime memory error");
669  }
670  BOOST_REQUIRE_EQUAL(error,true);
671 
672  #endif
673 
674 #endif
675 }
676 
677 BOOST_AUTO_TEST_CASE( grid_use)
678 {
679  /* tensor<int,3,3,3> c;
680  tensor<tensor<int,3,3,3>,3,3,3> c2;*/
681 
682  std::cout << "Grid unit test start" << "\n";
683 
684  size_t sz[3] = {GS_SIZE,GS_SIZE,GS_SIZE};
685 
686  // test the grid from dimensionality 1 to 8 with several size non multiple of two
687  // Dimension 8-1
688 
689  // With test coverage reduce the test size
690 
691 #ifndef TEST_COVERAGE_MODE
692  test_all_grid<8>(4);
693  test_all_grid<7>(8);
694  test_all_grid<6>(9);
695  test_all_grid<5>(18);
696  test_all_grid<4>(37);
697  test_all_grid<3>(126);
698  test_all_grid<2>(1414);
699  test_all_grid<1>(2000000);
700 
701 
702  // Special case grid of size 0
703  test_all_grid<8>(0);
704  test_all_grid<7>(0);
705  test_all_grid<6>(0);
706  test_all_grid<5>(0);
707  test_all_grid<4>(0);
708  test_all_grid<3>(0);
709  test_all_grid<2>(0);
710  test_all_grid<1>(0);
711 #else
712  test_all_grid<4>(4);
713  test_all_grid<3>(8);
714  test_all_grid<2>(16);
715  test_all_grid<1>(256);
716 #endif
717 
718  // Test the 3d gpu grid with CudaMemory and HeapMemory with different size
719 
720  for (int i = 0 ; i <= GS_SIZE ; i++)
721  {
722 #ifdef CUDA_GPU
723 
724  {
727  c3.setMemory();
729  test_layout_grid3d(c3,i);
730  }
731 
732 #endif
733 
735  c3.setMemory();
736  test_layout_grid3d(c3,i);}
737 
738  {
741  c3.setMemory();
743  test_layout_grid3d(c3,i);}
744 
745  // Test the 3d cpu grid with Cudamemory and HeapMemory
746 
748  c3.setMemory();
749  test_layout_grid3d(c3,i);}
750 
751  }
752 
753  std::cout << "Grid unit test end" << "\n";
754 }
755 
756 /* \brief This is an ordinary test simple 3D with plain C array
757  *
758  * This is an ordinary test simple 3D with plain C array
759  *
760  */
761 
762 BOOST_AUTO_TEST_CASE( C_array_test )
763 {
764  // Testing the grids
765 
766 #ifdef VERBOSE_TEST
767  std::cout << "Grid size known at runtime" << "\n";
768  std::cout << "1D Array with index calculation: " << "\n";
769 #endif
770 
771  Point_orig<float> * pA = new Point_orig<float>[GS_SIZE*GS_SIZE*GS_SIZE];
772 
773  int gs_sq = GS_SIZE*GS_SIZE;
774  int gs = GS_SIZE;
775 
776 #ifdef VERBOSE_TEST
777  timer t;
778  t.start();
779 #endif
780 
781  for (int i = 0 ; i < GS_SIZE ; i++)
782  {
783  for (int j = 0 ; j < GS_SIZE ; j++)
784  {
785  for (int k = 0 ; k < GS_SIZE ; k++)
786  {
787  pA[i*gs_sq+j*gs+k].x = 1.1f;
788  pA[i*gs_sq+j*gs+k].y = 1.2f;
789  pA[i*gs_sq+j*gs+k].z = 1.3f;
790  pA[i*gs_sq+j*gs+k].s = 1.0f;
791 
792  pA[i*gs_sq+j*gs+k].v[0] = 1.0f;
793  pA[i*gs_sq+j*gs+k].v[1] = 2.0f;
794  pA[i*gs_sq+j*gs+k].v[2] = 3.0f;
795 
796  pA[i*gs_sq+j*gs+k].t[0][0] = 1.0f;
797  pA[i*gs_sq+j*gs+k].t[0][1] = 2.0f;
798  pA[i*gs_sq+j*gs+k].t[0][2] = 3.0f;
799  pA[i*gs_sq+j*gs+k].t[1][0] = 4.0f;
800  pA[i*gs_sq+j*gs+k].t[1][1] = 5.0f;
801  pA[i*gs_sq+j*gs+k].t[1][2] = 6.0f;
802  pA[i*gs_sq+j*gs+k].t[2][0] = 7.0f;
803  pA[i*gs_sq+j*gs+k].t[2][1] = 8.0f;
804  pA[i*gs_sq+j*gs+k].t[2][2] = 9.0f;
805  }
806  }
807  }
808 
809  delete [] pA;
810 
811 #ifdef VERBOSE_TEST
812  t.stop();
813 
814  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";
815 #endif
816 }
817 
818 BOOST_AUTO_TEST_CASE(grid_operator_equal)
819 {
821 
822  size_t sz[] = {16,16};
823 
824  typedef Box<2,float> b;
825 
826  grid_cpu<2,Box<2,float>> g1(sz);
827  g1.setMemory();
828 
829  auto it = g1.getIterator();
830 
831  while (it.isNext())
832  {
833  auto key = it.get();
834 
835  g1.get<b::p1>(key)[0] = key.get(0);
836  g1.get<b::p2>(key)[1] = key.get(1);
837 
838  ++it;
839  }
840 
842  g2 = g1;
843 
845 
846  bool ret = (g2 == g1);
847 
848  BOOST_REQUIRE_EQUAL(ret,true);
849 }
850 
858 void fill_2_grid_data(grid_cpu<2,Box<2,float>> & g1, grid_cpu<2,Box<2,float>> & g2)
859 {
860  typedef Box<2,float> b;
861 
862  auto it1 = g1.getIterator();
863 
864  while (it1.isNext())
865  {
866  auto key = it1.get();
867 
868  g1.get<b::p1>(key)[0] = key.get(0);
869  g1.get<b::p2>(key)[1] = key.get(1);
870 
871  ++it1;
872  }
873 
874  auto it2 = g2.getIterator();
875 
876  while (it2.isNext())
877  {
878  auto key = it2.get();
879 
880  g2.get<b::p1>(key)[0] = key.get(0);
881  g2.get<b::p2>(key)[1] = key.get(1);
882 
883  ++it2;
884  }
885 
886 
887 }
888 
889 BOOST_AUTO_TEST_CASE(grid_operator_swap)
890 {
891  size_t sz1[] = {16,16};
892  size_t sz2[] = {5,5};
893 
894  grid_cpu<2,Box<2,float>> g1_old(sz1);
895  g1_old.setMemory();
896  grid_cpu<2,Box<2,float>> g2_old(sz2);
897  g2_old.setMemory();
898  grid_cpu<2,Box<2,float>> g1(sz1);
899  g1.setMemory();
900  grid_cpu<2,Box<2,float>> g2(sz2);
901  g2.setMemory();
902 
903  fill_2_grid_data(g1_old,g2_old);
904  fill_2_grid_data(g1,g2);
905 
907 
908  g2.swap(g1);
909 
911 
912  bool ret = (g2 == g1_old);
913  BOOST_REQUIRE_EQUAL(ret,true);
914  ret = (g1 == g2_old);
915  BOOST_REQUIRE_EQUAL(ret,true);
916 }
917 
918 BOOST_AUTO_TEST_CASE(grid_resize_less)
919 {
920  size_t sz1[] = {256,256};
921  size_t sz2[] = {5,5};
922 
923  grid_cpu<2,Box<2,float>> g1(sz1);
924  g1.setMemory();
925 
926  fill_2_grid_data(g1,g1);
927 
928 
929  g1.resize(sz2);
930 
931  BOOST_REQUIRE_EQUAL(g1.size(),25ul);
932 }
933 
934 BOOST_AUTO_TEST_SUITE_END()
935 
936 #endif
static std::vector< comb< dim > > getCombinations_R(size_t d)
Definition: HyperCube.hpp:100
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
double getwct()
Return the elapsed real time.
Definition: timer.hpp:108
mem_id get(size_t i) const
Get the i index.
Definition: grid_key.hpp:394
double getcputime()
Return the cpu time.
Definition: timer.hpp:120
void fill()
fill
Definition: Point_test.hpp:366
It model an expression expr1 * expr2.
Definition: mul.hpp:119
const grid_key_dx< dim > & get() const
Get the actual key.
void start()
Start the timer.
Definition: timer.hpp:73
bool isNext()
Check if there is the next element.
This class represent an N-dimensional box.
Definition: Box.hpp:56
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:201
void set(a v, T...t)
set the Key from a list of numbers
Definition: grid_key.hpp:305
Definition of a class Point in plain C++ and boost::vector for testing purpose.
Definition: Point_test.hpp:4
void set_d(size_t i, mem_id id)
Set the i index.
Definition: grid_key.hpp:407
Test structure used for several test.
Definition: Point_test.hpp:105
Class for cpu time benchmarking.
Definition: timer.hpp:25
void stop()
Stop the timer.
Definition: timer.hpp:97