In this example we discuss how to switch from Parallel to serial and from serial to parallel. This is in general useful if you are trying to port a serial program into openfpm, and you want to do it progressively or in parts
Before using any functionality the library must be initialized. After initialization we can create the Vcluster object
First we start from a serial section in which we have one external std::vector<my_particle> containing particles position and properties. Because the code is serial the full serial part is enclosed into an if condition that only processor 0 execute the code. Processor 0 only execute the code and create 100 particles.
The parallel section instead require an equivalent vector_dist. In particular we initialize the vector dist with 0 particles on a domain from (0.0,0.0) to (2.0,3.0).
As soon as we want to go parallel we want to convert the information from the std::vector to vector_dist. To do this, we call the function serial_to_parallel that convert the serial std::vector into vector_dist. After we did the conversion we can use pparts to do operations on it.
This function convert the serial set of particles into the parallel set of particles To do this we first clear pparts than only the processor 0 fill pparts from the serial set of particles parts. After we filled pparts we redistribute the particles across processors using the function map. If the particles are uniformly distributed we can skip the second part an comment addComputationCosts(),decompose(),and the second map(). Otherwise these three lines redistribute the particles across processors in order to have (almost) an equal number of particles across processors independently from their density
To reconvert to serial we use the function parallel_to_serial this function does the opposite to convert the vector_dist pparts into std::vector parts. Once we have converted we can use parts, in this case we simply move the particles of 0.01 on x and 0.01 on y
This function convert the parallel set of particles into the serial set of particles. To do this we first get the position and properties particle vector from each processor. Than we create two other equivalent vector that will contain the collected vector of position and vector of properties. We use the function SGather to collect on processor 0 all the data. Once collected we move the data from the collected vectors into parts
At the very end of the program we have always to de-initialize the library