OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
grid_dist_id_iterator_sub.hpp
1 /*
2  * grid_dist_id_iterator_sub.hpp
3  *
4  * Created on: Oct 14, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef SRC_GRID_GRID_DIST_ID_ITERATOR_SUB_HPP_
9 #define SRC_GRID_GRID_DIST_ID_ITERATOR_SUB_HPP_
10 
11 
21 template<unsigned int dim, typename device_grid>
23 {
24  // sub_set of the grid where to iterate
25  struct sub_set
26  {
31  };
32 
34  size_t g_c;
35 
38 
41 
44 
47 
50 
60  bool compute_subset(size_t gc, grid_key_dx<dim> & start_c, grid_key_dx<dim> & stop_c)
61  {
62  // Intersect the grid keys
63 
64  for (size_t i = 0 ; i < dim ; i++)
65  {
66  long int start_p = gdb_ext.get(g_c).Dbox.getP1().get(i) + gdb_ext.get(g_c).origin.get(i);
67  long int stop_p = gdb_ext.get(g_c).Dbox.getP2().get(i) + gdb_ext.get(g_c).origin.get(i);
68  if (start.get(i) <= start_p)
69  start_c.set_d(i,gdb_ext.get(g_c).Dbox.getP1().get(i));
70  else if (start.get(i) <= stop_p)
71  start_c.set_d(i,start.get(i) - gdb_ext.get(g_c).origin.get(i));
72  else
73  return false;
74 
75  if (stop.get(i) >= stop_p)
76  stop_c.set_d(i,gdb_ext.get(g_c).Dbox.getP2().get(i));
77  else if (stop.get(i) >= start_p)
78  stop_c.set_d(i,stop.get(i) - gdb_ext.get(g_c).origin.get(i));
79  else
80  return false;
81  }
82 
83  return true;
84  }
85 
90  {
91  // start and stop for the subset grid
92  grid_key_dx<dim> start_c;
93  grid_key_dx<dim> stop_c;
94 
95  // When the grid has size 0 potentially all the other informations are garbage
96  while (g_c < gList.size() &&
97  (gList.get(g_c).size() == 0 || gdb_ext.get(g_c).Dbox.isValid() == false || compute_subset(g_c,start_c,stop_c) == false ))
98  {g_c++;}
99 
100  // get the next grid iterator
101  if (g_c < gList.size())
102  {
103  a_it.reinitialize(gList.get(g_c).getIterator(start_c,stop_c));
104  }
105  }
106 
107  public:
108 
115  {
116  g_c = tmp.g_c;
117  gList = tmp.gList;
118  gdb_ext = tmp.gdb_ext;
119  start = tmp.start;
120  stop = tmp.stop;
121  a_it.reinitialize(tmp.a_it);
122 
123  return *this;
124  }
125 
132  :g_c(tmp.g_c),gList(tmp.gList),gdb_ext(tmp.gdb_ext),start(tmp.start),stop(tmp.stop)
133  {
134  // get the next grid iterator
135  if (g_c < gList.size())
136  {
137  a_it.reinitialize(tmp.a_it);
138  }
139  }
140 
150  :g_c(0),gList(gk),gdb_ext(gdb_ext),start(start),stop(stop)
151  {
152  // Initialize the current iterator
153  // with the first grid
154  selectValidGrid();
155  }
156 
157  // Destructor
159  {
160  }
161 
169  {
170  ++a_it;
171 
172  // check if a_it is at the end
173 
174  if (a_it.isNext() == true)
175  return *this;
176  else
177  {
178  // switch to the new grid
179  g_c++;
180 
181  selectValidGrid();
182  }
183 
184  return *this;
185  }
186 
192  inline bool isNext()
193  {
194  // If there are no other grid stop
195 
196  if (g_c >= gList.size())
197  return false;
198 
199  return true;
200  }
201 
208  {
209  return grid_dist_key_dx<dim>(g_c,a_it.get());
210  }
211 
221  {
222  // Get the sub-domain id
223  size_t sub_id = k.getSub();
224 
225  grid_key_dx<dim> k_glob = k.getKey();
226 
227  // shift
228  k_glob = k_glob + gdb_ext.get(sub_id).origin;
229 
230  return k_glob;
231  }
232 
233  /* \brief Get the starting point of the grid iterator
234  *
235  * \return the starting point
236  *
237  */
238  inline grid_key_dx<dim> getStart() const
239  {
240  return start;
241  }
242 
243  /* \brief Get the stop point of the grid iterator
244  *
245  * \return the stop point
246  *
247  */
248  inline grid_key_dx<dim> getStop() const
249  {
250  return stop;
251  }
252 
257  inline size_t N_loc_grid()
258  {
259  return gList.size();
260  }
261 
270  inline size_t loc_grid_info_start(size_t i,size_t j)
271  {
272  return gdb_ext.get(i).DBox.getLow(i);
273  }
274 
283  inline size_t loc_grid_info_size(size_t i,size_t j)
284  {
285  return gdb_ext.get(i).GDBox.getHigh(i);
286  }
287 };
288 
289 
291 
292 #define WHILE_M(grid,stencil) auto & ginfo = grid.getLocalGridsInfo();\
293  for (size_t s = 0 ; s < grid.getN_loc_grid() ; s++)\
294  {\
295  auto it = grid.get_loc_grid_iterator_stencil(s,stencil);\
296 \
297  int lo[3] = {(int)ginfo.get(s).Dbox.getLow(0),(int)ginfo.get(s).Dbox.getLow(1),(int)ginfo.get(s).Dbox.getLow(2)};\
298  int hi[3] = {(int)ginfo.get(s).Dbox.getHigh(0),(int)ginfo.get(s).Dbox.getHigh(1),(int)ginfo.get(s).Dbox.getHigh(2)};\
299 \
300  int uhi[3] = {(int)ginfo.get(s).GDbox.getHigh(0),(int)ginfo.get(s).GDbox.getHigh(1),(int)ginfo.get(s).GDbox.getHigh(2)};\
301 \
302  int sx = uhi[0]+1;\
303  int sxsy = (uhi[0]+1)*(uhi[1]+1);
304 
305 #define ITERATE_3D_M(n_pt) int i = lo[2];\
306  for ( ; i <= hi[2] ; i+=1)\
307  {\
308  int j = lo[1];\
309  for ( ; j <= hi[1] ; j+=1)\
310  {\
311  int k = lo[0];\
312  for ( ; k <= hi[0] ; k+=n_pt)\
313  {
314 
315 
316 #define GET_GRID_M(grid) grid.get_loc_grid(s);
317 
318 
319 #define END_LOOP_M(n_pt) it.private_sum<n_pt>();\
320  }\
321  it.private_adjust( - k + sx + lo[0]);\
322  }\
323  it.private_adjust(- j*sx + sxsy + lo[1]*sx);\
324  }\
325  }
326 
327 #endif /* SRC_GRID_GRID_DIST_ID_ITERATOR_SUB_HPP_ */
grid_dist_iterator_sub(const grid_dist_iterator_sub< dim, device_grid > &tmp)
Copy constructor.
grid_key_dx< dim > start
start key
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
grid_key_dx< dim > stop
stop key
bool isNext()
Check if there is the next element.
size_t N_loc_grid()
Return the number of local grids.
size_t size()
Stub size.
Definition: map_vector.hpp:70
Grid key for a distributed grid.
grid_dist_iterator_sub< dim, device_grid > & operator++()
Get the next element.
grid_dist_iterator_sub(const grid_key_dx< dim > &start, const grid_key_dx< dim > &stop, const openfpm::vector< device_grid > &gk, const openfpm::vector< GBoxes< device_grid::dims >> &gdb_ext)
Constructor of the distributed grid iterator.
bool compute_subset(size_t gc, grid_key_dx< dim > &start_c, grid_key_dx< dim > &stop_c)
compute the subset where it has to iterate
This structure store the Box that define the domain inside the Ghost + domain box.
Definition: GBoxes.hpp:39
size_t loc_grid_info_size(size_t i, size_t j)
Return the component j of the stop point (P2) of the domain part for the local grid i...
grid_key_dx_iterator_sub< dim > a_it
Actual iterator.
const openfpm::vector< device_grid > & gList
List of the grids we are going to iterate.
size_t g_c
grid list counter
grid_key_dx< dim > getGKey(const grid_dist_key_dx< dim > &k)
Convert a g_dist_key_dx into a global key.
grid_key_dx< dim > stop
stop point where iterate
Distributed grid iterator.
grid_key_dx< dim > get() const
Return the actual grid key iterator.
grid_dist_iterator_sub< dim, device_grid > & operator=(const grid_dist_iterator_sub< dim, device_grid > &tmp)
Copy operator=.
void selectValidGrid()
from g_c increment g_c until you find a valid grid
grid_key_dx< dim > start
start point where iterate
void set_d(size_t i, mem_id id)
Set the i index.
Definition: grid_key.hpp:407
bool isNext()
Check if there is the next element.
const openfpm::vector< GBoxes< device_grid::dims > > & gdb_ext
Extension of each grid: domain and ghost + domain.
size_t loc_grid_info_start(size_t i, size_t j)
Return the component j of the starting point (P1) of the domain part for the local grid i...
size_t getSub() const
Get the local grid.
void reinitialize(const grid_key_dx_iterator_sub< dim > &g_s_it)
Reinitialize the iterator.
grid_key_dx< dim > getKey() const
Get the key.