OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
MemFast.hpp
1 /*
2  * MemFast.hpp
3  *
4  * Created on: Mar 22, 2015
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef MEMFAST_HPP_
9 #define MEMFAST_HPP_
10 
11 #include "config.h"
12 #include "Space/SpaceBox.hpp"
13 #include "util/mathutil.hpp"
14 #include "Space/Shape/HyperCube.hpp"
15 #include "NN/CellList/CellListIterator.hpp"
16 #include <unordered_map>
17 #include "util/common.hpp"
18 #include "Vector/map_vector.hpp"
19 
20 template <typename Memory, template <typename> class layout_base,typename local_index>
22 {
24  local_index slot;
25 
28 
31 
35 
36 public:
37 
38  typedef local_index local_index_type;
39 
42  {}
43 
44  inline __device__ int getNelements(int id) const
45  {
46  return (int)cl_n.template get<0>(id);
47  }
48 
57  inline __device__ unsigned int get(unsigned int cell, unsigned int ele)
58  {
59  return cl_base.template get<0>(cell * slot + ele);
60  }
61 
62 
71  inline unsigned int get(unsigned int cell, unsigned int ele) const
72  {
73  return cl_base.template get<0>(cell * slot + ele);
74  }
75 };
76 
87 template <typename Memory = HeapMemory, typename local_index = size_t>
88 class Mem_fast
89 {
91  local_index slot;
92 
95 
97  typedef typename openfpm::vector<aggregate<local_index>,Memory> base;
98 
102 
107  inline void realloc()
108  {
109  // we do not have enough slots reallocate the basic structure with more
110  // slots
111  base cl_base_(2*slot * cl_n.size());
112 
113  // copy cl_base
114  for (size_t i = 0 ; i < cl_n.size() ; i++)
115  {
116  for (local_index j = 0 ; j < cl_n.template get<0>(i) ; j++)
117  {cl_base_.template get<0>(2*i*slot + j) = cl_base.template get<0>(slot * i + j);}
118  }
119 
120  // Double the number of slots
121  slot *= 2;
122 
123  // swap the memory
124  cl_base.swap(cl_base_);
125  }
126 
127 
128 public:
129 
131 
132  typedef local_index local_index_type;
133 
139  inline size_t size() const
140  {
141  return cl_n.size();
142  }
143 
147  inline void destroy()
148  {
150  cl_base.swap(base());
151  }
152 
159  inline void init_to_zero(local_index slot, local_index tot_n_cell)
160  {
161  this->slot = slot;
162 
163  // create the array that store the number of particle on each cell and se it to 0
164 
165  cl_n.resize(tot_n_cell);
166  cl_n.template fill<0>(0);
167 
168  // create the array that store the cell id
169 
170  cl_base.resize(tot_n_cell * slot);
171  }
172 
178  inline void operator=(const Mem_fast<Memory,local_index> & mem)
179  {
180  slot = mem.slot;
181 
182  cl_n = mem.cl_n;
183  cl_base = mem.cl_base;
184  }
185 
192  {
193  this->swap(mem);
194  }
195 
201  template<typename Memory2>
203  {
204  slot = mem.private_get_slot();
205 
206  cl_n = mem.private_get_cl_n();
208  }
209 
216  inline void addCell(local_index cell_id, local_index ele)
217  {
218  // Get the number of element the cell is storing
219 
220  local_index nl = getNelements(cell_id);
221 
222  if (nl + 1 >= slot)
223  {
224  realloc();
225  }
226 
227  // we have enough slot to store another neighbor element
228 
229  cl_base.template get<0>(slot * cell_id + cl_n.template get<0>(cell_id)) = ele;
230  cl_n.template get<0>(cell_id)++;
231  }
232 
239  inline void add(local_index cell_id, local_index ele)
240  {
241  // add the element to the cell
242 
243  this->addCell(cell_id,ele);
244  }
245 
254  inline auto get(local_index cell, local_index ele) -> decltype(cl_base.template get<0>(cell * slot + ele)) &
255  {
256  return cl_base.template get<0>(cell * slot + ele);
257  }
258 
259 
268  inline auto get(local_index cell, local_index ele) const -> decltype(cl_base.template get<0>(cell * slot + ele)) &
269  {
270  return cl_base.template get<0>(cell * slot + ele);
271  }
272 
273 
280  inline void remove(local_index cell, local_index ele)
281  {
282  cl_n.template get<0>(cell)--;
283  }
284 
292  inline size_t getNelements(const local_index cell_id) const
293  {
294  return cl_n.template get<0>(cell_id);
295  }
296 
297 
304  {
305  cl_n.swap(mem.cl_n);
306  cl_base.swap(mem.cl_base);
307 
308  size_t cl_slot_tmp = mem.slot;
309  mem.slot = slot;
310  slot = cl_slot_tmp;
311  }
312 
318  inline void swap(Mem_fast<Memory,local_index> && mem)
319  {
320  slot = mem.slot;
321 
322  cl_n.swap(mem.cl_n);
323  cl_base.swap(mem.cl_base);
324  }
325 
331  inline void clear()
332  {
333  for (size_t i = 0 ; i < cl_n.size() ; i++)
334  {cl_n.template get<0>(i) = 0;}
335  }
336 
344  inline const local_index & getStartId(local_index cell_id) const
345  {
346  return cl_base.template get<0>(cell_id*slot);
347  }
348 
356  inline const local_index & getStopId(local_index cell_id) const
357  {
358  return cl_base.template get<0>(cell_id*slot+cl_n.template get<0>(cell_id));
359  }
360 
368  inline const local_index & get_lin(const local_index * part_id) const
369  {
370  return *part_id;
371  }
372 
373 public:
374 
376  typedef local_index loc_index;
377 
383  inline Mem_fast(local_index slot)
384  :slot(slot)
385  {}
386 
392  inline void set_slot(local_index slot)
393  {
394  this->slot = slot;
395  }
396 
397 #ifdef CUDA_GPU
398 
405  {
407 
408  return mfk;
409  }
410 
411  void hostToDevice()
412  {
413  cl_n.template hostToDevice<0>();
414  cl_base.template hostToDevice<0>();
415  }
416 
417 #endif
418 
425  {
426  return cl_n;
427  }
428 
434  const int & private_get_slot() const
435  {
436  return slot;
437  }
438 
444  const base & private_get_cl_base() const
445  {
446  return cl_base;
447  }
448 
449 };
450 
451 
452 #endif /* CELLLISTSTANDARD_HPP_ */
base cl_base
Definition: MemFast.hpp:101
const base & private_get_cl_base() const
Return the private data-structure cl_base.
Definition: MemFast.hpp:444
openfpm::vector_gpu_ker< aggregate< local_index >, layout_base > cl_n
number of particle in each cell list
Definition: MemFast.hpp:27
void add(local_index cell_id, local_index ele)
Add an element to the cell.
Definition: MemFast.hpp:239
__device__ unsigned int get(unsigned int cell, unsigned int ele)
Get an element in the cell.
Definition: MemFast.hpp:57
const local_index & getStopId(local_index cell_id) const
Get the last element of a cell (as reference)
Definition: MemFast.hpp:356
grid interface available when on gpu
auto get(local_index cell, local_index ele) -> decltype(cl_base.template get< 0 >(cell *slot+ele)) &
Get an element in the cell.
Definition: MemFast.hpp:254
local_index slot
Number of slot for each cell.
Definition: MemFast.hpp:24
openfpm::vector< aggregate< local_index >, Memory > cl_n
number of particle in each cell list
Definition: MemFast.hpp:94
base cl_base
Definition: MemFast.hpp:34
local_index slot
Number of slot for each cell.
Definition: MemFast.hpp:91
size_t size() const
return the number of elements
Definition: MemFast.hpp:139
It is a class that work like a vector of vector.
Definition: MemFast.hpp:88
const local_index & getStartId(local_index cell_id) const
Get the first element of a cell (as reference)
Definition: MemFast.hpp:344
size_t size()
Stub size.
Definition: map_vector.hpp:211
void swap(Mem_fast< Memory, local_index > &mem)
swap to Mem_fast object
Definition: MemFast.hpp:303
const local_index & get_lin(const local_index *part_id) const
Just return the value pointed by part_id.
Definition: MemFast.hpp:368
void operator=(const Mem_fast< Memory, local_index > &mem)
copy an object Mem_fast
Definition: MemFast.hpp:178
const int & private_get_slot() const
Return the private slot.
Definition: MemFast.hpp:434
void clear()
Delete all the elements in the Cell-list.
Definition: MemFast.hpp:331
void operator=(Mem_fast< Memory, local_index > &&mem)
copy an object Mem_fast
Definition: MemFast.hpp:191
void realloc()
realloc the data structures
Definition: MemFast.hpp:107
local_index loc_index
expose the type of the local index
Definition: MemFast.hpp:376
void swap(Mem_fast< Memory, local_index > &&mem)
swap to Mem_fast object
Definition: MemFast.hpp:318
void init_to_zero(local_index slot, local_index tot_n_cell)
Initialize the data to zero.
Definition: MemFast.hpp:159
unsigned int get(unsigned int cell, unsigned int ele) const
Get an element in the cell.
Definition: MemFast.hpp:71
void remove(local_index cell, local_index ele)
Remove an element in the cell.
Definition: MemFast.hpp:280
void copy_general(const Mem_fast< Memory2, local_index > &mem)
copy an object Mem_fast
Definition: MemFast.hpp:202
size_t getNelements(const local_index cell_id) const
Get the number of elements in the cell.
Definition: MemFast.hpp:292
Mem_fast(local_index slot)
Constructor.
Definition: MemFast.hpp:383
openfpm::vector_gpu_ker< aggregate< local_index >, layout_base > base
base that store the data
Definition: MemFast.hpp:30
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
Definition: aggregate.hpp:214
auto get(local_index cell, local_index ele) const -> decltype(cl_base.template get< 0 >(cell *slot+ele)) &
Get an element in the cell.
Definition: MemFast.hpp:268
openfpm::vector< aggregate< local_index >, Memory > base
base that store the data
Definition: MemFast.hpp:97
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:202
void set_slot(local_index slot)
Set the number of slot for each cell.
Definition: MemFast.hpp:392
void destroy()
Destroy the internal memory including the retained one.
Definition: MemFast.hpp:147
void addCell(local_index cell_id, local_index ele)
Add an element to the cell.
Definition: MemFast.hpp:216
const openfpm::vector< aggregate< local_index >, Memory > & private_get_cl_n() const
Return the private data-structure cl_n.
Definition: MemFast.hpp:424