OpenFPM  5.2.0
Project that contain the implementation of distributed structures
vector_dist_subset.hpp
1 //
2 // Created by Abhinav Singh on 24.02.20.
3 //
4 
5 #ifndef OPENFPM_PDATA_VECTOR_DIST_SUBSET_HPP
6 #define OPENFPM_PDATA_VECTOR_DIST_SUBSET_HPP
7 
8 #include "vector_dist.hpp"
9 
10 template<unsigned int dim,
11  typename St,
12  typename prop,
14  typename Memory = HeapMemory,
15  template<typename> class layout_base = memory_traits_lin>
16 class vector_dist_ws : public vector_dist<dim,St,typename AggregateAppend<int,prop>::type,Decomposition,Memory,layout_base>
17 {
18  public:
19 
20  typedef prop value_type;
21 
23 
24  typedef boost::mpl::int_<AggregateAppend<int,prop>::type::max_prop-1> flag_prop;
25 
26  void setSubset(size_t key, int sub_id)
27  {
28  this->template getProp<flag_prop::value>(key) = sub_id;
29  }
30 
31  int getSubset(size_t key)
32  {
33  return this->template getProp<flag_prop::value>(key);
34  }
35 
36  void ghost_get_subset()
37  {
38  this->template ghost_get<flag_prop::value>(NO_POSITION | SKIP_LABELLING);
39  }
40 
41  void getLastSubset(int sub_id)
42  {
43  this->template getProp<flag_prop::value>(this->size_local()-1) = sub_id;
44  }
45 
46  inline bool write_frame(std::string out, size_t iteration, int opt = VTK_WRITER)
47  {
48  auto &prop_names=this->getPropNames();
49  if(prop_names.size()==prop::max_prop){
50  prop_names.add({"SubsetNumber"});
51  }
52 
53  return vector_dist<dim,St,typename AggregateAppend<int,prop>::type,Decomposition,Memory,layout_base>::write_frame(out,iteration,opt);
54  }
55  inline bool write_frame(std::string out, size_t iteration,double time, int opt = VTK_WRITER)
56  {
57  auto &prop_names=this->getPropNames();
58  if(prop_names.size()==prop::max_prop){
59  prop_names.add({"SubsetNumber"});
60  }
61 
62  return vector_dist<dim,St,typename AggregateAppend<int,prop>::type,Decomposition,Memory,layout_base>::write_frame(out,iteration,time,opt);
63  }
64 
65  inline bool write(std::string out,int opt = VTK_WRITER)
66  {
67  auto &prop_names=this->getPropNames();
68  if(prop_names.size()==prop::max_prop){
69  prop_names.add({"SubsetNumber"});
70  }
71 
72  return vector_dist<dim,St,typename AggregateAppend<int,prop>::type,Decomposition,Memory,layout_base>::write(out,"",opt);
73  }
74 
75 };
76 
77 template<unsigned int dim,
78  typename St,
79  typename prop,
81  typename Memory = HeapMemory,
82  template<typename> class layout_base = memory_traits_lin>
84 {
86 
87  typedef boost::mpl::int_<AggregateAppend<int,prop>::type::max_prop-1> flag_prop;
88 
89  ivector_dist & vd;
90 
92 
93  size_t sub_id;
94 
95 #ifdef SE_CLASS1
96  int subsetUpdate_ctr=0;
97 #endif
98 
99  void check_gm()
100  {
101  #ifdef SE_CLASS1
102  for (size_t i = 0 ; i < pid.size() ; i++)
103  {
104  if (pid.template get<0>(i) >= vd.size_local())
105  {
106  std::cout << __FILE__ << ":" << __LINE__ << " Error you have ghost particles in your subset" << std::endl;
107  }
108  }
109  #endif
110  }
111 
112 public:
113 
116 
118 
120 
122  typedef typename ivector_dist::stype stype;
123 
125  static const unsigned int dims = ivector_dist::dims;
126 
128  typedef int yes_i_am_vector_subset;
129 
131  typedef std::integral_constant<bool,true> is_it_a_subset;
132 
134  int sub_id)
135  :vd(vd),sub_id(sub_id)
136  {
137 #ifdef SE_CLASS1
138  subsetUpdate_ctr=vd.getMapCtr();
139 #endif
140  // construct pid vector
141 
142  auto it = vd.getDomainIterator();
143 
144  while (it.isNext())
145  {
146  auto p = it.get();
147 
148  if (vd.template getProp<flag_prop::value>(p) == sub_id)
149  {
150  pid.add();
151  pid.template get<0>(pid.size()-1) = p;
152  }
153 
154  ++it;
155  }
156 
157  check_gm();
158  }
159 
160  void ghost_get_subset()
161  {
162  vd.template ghost_get<flag_prop::value>(NO_POSITION | SKIP_LABELLING);
163  }
164 
171  {
172  return pid;
173  }
174 
175 #ifdef SE_CLASS1
176  int getUpdateCtr() const{
177  return subsetUpdate_ctr;
178  }
179  int getMapCtr()
180  {
181  return vd.getMapCtr();
182  }
183 #endif
184 
188  inline void update()
189  {
190 #ifdef SE_CLASS1
191  subsetUpdate_ctr=vd.getMapCtr();
192 #endif
193 
194  ghost_get_subset();
195 
196  pid.clear();
197 
198  auto it = vd.getDomainIterator();
199 
200  while (it.isNext())
201  {
202  auto p = it.get();
203 
204  if (vd.template getProp<flag_prop::value>(p) == sub_id)
205  {
206  pid.add();
207  pid.template get<0>(pid.size()-1) = p;
208  }
209 
210  ++it;
211  }
212 
213  check_gm();
214  }
215 
222  {
223  return vd.getDecomposition();
224  }
225 
231  inline const Decomposition & getDecomposition() const
232  {
233  return vd.getDecomposition();
234  }
235 
241  size_t size_local() const
242  {
243  return vd.size_local();
244  }
245 
251  size_t size_local_subset() const
252  {
253  return pid.size();
254  }
255 
256 #ifndef ONLY_READWRITE_GETTER
257 
267  inline auto getPos(size_t vec_key) -> decltype(vd.getPos(vec_key))
268  {
269  return vd.getPos(vec_key);
270  }
271 
281  inline auto getPos(size_t vec_key) const -> decltype(vd.getPos(vec_key))
282  {
283  return vd.getPos(vec_key);
284  }
285 
295  inline auto getPosSubset(size_t vec_key) -> decltype(vd.getPos(vec_key))
296  {
297  return vd.getPos(pid.template get<0>(vec_key));
298  }
299 
309  inline auto getPosSubset(size_t vec_key) const -> decltype(vd.getPos(vec_key))
310  {
311  return vd.getPos(pid.template get<0>(vec_key));
312  }
313 
319  template<unsigned int ... prp>
320  inline void hostToDeviceProp()
321  {
322  vd.template hostToDeviceProp<prp ...>();
323  }
324 
330  inline void hostToDevicePos()
331  {
332  vd.template hostToDevicePos<0>();
333  }
334 
345  template<unsigned int id> inline auto getProp(size_t vec_key) -> decltype(vd.template getProp<id>(vec_key))
346  {
347  return vd.template getProp<id>(vec_key);
348  }
349 
360  template<unsigned int id> inline auto getProp(size_t vec_key) const -> decltype(vd.template getProp<id>(vec_key))
361  {
362  return vd.template getProp<id>(vec_key);
363  }
364 
375  template<unsigned int id> inline auto getPropSubset(size_t vec_key) -> decltype(vd.template getProp<id>(vec_key))
376  {
377  return vd.template getProp<id>(pid.template get<0>(vec_key));
378  }
379 
390  template<unsigned int id> inline auto getPropSubset(size_t vec_key) const -> decltype(vd.template getProp<id>(vec_key))
391  {
392  return vd.template getProp<id>(pid.template get<0>(vec_key));
393  }
394 
395 #endif
396 
403  {
404 #ifdef SE_CLASS3
405  se3.getIterator();
406 #endif
407 
408  return vector_dist_iterator_subset(0,pid.size(),pid);
409  }
410 
421  template<typename CellL = CellList<dim, St, Mem_fast<>, shift<dim, St>, typename std::remove_reference<decltype(vd.getPosVector())>::type > >
422  CellL getCellList(St r_cut, bool no_se3 = false)
423  {
424 #ifdef SE_CLASS3
425  if (no_se3 == false)
426  {se3.getNN();}
427 #endif
428 
429  // Get ghost and anlarge by 1%
431  g.magnify(1.013);
432 
433  return getCellList<CellL>(r_cut, g,no_se3);
434  }
435 
447  template<
448  unsigned int opt=VL_NON_SYMMETRIC,
449  typename VerletList_type = VerletList<dim,St,opt,Mem_fast<>,shift<dim,St>,typename std::remove_reference<decltype(vd.getPosVector())>::type>
450  >
451  VerletList_type getVerlet(St r_cut, bool no_se3 = false)
452  {
453 #ifdef SE_CLASS3
454  if (no_se3 == false)
455  {se3.getNN();}
456 #endif
457 
458  VerletList_type verletList;
459 
460  auto cellList = this->template getCellList<typename std::remove_reference<decltype(verletList.getInternalCellList())>::type>(r_cut, no_se3);
461 
462  if (verletList.getOpt() & VL_NON_SYMMETRIC)
463  {
464  auto it = getDomainIterator();
465  verletList.Initialize(cellList, r_cut, it, vd.getPosVector(), vd.size_local());
466  }
467 
468  else {
469  std::cerr << __FILE__ << ":" << __LINE__ << " vector_dist_subset::getVerletList supports VL_NON_SYMMETRIC option only! " << std::endl;
470  }
471 
472  return verletList;
473  }
474 
475 
488  template<
489  unsigned int opt,
490  typename Mem_type,
491  typename vPos_type>
492  void updateVerlet(VerletList<dim,St,opt,Mem_type,shift<dim,St>,vPos_type>& verletList, St r_cut, bool no_se3 = false)
493  {
494 #ifdef SE_CLASS3
495  if (no_se3 == false)
496  {se3.getNN();}
497 #endif
498 
499  VerletList<dim,St,opt,Mem_type,shift<dim,St>,vPos_type> ver_tmp = getVerlet<opt, VerletList<dim,St,opt,Mem_type,shift<dim,St>,vPos_type>>(r_cut);
500  verletList.swap(ver_tmp);
501  }
502 
508  bool isSubset() const
509  {
510  return true;
511  }
512 
529  template<typename CellL = CellList<dim, St, Mem_fast<>, shift<dim, St> > >
530  CellL getCellList(St r_cut, const Ghost<dim, St> & enlarge, bool no_se3 = false)
531  {
532 #ifdef SE_CLASS3
533  if (no_se3 == false)
534  {se3.getNN();}
535 #endif
536 
537  CellL cell_list;
538 
539  // Division array
540  size_t div[dim];
541 
542  // get the processor bounding box
544 
545  // Processor bounding box
546  cl_param_calculate(pbox, div, r_cut, enlarge);
547 
548  cell_list.Initialize(pbox, div);
549  cell_list.setGhostMarker(pid.size());
550  cell_list.set_ndec(vd.getDecomposition().get_ndec());
551 
552  cell_list.clear();
553 
554  auto it = getDomainIterator();
555 
556  while (it.isNext())
557  {
558  size_t key = it.get();
559 
560  Point<dim,St> pos = getPos(key);
561 
562  cell_list.add(pos,key);
563 
564  ++it;
565  }
566 
567  // Add also the ghost
568 
569  auto git = vd.getGhostIterator();
570 
571  while (git.isNext())
572  {
573  size_t key = git.get();
574 
575  Point<dim,St> pos = vd.getPos(key);
576 
577  if (vd.template getProp<flag_prop::value>(key) == sub_id)
578  {
579  cell_list.add(pos,key);
580  }
581 
582  ++git;
583  }
584 
585  return cell_list;
586  }
587 
597  {
598  vd = v.vd;
599  pid = v.pid;
600 
601  return *this;
602  }
603 };
604 
605 
606 template<unsigned int dim, typename St, typename prop, typename Decomposition = CartDecomposition<dim,St,CudaMemory,memory_traits_lin>> using vector_dist_ws_gpu = vector_dist_ws<dim,St,prop,Decomposition,CudaMemory,memory_traits_lin>;
607 template<unsigned int dim, typename St, typename prop, typename Decomposition = CartDecomposition<dim,St,CudaMemory,memory_traits_lin>> using vector_dist_subset_gpu = vector_dist_subset<dim,St,prop,Decomposition,CudaMemory,memory_traits_lin>;
608 
609 #endif //OPENFPM_PDATA_VECTOR_DIST_SUBSET_HPP
void magnify(T mg)
Magnify the box.
Definition: Box.hpp:888
Box< dim, T > & getProcessorBounds()
Return the bounding box containing union of all the sub-domains for the local processor.
const Ghost< dim, T > & getGhost() const
Return the ghost.
size_t get_ndec()
Get the decomposition counter.
Class for FAST cell list implementation.
Definition: CellList.hpp:558
This class define the domain decomposition interface.
This class allocate, and destroy CPU memory.
Definition: HeapMemory.hpp:40
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:28
Class for Verlet list implementation.
Definition: VerletList.hpp:269
void swap(VerletList< dim, T, opt, Mem_type, transform, vPos_type, CellListImpl > &vl)
Swap the memory.
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:204
size_t size()
Stub size.
Definition: map_vector.hpp:212
Iterator that Iterate across particle indexes.
vect_dist_key_dx get()
Get the actual key.
auto getPropSubset(size_t vec_key) const -> decltype(vd.template getProp< id >(vec_key))
Get the property of an element.
auto getPos(size_t vec_key) const -> decltype(vd.getPos(vec_key))
Get the position of an element.
vector_dist_subset< dim, St, prop, Decomposition, Memory, layout_base > & operator=(const vector_dist_subset< dim, St, prop, Decomposition, Memory, layout_base > &v)
Operator= for distributed vector.
const Decomposition & getDecomposition() const
Get the decomposition.
auto getPosSubset(size_t vec_key) -> decltype(vd.getPos(vec_key))
Get the position of an element.
auto getProp(size_t vec_key) -> decltype(vd.template getProp< id >(vec_key))
Get the property of an element.
std::integral_constant< bool, true > is_it_a_subset
Subset detection.
static const unsigned int dims
dimensions of space
auto getPropSubset(size_t vec_key) -> decltype(vd.template getProp< id >(vec_key))
Get the property of an element.
CellL getCellList(St r_cut, bool no_se3=false)
Construct a cell list starting from the stored particles.
VerletList_type getVerlet(St r_cut, bool no_se3=false)
Construct a Verlet List from the stored particles.
void hostToDeviceProp()
Move the memory from the device to host memory.
size_t size_local() const
return the local size of vector_dist_ws
auto getPosSubset(size_t vec_key) const -> decltype(vd.getPos(vec_key))
Get the position of an element.
ivector_dist::stype stype
space type
openfpm::vector< aggregate< int > > & getIds()
Return the ids.
bool isSubset() const
Indicate that this class is not a subset.
vector_dist_iterator_subset getDomainIterator() const
Get an iterator that traverse the particles in the domain.
Decomposition & getDecomposition()
Get the decomposition.
void updateVerlet(VerletList< dim, St, opt, Mem_type, shift< dim, St >, vPos_type > &verletList, St r_cut, bool no_se3=false)
Update an existing Verlet List.
ivector_dist::value_type value_type
property object
auto getProp(size_t vec_key) const -> decltype(vd.template getProp< id >(vec_key))
Get the property of an element.
size_t size_local_subset() const
return the local size of the vector
void update()
Update the subset indexes.
auto getPos(size_t vec_key) -> decltype(vd.getPos(vec_key))
Get the position of an element.
CellL getCellList(St r_cut, const Ghost< dim, St > &enlarge, bool no_se3=false)
Construct a cell list starting from the stored particles.
void hostToDevicePos()
Move the memory from the device to host memory.
Distributed vector.
Decomposition & getDecomposition()
Get the decomposition.
vector_dist_iterator getGhostIterator() const
Get the iterator across the position of the ghost particles.
auto getPos(vect_dist_key_dx vec_key) -> decltype(vPos.template get< 0 >(vec_key.getKey()))
Get the position of an element.
const vector_dist_pos & getPosVector() const
return the position vector of all the particles
vector_dist_iterator getDomainIterator() const
Get an iterator that traverse the particles in the domain.
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
Definition: aggregate.hpp:221
Transform the boost::fusion::vector into memory specification (memory_traits)