OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
SparseGrid_iterator.hpp
1 /*
2  * SparseGrid_iterator.hpp
3  *
4  * Created on: Oct 24, 2017
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_SPARSEGRID_SPARSEGRID_ITERATOR_HPP_
9 #define OPENFPM_DATA_SRC_SPARSEGRID_SPARSEGRID_ITERATOR_HPP_
10 
17 {
19  size_t chunk;
20 
22  size_t lin_id;
23 
24 public:
25 
26  inline grid_key_sparse_lin_dx(size_t chunk, size_t lin_id)
28  {
29 
30  }
31 
37  inline size_t getChunk() const
38  {
39  return chunk;
40  }
41 
47  inline size_t getPos() const
48  {
49  return lin_id;
50  }
51 };
52 
53 
58 template<unsigned int n_ele>
59 inline void fill_mask(short unsigned int (& mask_it)[n_ele],
60  const unsigned char (& mask)[n_ele],
61  int & mask_nele)
62 {
63  mask_nele = 0;
64 
65  for (size_t i = 0 ; i < n_ele ; i++)
66  {
67  if (mask[i] & 1)
68  {
69  mask_it[mask_nele] = i;
70  mask_nele++;
71  }
72  }
73 }
74 
79 template<unsigned int dim, unsigned int n_ele>
80 inline void fill_mask_box(short unsigned int (& mask_it)[n_ele],
81  const unsigned char (& mask)[n_ele],
82  size_t & mask_nele,
83  Box<dim,size_t> & bx,
84  const grid_key_dx<dim> (& loc_grid)[n_ele])
85 {
86  mask_nele = 0;
87 
88  for (size_t i = 0 ; i < n_ele ; i++)
89  {
90  size_t id = i;
91 
92  bool is_inside = true;
93  // we check the point is inside inte
94  for (size_t j = 0 ; j < dim ; j++)
95  {
96  if (loc_grid[id].get(j) < (long int)bx.getLow(j) ||
97  loc_grid[id].get(j) > (long int)bx.getHigh(j))
98  {
99  is_inside = false;
100 
101  break;
102  }
103  }
104 
105  if (is_inside == true && (mask[i] & 1))
106  {
107  mask_it[mask_nele] = id;
108  mask_nele++;
109  }
110  }
111 }
112 
119 template<unsigned int n_ele>
120 struct mheader
121 {
123  unsigned char mask[n_ele];
124 };
125 
126 
133 template<unsigned int dim>
134 struct cheader
135 {
138 
140  int nele;
141 };
142 
147 template<unsigned dim, unsigned int n_ele>
149 {
150  const static int cnk_pos = 0;
151  const static int cnk_nele = 1;
152  const static int cnk_mask = 2;
153 
156 
159 
161  const grid_key_dx<dim> (* lin_id_pos)[n_ele];
162 
164  size_t chunk_id;
165 
167  size_t mask_nele;
168 
170  size_t mask_it_pnt;
171 
174 
177 
180 
182  size_t sz_cnk[dim];
183 
185  short unsigned int mask_it[n_ele];
186 
192  {
193  mask_it_pnt = 0;
194  mask_nele = 0;
195 
196  while (mask_nele == 0 && chunk_id < header_inf->size())
197  {
198  auto & mask = header_mask->get(chunk_id).mask;
199 
200  Box<dim,size_t> cnk_box;
201 
202  for (size_t i = 0 ; i < dim ; i++)
203  {
204  cnk_box.setLow(i,header_inf->get(chunk_id).pos.get(i));
205  cnk_box.setHigh(i,header_inf->get(chunk_id).pos.get(i) + sz_cnk[i] - 1);
206  }
207 
208  Box<dim,size_t> inte;
209 
210  if (bx.Intersect(cnk_box,inte) == true)
211  {
212  inte -= header_inf->get(chunk_id).pos.toPoint();
213  fill_mask_box<dim,n_ele>(mask_it,mask,mask_nele,inte,*lin_id_pos);
214  }
215  else
216  {mask_nele = 0;}
217 
218  chunk_id = (mask_nele == 0)?chunk_id + 1:chunk_id;
219 
220  }
221  }
222 
223 public:
224 
232 
235  const grid_key_dx<dim> (& lin_id_pos)[n_ele],
236  const grid_key_dx<dim> & start,
237  const grid_key_dx<dim> & stop,
238  const size_t (& sz_cnk)[dim])
241  {
242  for (size_t i = 0 ; i < dim ; i++)
243  {
244  this->sz_cnk[i] = sz_cnk[i];
245 
246  bx.setLow(i,start.get(i));
247  bx.setHigh(i,stop.get(i));
248  }
249 
251  }
252 
262  {
263  header_inf = g_s_it.header_inf;
264  header_mask = g_s_it.header_mask;
265  lin_id_pos = g_s_it.lin_id_pos;
266  chunk_id = g_s_it.chunk_id;
267  mask_nele = g_s_it.mask_nele;
268  mask_it_pnt = g_s_it.mask_it_pnt;
269  start = g_s_it.start;
270  stop = g_s_it.stop;
271  bx = g_s_it.bx;
272  for (size_t i = 0 ; i < dim ; i++)
273  {sz_cnk[i] = g_s_it.sz_cnk[i];}
274 
275  memcpy(mask_it,g_s_it.mask_it,sizeof(short unsigned int)*n_ele);
276  }
277 
278  inline grid_key_sparse_dx_iterator_sub<dim,n_ele> & operator++()
279  {
280  mask_it_pnt++;
281 
282  if (mask_it_pnt < mask_nele)
283  {
284  return *this;
285  }
286 
287  chunk_id++;
288  mask_it_pnt = 0;
289 
290  if (chunk_id < header_inf->size())
291  {
293  }
294 
295  return *this;
296  }
297 
298 
304  inline grid_key_dx<dim> get() const
305  {
306  grid_key_dx<dim> k_pos;
307 
308  size_t lin_id = mask_it[mask_it_pnt];
309 
310  for (size_t i = 0 ; i < dim ; i++)
311  {
312  k_pos.set_d(i,(*lin_id_pos)[lin_id].get(i) + header_inf->get(chunk_id).pos.get(i));
313  }
314 
315  return k_pos;
316  }
317 
326  {
328  }
329 
335  bool isNext()
336  {
337  return chunk_id < header_inf->size();
338  }
339 
345  const grid_key_dx<dim> & getStart() const
346  {
347  return start;
348  }
349 
355  const grid_key_dx<dim> & getStop() const
356  {
357  return stop;
358  }
359 
366  {return header_inf;}
367 
374  {return header_mask;}
375 
381  const grid_key_dx<dim> (* private_get_lin_id_pos() const)[n_ele]
382  {return lin_id_pos;}
383 
389  size_t private_get_chunk_id() const
390  {return chunk_id;}
391 
397  size_t private_get_mask_nele() const
398  {return mask_nele;}
399 
400 
406  size_t private_get_mask_it_pnt() const
407  {return mask_it_pnt;}
408 
414  const short unsigned int (& private_get_mask_it() const) [n_ele]
415  {
416  return mask_it;
417  }
418 };
419 
424 template<unsigned dim, unsigned int n_ele>
426 {
429 
432 
434  const grid_key_dx<dim> (* lin_id_pos)[n_ele];
435 
437  size_t chunk_id;
438 
441 
443  size_t mask_it_pnt;
444 
446  short unsigned int mask_it[n_ele];
447 
453  {
454  mask_nele = 0;
455  mask_it_pnt = 0;
456 
457  while (mask_nele == 0 && chunk_id < header_inf->size())
458  {
459  auto & mask = header_mask->get(chunk_id).mask;
460 
461  fill_mask<n_ele>(mask_it,mask,mask_nele);
462 
463  chunk_id = (mask_nele == 0)?chunk_id + 1:chunk_id;
464 
465  }
466  }
467 
468 public:
469 
477 
480  const grid_key_dx<dim> (* lin_id_pos)[n_ele])
482  {
484  }
485 
486  inline grid_key_sparse_dx_iterator<dim,n_ele> & operator++()
487  {
488  mask_it_pnt++;
489 
490  if (mask_it_pnt < mask_nele)
491  {
492  return *this;
493  }
494 
495  chunk_id++;
496  mask_it_pnt = 0;
497 
498  if (chunk_id < header_inf->size())
499  {
501  }
502 
503  return *this;
504  }
505 
515  {
516  header_mask = g_s_it.header_mask;
517  header_inf = g_s_it.header_inf;
518  lin_id_pos = g_s_it.lin_id_pos;
519  chunk_id = g_s_it.chunk_id;
520  mask_nele = g_s_it.mask_nele;
521  mask_it_pnt = g_s_it.mask_it_pnt;
522 
523  memcpy(mask_it,g_s_it.mask_it,sizeof(short unsigned int)*n_ele);
524  }
525 
535  {
539  chunk_id = 0;
540 
542  }
543 
552  {
554  }
555 
561  inline grid_key_dx<dim> get() const
562  {
563  grid_key_dx<dim> k_pos;
564 
565  size_t lin_id = mask_it[mask_it_pnt];
566 
567  for (size_t i = 0 ; i < dim ; i++)
568  {
569  k_pos.set_d(i,(*lin_id_pos)[lin_id].get(i) + header_inf->get(chunk_id).pos.get(i));
570  }
571 
572  return k_pos;
573  }
574 
580  bool isNext()
581  {
582  return chunk_id < header_inf->size();
583  }
584 };
585 
586 #endif /* OPENFPM_DATA_SRC_SPARSEGRID_SPARSEGRID_ITERATOR_HPP_ */
grid_key_dx< dim > get() const
Return the position of the actual point in coordinates.
int nele
how many elements in the chunk are set
short unsigned int mask_it[n_ele]
set of index in the chunk on which we have to iterate
size_t private_get_mask_it_pnt() const
Return the private member mask_it_pnt.
grid_key_sparse_lin_dx getKeyF()
Return the actual point linearized.
const grid_key_dx< dim >(* private_get_lin_id_pos() const)[n_ele]
Return the private member lin_id_pos.
__device__ __host__ T getLow(int i) const
get the i-coordinate of the low bound interval of the box
Definition: Box.hpp:556
grid_key_dx< dim > pos
where is located the chunk
const grid_key_dx< dim > & getStop() const
Return the stop point for the iteration.
const short unsigned int(& private_get_mask_it() const)[n_ele]
Return the private member mask_it.
size_t lin_id
linearized id
__device__ __host__ index_type get(index_type i) const
Get the i index.
Definition: grid_key.hpp:503
Grid key sparse iterator.
size_t chunk_id
point to the actual chunk
size_t size()
Stub size.
Definition: map_vector.hpp:211
const grid_key_dx< dim > & getStart() const
Return the starting point for the iteration.
bool isNext()
Return true if there is a next grid point.
const grid_key_dx< dim >(* lin_id_pos)[n_ele]
linearized id to position in coordinates conversion
This structure contain the information of a chunk.
Box< dim, size_t > bx
Sub-grid box.
size_t chunk_id
point to the actual chunk
const openfpm::vector< mheader< n_ele > > * header_mask
It store the information of each chunk mask.
__device__ __host__ void setHigh(int i, T val)
set the high interval of the box
Definition: Box.hpp:544
const openfpm::vector< mheader< n_ele > > * header_mask
It store the information of each chunk.
size_t getPos() const
Return the linearized position in the chunk.
grid_key_dx< dim > start
Starting point.
const grid_key_dx< dim >(* lin_id_pos)[n_ele]
linearized id to position in coordinates conversion
const openfpm::vector< cheader< dim > > * header_inf
it store the information of each chunk
__device__ __host__ void setLow(int i, T val)
set the low interval of the box
Definition: Box.hpp:533
grid_key_dx< dim > get() const
Return the position of the actual point in coordinates.
size_t private_get_chunk_id() const
Return the private member chunk_id.
size_t mask_it_pnt
Actual point in mask it.
void reinitialize(const grid_key_sparse_dx_iterator< dim, n_ele > &g_s_it)
Reinitialize the iterator.
KeyT const ValueT ValueT OffsetIteratorT OffsetIteratorT int
[in] The number of segments that comprise the sorting data
const openfpm::vector< mheader< n_ele > > * private_get_header_mask() const
Return the private member header.
void SelectValidAndFill_mask_it()
Everytime we move to a new chunk we calculate on which indexes we have to iterate.
size_t mask_it_pnt
Actual point in mask it.
grid_key_dx< dim > stop
Stop point.
size_t getChunk() const
Return the chunk id.
size_t mask_nele
Number of points in mask_it.
__device__ __host__ bool Intersect(const Box< dim, T > &b, Box< dim, T > &b_out) const
Intersect.
Definition: Box.hpp:95
Grid key sparse iterator on a sub-part of the domain.
bool isNext()
Return true if there is a next grid point.
It store the position in space of the sparse grid.
const openfpm::vector< cheader< dim > > * header_inf
It store the information of each chunk.
__device__ __host__ void set_d(index_type i, index_type id)
Set the i index.
Definition: grid_key.hpp:516
short unsigned int mask_it[n_ele]
set of index in the chunk on which we have to iterate
unsigned char mask[n_ele]
which elements in the chunks are set
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:202
const openfpm::vector< cheader< dim > > * private_get_header_inf() const
Return the private member header.
__device__ __host__ T getHigh(int i) const
get the high interval of the box
Definition: Box.hpp:567
size_t private_get_mask_nele() const
Return the private member mask_nele.
int mask_nele
Number of points in mask_it.
grid_key_sparse_dx_iterator_sub()
Default constructor.
void reinitialize(const grid_key_sparse_dx_iterator_sub< dim, n_ele > &g_s_it)
Reinitialize the iterator.
grid_key_sparse_lin_dx getKeyF()
Return the actual point.
This structure contain the information of a chunk.
grid_key_sparse_dx_iterator()
Default constructor.
void SelectValidAndFill_mask_it()
Everytime we move to a new chunk we calculate on which indexes we have to iterate.
void reinitialize(const grid_key_sparse_dx_iterator_sub< dim, n_ele > &g_s_it)
Reinitialize the iterator.