OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
SparseGrid_unit_tests.cpp
1 /*
2  * SparseGrid_unit_tests.cpp
3  *
4  * Created on: Oct 22, 2017
5  * Author: i-bird
6  */
7 
8 #define DISABLE_MPI_WRITTERS
9 
10 #define BOOST_TEST_DYN_LINK
11 #include <boost/test/unit_test.hpp>
12 #include "SparseGrid/SparseGrid.hpp"
13 #include "NN/CellList/CellDecomposer.hpp"
14 #include <math.h>
15 //#include "util/debug.hpp"
16 
17 BOOST_AUTO_TEST_SUITE( sparse_grid_test )
18 
19 template <typename grid_type, typename cell_decomposer>
20 size_t fill_sphere(grid_type & grid, cell_decomposer & cdsm)
21 {
22  size_t tot_count = 0;
23  double r = 0.3;
24  double omega = 0.0;
25  double phi = 0.0;
26 
27  // 3D sphere
28 
29  for (r = 0.3 ; r < 0.35 ;r += 0.001)
30  {
31  for (omega = 0.0; omega < M_PI ; omega += 0.006)
32  {
33  for (phi = 0.0; phi < 2.0*M_PI ; phi += 0.006)
34  {
36 
37  p.get(0) = r*sin(omega)*sin(phi) + 0.5;
38  p.get(1) = r*sin(omega)*cos(phi) + 0.5;
39  p.get(2) = r*cos(omega) + 0.5;
40 
41  // convert point into grid point
42 
43  grid_key_dx<3> kd = cdsm.getCellGrid(p);
44 
45  grid.template insert<0>(kd) = sin(omega)*sin(omega)*sin(2*phi);
46  grid.template insert<1>(kd) = 0;
47  }
48  }
49  }
50 
51  auto it = grid.getIterator();
52 
53  while (it.isNext())
54  {
55  tot_count++;
56 
57  ++it;
58  }
59 
60  return tot_count;
61 }
62 
63 template <typename grid_type, typename cell_decomposer>
64 size_t fill_sphere_quad(grid_type & grid, cell_decomposer & cdsm)
65 {
66  size_t tot_count = 0;
67  double r = 0.3;
68  double omega = 0.0;
69  double phi = 0.0;
70 
71  // 3D sphere
72 
73  for (r = 0.3 ; r < 0.4 ;r += 0.001)
74  {
75  for (omega = 0.0; omega < M_PI ; omega += 0.006)
76  {
77  for (phi = 0.0; phi < 2.0*M_PI ; phi += 0.006)
78  {
80 
81  p.get(0) = r*sin(omega)*sin(phi) + 0.5;
82  p.get(1) = r*sin(omega)*cos(phi) + 0.5;
83  p.get(2) = r*cos(omega) + 0.5;
84 
85  // convert point into grid point
86 
87  grid_key_dx<3> kd = cdsm.getCellGrid(p);
88 
89  grid.template insert<0>(kd) = kd.get(0)*kd.get(0) + kd.get(1)*kd.get(1) + kd.get(2)*kd.get(2);
90  grid.template insert<1>(kd) = 0;
91  }
92  }
93  }
94 
95  auto it = grid.getIterator();
96 
97  while (it.isNext())
98  {
99  tot_count++;
100 
101  ++it;
102  }
103 
104  return tot_count;
105 }
106 
107 template <typename grid_type, typename cell_decomposer>
108 size_t fill_sphere_quad_v(grid_type & grid, cell_decomposer & cdsm)
109 {
110  size_t tot_count = 0;
111  double r = 0.3;
112  double omega = 0.0;
113  double phi = 0.0;
114 
115  // 3D sphere
116 
117  for (r = 0.3 ; r < 0.4 ;r += 0.001)
118  {
119  for (omega = 0.0; omega < M_PI ; omega += 0.006)
120  {
121  for (phi = 0.0; phi < 2.0*M_PI ; phi += 0.006)
122  {
123  Point<3,float> p;
124 
125  p.get(0) = r*sin(omega)*sin(phi) + 0.5;
126  p.get(1) = r*sin(omega)*cos(phi) + 0.5;
127  p.get(2) = r*cos(omega) + 0.5;
128 
129  // convert point into grid point
130 
131  grid_key_dx<3> kd = cdsm.getCellGrid(p);
132 
133  grid.template insert<0>(kd) = kd.get(0)*kd.get(0) + kd.get(1)*kd.get(1) + kd.get(2)*kd.get(2);
134  grid.template insert<1>(kd) = 0;
135  grid.template insert<3>(kd)[0] = kd.get(0)*kd.get(0) + kd.get(1)*kd.get(1) + kd.get(2)*kd.get(2) + 10000;
136  grid.template insert<3>(kd)[1] = kd.get(0)*kd.get(0) + kd.get(1)*kd.get(1) + kd.get(2)*kd.get(2) + 60000;
137  grid.template insert<3>(kd)[2] = kd.get(0)*kd.get(0) + kd.get(1)*kd.get(1) + kd.get(2)*kd.get(2) + 80000;
138  }
139  }
140  }
141 
142  auto it = grid.getIterator();
143 
144  while (it.isNext())
145  {
146  tot_count++;
147 
148  ++it;
149  }
150 
151  return tot_count;
152 }
153 
154 BOOST_AUTO_TEST_CASE( sparse_grid_use_test)
155 {
156  size_t sz[3] = {10000,10000,10000};
157 
159 
160  grid.getBackgroundValue().template get<0>() = 0.0;
161 
162  // We fill a sphere with a band
163 
164  grid_key_dx<3> key1({5000,5000,5000});
165  grid_key_dx<3> key2({5001,5001,5001});
166  grid_key_dx<3> key3({5002,5003,5003});
167 
168  grid.template insert<0>(key1) = 1.0;
169  grid.template insert<0>(key2) = 2.0;
170  grid.template insert<0>(key3) = 3.0;
171 
172  BOOST_REQUIRE_EQUAL(grid.template get<0>(key1),1.0);
173  BOOST_REQUIRE_EQUAL(grid.template get<0>(key2),2.0);
174  BOOST_REQUIRE_EQUAL(grid.template get<0>(key3),3.0);
175 
176  auto it = grid.getIterator();
177 
178  size_t count = 0;
179 
180  while (it.isNext())
181  {
182  count++;
183 
184  ++it;
185  }
186 
187  BOOST_REQUIRE_EQUAL(count,(size_t)3);
188 }
189 
190 BOOST_AUTO_TEST_CASE( sparse_grid_fill_all_test)
191 {
192  size_t sz[3] = {171,171,171};
193 
195 
196  grid.getBackgroundValue().template get<0>() = 0.0;
197 
198  grid_sm<3,void> g_sm(sz);
199 
200  grid_key_dx_iterator<3> kit(g_sm);
201 
202  while (kit.isNext())
203  {
204  auto key = kit.get();
205 
206  grid.template insert<0>(key) = g_sm.LinId(key);
207 
208  ++kit;
209  }
210 
211  auto it = grid.getIterator();
212 
213  size_t count = 0;
214 
215  bool match = true;
216 
217  while (it.isNext())
218  {
219  auto key = it.get();
220 
221  // return a grid_key_dx
222  auto key_pos = it.getKeyF();
223 
224  match &= (grid.template get<0>(key_pos) == g_sm.LinId(key));
225 
226  count++;
227 
228  ++it;
229  }
230 
231  BOOST_REQUIRE_EQUAL(count,(size_t)171*171*171);
232  BOOST_REQUIRE_EQUAL(grid.size(),(size_t)171*171*171);
233  BOOST_REQUIRE_EQUAL(match,true);
234 
235  // remove all points
236 
237  grid_key_dx_iterator<3> kit2(g_sm);
238 
239  while (kit2.isNext())
240  {
241  auto key = kit2.get();
242 
243  grid.remove(key);
244 
245  ++kit2;
246  }
247 
248  size_t tot = grid.size();
249  BOOST_REQUIRE_EQUAL(tot,0ul);
250 }
251 
252 
253 BOOST_AUTO_TEST_CASE( sparse_grid_fill_sparse_test)
254 {
255  size_t sz[3] = {500,500,500};
256 
258 
259  grid.getBackgroundValue().template get<0>() = 0.0;
260 
261  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
262 
263  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
264 
265  cdsm.setDimensions(domain, sz, 0);
266 
267  fill_sphere(grid,cdsm);
268 
269  double r = 0.3;
270  double omega = 0.0;
271  double phi = 0.0;
272 
273  for (r = 0.3 ; r < 0.35 ;r += 0.001)
274  {
275  for (omega = 0.0; omega < M_PI ; omega += 0.006)
276  {
277  for (phi = 0.0; phi < 2.0*M_PI ; phi += 0.006)
278  {
279 
280  Point<3,float> p;
281 
282  p.get(0) = r*sin(omega)*sin(phi) + 0.5;
283  p.get(1) = r*sin(omega)*cos(phi) + 0.5;
284  p.get(2) = r*cos(omega) + 0.5;
285 
286  // convert point into grid point
287 
288  grid_key_dx<3> kd = cdsm.getCellGrid(p);
289 
290 
291  if (grid.template get<0>(kd) == sin(omega)*sin(omega)*sin(2*phi))
292  {grid.template insert<1>(kd) = 1;}
293 
294  }
295  }
296  }
297 
298  auto it = grid.getIterator();
299 
300  bool match = true;
301 
302  while(it.isNext())
303  {
304  auto key = it.get();
305 
306  if (grid.template get<1>(key) == 0)
307  {match = false;}
308 
309  ++it;
310  }
311 
312  BOOST_REQUIRE_EQUAL(match,true);
313 
314  // remove the points
315 
316  for (r = 0.3 ; r < 0.35 ;r += 0.001)
317  {
318  for (omega = 0.0; omega < M_PI ; omega += 0.006)
319  {
320  for (phi = 0.0; phi < 2.0*M_PI ; phi += 0.006)
321  {
322 
323  Point<3,float> p;
324 
325  p.get(0) = r*sin(omega)*sin(phi) + 0.5;
326  p.get(1) = r*sin(omega)*cos(phi) + 0.5;
327  p.get(2) = r*cos(omega) + 0.5;
328 
329  // convert point into grid point
330 
331  grid_key_dx<3> kd = cdsm.getCellGrid(p);
332 
333 
334  grid.remove(kd);
335 
336  }
337  }
338  }
339 
340  size_t tot;
341  tot = grid.size();
342 
343  BOOST_REQUIRE_EQUAL(tot,0ul);
344 }
345 
346 
347 BOOST_AUTO_TEST_CASE( sparse_grid_resize_test)
348 {
349  size_t sz[3] = {500,500,500};
350 
353 
354  grid.getBackgroundValue().template get<0>() = 0.0;
355 
356  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
357 
358  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
359 
360  cdsm.setDimensions(domain, sz, 0);
361 
362  double r = 0.3;
363  double omega = 0.0;
364  double phi = 0.0;
365 
366  // 3D sphere
367 
368  for (r = 0.3 ; r < 0.35 ;r += 0.001)
369  {
370  for (omega = 0.0; omega < M_PI ; omega += 0.006)
371  {
372  for (phi = 0.0; phi < 2.0*M_PI ; phi += 0.006)
373  {
374  Point<3,float> p;
375 
376  p.get(0) = r*sin(omega)*sin(phi) + 0.5;
377  p.get(1) = r*sin(omega)*cos(phi) + 0.5;
378  p.get(2) = r*cos(omega) + 0.5;
379 
380  // convert point into grid point
381 
382  grid_key_dx<3> kd = cdsm.getCellGrid(p);
383 
384  grid.template insert<0>(kd) = sin(omega)*sin(omega)*sin(2*phi);
385  grid.template insert<1>(kd) = 0;
386  grid2.template insert<0>(kd) = sin(omega)*sin(omega)*sin(2*phi);
387  grid2.template insert<1>(kd) = 0;
388  }
389  }
390  }
391 
392  size_t sz_b[3] = {1024,1024,1024};
393  grid2.resize(sz_b);
394 
395  // Check that both grid contain the same information
396 
397  auto it = grid2.getIterator();
398 
399  bool match = true;
400 
401  while(it.isNext())
402  {
403  auto key = it.get();
404 
405  if (grid.template get<0>(key) != grid2.template get<0>(key))
406  {
407  match = false;
408  break;
409  }
410 
411  ++it;
412  }
413 
414  BOOST_REQUIRE_EQUAL(match,true);
415 
416  // now we resize smalle
417 
418  size_t sz_s[3] = {250,250,250};
419  grid2.resize(sz_s);
420 
421  //
422 
423  auto it2 = grid.getIterator();
424 
425  match = true;
426 
427  while(it2.isNext())
428  {
429  auto key = it2.get();
430 
431  // we check if the key is inside
432 
433  bool cin = true;
434 
435  cin &= (size_t)key.get(0) < sz_s[0];
436  cin &= (size_t)key.get(1) < sz_s[1];
437  cin &= (size_t)key.get(2) < sz_s[2];
438 
439 
440  if (cin == true)
441  {
442  if (grid.template get<0>(key) != grid2.template get<0>(key))
443  {
444  match = false;
445  break;
446  }
447  }
448 
449  ++it2;
450  }
451 
452  BOOST_REQUIRE_EQUAL(match,true);
453 }
454 
455 
456 
457 BOOST_AUTO_TEST_CASE( sparse_grid_fill_all_with_resize_test)
458 {
459  size_t sz[3] = {10,10,171};
460 
462 
463  grid.getBackgroundValue().template get<0>() = 0.0;
464 
465  grid_sm<3,void> g_sm(sz);
466 
467  grid_key_dx_iterator<3> kit(g_sm);
468 
469  while (kit.isNext())
470  {
471  auto key = kit.get();
472 
473  grid.template insert<0>(key) = g_sm.LinId(key);
474 
475  ++kit;
476  }
477 
478  size_t sz_b[3] = {20,20,200};
479 
480  // now we increase the size
481  grid.resize(sz_b);
482 
483  auto it = grid.getIterator();
484 
485  size_t count = 0;
486 
487  bool match = true;
488 
489  while (it.isNext())
490  {
491  auto key = it.get();
492 
493  // return a grid_key_dx
494  auto key_pos = it.getKeyF();
495 
496  match &= (grid.template get<0>(key_pos) == g_sm.LinId(key));
497 
498  count++;
499 
500  ++it;
501  }
502 
503  BOOST_REQUIRE_EQUAL(count,(size_t)10*10*171);
504  BOOST_REQUIRE_EQUAL(grid.size(),(size_t)10*10*171);
505  BOOST_REQUIRE_EQUAL(match,true);
506 
507  // refill with the full set of point
508 
509  grid_sm<3,void> g_sm2(sz_b);
510 
511  grid_key_dx_iterator<3> kit2(g_sm2);
512 
513  while (kit2.isNext())
514  {
515  auto key = kit2.get();
516 
517  grid.template insert<0>(key) = g_sm2.LinId(key);
518 
519  ++kit2;
520  }
521 
522  auto it2 = grid.getIterator();
523 
524  count = 0;
525 
526  match = true;
527 
528  while (it2.isNext())
529  {
530  auto key = it2.get();
531 
532  // return a grid_key_dx
533  auto key_pos = it2.getKeyF();
534 
535  match &= (grid.template get<0>(key_pos) == g_sm2.LinId(key));
536 
537  count++;
538 
539  ++it2;
540  }
541 
542  BOOST_REQUIRE_EQUAL(count,(size_t)20*20*200);
543  BOOST_REQUIRE_EQUAL(grid.size(),(size_t)20*20*200);
544  BOOST_REQUIRE_EQUAL(match,true);
545 }
546 
547 BOOST_AUTO_TEST_CASE( sparse_grid_insert_o_test)
548 {
549  size_t sz[3] = {10,10,171};
550 
552 
553  grid.getBackgroundValue().template get<0>() = 0.0;
554 
555  size_t ele;
556  grid_key_dx<3> key({5,5,90});
557 
558  auto & flt = grid.insert_o(key,ele).template get<0>();
559  flt[ele] = 117.0;
560 
561  BOOST_REQUIRE_EQUAL(grid.template get<0>(key),117.0);
562 }
563 
564 
565 BOOST_AUTO_TEST_CASE( sparse_grid_sub_grid_it)
566 {
567  size_t sz[3] = {171,171,171};
568 
570 
571  grid.getBackgroundValue().template get<0>() = 0.0;
572 
573  grid_sm<3,void> g_sm(sz);
574 
575  grid_key_dx_iterator<3> kit(g_sm);
576 
577  while (kit.isNext())
578  {
579  auto key = kit.get();
580 
581  grid.template insert<0>(key) = g_sm.LinId(key);
582 
583  ++kit;
584  }
585 
586  grid_key_dx<3> start({21,21,21});
587  grid_key_dx<3> stop({90,90,90});
588 
589  bool error = false;
590  size_t count = 0;
591  auto it_sub = grid.getIterator(start,stop);
592 
593  while (it_sub.isNext())
594  {
595  auto gkey = it_sub.get();
596 
597  if (gkey.get(0) < start.get(0) ||
598  gkey.get(1) < start.get(1) ||
599  gkey.get(2) < start.get(2) ||
600  gkey.get(0) > stop.get(0) ||
601  gkey.get(1) > stop.get(1) ||
602  gkey.get(2) > stop.get(2))
603  {
604  error = true;
605  }
606 
607  count++;
608 
609  ++it_sub;
610  }
611 
612  size_t tot = (stop.get(2) - start.get(2) + 1)*(stop.get(1) - start.get(1) + 1)*(stop.get(0) - start.get(0) + 1);
613  BOOST_REQUIRE_EQUAL(error,false);
614  BOOST_REQUIRE_EQUAL(count,tot);
615 }
616 
617 
618 BOOST_AUTO_TEST_CASE( sparse_grid_sub_grid_it_quarter_sphere)
619 {
620  size_t sz[3] = {501,501,501};
621  size_t sz_cell[3] = {500,500,500};
622 
624 
625  grid.getBackgroundValue().template get<0>() = 0.0;
626 
627  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
628 
629  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
630 
631  cdsm.setDimensions(domain, sz_cell, 0);
632 
633  fill_sphere(grid,cdsm);
634 
635  grid_key_dx<3> start({0,0,0});
636  grid_key_dx<3> stop({250,250,250});
637 
638  bool error = false;
639  size_t count = 0;
640  auto it_sub = grid.getIterator(start,stop);
641 
642  while (it_sub.isNext())
643  {
644  auto gkey = it_sub.get();
645 
646  if (gkey.get(0) < start.get(0) ||
647  gkey.get(1) < start.get(1) ||
648  gkey.get(2) < start.get(2) ||
649  gkey.get(0) > stop.get(0) ||
650  gkey.get(1) > stop.get(1) ||
651  gkey.get(2) > stop.get(2))
652  {
653  error = true;
654  }
655 
656  // Check that the point is in the sphere
657 
658  double radius = (gkey.get(0) - 250)*(gkey.get(0) - 250) +
659  (gkey.get(1) - 250)*(gkey.get(1) - 250) +
660  (gkey.get(2) - 250)*(gkey.get(2) - 250);
661 
662  radius = sqrt(radius);
663 
664  if (radius < 150 || radius >= 175)
665  {
666  // if is not in the radius remove it
667  grid.remove(gkey);
668  }
669 
670  count++;
671 
672  ++it_sub;
673  }
674 
675  BOOST_REQUIRE_EQUAL(error,false);
676 
677  // We go again across the point now every point out the sphere is an error
678 
679  count = 0;
680  auto it_sub2 = grid.getIterator(start,stop);
681 
682  while (it_sub2.isNext())
683  {
684  auto gkey = it_sub2.get();
685 
686  if (gkey.get(0) < start.get(0) ||
687  gkey.get(1) < start.get(1) ||
688  gkey.get(2) < start.get(2) ||
689  gkey.get(0) > stop.get(0) ||
690  gkey.get(1) > stop.get(1) ||
691  gkey.get(2) > stop.get(2))
692  {
693  error = true;
694  }
695 
696  // Check that the point is in the sphere
697 
698  double radius = (gkey.get(0) - 250)*(gkey.get(0) - 250) +
699  (gkey.get(1) - 250)*(gkey.get(1) - 250) +
700  (gkey.get(2) - 250)*(gkey.get(2) - 250);
701 
702  radius = sqrt(radius);
703 
704  if (radius < 150 || radius >= 175)
705  {
706  error = true;
707  }
708 
709  count++;
710 
711  ++it_sub2;
712  }
713 
714  BOOST_REQUIRE_EQUAL(error,false);
715 }
716 
717 BOOST_AUTO_TEST_CASE( sparse_grid_fast_stencil)
718 {
719  size_t sz[3] = {501,501,501};
720  size_t sz_cell[3] = {500,500,500};
721 
723 
724  grid.getBackgroundValue().template get<0>() = 0.0;
725 
726  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
727 
728  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
729 
730  cdsm.setDimensions(domain, sz_cell, 0);
731 
732  fill_sphere_quad(grid,cdsm);
733 
734  grid_key_dx<3> start({1,1,1});
735  grid_key_dx<3> stop({499,499,499});
736 
737  for (int i = 0 ; i < 1 ; i++)
738  {
739 
740  grid.private_get_nnlist().resize(NNStar_c<3>::nNN * grid.private_get_header_inf().size());
741  auto it = grid.getBlockIterator<1>(start,stop);
742 
743  unsigned char mask[decltype(it)::sizeBlockBord];
744  __attribute__ ((aligned (32))) double block_bord_src[decltype(it)::sizeBlockBord];
745  __attribute__ ((aligned (32))) double block_bord_dst[decltype(it)::sizeBlock];
746 
747  const Vc::double_v six(6.0);
748 
749  while (it.isNext())
750  {
751  it.loadBlockBorder<0,NNStar_c<3>,false>(block_bord_src,mask);
752 
753  for (int i = 0 ; i < 1 ; i++)
754  {
755 
756  for (int k = it.start_b(2) ; k < it.stop_b(2) ; k++)
757  {
758  for (int j = it.start_b(1) ; j < it.stop_b(1) ; j++)
759  {
760  int c = it.LinB(it.start_b(0),j,k);
761 
762  int xp = c+1;
763  int xm = c-1;
764 
765  int yp = it.LinB(it.start_b(0),j+1,k);
766  int ym = it.LinB(it.start_b(0),j-1,k);
767 
768  int zp = it.LinB(it.start_b(0),j,k+1);
769  int zm = it.LinB(it.start_b(0),j,k-1);
770 
771  for (int i = it.start_b(0) ; i < it.stop_b(0) ; i++)
772  {
773  // we do only id exist the point
774  if (mask[c] == false) {continue;}
775 
776  bool surround = mask[xp] & mask[xm] & mask[ym] & mask[yp] & mask[zp] & mask[zm];
777 
778  double Lap = block_bord_src[xp] + block_bord_src[xm] +
779  block_bord_src[yp] + block_bord_src[ym] +
780  block_bord_src[zp] + block_bord_src[zm] - 6.0*block_bord_src[c];
781 
782  Lap = (surround)?Lap:6.0;
783 
784  block_bord_dst[it.LinB_off(i,j,k)] = Lap;
785 
786  c++;
787  xp++;
788  xm++;
789  yp++;
790  ym++;
791  zp++;
792  zm++;
793  }
794  }
795  }
796 
797  }
798 
799  it.storeBlock<1>(block_bord_dst);
800 
801  ++it;
802  }
803 
804  }
805 
806  bool check = true;
807  auto it2 = grid.getIterator();
808  while (it2.isNext())
809  {
810  auto p = it2.get();
811 
812  check &= grid.template get<1>(p) == 6;
813 
814  ++it2;
815  }
816 
817  BOOST_REQUIRE_EQUAL(check,true);
818  // Check correct-ness
819 
820 // print_grid("debug_out",grid);
821 }
822 
823 BOOST_AUTO_TEST_CASE( sparse_grid_fast_stencil_vectorized)
824 {
825  size_t sz[3] = {501,501,501};
826  size_t sz_cell[3] = {500,500,500};
827 
829 
830  grid.getBackgroundValue().template get<0>() = 0.0;
831 
832  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
833 
834  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
835 
836  cdsm.setDimensions(domain, sz_cell, 0);
837 
838  fill_sphere_quad(grid,cdsm);
839 
840  grid_key_dx<3> start({1,1,1});
841  grid_key_dx<3> stop({499,499,499});
842 
843  grid.private_get_nnlist().resize(NNStar_c<3>::nNN * grid.private_get_header_inf().size());
844 
845  for (int i = 0 ; i < 1 ; i++)
846  {
847 
848  auto it = grid.getBlockIterator<1>(start,stop);
849 
850  unsigned char mask[decltype(it)::sizeBlockBord];
851  unsigned char mask_sum[decltype(it)::sizeBlockBord];
852  __attribute__ ((aligned (32))) double block_bord_src[decltype(it)::sizeBlockBord];
853  __attribute__ ((aligned (32))) double block_bord_dst[decltype(it)::sizeBlock];
854 
855  typedef typename boost::mpl::at<decltype(it)::stop_border_vmpl,boost::mpl::int_<0>>::type sz0;
856  typedef typename boost::mpl::at<decltype(it)::stop_border_vmpl,boost::mpl::int_<1>>::type sz1;
857  typedef typename boost::mpl::at<decltype(it)::stop_border_vmpl,boost::mpl::int_<2>>::type sz2;
858 
859  const Vc::double_v six(6.0);
860 
861  while (it.isNext())
862  {
863  it.loadBlockBorder<0,NNStar_c<3>,false>(block_bord_src,mask);
864 
865  // Sum the mask
866  for (int k = it.start_b(2) ; k < it.stop_b(2) ; k++)
867  {
868  for (int j = it.start_b(1) ; j < it.stop_b(1) ; j++)
869  {
870  int c = it.LinB(it.start_b(0),j,k);
871 
872  int yp = it.LinB(it.start_b(0),j+1,k);
873  int ym = it.LinB(it.start_b(0),j-1,k);
874 
875  int zp = it.LinB(it.start_b(0),j,k+1);
876  int zm = it.LinB(it.start_b(0),j,k-1);
877 
878  for (int i = it.start_b(0) ; i < it.stop_b(0) ; i += sizeof(size_t))
879  {
880  size_t cmd = *(size_t *)&mask[c];
881 
882  if (cmd == 0) {continue;}
883 
884  size_t xpd = *(size_t *)&mask[c+1];
885  size_t xmd = *(size_t *)&mask[c-1];
886  size_t ypd = *(size_t *)&mask[yp];
887  size_t ymd = *(size_t *)&mask[ym];
888  size_t zpd = *(size_t *)&mask[zp];
889  size_t zmd = *(size_t *)&mask[zm];
890 
891  size_t sum = xpd + xmd +
892  ypd + ymd +
893  zpd + zmd + cmd;
894 
895  *(size_t *)&mask_sum[c] = sum;
896 
897  c += sizeof(size_t);
898  yp += sizeof(size_t);
899  ym += sizeof(size_t);
900  zp += sizeof(size_t);
901  zm += sizeof(size_t);
902  }
903  }
904  }
905 
906  for (int k = it.start_b(2) ; k < it.stop_b(2) ; k++)
907  {
908  for (int j = it.start_b(1) ; j < it.stop_b(1) ; j++)
909  {
910  int c = it.LinB(it.start_b(0),j,k);
911 
912  int yp = it.LinB(it.start_b(0),j+1,k);
913  int ym = it.LinB(it.start_b(0),j-1,k);
914 
915  int zp = it.LinB(it.start_b(0),j,k+1);
916  int zm = it.LinB(it.start_b(0),j,k-1);
917 
918  int cd = it.LinB_off(it.start_b(0),j,k);
919 
920  for (int i = it.start_b(0) ; i < it.stop_b(0) ; i += Vc::double_v::Size)
921  {
922  Vc::Mask<double> cmp;
923 
924  if (Vc::double_v::Size == 2)
925  {
926  cmp[0] = mask[c] == true;
927  cmp[1] = mask[c+1] == true;
928  }
929  else if (Vc::double_v::Size == 4)
930  {
931  cmp[0] = mask[c] == true;
932  cmp[1] = mask[c+1] == true;
933  cmp[2] = mask[c+2] == true;
934  cmp[3] = mask[c+3] == true;
935  }
936  else
937  {
938  std::cout << "UNSUPPORTED" << std::endl;
939  exit(1);
940  }
941 
942 
943  // we do only id exist the point
944  if (Vc::none_of(cmp) == true) {continue;}
945 
946  Vc::Mask<double> surround;
947 
948  Vc::double_v xpd(&block_bord_src[c+1],Vc::Unaligned);
949  Vc::double_v xmd(&block_bord_src[c-1],Vc::Unaligned);
950  Vc::double_v ypd(&block_bord_src[c+sz0::value],Vc::Unaligned);
951  Vc::double_v ymd(&block_bord_src[c-sz0::value],Vc::Unaligned);
952  Vc::double_v zpd(&block_bord_src[zp],Vc::Unaligned);
953  Vc::double_v zmd(&block_bord_src[zm],Vc::Unaligned);
954  Vc::double_v cmd(&block_bord_src[c],Vc::Unaligned);
955 
956  Vc::double_v Lap = xpd + xmd +
957  ypd + ymd +
958  zpd + zmd - 6.0*cmd;
959 
960  if (Vc::double_v::Size == 2)
961  {
962  surround[0] = (mask_sum[c] == 7);
963  surround[1] = (mask_sum[c+1] == 7);
964  }
965  else if (Vc::double_v::Size == 4)
966  {
967  surround[0] = (mask_sum[c] == 7);
968  surround[1] = (mask_sum[c+1] == 7);
969  surround[2] = (mask_sum[c+2] == 7);
970  surround[3] = (mask_sum[c+3] == 7);
971  }
972 
973  Lap = Vc::iif(surround,Lap,six);
974 
975  Lap.store(&block_bord_dst[cd],cmp,Vc::Aligned);
976 
977  c+=Vc::double_v::Size;
978  zp+=Vc::double_v::Size;
979  zm+=Vc::double_v::Size;
980  cd+=Vc::double_v::Size;
981  }
982  }
983  }
984 
985  it.storeBlock<1>(block_bord_dst);
986 
987  ++it;
988  }
989 
990  }
991 
992  bool check = true;
993  auto it2 = grid.getIterator();
994  while (it2.isNext())
995  {
996  auto p = it2.get();
997 
998  check &= grid.template get<1>(p) == 6;
999 
1000  ++it2;
1001  }
1002 
1003  BOOST_REQUIRE_EQUAL(check,true);
1004  // Check correct-ness
1005 
1006  //print_grid("debug_out",grid);
1007 }
1008 
1009 BOOST_AUTO_TEST_CASE( sparse_grid_fast_stencil_vectorized_block_skip)
1010 {
1011  size_t sz[3] = {501,501,501};
1012  size_t sz_cell[3] = {500,500,500};
1013 
1015 
1016  grid.getBackgroundValue().template get<0>() = 0.0;
1017 
1018  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
1019 
1020  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
1021 
1022  cdsm.setDimensions(domain, sz_cell, 0);
1023 
1024  fill_sphere_quad(grid,cdsm);
1025 
1026  grid.reorder();
1027 
1028  grid_key_dx<3> start({1,1,1});
1029  grid_key_dx<3> stop({499,499,499});
1030 
1031  for (int i = 0 ; i < 1 ; i++)
1032  {
1033 
1034  auto it = grid.getBlockIterator<1>(start,stop);
1035 
1036  auto & datas = grid.private_get_data();
1037  auto & headers = grid.private_get_header_mask();
1038 
1039  typedef typename boost::mpl::at<decltype(it)::stop_border_vmpl,boost::mpl::int_<0>>::type sz0;
1040  typedef typename boost::mpl::at<decltype(it)::stop_border_vmpl,boost::mpl::int_<1>>::type sz1;
1041  typedef typename boost::mpl::at<decltype(it)::stop_border_vmpl,boost::mpl::int_<2>>::type sz2;
1042 
1043  typedef typename sgrid_soa<3,aggregate<double,double,int>,HeapMemory>::chunking_type chunking;
1044 
1045  const Vc::double_v one(1.0);
1046 
1047  while (it.isNext())
1048  {
1049  // Load
1050  long int offset_jump[6];
1051  size_t cid = it.getChunkId();
1052 
1053  auto chunk = datas.get(cid);
1054  auto & mask = headers.get(cid);
1055 
1056  bool exist;
1057  grid_key_dx<3> p = grid.getChunkPos(cid) + grid_key_dx<3>({-1,0,0});
1058  long int r = grid.getChunk(p,exist);
1059  offset_jump[0] = (r-cid)*decltype(it)::sizeBlock;
1060 
1061  p = grid.getChunkPos(cid) + grid_key_dx<3>({1,0,0});
1062  r = grid.getChunk(p,exist);
1063  offset_jump[1] = (r-cid)*decltype(it)::sizeBlock;
1064 
1065  p = grid.getChunkPos(cid) + grid_key_dx<3>({0,-1,0});
1066  r = grid.getChunk(p,exist);
1067  offset_jump[2] = (r-cid)*decltype(it)::sizeBlock;
1068 
1069  p = grid.getChunkPos(cid) + grid_key_dx<3>({0,1,0});
1070  r = grid.getChunk(p,exist);
1071  offset_jump[3] = (r-cid)*decltype(it)::sizeBlock;
1072 
1073  p = grid.getChunkPos(cid) + grid_key_dx<3>({0,0,-1});
1074  r = grid.getChunk(p,exist);
1075  offset_jump[4] = (r-cid)*decltype(it)::sizeBlock;
1076 
1077  p = grid.getChunkPos(cid) + grid_key_dx<3>({0,0,1});
1078  r = grid.getChunk(p,exist);
1079  offset_jump[5] = (r-cid)*decltype(it)::sizeBlock;
1080 
1081  // Load offset jumps
1082 
1083  long int s2 = 0;
1084 
1085  typedef boost::mpl::at<typename chunking::type,boost::mpl::int_<2>>::type sz;
1086  typedef boost::mpl::at<typename chunking::type,boost::mpl::int_<1>>::type sy;
1087  typedef boost::mpl::at<typename chunking::type,boost::mpl::int_<0>>::type sx;
1088 
1089  for (int v = 0 ; v < sz::value ; v++)
1090  {
1091  for (int j = 0 ; j < sy::value ; j++)
1092  {
1093  for (int k = 0 ; k < sx::value ; k += Vc::double_v::Size)
1094  {
1095  // we do only id exist the point
1096  if (*(int *)&mask.mask[s2] == 0) {s2 += Vc::double_v::Size; continue;}
1097 
1098  Vc::Mask<double> surround;
1099 
1106 
1107  Vc::double_v xm;
1108  Vc::double_v xp;
1109 
1110  Vc::double_v cmd(&chunk.template get<0>()[s2]);
1111 
1112  // Load x-1
1113  long int sumxm = s2-1;
1114  sumxm += (k==0)?offset_jump[0] + sx::value:0;
1115 
1116  // Load x+1
1117  long int sumxp = s2+Vc::double_v::Size;
1118  sumxp += (k+Vc::double_v::Size == sx::value)?offset_jump[1] - sx::value:0;
1119 
1120  long int sumym = (j == 0)?offset_jump[2] + (sy::value-1)*sx::value:-sx::value;
1121  sumym += s2;
1122  long int sumyp = (j == sy::value-1)?offset_jump[3] - (sy::value - 1)*sx::value:sx::value;
1123  sumyp += s2;
1124  long int sumzm = (v == 0)?offset_jump[4] + (sz::value-1)*sx::value*sy::value:-sx::value*sy::value;
1125  sumzm += s2;
1126  long int sumzp = (v == sz::value-1)?offset_jump[5] - (sz::value - 1)*sx::value*sy::value:sx::value*sy::value;
1127  sumzp += s2;
1128 
1129  if (Vc::double_v::Size == 2)
1130  {
1131  mxm.uc[0] = mask.mask[sumxm];
1132  mxm.uc[1] = mask.mask[s2];
1133 
1134  mxp.uc[0] = mask.mask[s2+1];
1135  mxp.uc[1] = mask.mask[sumxp];
1136  }
1137  else if (Vc::double_v::Size == 4)
1138  {
1139  mxm.uc[0] = mask.mask[sumxm];
1140  mxm.uc[1] = mask.mask[s2];
1141  mxm.uc[2] = mask.mask[s2+1];
1142  mxm.uc[3] = mask.mask[s2+2];
1143 
1144  mxp.uc[0] = mask.mask[s2+1];
1145  mxp.uc[1] = mask.mask[s2+2];
1146  mxp.uc[2] = mask.mask[s2+3];
1147  mxp.uc[3] = mask.mask[sumxp];
1148  }
1149  else
1150  {
1151  std::cout << "UNSUPPORTED" << std::endl;
1152  exit(1);
1153  }
1154 
1155  mym.i = *(int *)&mask.mask[sumym];
1156  myp.i = *(int *)&mask.mask[sumyp];
1157 
1158  mzm.i = *(int *)&mask.mask[sumzm];
1159  mzp.i = *(int *)&mask.mask[sumzp];
1160 
1161  if (Vc::double_v::Size == 2)
1162  {
1163  xm[0] = chunk.template get<0>()[sumxm];
1164  xm[1] = cmd[0];
1165 
1166  xp[0] = cmd[1];
1167  xp[1] = chunk.template get<0>()[sumxp];
1168  }
1169  else if (Vc::double_v::Size == 4)
1170  {
1171  xm[0] = chunk.template get<0>()[sumxm];
1172  xm[1] = cmd[0];
1173  xm[2] = cmd[1];
1174  xm[3] = cmd[2];
1175 
1176  xp[0] = cmd[1];
1177  xp[1] = cmd[2];
1178  xp[2] = cmd[3];
1179  xp[3] = chunk.template get<0>()[sumxp];
1180  }
1181 
1182  // Load y and z direction
1183 
1184  Vc::double_v ym(&chunk.template get<0>()[sumym],Vc::Aligned);
1185  Vc::double_v yp(&chunk.template get<0>()[sumyp],Vc::Aligned);
1186  Vc::double_v zm(&chunk.template get<0>()[sumzm],Vc::Aligned);
1187  Vc::double_v zp(&chunk.template get<0>()[sumzp],Vc::Aligned);
1188 
1189  // Calculate
1190 
1192  tot_m.i = mxm.i + mxp.i + mym.i + myp.i + mzm.i + mzp.i;
1193 
1194  if (Vc::double_v::Size == 2)
1195  {
1196  surround[0] = (tot_m.uc[0] == 6);
1197  surround[1] = (tot_m.uc[1] == 6);
1198  }
1199  else if (Vc::double_v::Size == 4)
1200  {
1201  surround[0] = (tot_m.uc[0] == 6);
1202  surround[1] = (tot_m.uc[1] == 6);
1203  surround[2] = (tot_m.uc[2] == 6);
1204  surround[3] = (tot_m.uc[3] == 6);
1205  }
1206 
1207  Vc::double_v Lap = xp + xm +
1208  yp + ym +
1209  zp + zm - 6.0*cmd;
1210 
1211  Lap = Vc::iif(surround,Lap,one);
1212 
1213  Lap.store(&chunk.template get<1>()[s2],Vc::Aligned);
1214 
1215  s2 += Vc::double_v::Size;
1216  }
1217  }
1218  }
1219 
1220  ++it;
1221  }
1222 
1223  }
1224 
1225  int tot_six = 0;
1226  int tot_one = 0;
1227 
1228  bool check = true;
1229  auto it2 = grid.getIterator();
1230  while (it2.isNext())
1231  {
1232  auto p = it2.get();
1233 
1234  check &= (grid.template get<1>(p) == 6 || grid.template get<1>(p) == 1);
1235 
1236  if (grid.template get<1>(p) == 1)
1237  {
1238  tot_one++;
1239  }
1240 
1241  if (grid.template get<1>(p) == 6)
1242  {
1243  tot_six++;
1244  }
1245 
1246  ++it2;
1247  }
1248 
1249  BOOST_REQUIRE_EQUAL(check,true);
1250  BOOST_REQUIRE_EQUAL(tot_six,15857813);
1251  BOOST_REQUIRE_EQUAL(tot_one,2977262);
1252  // Check correct-ness
1253 
1254  //print_grid("debug_out",grid);
1255 }
1256 
1257 BOOST_AUTO_TEST_CASE( sparse_grid_fast_stencil_vectorized_simplified)
1258 {
1259  size_t sz[3] = {501,501,501};
1260  size_t sz_cell[3] = {500,500,500};
1261 
1263 
1264  grid.getBackgroundValue().template get<0>() = 0.0;
1265 
1266  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
1267 
1268  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
1269 
1270  cdsm.setDimensions(domain, sz_cell, 0);
1271 
1272  fill_sphere_quad(grid,cdsm);
1273 
1274  grid_key_dx<3> start({1,1,1});
1275  grid_key_dx<3> stop({250,250,250});
1276 
1277  for (int i = 0 ; i < 1 ; i++)
1278  {
1279 
1280  int stencil[6][3] = {{1,0,0},{-1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
1281 
1282  grid.conv<0,1,1>(stencil,start,stop,[](Vc::double_v (& xs)[7], unsigned char * mask_sum){
1283  Vc::double_v Lap = xs[1] + xs[2] +
1284  xs[3] + xs[4] +
1285  xs[5] + xs[6] - 6.0*xs[0];
1286 
1287  auto surround = load_mask<Vc::double_v>(mask_sum);
1288 
1289  auto sur_mask = (surround == 6.0);
1290 
1291  Lap = Vc::iif(sur_mask,Lap,Vc::double_v(1.0));
1292 
1293  return Lap;
1294  });
1295  }
1296 
1297  int tot_six = 0;
1298  int tot_one = 0;
1299 
1300  bool check = true;
1301  auto it2 = grid.getIterator(start,stop);
1302  while (it2.isNext())
1303  {
1304  auto p = it2.get();
1305 
1306  check &= (grid.template get<1>(p) == 6 || grid.template get<1>(p) == 1);
1307 
1308  // Check the six should be a six
1309  auto xp = p.move(0,1);
1310  auto xm = p.move(0,-1);
1311 
1312  auto yp = p.move(1,1);
1313  auto ym = p.move(1,-1);
1314 
1315  auto zp = p.move(2,1);
1316  auto zm = p.move(2,-1);
1317 
1318  bool is_six;
1319  if (grid.existPoint(xp) && grid.existPoint(xm) &&
1320  grid.existPoint(yp) && grid.existPoint(ym) &&
1321  grid.existPoint(zp) && grid.existPoint(zm))
1322  {
1323  is_six = true;
1324  }
1325  else
1326  {
1327  is_six = false;
1328  }
1329 
1330  if (is_six == true && grid.template get<1>(p) != 6.0)
1331  {
1332  check = false;
1333  break;
1334  }
1335 
1336  if (grid.template get<1>(p) == 1)
1337  {
1338  tot_one++;
1339  }
1340 
1341  if (grid.template get<1>(p) == 6)
1342  {
1343  tot_six++;
1344  }
1345 
1346  ++it2;
1347  }
1348 
1349  BOOST_REQUIRE_EQUAL(check,true);
1350  BOOST_REQUIRE_EQUAL(tot_six,2020091);
1351  BOOST_REQUIRE_EQUAL(tot_one,376791);
1352  // Check correct-ness
1353 
1354 // print_grid("debug_out.vtk",grid);
1355 }
1356 
1357 BOOST_AUTO_TEST_CASE( sparse_grid_fast_stencil_vectorized_cross_simplified)
1358 {
1359  size_t sz[3] = {501,501,501};
1360  size_t sz_cell[3] = {500,500,500};
1361 
1363 
1364  grid.getBackgroundValue().template get<0>() = 0.0;
1365 
1366  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
1367 
1368  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
1369 
1370  cdsm.setDimensions(domain, sz_cell, 0);
1371 
1372  fill_sphere_quad(grid,cdsm);
1373 
1374  //grid.reorder();
1375 
1376  grid_key_dx<3> start({1,1,1});
1377  grid_key_dx<3> stop({499,499,499});
1378 
1379  for (int i = 0 ; i < 1 ; i++)
1380  {
1381  grid.conv_cross<0,1,1>(start,stop,[]( Vc::double_v & cmd, cross_stencil_v<double> & s,
1382  unsigned char * mask_sum){
1383 
1384  Vc::double_v Lap = s.xm + s.xp +
1385  s.ym + s.yp +
1386  s.zm + s.zp - 6.0*cmd;
1387 
1388  Vc::Mask<double> surround;
1389 
1390  for (int i = 0 ; i < Vc::double_v::Size ; i++)
1391  {surround[i] = (mask_sum[i] == 6);}
1392 
1393  Lap = Vc::iif(surround,Lap,Vc::double_v(1.0));
1394 
1395  return Lap;
1396  });
1397  }
1398 
1399  int tot_six = 0;
1400  int tot_one = 0;
1401 
1402  bool check = true;
1403  auto it2 = grid.getIterator(start,stop);
1404  while (it2.isNext())
1405  {
1406  auto p = it2.get();
1407 
1408  check &= (grid.template get<1>(p) == 6 || grid.template get<1>(p) == 1);
1409 
1410  // Check the six should be a six
1411  auto xp = p.move(0,1);
1412  auto xm = p.move(0,-1);
1413 
1414  auto yp = p.move(1,1);
1415  auto ym = p.move(1,-1);
1416 
1417  auto zp = p.move(2,1);
1418  auto zm = p.move(2,-1);
1419 
1420  bool is_six;
1421  if (grid.existPoint(xp) && grid.existPoint(xm) &&
1422  grid.existPoint(yp) && grid.existPoint(ym) &&
1423  grid.existPoint(zp) && grid.existPoint(zm))
1424  {
1425  is_six = true;
1426  }
1427  else
1428  {
1429  is_six = false;
1430  }
1431 
1432  if (is_six == true && grid.template get<1>(p) != 6.0)
1433  {
1434  check = false;
1435  break;
1436  }
1437 
1438  if (grid.template get<1>(p) == 1)
1439  {
1440  tot_one++;
1441  }
1442 
1443  if (grid.template get<1>(p) == 6)
1444  {
1445  tot_six++;
1446  }
1447 
1448  ++it2;
1449  }
1450 
1451  BOOST_REQUIRE_EQUAL(check,true);
1452  BOOST_REQUIRE_EQUAL(tot_six,15857813);
1453  BOOST_REQUIRE_EQUAL(tot_one,2977262);
1454  // Check correct-ness
1455 
1456 // print_grid("debug_out",grid);
1457 }
1458 
1459 BOOST_AUTO_TEST_CASE( sparse_grid_fast_stencil_vectorized_cross_simplified_float)
1460 {
1461  size_t sz[3] = {501,501,501};
1462  size_t sz_cell[3] = {500,500,500};
1463 
1465 
1466  grid.getBackgroundValue().template get<0>() = 0.0;
1467 
1468  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
1469 
1470  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
1471 
1472  cdsm.setDimensions(domain, sz_cell, 0);
1473 
1474  fill_sphere_quad(grid,cdsm);
1475 
1476  //grid.reorder();
1477 
1478  grid_key_dx<3> start({1,1,1});
1479  grid_key_dx<3> stop({499,499,499});
1480 
1481  for (int i = 0 ; i < 1 ; i++)
1482  {
1483  grid.conv_cross<0,1,1>(start,stop,[]( Vc::float_v & cmd, cross_stencil_v<float> & s,
1484  unsigned char * mask_sum){
1485 
1486  Vc::float_v Lap = s.xm + s.xp +
1487  s.ym + s.yp +
1488  s.zm + s.zp - 6.0f*cmd;
1489 
1490  Vc::Mask<float> surround;
1491 
1492  for (int i = 0 ; i < Vc::float_v::Size ; i++)
1493  {surround[i] = (mask_sum[i] == 6);}
1494 
1495  Lap = Vc::iif(surround,Lap,Vc::float_v(1.0f));
1496 
1497  return Lap;
1498  });
1499  }
1500 
1501  int tot_six = 0;
1502  int tot_one = 0;
1503 
1504  bool check = true;
1505  auto it2 = grid.getIterator(start,stop);
1506  while (it2.isNext())
1507  {
1508  auto p = it2.get();
1509 
1510  check &= (grid.template get<1>(p) == 6 || grid.template get<1>(p) == 1);
1511 
1512  // Check the six should be a six
1513  auto xp = p.move(0,1);
1514  auto xm = p.move(0,-1);
1515 
1516  auto yp = p.move(1,1);
1517  auto ym = p.move(1,-1);
1518 
1519  auto zp = p.move(2,1);
1520  auto zm = p.move(2,-1);
1521 
1522  bool is_six;
1523  if (grid.existPoint(xp) && grid.existPoint(xm) &&
1524  grid.existPoint(yp) && grid.existPoint(ym) &&
1525  grid.existPoint(zp) && grid.existPoint(zm))
1526  {
1527  is_six = true;
1528  }
1529  else
1530  {
1531  is_six = false;
1532  }
1533 
1534  if (is_six == true && grid.template get<1>(p) != 6.0)
1535  {
1536  check = false;
1537  break;
1538  }
1539 
1540  if (grid.template get<1>(p) == 1)
1541  {
1542  tot_one++;
1543  }
1544 
1545  if (grid.template get<1>(p) == 6)
1546  {
1547  tot_six++;
1548  }
1549 
1550  ++it2;
1551  }
1552 
1553  BOOST_REQUIRE_EQUAL(check,true);
1554  BOOST_REQUIRE_EQUAL(tot_six,15857813);
1555  BOOST_REQUIRE_EQUAL(tot_one,2977262);
1556  // Check correct-ness
1557 
1558 // print_grid("debug_out",grid);
1559 }
1560 
1561 constexpr int x = 0;
1562 constexpr int y = 1;
1563 constexpr int z = 2;
1564 
1565 BOOST_AUTO_TEST_CASE( sparse_grid_fast_stencil_vectorized_cross_simplified_ids)
1566 {
1567  size_t sz[3] = {501,501,501};
1568  size_t sz_cell[3] = {500,500,500};
1569 
1571 
1572  grid.getBackgroundValue().template get<0>() = 0.0;
1573 
1574  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
1575 
1576  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
1577 
1578  cdsm.setDimensions(domain, sz_cell, 0);
1579 
1580  fill_sphere_quad(grid,cdsm);
1581 
1582  //grid.reorder();
1583 
1584  grid_key_dx<3> start({1,1,1});
1585  grid_key_dx<3> stop({499,499,499});
1586 
1587  for (int i = 0 ; i < 1 ; i++)
1588  {
1589  grid.conv_cross_ids<1,double>(start,stop,[](auto & grid, auto & ids,
1590  unsigned char * mask_sum){
1591  Vc::double_v cmd;
1592 
1593  Vc::double_v xm;
1594  Vc::double_v xp;
1595  Vc::double_v ym;
1596  Vc::double_v yp;
1597  Vc::double_v zm;
1598  Vc::double_v zp;
1599 
1600  load_crs<x,-1,0>(xm,grid,ids);
1601  load_crs<x,1,0>(xp,grid,ids);
1602  load_crs<y,-1,0>(ym,grid,ids);
1603  load_crs<y,1,0>(yp,grid,ids);
1604  load_crs<z,-1,0>(zm,grid,ids);
1605  load_crs<z,1,0>(zp,grid,ids);
1606  load_crs<x,0,0>(cmd,grid,ids);
1607 
1608  Vc::double_v Lap = xm + xp +
1609  ym + yp +
1610  zm + zp - 6.0*cmd;
1611 
1612  Vc::Mask<double> surround;
1613 
1614  for (int i = 0 ; i < Vc::double_v::Size ; i++)
1615  {surround[i] = (mask_sum[i] == 6);}
1616 
1617  Lap = Vc::iif(surround,Lap,Vc::double_v(1.0));
1618 
1619  store_crs<1>(grid,Lap,ids);
1620  });
1621  }
1622 
1623  int tot_six = 0;
1624  int tot_one = 0;
1625 
1626  bool check = true;
1627  auto it2 = grid.getIterator(start,stop);
1628  while (it2.isNext())
1629  {
1630  auto p = it2.get();
1631 
1632  check &= (grid.template get<1>(p) == 6 || grid.template get<1>(p) == 1);
1633 
1634  // Check the six should be a six
1635  auto xp = p.move(0,1);
1636  auto xm = p.move(0,-1);
1637 
1638  auto yp = p.move(1,1);
1639  auto ym = p.move(1,-1);
1640 
1641  auto zp = p.move(2,1);
1642  auto zm = p.move(2,-1);
1643 
1644  bool is_six;
1645  if (grid.existPoint(xp) && grid.existPoint(xm) &&
1646  grid.existPoint(yp) && grid.existPoint(ym) &&
1647  grid.existPoint(zp) && grid.existPoint(zm))
1648  {
1649  is_six = true;
1650  }
1651  else
1652  {
1653  is_six = false;
1654  }
1655 
1656  if (is_six == true && grid.template get<1>(p) != 6.0)
1657  {
1658  check = false;
1659  break;
1660  }
1661 
1662  if (grid.template get<1>(p) == 1)
1663  {
1664  tot_one++;
1665  }
1666 
1667  if (grid.template get<1>(p) == 6)
1668  {
1669  tot_six++;
1670  }
1671 
1672  ++it2;
1673  }
1674 
1675  BOOST_REQUIRE_EQUAL(check,true);
1676  BOOST_REQUIRE_EQUAL(tot_six,15857813);
1677  BOOST_REQUIRE_EQUAL(tot_one,2977262);
1678  // Check correct-ness
1679 
1680 // print_grid("debug_out",grid);
1681 }
1682 
1683 BOOST_AUTO_TEST_CASE( sparse_grid_fast_stencil_vectorized_cross_simplified_ids_vector)
1684 {
1685  size_t sz[3] = {501,501,501};
1686  size_t sz_cell[3] = {500,500,500};
1687 
1689 
1690  grid.getBackgroundValue().template get<0>() = 0.0;
1691 
1692  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
1693 
1694  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
1695 
1696  cdsm.setDimensions(domain, sz_cell, 0);
1697 
1698  fill_sphere_quad_v(grid,cdsm);
1699 
1700  //grid.reorder();
1701 
1702  grid_key_dx<3> start({1,1,1});
1703  grid_key_dx<3> stop({499,499,499});
1704 
1705  for (int i = 0 ; i < 1 ; i++)
1706  {
1707  grid.conv_cross_ids<1,double>(start,stop,[](auto & grid, auto & ids,
1708  unsigned char * mask_sum){
1709  Vc::double_v cmd;
1710 
1711  Vc::double_v xm;
1712  Vc::double_v xp;
1713  Vc::double_v ym;
1714  Vc::double_v yp;
1715  Vc::double_v zm;
1716  Vc::double_v zp;
1717 
1718  load_crs<x,-1,0>(xm,grid,ids);
1719  load_crs<x,1,0>(xp,grid,ids);
1720  load_crs<y,-1,0>(ym,grid,ids);
1721  load_crs<y,1,0>(yp,grid,ids);
1722  load_crs<z,-1,0>(zm,grid,ids);
1723  load_crs<z,1,0>(zp,grid,ids);
1724  load_crs<x,0,0>(cmd,grid,ids);
1725 
1726  Vc::double_v Lap = xm + xp +
1727  ym + yp +
1728  zm + zp - 6.0*cmd;
1729 
1730  // Lap for the vector
1731 
1732  load_crs_v<x,-1,x,3>(xm,grid,ids);
1733  load_crs_v<x,1,x,3>(xp,grid,ids);
1734  load_crs_v<y,-1,x,3>(ym,grid,ids);
1735  load_crs_v<y,1,x,3>(yp,grid,ids);
1736  load_crs_v<z,-1,x,3>(zm,grid,ids);
1737  load_crs_v<z,1,x,3>(zp,grid,ids);
1738  load_crs_v<x,0,x,3>(cmd,grid,ids);
1739 
1740  Vc::double_v Lap_x = xm + xp +
1741  ym + yp +
1742  zm + zp - 6.0*cmd;
1743 
1744  load_crs_v<x,-1,y,3>(xm,grid,ids);
1745  load_crs_v<x,1,y,3>(xp,grid,ids);
1746  load_crs_v<y,-1,y,3>(ym,grid,ids);
1747  load_crs_v<y,1,y,3>(yp,grid,ids);
1748  load_crs_v<z,-1,y,3>(zm,grid,ids);
1749  load_crs_v<z,1,y,3>(zp,grid,ids);
1750  load_crs_v<x,0,y,3>(cmd,grid,ids);
1751 
1752  Vc::double_v Lap_y = xm + xp +
1753  ym + yp +
1754  zm + zp - 6.0*cmd;
1755 
1756  load_crs_v<x,-1,z,3>(xm,grid,ids);
1757  load_crs_v<x,1,z,3>(xp,grid,ids);
1758  load_crs_v<y,-1,z,3>(ym,grid,ids);
1759  load_crs_v<y,1,z,3>(yp,grid,ids);
1760  load_crs_v<z,-1,z,3>(zm,grid,ids);
1761  load_crs_v<z,1,z,3>(zp,grid,ids);
1762  load_crs_v<x,0,z,3>(cmd,grid,ids);
1763 
1764  Vc::double_v Lap_z = xm + xp +
1765  ym + yp +
1766  zm + zp - 6.0*cmd;
1767 
1768  Vc::Mask<double> surround;
1769 
1770  for (int i = 0 ; i < Vc::double_v::Size ; i++)
1771  {surround[i] = (mask_sum[i] == 6);}
1772 
1773  Lap = Vc::iif(surround,Lap,Vc::double_v(1.0));
1774  Lap_x = Vc::iif(surround,Lap_x,Vc::double_v(1.0));
1775  Lap_y = Vc::iif(surround,Lap_y,Vc::double_v(1.0));
1776  Lap_z = Vc::iif(surround,Lap_z,Vc::double_v(1.0));
1777 
1778  store_crs<1>(grid,Lap,ids);
1779  store_crs_v<4,x>(grid,Lap_x,ids);
1780  store_crs_v<4,y>(grid,Lap_y,ids);
1781  store_crs_v<4,z>(grid,Lap_z,ids);
1782  });
1783  }
1784 
1785  int tot_six = 0;
1786  int tot_one = 0;
1787 
1788  bool check = true;
1789  auto it2 = grid.getIterator(start,stop);
1790  while (it2.isNext())
1791  {
1792  auto p = it2.get();
1793 
1794  check &= (grid.template get<1>(p) == 6 || grid.template get<1>(p) == 1);
1795  check &= (grid.template get<4>(p)[0] == 6 || grid.template get<4>(p)[0] == 1);
1796  check &= (grid.template get<4>(p)[1] == 6 || grid.template get<4>(p)[1] == 1);
1797  check &= (grid.template get<4>(p)[2] == 6 || grid.template get<4>(p)[2] == 1);
1798 
1799  // Check the six should be a six
1800  auto xp = p.move(0,1);
1801  auto xm = p.move(0,-1);
1802 
1803  auto yp = p.move(1,1);
1804  auto ym = p.move(1,-1);
1805 
1806  auto zp = p.move(2,1);
1807  auto zm = p.move(2,-1);
1808 
1809  bool is_six;
1810  if (grid.existPoint(xp) && grid.existPoint(xm) &&
1811  grid.existPoint(yp) && grid.existPoint(ym) &&
1812  grid.existPoint(zp) && grid.existPoint(zm))
1813  {
1814  is_six = true;
1815  }
1816  else
1817  {
1818  is_six = false;
1819  }
1820 
1821  if (is_six == true && grid.template get<1>(p) != 6.0)
1822  {
1823  check = false;
1824  break;
1825  }
1826 
1827  if (is_six == true && grid.template get<4>(p)[0] != 6.0)
1828  {
1829  check = false;
1830  break;
1831  }
1832 
1833  if (is_six == true && grid.template get<4>(p)[1] != 6.0)
1834  {
1835  check = false;
1836  break;
1837  }
1838 
1839  if (is_six == true && grid.template get<4>(p)[2] != 6.0)
1840  {
1841  check = false;
1842  break;
1843  }
1844 
1845  if (grid.template get<1>(p) == 1)
1846  {
1847  tot_one++;
1848  }
1849 
1850  if (grid.template get<1>(p) == 6)
1851  {
1852  tot_six++;
1853  }
1854 
1855  ++it2;
1856  }
1857 
1858  BOOST_REQUIRE_EQUAL(check,true);
1859  BOOST_REQUIRE_EQUAL(tot_six,15857813);
1860  BOOST_REQUIRE_EQUAL(tot_one,2977262);
1861  // Check correct-ness
1862 
1863 // print_grid("debug_out",grid);
1864 }
1865 
1866 BOOST_AUTO_TEST_CASE( sparse_grid_slow_stencil)
1867 {
1868  size_t sz[3] = {501,501,501};
1869  size_t sz_cell[3] = {500,500,500};
1870 
1872 
1873  grid.getBackgroundValue().template get<0>() = 0.0;
1874 
1875  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
1876 
1877  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
1878 
1879  cdsm.setDimensions(domain, sz_cell, 0);
1880 
1881  fill_sphere_quad(grid,cdsm);
1882 
1883  grid_key_dx<3> start({1,1,1});
1884  grid_key_dx<3> stop({499,499,499});
1885 
1886  for (int i = 0 ; i < 1 ; i++)
1887  {
1888 
1889  auto it = grid.getIterator();
1890 
1891  while (it.isNext())
1892  {
1893  // center point
1894  auto p = it.get();
1895 
1896  // plus,minus X,Y,Z
1897  auto mx = p.move(0,-1);
1898  auto px = p.move(0,1);
1899  auto my = p.move(1,-1);
1900  auto py = p.move(1,1);
1901  auto mz = p.move(2,-1);
1902  auto pz = p.move(2,1);
1903 
1904  bool surround = grid.existPoint(mx) & grid.existPoint(px) & grid.existPoint(py) & grid.existPoint(my) & grid.existPoint(mz) & grid.existPoint(pz);
1905 
1906  double Lap = grid.template get<0>(mz) + grid.template get<0>(pz) +
1907  grid.template get<0>(my) + grid.template get<0>(py) +
1908  grid.template get<0>(mx) + grid.template get<0>(px) - 6.0*grid.template get<0>(p);
1909 
1910  grid.template insert<1>(p) = (surround)?Lap:6.0;
1911 
1912  ++it;
1913  }
1914 
1915  }
1916 
1917  bool check = true;
1918  auto it2 = grid.getIterator();
1919  while (it2.isNext())
1920  {
1921  auto p = it2.get();
1922 
1923  check &= grid.template get<1>(p) == 6;
1924 
1925  ++it2;
1926  }
1927 
1928  BOOST_REQUIRE_EQUAL(check,true);
1929  // Check correct-ness
1930  //grid.write("debug_out");
1931 }
1932 
1933 template<typename sgrid> void Test_unpack_and_check_full(sgrid & grid)
1934 {
1935  grid_key_dx<3> end;
1936  grid_key_dx<3> zero({0,0,0});
1937  size_t req2 = 0;
1938  size_t req3 = 0;
1939  size_t sz[3];
1940 
1941  for (size_t i = 0 ; i < 3 ; i++)
1942  {
1943  end.set_d(i,grid.getGrid().size(i) - 1);
1944  sz[i] = 0;
1945  }
1946 
1947  grid.template packRequest<0>(req2);
1948  auto sub_it = grid.getIterator(zero,end);
1949  grid.template packRequest<0>(sub_it,req3);
1950 
1951  BOOST_REQUIRE_EQUAL(req2,req3);
1952 
1953  Pack_stat sts2;
1954  Pack_stat sts3;
1955 
1956  // allocate the memory 2
1957  HeapMemory pmem2;
1958  pmem2.allocate(req2);
1959  ExtPreAlloc<HeapMemory> & mem2 = *(new ExtPreAlloc<HeapMemory>(req2,pmem2));
1960  mem2.incRef();
1961 
1962  // allocate the memory 3
1963  HeapMemory pmem3;
1964  pmem3.allocate(req3);
1965  ExtPreAlloc<HeapMemory> & mem3 = *(new ExtPreAlloc<HeapMemory>(req3,pmem3));
1966  mem3.incRef();
1967 
1968  grid.template pack<0>(mem2,sts2);
1969  grid.template pack<0>(mem3,sub_it,sts3);
1970 
1971  BOOST_REQUIRE_EQUAL(mem2.size(),mem3.size());
1972 
1973  bool check = true;
1974 
1975  char * p2 = (char *)pmem2.getPointer();
1976  char * p3 = (char *)pmem3.getPointer();
1977 
1978  for (size_t i = 0 ; i < mem2.size(); i++)
1979  {
1980  check &= (p2[i] == p3[i]);
1981  }
1982 
1983  BOOST_REQUIRE_EQUAL(check,true);
1984 
1985  // Unpack on a Sparse grid without information
1986 
1987  Unpack_stat ps;
1988  sgrid empty(sz);
1989 
1990  empty.template unpack<0>(mem3,ps);
1991 
1992  BOOST_REQUIRE_EQUAL(empty.getGrid().size(0),grid.getGrid().size(0));
1993  BOOST_REQUIRE_EQUAL(empty.getGrid().size(1),grid.getGrid().size(1));
1994  BOOST_REQUIRE_EQUAL(empty.getGrid().size(2),grid.getGrid().size(2));
1995 
1996  auto it = empty.getIterator();
1997 
1998  while (it.isNext())
1999  {
2000  auto p = it.get();
2001 
2002  check &= (grid.template get<0>(p) == empty.template get<0>(p));
2003 
2004  ++it;
2005  }
2006 
2007  BOOST_REQUIRE_EQUAL(check,true);
2008 }
2009 
2010 
2011 template<typename sgrid> void Test_unpack_and_check_full_noprp(sgrid & grid)
2012 {
2013  grid_key_dx<3> end;
2014  grid_key_dx<3> zero({0,0,0});
2015  size_t req2 = 0;
2016  size_t req3 = 0;
2017  size_t sz[3];
2018 
2019  for (size_t i = 0 ; i < 3 ; i++)
2020  {
2021  end.set_d(i,grid.getGrid().size(i) - 1);
2022  sz[i] = 0;
2023  }
2024 
2025  grid.template packRequest(req2);
2026  auto sub_it = grid.getIterator(zero,end);
2027  grid.template packRequest<0,1>(sub_it,req3);
2028 
2029  BOOST_REQUIRE_EQUAL(req2,req3);
2030 
2031  Pack_stat sts2;
2032  Pack_stat sts3;
2033 
2034  // allocate the memory 2
2035  HeapMemory pmem2;
2036  pmem2.allocate(req2);
2037  ExtPreAlloc<HeapMemory> & mem2 = *(new ExtPreAlloc<HeapMemory>(req2,pmem2));
2038  mem2.incRef();
2039 
2040  // allocate the memory 3
2041  HeapMemory pmem3;
2042  pmem3.allocate(req3);
2043  ExtPreAlloc<HeapMemory> & mem3 = *(new ExtPreAlloc<HeapMemory>(req3,pmem3));
2044  mem3.incRef();
2045 
2046  mem2.fill(0);
2047  mem3.fill(0);
2048 
2049  grid.template pack(mem2,sts2);
2050  grid.template pack<0,1>(mem3,sub_it,sts3);
2051 
2052  BOOST_REQUIRE_EQUAL(mem2.size(),mem3.size());
2053 
2054  bool check = true;
2055 
2056  char * p2 = (char *)pmem2.getPointer();
2057  char * p3 = (char *)pmem3.getPointer();
2058 
2059  for (size_t i = 0 ; i < mem2.size(); i++)
2060  {
2061  check &= (p2[i] == p3[i]);
2062  }
2063 
2064  BOOST_REQUIRE_EQUAL(check,true);
2065 
2066  // Unpack on a Sparse grid without information
2067 
2068  Unpack_stat ps;
2069  sgrid empty(sz);
2070 
2071  empty.template unpack(mem2,ps);
2072 
2073  BOOST_REQUIRE_EQUAL(empty.getGrid().size(0),grid.getGrid().size(0));
2074  BOOST_REQUIRE_EQUAL(empty.getGrid().size(1),grid.getGrid().size(1));
2075  BOOST_REQUIRE_EQUAL(empty.getGrid().size(2),grid.getGrid().size(2));
2076 
2077  auto it = empty.getIterator();
2078 
2079  while (it.isNext())
2080  {
2081  auto p = it.get();
2082 
2083  check &= (grid.template get<0>(p) == empty.template get<0>(p));
2084 
2085  ++it;
2086  }
2087 
2088  BOOST_REQUIRE_EQUAL(check,true);
2089 }
2090 
2091 template<typename sgrid> void Test_unpack_and_check(sgrid & grid, sgrid & grid2, size_t (& sz_cell)[3])
2092 {
2093  grid.getBackgroundValue().template get<0>() = 0.0;
2094 
2095  grid_key_dx<3> start({0,0,0});
2096  grid_key_dx<3> stop({250,250,250});
2097 
2098  Box<3,size_t> bx({0,0,0},{250,250,250});
2099 
2100  auto sub_it = grid.getIterator(start,stop);
2101  size_t req = 0;
2102  grid.template packRequest<0>(sub_it,req);
2103 
2104  // allocate the memory
2105  HeapMemory pmem;
2106  pmem.allocate(req);
2107  ExtPreAlloc<HeapMemory> & mem = *(new ExtPreAlloc<HeapMemory>(req,pmem));
2108  mem.incRef();
2109 
2110  Pack_stat sts;
2111 
2112  grid.template pack<0>(mem,sub_it,sts);
2113 
2114  // now we unpack on another grid
2115 
2116  int ctx = 0;
2117  Unpack_stat usts;
2118  grid2.template unpack<0>(mem,sub_it,usts,ctx,rem_copy_opt::NONE_OPT);
2119 
2120  bool match = true;
2121  auto it = grid.getIterator();
2122 
2123  while (it.isNext())
2124  {
2125  auto key = it.get();
2126 
2127  if (bx.isInside(key.toPoint()) == true)
2128  {match &= grid.template get<0>(key) == grid2.template get<0>(key);}
2129 
2130  ++it;
2131  }
2132 
2133  BOOST_REQUIRE_EQUAL(match,true);
2134 }
2135 
2136 BOOST_AUTO_TEST_CASE( sparse_grid_sub_grid_it_packing)
2137 {
2138  size_t sz[3] = {501,501,501};
2139  size_t sz_cell[3] = {500,500,500};
2140 
2144 
2145  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
2146 
2147  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
2148 
2149  cdsm.setDimensions(domain, sz_cell, 0);
2150 
2151  fill_sphere(grid,cdsm);
2152 
2153  Test_unpack_and_check(grid,grid2,sz_cell);
2154 
2155  grid_key_dx<3> start({251,251,251});
2156  grid_key_dx<3> stop({490,490,490});
2157 
2158 
2159  size_t cnt2 = 0;
2160 
2161  auto sub_it = grid.getIterator(start,stop);
2162 
2163  while (sub_it.isNext())
2164  {
2165  auto p = sub_it.get();
2166 
2167  grid3.template insert<0>(p) = grid.template get<0>(p);
2168  grid3.template insert<1>(p) = grid.template get<1>(p);
2169 
2170  cnt2++;
2171 
2172  ++sub_it;
2173  }
2174 
2175  Test_unpack_and_check(grid,grid3,sz_cell);
2176 
2177  grid_key_dx<3> start2({251,251,251});
2178  grid_key_dx<3> stop2({490,490,490});
2179 
2180  size_t cnt = 0;
2181 
2182  auto sub_it2 = grid.getIterator(start2,stop2);
2183 
2184  bool match = true;
2185 
2186  while (sub_it2.isNext())
2187  {
2188  auto p = sub_it2.get();
2189 
2190  match &= grid3.template insert<0>(p) == grid.template get<0>(p);
2191  match &= grid3.template insert<1>(p) == grid.template get<1>(p);
2192 
2193  cnt++;
2194 
2195  ++sub_it2;
2196  }
2197 
2198  BOOST_REQUIRE_EQUAL(match,true);
2199  BOOST_REQUIRE_EQUAL(cnt,cnt2);
2200 }
2201 
2202 
2203 BOOST_AUTO_TEST_CASE( sparse_grid_remove_area)
2204 {
2205  size_t sz[3] = {501,501,501};
2206 
2208 
2209  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
2210 
2211 
2212  Box<3,size_t> bx_create({100,100,100},{400,400,400});
2213  Box<3,long int> bx_delete({150,150,150},{350,350,350});
2214 
2215  grid_sm<3,void> gs(sz);
2216  grid_key_dx_iterator_sub<3> sub(gs,bx_create.getKP1(),bx_create.getKP2());
2217 
2218  while (sub.isNext())
2219  {
2220  auto p = sub.get();
2221 
2222  grid.template insert<0>(p) = 1.0;
2223 
2224  ++sub;
2225  }
2226 
2227  grid.remove(bx_delete);
2228 
2229  bool check = true;
2230 
2231  size_t cnt = 0;
2232  auto it2 = grid.getIterator(bx_create.getKP1(),bx_create.getKP2());
2233  while (it2.isNext())
2234  {
2235  auto p = it2.get();
2236 
2237  check &= bx_delete.isInside(p.toPoint()) == false;
2238 
2239  cnt++;
2240 
2241  ++it2;
2242  }
2243 
2244  BOOST_REQUIRE_EQUAL(check,true);
2245 
2246  BOOST_REQUIRE_EQUAL(cnt,bx_create.getVolumeKey() - bx_delete.getVolumeKey());
2247 }
2248 
2249 BOOST_AUTO_TEST_CASE( sparse_grid_copy_to)
2250 {
2251  size_t sz[3] = {501,501,501};
2252 
2254 
2255  grid.getBackgroundValue().template get<0>() = 0.0;
2256 
2257  size_t sz_g2[3] = {259,27,27};
2259 
2260  grid_key_dx_iterator<3> key_it(grid2.getGrid());
2261  auto gs = grid2.getGrid();
2262 
2263  while (key_it.isNext())
2264  {
2265  auto key = key_it.get();
2266 
2267  grid2.insert<0>(key) = gs.LinId(key);
2268 
2269  ++key_it;
2270  }
2271 
2272  Box<3,size_t> bx_src({1,1,1},{255,23,23});
2273  Box<3,size_t> bx_dst({5,5,5},{259,27,27});
2274 
2275  grid.copy_to(grid2,bx_src,bx_dst);
2276 
2277  BOOST_REQUIRE(grid.size() != 0);
2278  BOOST_REQUIRE_EQUAL(grid.size(),bx_dst.getVolumeKey());
2279 
2280  bool match = true;
2281  auto it_check = grid.getIterator(bx_dst.getKP1(),bx_dst.getKP2());
2282 
2283  while (it_check.isNext())
2284  {
2285  auto key = it_check.get();
2286 
2287  grid_key_dx<3> key2 = key - bx_dst.getKP1();
2288  key2 += bx_src.getKP1();
2289 
2290  if (grid.template get<0>(key) != gs.LinId(key2))
2291  {match = false;}
2292 
2293  ++it_check;
2294  }
2295 
2296  BOOST_REQUIRE_EQUAL(match,true);
2297 
2298  bx_dst += Point<3,size_t>({0,40,40});
2299  grid.copy_to(grid2,bx_src,bx_dst);
2300 
2301  BOOST_REQUIRE(grid.size() != 0);
2302  BOOST_REQUIRE_EQUAL(grid.size(),2*bx_dst.getVolumeKey());
2303 
2304  match = true;
2305  auto it_check2 = grid.getIterator(bx_dst.getKP1(),bx_dst.getKP2());
2306 
2307  while (it_check2.isNext())
2308  {
2309  auto key = it_check2.get();
2310 
2311  grid_key_dx<3> key2 = key - bx_dst.getKP1();
2312  key2 += bx_src.getKP1();
2313 
2314  if (grid.template get<0>(key) != gs.LinId(key2))
2315  {match = false;}
2316 
2317  ++it_check2;
2318  }
2319 
2320  BOOST_REQUIRE_EQUAL(match,true);
2321 }
2322 
2323 BOOST_AUTO_TEST_CASE( sparse_pack_full )
2324 {
2325  size_t sz[3] = {501,501,501};
2326  size_t sz_cell[3] = {500,500,500};
2327 
2329 
2330  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
2331 
2332  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
2333 
2334  cdsm.setDimensions(domain, sz_cell, 0);
2335 
2336  fill_sphere(grid,cdsm);
2337 
2338  Test_unpack_and_check_full(grid);
2339 }
2340 
2341 BOOST_AUTO_TEST_CASE( sparse_pack_full_noprp )
2342 {
2343  size_t sz[3] = {501,501,501};
2344  size_t sz_cell[3] = {500,500,500};
2345 
2347 
2348  CellDecomposer_sm<3, float, shift<3,float>> cdsm;
2349 
2350  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
2351 
2352  cdsm.setDimensions(domain, sz_cell, 0);
2353 
2354  fill_sphere(grid,cdsm);
2355 
2356  Test_unpack_and_check_full_noprp(grid);
2357 }
2358 
2359 BOOST_AUTO_TEST_CASE( sparse_operator_equal )
2360 {
2361  size_t sz[3] = {270,270,270};
2362 
2364 
2365  grid.getBackgroundValue().template get<0>() = 0.0;
2366 
2368 
2369  grid_key_dx_iterator<3> key_it(grid.getGrid());
2370  auto gs = grid.getGrid();
2371 
2372  while (key_it.isNext())
2373  {
2374  auto key = key_it.get();
2375 
2376  grid.insert<0>(key) = gs.LinId(key);
2377 
2378  ++key_it;
2379  }
2380 
2381  grid2 = grid;
2382 
2383  BOOST_REQUIRE(grid.size() == grid2.size());
2384 
2385  bool match = true;
2386  auto it_check = grid.getIterator();
2387 
2388  while (it_check.isNext())
2389  {
2390  auto key = it_check.get();
2391 
2392  if (grid.template get<0>(key) != grid2.template get<0>(key))
2393  {match = false;}
2394 
2395  ++it_check;
2396  }
2397 
2398  BOOST_REQUIRE_EQUAL(match,true);
2399 }
2400 
2401 BOOST_AUTO_TEST_CASE( sparse_testing_clear )
2402 {
2403  size_t sz[3] = {10000,10000,10000};
2404 
2406 
2407  grid.getBackgroundValue().template get<0>() = 555.0;
2408 
2409  grid_key_dx<3> key1({5000,5000,5000});
2410  grid_key_dx<3> key2({5001,5001,5001});
2411  grid_key_dx<3> key3({5002,5003,5003});
2412 
2413  grid.template insert<0>(key1) = 1.0;
2414  grid.template insert<0>(key2) = 2.0;
2415  grid.template insert<0>(key3) = 3.0;
2416 
2417 
2418  grid.clear();
2419 
2420  grid_key_dx<3> keyzero({0,0,0});
2421  BOOST_REQUIRE_EQUAL(grid.template get<0>(keyzero),555.0);
2422 
2423  grid.template insert<0>(key1) = 1.0;
2424  grid.template insert<0>(key2) = 2.0;
2425  grid.template insert<0>(key3) = 3.0;
2426 
2427  BOOST_REQUIRE_EQUAL(grid.template get<0>(key1),1.0);
2428  BOOST_REQUIRE_EQUAL(grid.template get<0>(key2),2.0);
2429  BOOST_REQUIRE_EQUAL(grid.template get<0>(key3),3.0);
2430 
2431  BOOST_REQUIRE_EQUAL(grid.template get<0>(keyzero),555.0);
2432 }
2433 
2434 BOOST_AUTO_TEST_SUITE_END()
2435 
virtual size_t size() const
Get the size of the LAST allocated memory.
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
virtual bool allocate(size_t sz)
allocate memory
Definition: HeapMemory.cpp:33
__host__ __device__ Point< dim, typeT > toPoint() const
Convert to a point the grid_key_dx.
Definition: grid_key.hpp:457
__device__ __host__ index_type get(index_type i) const
Get the i index.
Definition: grid_key.hpp:503
This class allocate, and destroy CPU memory.
Definition: HeapMemory.hpp:39
virtual void * getPointer()
get a readable pointer with the data
Definition: HeapMemory.cpp:228
virtual void fill(unsigned char c)
fill host and device memory with the selected byte
size_t size() const
This is a distributed grid.
__device__ __host__ const T & get(unsigned int i) const
Get coordinate.
Definition: Point.hpp:172
const grid_key_dx< dim > & get() const
Get the actual key.
virtual void incRef()
Increment the reference counter.
Definition: ExtPreAlloc.hpp:98
Unpacking status object.
Definition: Pack_stat.hpp:15
Laplacian second order on h (spacing)
Definition: Laplacian.hpp:22
Declaration grid_sm.
Definition: grid_sm.hpp:147
Declaration grid_key_dx_iterator_sub.
Definition: grid_sm.hpp:156
__device__ __host__ void set_d(index_type i, index_type id)
Set the i index.
Definition: grid_key.hpp:516
It model an expression expr1 + ... exprn.
Definition: sum.hpp:92
Packing status object.
Definition: Pack_stat.hpp:60