main.cpp 11.5 KB
 Pietro Incardona committed Jul 30, 2016 1 2 3 4 `````` /*! \page Vector Vector * * \subpage Vector_0_simple * \subpage Vector_1_celllist `````` incardon committed Oct 19, 2016 5 `````` * \subpage Vector_1_ghost_get `````` Pietro Incardona committed Jul 30, 2016 6 7 `````` * \subpage Vector_2_expression * \subpage Vector_3_md `````` incardon committed Sep 21, 2016 8 9 `````` * \subpage Vector_4_reo_root * \subpage Vector_4_cp `````` Pietro Incardona committed Aug 12, 2016 10 `````` * \subpage Vector_4_mp_cl `````` incardon committed Oct 10, 2016 11 12 `````` * \subpage Vector_5_md_vl_sym * \subpage Vector_6_complex_usage `````` Pietro Incardona committed Jul 30, 2016 13 14 15 `````` * */ `````` incardon committed Sep 23, 2015 16 `````` `````` Pietro Incardona committed Jul 30, 2016 17 18 19 20 21 22 23 24 25 ``````/*! * \page Vector_0_simple Vector 0 simple * * * [TOC] * * * # Simple Vector example # {#simple_vector_example} * `````` incardon committed Sep 23, 2015 26 `````` * `````` Pietro Incardona committed May 24, 2016 27 28 `````` * This example show several basic functionalities of the distributed vector, A distributed vector is nothing else than * a set of particles in an N dimensional space `````` Pietro Incardona committed Jul 30, 2016 29 30 31 32 33 34 35 36 37 38 39 40 41 42 `````` * * \htmlonly * Video 1 * * Video 2 * * \endhtmlonly * `````` incardon committed Oct 19, 2016 43 `````` * ## inclusion ## {#e0_v_inclusion} `````` Pietro Incardona committed Jul 30, 2016 44 45 46 47 48 `````` * * In order to use distributed vectors in our code we have to include the file Vector/vector_dist.hpp * * \snippet Vector/0_simple/main.cpp inclusion * `````` incardon committed Sep 23, 2015 49 50 `````` */ `````` Pietro Incardona committed Jul 30, 2016 51 52 53 54 ``````//! \cond [inclusion] \endcond #include "Vector/vector_dist.hpp" //! \cond [inclusion] \endcond `````` incardon committed Sep 23, 2015 55 56 ``````int main(int argc, char* argv[]) { `````` Pietro Incardona committed Jul 30, 2016 57 58 59 60 61 62 63 64 65 `````` /*! * \page Vector_0_simple Vector 0 simple * * ## Initialization ## {#e0_s_init} * * Here we * * Initialize the library * * we create a Box that define our domain `````` incardon committed Oct 19, 2016 66 `````` * * An array that define our boundary conditions `````` Pietro Incardona committed Jul 30, 2016 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 `````` * * A Ghost object that will define the extension of the ghost part in physical units * * * * \snippet Vector/0_simple/main.cpp Initialization and parameters * * \htmlonly * Video 1 * * Video 2 * * \endhtmlonly * */ //! \cond [Initialization and parameters] \endcond // initialize the library `````` Pietro Incardona committed May 24, 2016 91 `````` openfpm_init(&argc,&argv); `````` incardon committed Sep 23, 2015 92 `````` `````` Pietro Incardona committed May 24, 2016 93 `````` // Here we define our domain a 2D box with internals from 0 to 1.0 for x and y `````` Pietro Incardona committed Feb 05, 2016 94 `````` Box<2,float> domain({0.0,0.0},{1.0,1.0}); `````` Pietro Incardona committed May 24, 2016 95 96 `````` // Here we define the boundary conditions of our problem `````` Pietro Incardona committed Feb 05, 2016 97 `````` size_t bc[2]={PERIODIC,PERIODIC}; `````` Pietro Incardona committed May 24, 2016 98 99 `````` // extended boundary around the domain, and the processor domain `````` Pietro Incardona committed Dec 28, 2015 100 `````` Ghost<2,float> g(0.01); `````` incardon committed Sep 23, 2015 101 `````` `````` Pietro Incardona committed Jul 30, 2016 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 `````` //! \cond [Initialization and parameters] \endcond /*! * \page Vector_0_simple Vector 0 simple * * ## Vector instantiation ## {#e0_s_vector_inst} * * Here we are creating a distributed vector defined by the following parameters * * * 2 is the Dimensionality of the space where the objects live * * float is the type used for the spatial coordinate of the particles * * float,float[3],float[3][3] is the information stored by each particle a scalar float, a vector float[3] and a tensor of rank 2 float[3][3] * the list of properties must be put into an aggregate data structure aggregate * * vd is the instantiation of the object * * The Constructor instead require: * * * Number of particles 4096 in this case * * Domain where is defined this structure * * bc boundary conditions * * g Ghost * * The following construct a vector where each processor has 4096 / N_proc (N_proc = number of processor) * objects with an undefined position in space. This non-space decomposition is also called data-driven * decomposition * * * \snippet Vector/0_simple/main.cpp vector instantiation * * \htmlonly * Video * * \endhtmlonly * */ //! \cond [vector instantiation] \endcond `````` Pietro Incardona committed May 24, 2016 144 145 146 147 148 149 150 151 152 153 `````` vector_dist<2,float, aggregate > vd(4096,domain,bc,g); // the scalar is the element at position 0 in the aggregate const int scalar = 0; // the vector is the element at position 1 in the aggregate const int vector = 1; // the tensor is the element at position 2 in the aggregate const int tensor = 2; `````` incardon committed Sep 23, 2015 154 `````` `````` Pietro Incardona committed Jul 30, 2016 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 `````` //! \cond [vector instantiation] \endcond /*! * \page Vector_0_simple Vector 0 simple * * ## Assign position ## {#e0_s_assign_pos} * * Get an iterator that go through the 4096 particles. Initially all the particles * has an undefined position state. In this cycle we define its position. In this * example we use iterators. Iterators are convenient way to explore/iterate data-structures in an * convenient and easy way * * \snippet Vector/0_simple/main.cpp assign position * * \htmlonly * Iterator Video * * \endhtmlonly * */ //! \cond [assign position] \endcond `````` Pietro Incardona committed May 24, 2016 181 `````` auto it = vd.getDomainIterator(); `````` incardon committed Sep 23, 2015 182 183 184 185 186 `````` while (it.isNext()) { auto key = it.get(); `````` Pietro Incardona committed May 24, 2016 187 `````` // we define x, assign a random position between 0.0 and 1.0 `````` Pietro Incardona committed Aug 09, 2016 188 `````` vd.getPos(key)[0] = (float)rand() / RAND_MAX; `````` Pietro Incardona committed May 24, 2016 189 190 `````` // we define y, assign a random position between 0.0 and 1.0 `````` Pietro Incardona committed Aug 09, 2016 191 `````` vd.getPos(key)[1] = (float)rand() / RAND_MAX; `````` incardon committed Sep 23, 2015 192 `````` `````` Pietro Incardona committed May 24, 2016 193 `````` // next particle `````` incardon committed Sep 23, 2015 194 195 196 `````` ++it; } `````` Pietro Incardona committed Jul 30, 2016 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 `````` //! \cond [assign position] \endcond /*! * \page Vector_0_simple Vector 0 simple * * ## Mapping particles ## {#e0_s_map} * * On a parallel program, once we define the position, we distribute the particles according to the underlying space decomposition * The default decomposition is created even before assigning the position to the object, and is calculated * giving to each processor an equal portion of space minimizing the surface to reduce communication. * * \snippet Vector/0_simple/main.cpp map * * \htmlonly * Parallelization Video * * Video 1 * * Video 2
