Project that contain the implementation of distributed structures
Loading...
Searching...
No Matches
Vector 0 simple
Simple Vector example
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
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<prop1,prop2,prop3, ... >
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
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
auto it = vd.getDomainIterator();
while (it.isNext())
{
auto key = it.get();
// we define x, assign a random position between 0.0 and 1.0
vd.getPos(key)[0] = (float)rand() / RAND_MAX;
// we define y, assign a random position between 0.0 and 1.0
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.
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.
//Counter we use it later
size_t cnt = 0;
// Get a particle iterator
it = vd.getDomainIterator();
// For each particle ...
while (it.isNext())
{
// ... p
auto p = it.get();
// we set the properties of the particle p
// the scalar property
vd.template getProp<scalar>(p) = 1.0;
vd.template getProp<vector>(p)[0] = 1.0;
vd.template getProp<vector>(p)[1] = 1.0;
vd.template getProp<vector>(p)[2] = 1.0;
vd.template getProp<tensor>(p)[0][0] = 1.0;
vd.template getProp<tensor>(p)[0][1] = 1.0;
vd.template getProp<tensor>(p)[0][2] = 1.0;
vd.template getProp<tensor>(p)[1][0] = 1.0;
vd.template getProp<tensor>(p)[1][1] = 1.0;
vd.template getProp<tensor>(p)[1][2] = 1.0;
vd.template getProp<tensor>(p)[2][0] = 1.0;
vd.template getProp<tensor>(p)[2][1] = 1.0;
vd.template getProp<tensor>(p)[2][2] = 1.0;
// increment the counter
cnt++;
// next particle
++it;
}
Reduce (sum numbers across processors)
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.
auto & v_cl = create_vcluster();
v_cl.sum(cnt);
v_cl.execute();
Visualization, write VTK files
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 visualized with a program like paraview. In case this program run on several processor each processor generate a VTK file. VTK has two format one is the ASCII that is human readable but produce bigger file the other is the binary that produce not-human readable files, but smaller and more efficent files to read for Paraview. In order to create a Binary VTK file use the option VTK_WRITER in combination with FORMAT_BINARY. By default properties does not have name the VTK writer will label them "attr0" for the first property "attr1" for the second and so on in case we want to assign names to the properties we can use the function setPropNames