OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
grid_dist_performance.hpp
1 /*
2  * grid_dist_performance.hpp
3  *
4  * Created on: Jun 27, 2017
5  * Author: i-bird
6  */
7 
8 #ifndef SRC_GRID_GRID_DIST_PERFORMANCE_HPP_
9 #define SRC_GRID_GRID_DIST_PERFORMANCE_HPP_
10 
11 #define GRID_ITERATOR_TESTS 30
12 #define GRID_INTERPOLATION_TESTS 30
13 
14 // Vectors to store the data for 3D
15 openfpm::vector<size_t> nk_grid_st;
16 openfpm::vector<size_t> nk_grid_int;
17 
18 // Property tree
20 {
21  boost::property_tree::ptree graphs;
22 };
23 
24 report_grid_iterator_test report_grid_iterator;
25 
26 BOOST_AUTO_TEST_SUITE( grid_iterator_performance_test )
27 
28 constexpr int x = 0;
29 constexpr int y = 1;
30 constexpr int z = 2;
31 
36 void grid_interpolation_benchmark(openfpm::vector<size_t> & nk_grid,
37  boost::property_tree::ptree & interpolation_graph)
38 {
39  for (size_t k = 0 ; k < nk_grid.size() ; k++)
40  {
41  size_t np = nk_grid.get(k);
42  size_t sz[] = {np,np,np};
43 
44  Ghost<3,long int> gg(3);
45 
46  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
47 
48  size_t tot_part = np*np*np;
49 
50  std::string base_p2m = "performance.interpolation.p2m(" + std::to_string(k) + ")";
51  std::string base_m2p = "performance.interpolation.m2p(" + std::to_string(k) + ")";
52 
53  interpolation_graph.put(base_p2m + ".grid.dim",3);
54  interpolation_graph.put(base_m2p + ".grid.dim",3);
55 
56  interpolation_graph.put(base_p2m + ".grid.x",np);
57  interpolation_graph.put(base_p2m + ".grid.y",np);
58  interpolation_graph.put(base_p2m + ".grid.z",np);
59 
60  interpolation_graph.put(base_m2p + ".grid.x",np);
61  interpolation_graph.put(base_m2p + ".grid.y",np);
62  interpolation_graph.put(base_m2p + ".grid.z",np);
63 
64  interpolation_graph.put(base_p2m + ".particles",tot_part);
65  interpolation_graph.put(base_m2p + ".particles",tot_part);
66 
68  vector_dist<3,float,aggregate<float>> vd(gd.getDecomposition(),tot_part);
69 
70  // Fill vd with particles
71 
72  auto vd_it = vd.getDomainIterator();
73 
74  while (vd_it.isNext())
75  {
76  auto p = vd_it.get();
77 
78 
79  vd.getPos(p)[0] = ((float)std::rand()) / RAND_MAX;
80  vd.getPos(p)[1] = ((float)std::rand()) / RAND_MAX;
81  vd.getPos(p)[2] = ((float)std::rand()) / RAND_MAX;
82 
83  vd.template getProp<0>(p) = 1.0;
84 
85  ++vd_it;
86  }
87 
88  vd.map();
89 
90  double mean;
91  double dev;
92  openfpm::vector<double> measures;
93  for (size_t j = 0 ; j < GRID_INTERPOLATION_TESTS ; j++)
94  {
95 
96  interpolate<decltype(vd),decltype(gd),mp4_kernel<float>> inte(vd,gd);
97 
98  timer tstl;
99  tstl.start();
100 
101  inte.p2m<0,0>(vd,gd);
102 
103  tstl.stop();
104  measures.add(tstl.getwct());
105  }
106  standard_deviation(measures,mean,dev);
107 
108  interpolation_graph.put(base_p2m + ".data.mean",mean);
109  interpolation_graph.put(base_p2m + ".data.dev",dev);
110 
111  std::cout << "Time particles to mesh " << mean << std::endl;
112 
113  measures.clear();
114  for (size_t j = 0 ; j < GRID_INTERPOLATION_TESTS ; j++)
115  {
116 
117  interpolate<decltype(vd),decltype(gd),mp4_kernel<float>> inte(vd,gd);
118 
119  timer tstl;
120  tstl.start();
121 
122  inte.m2p<0,0>(gd,vd);
123 
124  tstl.stop();
125  measures.add(tstl.getwct());
126  }
127  standard_deviation(measures,mean,dev);
128 
129  interpolation_graph.put(base_m2p + ".data.mean",mean);
130  interpolation_graph.put(base_m2p + ".data.dev",dev);
131 
132  std::cout << "Time mesh to particles " << mean << std::endl;
133  }
134 }
135 
140 double grid_iterator_benchmark_stencil(grid_dist_id<3, float, aggregate<long int>, CartDecomposition<3,float>> & g_dist, double & total)
141 {
142  grid_key_dx<3> star_stencil_3D[7] = {{0,0,0},
143  {-1,0,0},
144  {1,0,0},
145  {0,-1,0},
146  {0,1,0},
147  {0,0,-1},
148  {0,0,1}};
149 
150  timer tstl;
151  tstl.start();
152 
153  auto st_it = g_dist.getDomainIteratorStencil(star_stencil_3D);
154 
155  while (st_it.isNext())
156  {
157 
158  total+= 6*g_dist.template get<0>(st_it.getStencil<0>()) -
159  g_dist.template get<0>(st_it.getStencil<1>()) -
160  g_dist.template get<0>(st_it.getStencil<2>()) -
161  g_dist.template get<0>(st_it.getStencil<3>()) -
162  g_dist.template get<0>(st_it.getStencil<4>()) -
163  g_dist.template get<0>(st_it.getStencil<5>()) -
164  g_dist.template get<0>(st_it.getStencil<6>());
165 
166  ++st_it;
167  }
168 
169  tstl.stop();
170  return tstl.getwct();
171 }
172 
173 double grid_iterator_benchmark_norm(grid_dist_id<3, float, aggregate<long int>, CartDecomposition<3,float>> & g_dist, double & total)
174 {
175  timer tnorm;
176  tnorm.start();
177 
178  auto norm_it = g_dist.getDomainIterator();
179 
180  while (norm_it.isNext())
181  {
182  // center point
183  auto key = norm_it.get();
184 
185  total+= 6*g_dist.template get<0>(key) -
186  g_dist.template get<0>(key.move(x,-1)) -
187  g_dist.template get<0>(key.move(x,1)) -
188  g_dist.template get<0>(key.move(y,-1)) -
189  g_dist.template get<0>(key.move(y,1)) -
190  g_dist.template get<0>(key.move(z,-1)) -
191  g_dist.template get<0>(key.move(z,1));
192 
193  ++norm_it;
194  }
195 
196  tnorm.stop();
197  return tnorm.getwct();
198 }
199 
203 template<unsigned int dim> void grid_iterator_benchmark(openfpm::vector<size_t> & nk_grid,
204  boost::property_tree::ptree & iterator_graph)
205 {
206  std::string str("Testing " + std::to_string(dim) + "D grid iterator stencil and normal");
207  print_test_v(str,0);
208 
209  {
210  //For different grid sizes
211  for (size_t i = 0; i < nk_grid.size(); i++ )
212  {
213  size_t sz[dim];
214 
215  //Number of particles
216  size_t k = nk_grid.get(i);
217 
218  std::string base = "performance.grid.iterators(" + std::to_string(i) + ")";
219  iterator_graph.put(base + ".grid.dim",3);
220 
221  iterator_graph.put(base + ".grid.x",k);
222  iterator_graph.put(base + ".grid.y",k);
223  iterator_graph.put(base + ".grid.z",k);
224 
225  BOOST_TEST_CHECKPOINT( "Testing " << dim << "D grid iterator performance k=" << k );
226 
227  Box<dim,float> box;
228 
229  for (size_t i = 0; i < dim; i++)
230  {
231  box.setLow(i,0.0);
232  box.setHigh(i,1.0);
233  sz[i] = k;
234  }
235 
236  Ghost<3,long int> g(1);
237 
238  // Distributed grid with id decomposition
240 
241  // fill the grid with values
242 
243  auto it = g_dist.getDomainGhostIterator();
244 
245  while (it.isNext())
246  {
247  auto p = it.get();
248  auto gkey = it.getGKey(p);
249 
250  g_dist.template get<0>(p) = gkey.get(0) + gkey.get(1) + gkey.get(2);
251 
252  ++it;
253  }
254 
255  g_dist.ghost_get<0>();
256 
257  double total = 0;
258 
259  double mean;
260  double dev;
261  openfpm::vector<double> measures;
262 
263  for (size_t j = 0 ; j < GRID_ITERATOR_TESTS ; j++)
264  {measures.add(grid_iterator_benchmark_stencil(g_dist,total));}
265  standard_deviation(measures,mean,dev);
266 
267  iterator_graph.put(base + ".stencil.data.mean",mean);
268  iterator_graph.put(base + ".stencil.data.dev",dev);
269 
270  std::cout << "Size: " << nk_grid.get(i) << " stencil: " << mean << std::endl;
271 
273 
274  measures.clear();
275  for (size_t j = 0 ; j < GRID_ITERATOR_TESTS ;j++)
276  {measures.add(grid_iterator_benchmark_norm(g_dist,total));}
277  standard_deviation(measures,mean,dev);
278 
279  iterator_graph.put(base + ".normal.data.mean",mean);
280  iterator_graph.put(base + ".normal.data.dev",dev);
281 
282  std::cout << "Size: " << nk_grid.get(i) << " normal: " << mean << std::endl;
283  }
284  }
285 }
286 
287 
288 
289 BOOST_AUTO_TEST_CASE( grid_interpolation_benchmark_test )
290 {
291  nk_grid_int.add(96);
292  nk_grid_int.add(128);
293  nk_grid_int.add(192);
294 
295  //Benchmark test for 2D and 3D
296  grid_interpolation_benchmark(nk_grid_int,
297  report_grid_iterator.graphs);
298 }
299 
300 BOOST_AUTO_TEST_CASE( grid_iterator_benchmark_test )
301 {
302  nk_grid_st.add(96);
303  nk_grid_st.add(128);
304  nk_grid_st.add(192);
305 
306  //Benchmark test for 2D and 3D
307  grid_iterator_benchmark<3>(nk_grid_st,
308  report_grid_iterator.graphs);
309 }
310 
311 
312 BOOST_AUTO_TEST_CASE(grid_iterator_performance_write_report_final)
313 {
314  report_grid_iterator.graphs.put("graphs.graph(0).type","line");
315  report_grid_iterator.graphs.add("graphs.graph(0).title","Grid iterators performance for stencil");
316  report_grid_iterator.graphs.add("graphs.graph(0).x.title","Number of grid points");
317  report_grid_iterator.graphs.add("graphs.graph(0).y.title","Time seconds");
318  report_grid_iterator.graphs.add("graphs.graph(0).y.data(0).source","performance.grid.iterators(#).normal.data.mean");
319  report_grid_iterator.graphs.add("graphs.graph(0).x.data(0).source","performance.grid.iterators(#).grid.x");
320  report_grid_iterator.graphs.add("graphs.graph(0).y.data(0).title","Normal iterator");
321  report_grid_iterator.graphs.add("graphs.graph(0).y.data(1).source","performance.grid.iterators(#).stencil.data.mean");
322  report_grid_iterator.graphs.add("graphs.graph(0).x.data(1).source","performance.grid.iterators(#).grid.x");
323  report_grid_iterator.graphs.add("graphs.graph(0).y.data(1).title","Stencil specialized iterator");
324  report_grid_iterator.graphs.add("graphs.graph(0).options.log_y","true");
325 
326  report_grid_iterator.graphs.put("graphs.graph(1).type","line");
327  report_grid_iterator.graphs.add("graphs.graph(1).title","Grid p2m performance");
328  report_grid_iterator.graphs.add("graphs.graph(1).x.title","Number of grid points");
329  report_grid_iterator.graphs.add("graphs.graph(1).y.title","Time seconds");
330  report_grid_iterator.graphs.add("graphs.graph(1).y.data(0).source","performance.interpolation.p2m(#).data.mean");
331  report_grid_iterator.graphs.add("graphs.graph(1).y.data(0).title","Interpolation p2m");
332  report_grid_iterator.graphs.add("graphs.graph(1).x.data(0).source","performance.interpolation.p2m(#).grid.x");
333  report_grid_iterator.graphs.add("graphs.graph(1).options.log_y","true");
334 
335  report_grid_iterator.graphs.put("graphs.graph(2).type","line");
336  report_grid_iterator.graphs.add("graphs.graph(2).title","Grid m2p performance");
337  report_grid_iterator.graphs.add("graphs.graph(2).x.title","Number of grid points");
338  report_grid_iterator.graphs.add("graphs.graph(2).y.title","Time seconds");
339  report_grid_iterator.graphs.add("graphs.graph(2).x","performance.interpolation.m2p(#).grid.x");
340  report_grid_iterator.graphs.add("graphs.graph(2).y.data(0).source","performance.interpolation.m2p(#).data.mean");
341  report_grid_iterator.graphs.add("graphs.graph(2).y.data(0).title","Interpolation m2p");
342  report_grid_iterator.graphs.add("graphs.graph(2).x.data(0).source","performance.interpolation.m2p(#).grid.x");
343  report_grid_iterator.graphs.add("graphs.graph(2).options.log_y","true");
344 
345  if (create_vcluster().rank() == 0)
346  {
347  boost::property_tree::xml_writer_settings<std::string> settings(' ', 4);
348  boost::property_tree::write_xml("grid_performance.xml", report_grid_iterator.graphs,std::locale(),settings);
349 
350  GoogleChart cg;
351 
352  std::string file_xml_ref(test_dir);
353  file_xml_ref += std::string("/openfpm_pdata/grid_performance_ref.xml");
354 
355  StandardXMLPerformanceGraph("grid_performance.xml",file_xml_ref,cg);
356 
357  if (create_vcluster().getProcessUnitID() == 0)
358  {
359  addUpdateTime(cg,create_vcluster().size(),"pdata","grid_performance");
360 
361  cg.write("grid_performance.html");
362  }
363  }
364 }
365 
366 
367 BOOST_AUTO_TEST_SUITE_END()
368 
369 
370 
371 #endif /* SRC_GRID_GRID_DIST_PERFORMANCE_HPP_ */
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
auto getPos(vect_dist_key_dx vec_key) -> decltype(v_pos.template get< 0 >(vec_key.getKey()))
Get the position of an element.
size_t size()
Stub size.
Definition: map_vector.hpp:211
double getwct()
Return the elapsed real time.
Definition: timer.hpp:130
Definition: Ghost.hpp:39
__device__ __host__ void setHigh(int i, T val)
set the high interval of the box
Definition: Box.hpp:544
Small class to produce graph with Google chart in HTML.
void map(size_t opt=NONE)
It move all the particles that does not belong to the local processor to the respective processor.
This class decompose a space into sub-sub-domains and distribute them across processors.
This is a distributed grid.
__device__ __host__ void setLow(int i, T val)
set the low interval of the box
Definition: Box.hpp:533
void start()
Start the timer.
Definition: timer.hpp:90
void write(std::string file)
It write the graphs on file in html format using Google charts.
Distributed vector.
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
Definition: aggregate.hpp:214
vector_dist_iterator getDomainIterator() const
Get an iterator that traverse the particles in the domain.
Class for cpu time benchmarking.
Definition: timer.hpp:27
Main class for interpolation Particle to mest p2m and Mesh to particle m2p.
void stop()
Stop the timer.
Definition: timer.hpp:119