OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
main.cpp
1 /*
2  * ### WIKI 1 ###
3  *
4  * ## Security enhancement example
5  *
6  * This example show several basic functionalities of Security Enhancements
7  *
8  */
9 
10 #define SE_CLASS1
11 #define SE_CLASS2
12 #define SE_CLASS3
13 #define THROW_ON_ERROR
14 #include "Memleak_check.hpp"
15 #include "Vector/vector_dist.hpp"
16 #include "Decomposition/CartDecomposition.hpp"
17 #include "Point_test.hpp"
18 
19 /*
20  * ### WIKI END ###
21  */
22 
23 
24 int main(int argc, char* argv[])
25 {
26  //
27  // ### WIKI 2 ###
28  //
29  // With print_unalloc we can check how much memory has been allocated and which structure
30  // has been allocated, initially there are not
31  //
32 
33  std::cout << "Allocated memory before initializing \n";
34  print_alloc();
35  std::cout << "\n";
36  std::cout << "\n";
37  std::cout << "\n";
38 
39  //
40  // ### WIKI 3 ###
41  //
42  // Here we Initialize the library, than we create a uniform random generator between 0 and 1 to to generate particles
43  // randomly in the domain, we create a Box that define our domain, boundary conditions and ghost
44  //
45  openfpm_init(&argc,&argv);
46  Vcluster & v_cl = create_vcluster();
47 
48  typedef Point<2,float> s;
49 
50  Box<2,float> box({0.0,0.0},{1.0,1.0});
51  size_t bc[2]={NON_PERIODIC,NON_PERIODIC};
52  Ghost<2,float> g(0.01);
53 
54  //
55  // ### WIKI 4 ###
56  //
57  // Here we ask again for the used memory, as we can see Vcluster and several other structures encapsulated inside
58  // Vcluster register itself
59  //
60  if (v_cl.getProcessUnitID() == 0)
61  {
62  std::cout << "Allocated memory after initialization \n";
63  print_alloc();
64  std::cout << "\n";
65  std::cout << "\n";
66  std::cout << "\n";
67  }
68 
69  //
70  // ### WIKI 5 ###
71  //
72  // Here we are creating a distributed vector defined by the following parameters
73  //
74  // * Dimensionality of the space where the objects live 2D (1° template parameters)
75  // * Type of the space, float (2° template parameters)
76  // * Information stored by each object (3* template parameters), in this case a Point_test store 4 scalars
77  // 1 vector and an asymmetric tensor of rank 2
78  // * Strategy used to decompose the space
79  //
80  // The Constructor instead require:
81  //
82  // * Number of particles 4096 in this case
83  // * Domain where is defined this structure
84  //
85  // The following construct a vector where each processor has 4096 / N_proc (N_proc = number of processor)
86  // objects with an undefined position in space. This non-space decomposition is also called data-driven
87  // decomposition
88  //
89  {
91 
92  //
93  // ### WIKI 6 ###
94  //
95  // we create a key that for sure overflow the local datastructure, 2048 with this program is started with more than 3
96  // processors, try and catch are optionals in case you want to recover from a buffer overflow
97  //
98  try
99  {
100  vect_dist_key_dx vt(5048);
101  auto it = vd.getPos(vt);
102  }
103  catch (size_t e)
104  {
105  std::cerr << "Error notification of overflow \n";
106  }
107  }
108  //
109  // ### WIKI 7 ###
110  //
111  // At this point the vector went out of the scope and if destroyed
112  // we create, now two of them using new
113  //
114 
116  vector_dist<2,float, Point_test<float>, CartDecomposition<2,float> > * vd2 = new vector_dist<2,float, Point_test<float>, CartDecomposition<2,float> >(4096,box,bc,g);
117 
118  //
119  // ### WIKI 8 ###
120  //
121  // we can check that these two structure produce an explicit allocation checking
122  // for registered pointers and structures with print_alloc, in the list we see 2 additional
123  // entry for distributed vector in yellow, pdata to work use the data structures that register
124  // itself in magenta, the same things happen for the real memory allocation from devices in
125  // fully green
126  //
127 
128  if (v_cl.getProcessUnitID() == 0)
129  {
130  std::cout << "Allocated memory with 2 vectors \n";
131  print_alloc();
132  std::cout << "\n";
133  std::cout << "\n";
134  std::cout << "\n";
135  }
136 
137  //
138  // ### WIKI 9 ###
139  //
140  // we can also ask to the structure to identify their-self in the list
141  //
142 
143  std::cout << "Vector id: " << vd1->who() << "\n";
144  std::cout << "Vector id: " << vd2->who() << "\n";
145 
146  //
147  // ### WIKI 10 ###
148  //
149  // delete vd1 and print allocated memory, one distributed vector disappear
150  //
151 
152  delete vd1;
153 
154  if (v_cl.getProcessUnitID() == 0)
155  {
156  std::cout << "Allocated memory with 1 vector \n";
157  print_alloc();
158  std::cout << "\n";
159  std::cout << "\n";
160  std::cout << "\n";
161  }
162 
163  //
164  // ### WIKI 11 ###
165  //
166  // delete vd2 and print allocated memory, all distributed vector de-register
167  //
168 
169  delete vd2;
170 
171  if (v_cl.getProcessUnitID() == 0)
172  {
173  std::cout << "Allocated memory with 1 vector \n";
174  print_alloc();
175  std::cout << "\n";
176  std::cout << "\n";
177  std::cout << "\n";
178  }
179 
180  //
181  // ### WIKI 12 ###
182  //
183  // Try to use a deleted object
184  //
185  try
186  {
187  vect_dist_key_dx vt(0);
188  auto it = vd1->getPos(vt);
189  }
190  catch (std::exception & e)
191  {
192  std::cerr << "Error notification of invalid usage of deleted object \n";
193  }
194 
195  //
196  // ### WIKI 13 ###
197  //
198  // Deinitialize the library
199  //
200  openfpm_finalize();
201 }
202 
size_t getProcessUnitID()
Get the process unit id.
auto getPos(vect_dist_key_dx vec_key) -> decltype(v_pos.template get< 0 >(vec_key.getKey()))
Get the position of an element.
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
Grid key for a distributed grid.
long int who()
It return the id of structure in the allocation list.
Definition: Ghost.hpp:39
Implementation of VCluster class.
Definition: VCluster.hpp:36
This class decompose a space into sub-sub-domains and distribute them across processors.
This class represent an N-dimensional box.
Definition: Box.hpp:56
Distributed vector.