OpenFPM  5.2.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/Shape/Box.hpp"
13 #include "util/mathutil.hpp"
14 #include "Space/Shape/HyperCube.hpp"
15 #include <unordered_map>
16 #include "util/common.hpp"
17 #include "Vector/map_vector.hpp"
18 
19 template <typename Memory, template <typename> class layout_base,typename local_index>
21 {
23  local_index slot;
24 
27 
30 
34 
35 public:
36 
37  typedef local_index local_index_type;
38 
41  {}
42 
43  inline __device__ int getNelements(int id) const
44  {
45  return (int)cl_n.template get<0>(id);
46  }
47 
56  inline __device__ unsigned int get(unsigned int cell, unsigned int ele)
57  {
58  return cl_base.template get<0>(cell * slot + ele);
59  }
60 
61 
70  inline unsigned int get(unsigned int cell, unsigned int ele) const
71  {
72  return cl_base.template get<0>(cell * slot + ele);
73  }
74 };
75 
86 template <typename Memory = HeapMemory, typename local_index = size_t>
87 class Mem_fast
88 {
90  local_index slot;
91 
94 
96  typedef typename openfpm::vector<aggregate<local_index>,Memory> base;
97 
100 
104 
109  inline void realloc()
110  {
111  // we do not have enough slots reallocate the basic structure with more
112  // slots
113  base cl_base_(2*slot * cl_n.size());
114 
115  // copy cl_base
116  for (size_t i = 0 ; i < cl_n.size() ; i++)
117  {
118  for (local_index j = 0 ; j < cl_n.template get<0>(i) ; j++)
119  {cl_base_.template get<0>(2*i*slot + j) = cl_base.template get<0>(slot * i + j);}
120  }
121 
122  // Double the number of slots
123  slot *= 2;
124 
125  // swap the memory
126  cl_base.swap(cl_base_);
127  }
128 
129 
130 public:
131 
133 
134  typedef local_index local_index_type;
135 
141  inline size_t size() const
142  {
143  return cl_n.size();
144  }
145 
149  inline void destroy()
150  {
153  cl_base.swap(base());
154  }
155 
162  inline void init_to_zero(local_index slot, local_index tot_n_cell)
163  {
164  this->slot = slot;
165 
166  // create the array that store the number of particle on each cell and se it to 0
167 
168  cl_n.resize(tot_n_cell);
169  cl_n.template fill<0>(0);
170 
171  ghostMarkers.resize(tot_n_cell);
172  ghostMarkers.fill(0);
173 
174  // create the array that store the cell id
175 
176  cl_base.resize(tot_n_cell * slot);
177  }
178 
184  inline void operator=(const Mem_fast<Memory,local_index> & mem)
185  {
186  slot = mem.slot;
187 
189  cl_n = mem.cl_n;
190  cl_base = mem.cl_base;
191  }
192 
199  {
200  this->swap(mem);
201  }
202 
208  template<typename Memory2>
210  {
211  slot = mem.private_get_slot();
212 
214  cl_n = mem.private_get_cl_n();
216  }
217 
224  inline void addCellGhostMarkers()
225  {
226  ghostMarkers.resize(cl_n.size());
227 
228  for (int i = 0; i < cl_n.size(); ++i)
229  {
230  ghostMarkers.get(i) = cl_n.template get<0>(i);
231  }
232  }
233 
237  inline size_t getGhostMarker(local_index cell_id) const
238  {
239  return ghostMarkers.get(cell_id);
240  }
241 
248  inline void addCell(local_index cell_id, local_index ele)
249  {
250  // Get the number of element the cell is storing
251 
252  local_index nl = getNelements(cell_id);
253 
254  if (nl + 1 >= slot)
255  {
256  realloc();
257  }
258 
259  // we have enough slot to store another neighbor element
260 
261  cl_base.template get<0>(slot * cell_id + cl_n.template get<0>(cell_id)) = ele;
262  cl_n.template get<0>(cell_id)++;
263  }
264 
273  inline auto get(local_index cell, local_index ele) -> decltype(cl_base.template get<0>(cell * slot + ele)) &
274  {
275  return cl_base.template get<0>(cell * slot + ele);
276  }
277 
278 
287  inline auto get(local_index cell, local_index ele) const -> decltype(cl_base.template get<0>(cell * slot + ele)) &
288  {
289  return cl_base.template get<0>(cell * slot + ele);
290  }
291 
298  inline void remove(local_index cell_id, local_index ele)
299  {
300  cl_n.template get<0>(cell_id)--;
301 
302  // shift all remaining elements left
303  for (int i = ele+1; i < cl_n.template get<0>(cell_id); ++i)
304  cl_base.template get<0>(slot * cell_id + i-1) = cl_base.template get<0>(slot * cell_id + i);
305  }
306 
307 
315  inline size_t getNelements(const local_index cell_id) const
316  {
317  return cl_n.template get<0>(cell_id);
318  }
319 
320 
327  {
328  ghostMarkers.swap(mem.ghostMarkers);
329  cl_n.swap(mem.cl_n);
330  cl_base.swap(mem.cl_base);
331 
332  size_t cl_slot_tmp = mem.slot;
333  mem.slot = slot;
334  slot = cl_slot_tmp;
335  }
336 
342  inline void swap(Mem_fast<Memory,local_index> && mem)
343  {
344  slot = mem.slot;
345 
346  ghostMarkers.swap(mem.ghostMarkers);
347  cl_n.swap(mem.cl_n);
348  cl_base.swap(mem.cl_base);
349  }
350 
356  inline void clear()
357  {
358  for (size_t i = 0 ; i < cl_n.size() ; i++)
359  {
360  cl_n.template get<0>(i) = 0;
361  ghostMarkers.get(i) = 0;
362  }
363  }
364 
370  inline void clear(local_index cell_id)
371  {
372  cl_n.template get<0>(cell_id) = 0;
373  }
374 
382  inline const local_index & getStartId(local_index cell_id) const
383  {
384  return cl_base.template get<0>(cell_id*slot);
385  }
386 
394  inline const local_index & getGhostId(local_index cell_id) const
395  {
396  return cl_base.template get<0>(cell_id*slot+ghostMarkers.get(cell_id));
397  }
398 
406  inline const local_index & getStopId(local_index cell_id) const
407  {
408  return cl_base.template get<0>(cell_id*slot+cl_n.template get<0>(cell_id));
409  }
410 
418  inline const local_index & get_lin(const local_index * part_id) const
419  {
420  return *part_id;
421  }
422 
423 public:
424 
426  typedef local_index loc_index;
427 
433  inline Mem_fast(local_index slot)
434  :slot(slot)
435  {}
436 
442  inline void set_slot(local_index slot)
443  {
444  this->slot = slot;
445  }
446 
447 #ifdef CUDA_GPU
448 
455  {
457 
458  return mfk;
459  }
460 
461  void hostToDevice()
462  {
463  cl_n.template hostToDevice<0>();
464  cl_base.template hostToDevice<0>();
465  }
466 
467 #endif
468 
475  {
476  return cl_n;
477  }
478 
485  {
486  return ghostMarkers;
487  }
488 
494  const int & private_get_slot() const
495  {
496  return slot;
497  }
498 
504  const base & private_get_cl_base() const
505  {
506  return cl_base;
507  }
508 
514  static bool pack()
515  {
516  return true;
517  }
518 
524  static bool packRequest()
525  {
526  return true;
527  }
528 
536  template<int ... prp> inline void packRequest(size_t & req) const
537  {
539  cl_n.template packRequest<prp...>(req);
540  cl_base.template packRequest<prp...>(req);
541  ghostMarkers.template packRequest<prp...>(req);
542  }
543 
544 
551  template<int ... prp> inline void pack(ExtPreAlloc<HeapMemory> & mem, Pack_stat & sts) const
552  {
554  cl_n.template pack<prp...>(mem, sts);
555  cl_base.template pack<prp...>(mem, sts);
556  ghostMarkers.template pack<prp...>(mem, sts);
557  }
558 
564  template<int ... prp, typename MemType> inline void unpack(ExtPreAlloc<MemType> & mem, Unpack_stat & ps)
565  {
567  cl_n.template unpack<prp...>(mem, ps);
568  cl_base.template unpack<prp...>(mem, ps);
569  ghostMarkers.template unpack<prp...>(mem, ps);
570  }
571 
572 };
573 
574 
575 #endif /* CELLLISTSTANDARD_HPP_ */
unsigned int get(unsigned int cell, unsigned int ele) const
Get an element in the cell.
Definition: MemFast.hpp:70
__device__ unsigned int get(unsigned int cell, unsigned int ele)
Get an element in the cell.
Definition: MemFast.hpp:56
openfpm::vector_gpu_ker< aggregate< local_index >, layout_base > base
base that store the data
Definition: MemFast.hpp:29
openfpm::vector_gpu_ker< aggregate< local_index >, layout_base > cl_n
number of particle in each cell list
Definition: MemFast.hpp:26
base cl_base
Definition: MemFast.hpp:33
local_index slot
Number of slot for each cell.
Definition: MemFast.hpp:23
It is a class that work like a vector of vector.
Definition: MemFast.hpp:88
const local_index & get_lin(const local_index *part_id) const
Just return the value pointed by part_id.
Definition: MemFast.hpp:418
openfpm::vector< aggregate< local_index >, Memory > base
base that store the data
Definition: MemFast.hpp:96
openfpm::vector< aggregate< local_index >, Memory > cl_n
number of particle in each cell list
Definition: MemFast.hpp:93
const openfpm::vector< aggregate< local_index >, Memory > & private_get_cl_n() const
Return the private data-structure cl_n.
Definition: MemFast.hpp:474
void set_slot(local_index slot)
Set the number of slot for each cell.
Definition: MemFast.hpp:442
local_index loc_index
expose the type of the local index
Definition: MemFast.hpp:426
void operator=(Mem_fast< Memory, local_index > &&mem)
copy an object Mem_fast
Definition: MemFast.hpp:198
const openfpm::vector< size_t > & getGhostMarkers() const
Return the private data-structure ghostMarkers.
Definition: MemFast.hpp:484
void init_to_zero(local_index slot, local_index tot_n_cell)
Initialize the data to zero.
Definition: MemFast.hpp:162
void realloc()
realloc the data structures
Definition: MemFast.hpp:109
void addCellGhostMarkers()
Add ghost marker to the cell.
Definition: MemFast.hpp:224
void clear(local_index cell_id)
Delete cell elements in Cell p.
Definition: MemFast.hpp:370
static bool pack()
Definition: MemFast.hpp:514
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:287
void packRequest(size_t &req) const
It calculate the number of byte required to serialize the object.
Definition: MemFast.hpp:536
void remove(local_index cell_id, local_index ele)
Remove an element in the cell.
Definition: MemFast.hpp:298
void clear()
Delete all the elements in every cell.
Definition: MemFast.hpp:356
static bool packRequest()
Definition: MemFast.hpp:524
void addCell(local_index cell_id, local_index ele)
Add an element to the cell.
Definition: MemFast.hpp:248
void destroy()
Destroy the internal memory including the retained one.
Definition: MemFast.hpp:149
const local_index & getGhostId(local_index cell_id) const
Get the index of the first ghost element.
Definition: MemFast.hpp:394
void swap(Mem_fast< Memory, local_index > &mem)
swap to Mem_fast object
Definition: MemFast.hpp:326
void unpack(ExtPreAlloc< MemType > &mem, Unpack_stat &ps)
unpack a vector
Definition: MemFast.hpp:564
void copy_general(const Mem_fast< Memory2, local_index > &mem)
copy an object Mem_fast
Definition: MemFast.hpp:209
openfpm::vector< size_t > ghostMarkers
ghost marker for every cell (non-ghost particles < gm (ghost marker))
Definition: MemFast.hpp:99
const base & private_get_cl_base() const
Return the private data-structure cl_base.
Definition: MemFast.hpp:504
base cl_base
Definition: MemFast.hpp:103
const local_index & getStartId(local_index cell_id) const
Get the first element of a cell (as reference)
Definition: MemFast.hpp:382
local_index slot
Number of slot for each cell.
Definition: MemFast.hpp:90
size_t getGhostMarker(local_index cell_id) const
Get ghost marker of the cell.
Definition: MemFast.hpp:237
void swap(Mem_fast< Memory, local_index > &&mem)
swap to Mem_fast object
Definition: MemFast.hpp:342
void pack(ExtPreAlloc< HeapMemory > &mem, Pack_stat &sts) const
pack a vector selecting the properties to pack
Definition: MemFast.hpp:551
size_t size() const
return the number of elements
Definition: MemFast.hpp:141
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:273
const int & private_get_slot() const
Return the private slot.
Definition: MemFast.hpp:494
const local_index & getStopId(local_index cell_id) const
Get the last element of a cell (as reference)
Definition: MemFast.hpp:406
size_t getNelements(const local_index cell_id) const
Get the number of elements in the cell.
Definition: MemFast.hpp:315
void operator=(const Mem_fast< Memory, local_index > &mem)
copy an object Mem_fast
Definition: MemFast.hpp:184
Mem_fast(local_index slot)
Constructor.
Definition: MemFast.hpp:433
Packing status object.
Definition: Pack_stat.hpp:61
static void pack(ExtPreAlloc< Mem >, const T &obj)
Error, no implementation.
Definition: Packer.hpp:56
static size_t packRequest(const T &obj, size_t &req)
Error, no implementation.
Definition: Packer.hpp:66
Unpacking status object.
Definition: Pack_stat.hpp:16
static void unpack(ExtPreAlloc< Mem >, T &obj)
Error, no implementation.
Definition: Unpacker.hpp:40
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:204
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
Definition: aggregate.hpp:221
grid interface available when on gpu