OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
SparseGridGpu_iterator.hpp
1 /*
2  * SparseGridGpu_iterator.hpp
3  *
4  * Created on: Sep 4, 2019
5  * Author: i-bird
6  */
7 
8 #ifndef SPARSEGRIDGPU_ITERATOR_HPP_
9 #define SPARSEGRIDGPU_ITERATOR_HPP_
10 
16 template<typename SparseGridGpu_type>
18 {
21 
23  int data_id;
24 
26  const SparseGridGpu_type & sparseGrid;
27 
28 public:
29 
34  inline sparse_grid_gpu_index(const SparseGridGpu_type & sparseGrid, int cnk_pos_id, int data_id)
36  {}
37 
46  {
47  auto indexCnk = sparseGrid.private_get_index_array().template get<0>(cnk_pos_id);
48 
49  auto coord = sparseGrid.getCoord(indexCnk*sparseGrid.getBlockSize() + data_id);
50 
52 
53  for (size_t i = 0; i < SparseGridGpu_type::dims ; i++)
54  {
55  p.get(i) = coord.get(i);
56  }
57 
58  return p;
59  }
60 
68  int get_cnk_pos_id() const
69  {
70  return cnk_pos_id;
71  }
72 
78  int get_data_id() const
79  {
80  return data_id;
81  }
82 
90  {
91  this->cnk_pos_id = cnk_pos_id;
92  }
93 
99  void set_data_id(int data_id)
100  {
101  this->data_id = data_id;
102  }
103 
112  {
115 
116  for (int i = 0 ; i < SparseGridGpu_type::dims ; i++)
117  {
118  ret.set_d(i,key.get(i) + p.get(i));
119  }
120 
121  return ret;
122  }
123 
130  inline void set_d(size_t i,int n)
131  {
132  auto indexCnk = sparseGrid.private_get_index_array().template get<0>(cnk_pos_id);
133 
134  auto coord = sparseGrid.getCoord(indexCnk*sparseGrid.getBlockSize() + data_id);
135 
136  coord.set_d(i,n);
137 
138  auto key = sparseGrid.get_sparse(coord);
139 
140  cnk_pos_id = key.cnk_pos_id;
141  data_id = key.data_id;
142  }
143 
144  /* \brief Return the coordinate in direction i
145  *
146  * \return the coordinates in direction i
147  *
148  */
149  inline unsigned int get(unsigned int i) const
150  {
151  auto indexCnk = sparseGrid.private_get_index_array().template get<0>(cnk_pos_id);
152 
153  return sparseGrid.getCoord(indexCnk*sparseGrid.getBlockSize() + data_id).get(i);
154  }
155 };
156 
157 template<unsigned int dim, typename SparseGridType>
159 {
161  unsigned int chunk;
162 
164  unsigned int pnt;
165 
167  const SparseGridType * sparseGrid;
168 
170  typedef typename std::remove_reference<decltype(sparseGrid->private_get_index_array())>::type index_array_type;
171 
173  typedef typename std::remove_reference<decltype(sparseGrid->private_get_data_array())>::type data_array_type;
174 
177 
180 
181  //Get the chunk type
182  typedef typename boost::mpl::at<typename data_array_type::value_type::type,boost::mpl::int_<0>>::type chunk_type;
183 
184  //Get the chunk type
185  typedef boost::mpl::int_<boost::mpl::size<typename data_array_type::value_type::type>::type::value-1> pMask;
186 
188  void SelectValid()
189  {
190  while (pnt < chunk_type::size && data->template get<pMask::value>(chunk)[pnt] == 0)
191  {
192  pnt++;
193  }
194 
195  while (pnt == chunk_type::size && chunk < ids->size())
196  {
197  chunk++;
198  pnt = 0;
199  while (pnt < chunk_type::size && data->template get<pMask::value>(chunk)[pnt] == 0)
200  {
201  pnt++;
202  }
203  }
204  }
205 
206 public:
207 
208 
209  inline SparseGridGpu_iterator()
210  :chunk(0),
211  pnt(0),
212  sparseGrid(NULL),
213  ids(NULL),
214  data(NULL)
215  {
216  }
217 
218 
225  inline SparseGridGpu_iterator(const SparseGridType & sparseGrid)
226  :chunk(0),
227  pnt(0),
229  ids(&sparseGrid.private_get_index_array()),
230  data(&sparseGrid.private_get_data_array())
231  {
232  SelectValid();
233  }
234 
242  bool isNext() const
243  {
244  return chunk < ids->size();
245  }
246 
255  {
256  ++pnt;
257 
258  if (pnt >= chunk_type::size)
259  {
260  ++chunk;
261  pnt = 0;
262  }
263 
264  SelectValid();
265 
266  return *this;
267  }
268 
275  {
277 
278  return spgi;
279  }
280 
287  {
288  this->operator=(it);
289  }
290 
292  {
293  chunk = it.chunk;
294  sparseGrid = it.sparseGrid;
295  ids = it.ids;
296  data = it.data;
297  pnt = it.pnt;
298 
299  return *this;
300  }
301 };
302 
303 
304 #endif /* SPARSEGRIDGPU_ITERATOR_HPP_ */
int get_cnk_pos_id() const
Get chunk position id.
std::remove_reference< decltype(sparseGrid->private_get_index_array())>::type index_array_type
array type for the indexes
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
unsigned int chunk
actual chunk
int get_data_id() const
Get chunk local index (the returned index < getblockSize())
void set_cnk_pos_id(int cnk_pos_id)
Set chunk position id.
const index_array_type * ids
vector of the chunk indexes
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:27
const data_array_type * data
vector containing each chunks datas
bool isNext() const
Check if there is the next element.
sparse_grid_gpu_index< SparseGridType > get() const
return the actual point
Point< SparseGridGpu_type::dims, size_t > toPoint() const
Convert to a point this index.
grid_key_dx< SparseGridGpu_type::dims > operator+(const Point< SparseGridGpu_type::dims, size_t > &p)
return toPoint() + p
Element index contain a data chunk index and a point index.
int cnk_pos_id
chunk position id
void set_d(size_t i, int n)
set the position of this key in direction i to n
__device__ __host__ const T & get(unsigned int i) const
Get coordinate.
Definition: Point.hpp:172
const SparseGridType * sparseGrid
original SparseGrid
SparseGridGpu_iterator(const SparseGridType &sparseGrid)
Constructor.
std::remove_reference< decltype(sparseGrid->private_get_data_array())>::type data_array_type
array type for the data
void reinitialize(const SparseGridGpu_iterator< dim, SparseGridType > &it)
Reinitialize the iterator.
void SelectValid()
Select the first valid point chunk.
const SparseGridGpu_type & sparseGrid
SparseGridGpu used to add functionalities.
unsigned int pnt
actual point inside the chunk
void set_data_id(int data_id)
Set chunk local index (the returned index < getblockSize())
__device__ __host__ void set_d(index_type i, index_type id)
Set the i index.
Definition: grid_key.hpp:516
sparse_grid_gpu_index(const SparseGridGpu_type &sparseGrid, int cnk_pos_id, int data_id)
Constructor from SparseGridGpu.
SparseGridGpu_iterator< dim, SparseGridType > & operator++()
Get the next element.