* * Video 3 * * \endhtmlonly * */ //! \cond [map] \endcond `````` incardon committed Sep 23, 2015 237 238 `````` vd.map(); `````` Pietro Incardona committed Jul 30, 2016 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 `````` //! \cond [map] \endcond /*! * \page Vector_0_simple Vector 0 simple * * ## Assign values to particles property ## {#assign_prop} * * We Iterate across all the particles, we count them using a local counter and we assign 1.0 to * all the particles properties. Each particle has a scalar, vector and tensor property. * * \snippet Vector/0_simple/main.cpp assign property * * */ //! \cond [assign property] \endcond `````` Pietro Incardona committed May 24, 2016 256 `````` //Counter we use it later `````` incardon committed Sep 23, 2015 257 `````` size_t cnt = 0; `````` Pietro Incardona committed May 24, 2016 258 259 260 261 262 `````` // Get a particle iterator it = vd.getDomainIterator(); // For each particle ... `````` incardon committed Sep 23, 2015 263 264 `````` while (it.isNext()) { `````` Pietro Incardona committed May 24, 2016 265 266 267 268 269 `````` // ... p auto p = it.get(); // we set the properties of the particle p `````` Pietro Incardona committed Jul 30, 2016 270 `````` // the scalar property `````` Pietro Incardona committed May 24, 2016 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 `````` vd.template getProp(p) = 1.0; vd.template getProp(p)[0] = 1.0; vd.template getProp(p)[1] = 1.0; vd.template getProp(p)[2] = 1.0; vd.template getProp(p)[0][0] = 1.0; vd.template getProp(p)[0][1] = 1.0; vd.template getProp(p)[0][2] = 1.0; vd.template getProp(p)[1][0] = 1.0; vd.template getProp(p)[1][1] = 1.0; vd.template getProp(p)[1][2] = 1.0; vd.template getProp(p)[2][0] = 1.0; vd.template getProp(p)[2][1] = 1.0; vd.template getProp(p)[2][2] = 1.0; // increment the counter `````` incardon committed Sep 23, 2015 288 289 `````` cnt++; `````` Pietro Incardona committed May 24, 2016 290 `````` // next particle `````` incardon committed Sep 23, 2015 291 292 293 `````` ++it; } `````` Pietro Incardona committed Jul 30, 2016 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 `````` //! \cond [assign property] \endcond /*! * \page Vector_0_simple Vector 0 simple * * ## Reduce (sum numbers across processors) ## {#e0_s_reduce} * * cnt contain the number of object the local processor contain, if we are interested to count the total number across the processors * we can use the function add, to sum across the processors. First we have to get an instance of Vcluster, queue an operation of add with * the variable count and finally execute. All the operations are asynchronous, execute work like a barrier and ensure that all the * queued operations are executed. * * \snippet Vector/0_simple/main.cpp reduce * */ //! \cond [reduce] \endcond `````` Pietro Incardona committed May 24, 2016 311 312 `````` auto & v_cl = create_vcluster(); `````` incardon committed Sep 23, 2015 313 314 315 `````` v_cl.sum(cnt); v_cl.execute(); `````` Pietro Incardona committed Jul 30, 2016 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 `````` //! \cond [reduce] \endcond /*! * \page Vector_0_simple Vector 0 simple * * ## Visualization, write VTK files ## {#e0_s_vis_vtk} * * With this function we output the particle position in VTK format. A VTK file * contain information about particle position and properties. Such file can be visualizaed * with program like paraview. In case this program run on several processor Each processor * generate a VTK file * * \snippet Vector/0_simple/main.cpp vtk * * \htmlonly * Video * * \endhtmlonly * */ //! \cond [vtk] \endcond vd.write("particles"); //! \cond [vtk] \endcond /*! * \page Vector_0_simple Vector 0 simple * `````` Pietro Incardona committed Sep 01, 2016 349 `````` * ## Finalize ## {#finalize_e0_sim} `````` Pietro Incardona committed Jul 30, 2016 350 `````` * `````` incardon committed Oct 20, 2016 351 `````` * At the very end of the program we have always de-initialize the library `````` Pietro Incardona committed Jul 30, 2016 352 353 354 355 356 357 358 `````` * * \snippet Vector/0_simple/main.cpp finalize * */ //! \cond [finalize] \endcond `````` Pietro Incardona committed Apr 14, 2016 359 `````` openfpm_finalize(); `````` Pietro Incardona committed Jul 30, 2016 360 361 362 363 364 365 `````` //! \cond [finalize] \endcond /*! * \page Vector_0_simple Vector 0 simple * `````` Pietro Incardona committed Sep 01, 2016 366 `````` * ## Full code ## {#code_e0_sim} `````` Pietro Incardona committed Jul 30, 2016 367 368 369 370 `````` * * \include Vector/0_simple/main.cpp * */ `````` incardon committed Sep 23, 2015 371 ``}``