OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
main.cpp
1 #include "Vector/vector_dist.hpp"
2 #include "data_type/aggregate.hpp"
3 #include "Decomposition/CartDecomposition.hpp"
4 #include "VCluster/VCluster.hpp"
5 
20 #define POS_ 0
21 
23 
25 {
27  double x[2];
28 
30  double prop0;
31 
33  long int prop1;
34 };
35 
37 
39 
40 void serial_to_parallel(std::vector<my_particle> & parts,
41  vector_dist<2,double,aggregate<double,long int>> & pparts)
42 {
43  auto & v_cl = create_vcluster();
44  pparts.clear();
45 
46  if (v_cl.getProcessUnitID() == 0)
47  {
48  for (size_t i = 0 ; i < parts.size() ; i++)
49  {
50  pparts.add();
51 
52  pparts.getLastPos()[0] = parts[i].x[0];
53  pparts.getLastPos()[1] = parts[i].x[1];
54 
55  pparts.getLastProp<0>() = parts[i].prop0;
56  pparts.getLastProp<1>() = parts[i].prop1;
57  }
58  }
59 
60  pparts.map<Error>();
61  pparts.addComputationCosts();
62  pparts.getDecomposition().decompose();
63  pparts.map<Error>();
64 }
65 
67 
69 
70 void parallel_to_serial(std::vector<my_particle> & parts,
71  vector_dist<2,double,aggregate<double,long int>> & pparts)
72 {
73  auto & v_cl = create_vcluster();
74 
75  // here we collect on the processor 0
76 
77  auto & pos = pparts.getPosVector();
78  auto & prp = pparts.getPropVector();
79 
80  std::remove_reference<decltype(pos)>::type pos_collect;
81  std::remove_reference<decltype(prp)>::type prp_collect;
82 
83  // we collecto everything on processor 0 on pos_collect and prp_collect
84  v_cl.SGather(pos,pos_collect,0);
85  v_cl.SGather(prp,prp_collect,0);
86 
87  if (v_cl.getProcessUnitID() == 0)
88  {
89  parts.clear();
90 
91  for (size_t i = 0 ; i < pos_collect.size() ; i++)
92  {
93  struct my_particle p;
94 
95  p.x[0] = pos_collect.get<POS_>(i)[0];
96  p.x[1] = pos_collect.get<POS_>(i)[1];
97 
98  p.prop0 = prp_collect.get<0>(i);
99  p.prop1 = prp_collect.get<1>(i);
100 
101  parts.push_back(p);
102  }
103  }
104 }
105 
107 
108 int main(int argc, char* argv[])
109 {
124 
126  openfpm_init(&argc,&argv);
127  Vcluster<> & v_cl = create_vcluster();
128 
130 
148 
150  std::vector<my_particle> parts;
151 
152  // id of the processor calling this function
153  long int proc_id = v_cl.getProcessUnitID();
154 
155  if (proc_id == 0)
156  {
157  // We create 100 particles randomly between (0,0) and (2.0,3.0) serially
158 
159  for (size_t i = 0 ; i < 100 ; i++)
160  {
161  my_particle p;
162 
163  p.x[0] = 2.0*(double)rand()/RAND_MAX;
164  p.x[1] = 3.0*(double)rand()/RAND_MAX;
165  p.prop0 = 0;
166  p.prop1 = 0;
167  parts.push_back(p);
168  }
169  }
170 
172 
222 
224  Box<2,double> domain({0.0,0.0},{2.0,3.0});
225 
226  Ghost<2,double> gs(0.02);
227  size_t bc[2] = {PERIODIC,PERIODIC};
228 
229  vector_dist<2,double,aggregate<double,long int>> pparts(0,domain,bc,gs);
230 
232 
233  // We do 100 iteration
234  for (size_t i = 0 ; i < 100 ; i++)
235  {
237 
238  // here we convert from serial to parallel moving the data from
239  // the serial part to pparts
240 
241  serial_to_parallel(parts,pparts);
242 
243  // Now we can use pparts to
244 
245  auto it = pparts.getDomainIterator();
246 
247  while (it.isNext())
248  {
249  auto key = it.get();
250 
251  pparts.getPos(key)[0] += 0.01;
252  pparts.getPos(key)[1] += 0.01;
253 
254  pparts.getProp<0>(key) = i*i;
255  pparts.getProp<1>(key) = i;
256 
257  ++it;
258  }
259 
260  pparts.write_frame("output",i);
261 
263 
265 
266  parallel_to_serial(parts,pparts);
267 
268  // id of the processor calling this function
269  long int proc_id = v_cl.getProcessUnitID();
270 
271  if (proc_id == 0)
272  {
273  // Here we are serial with parts updates
274 
275  for (size_t i = 0 ; i < parts.size() ; i++)
276  {
277  parts[i].x[0] += 0.01;
278  parts[i].x[1] += 0.01;
279 
280  parts[i].prop0 = i*i;
281  parts[i].prop1 = i;
282  }
283  }
284 
286  }
287 
299 
301  openfpm_finalize();
302 
304 }
size_t getProcessUnitID()
Get the process unit id.
long int prop1
property 1
Definition: main.cpp:33
double prop0
property 0
Definition: main.cpp:30
double x[2]
position
Definition: main.cpp:27
Definition: Ghost.hpp:39
Implementation of VCluster class.
Definition: VCluster.hpp:58
Out-of-bound policy kill the program.
This class represent an N-dimensional box.
Definition: Box.hpp:60
Distributed vector.
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
Definition: aggregate.hpp:214