<h1>Using pNbody in parallel<aclass="headerlink"href="#using-pnbody-in-parallel"title="Permalink to this headline">¶</a></h1>
<p>With <strong>pNbody</strong>, it is possible to run scripts in parallel, using the <ttclass="docutils literal"><spanclass="pre">mpi</span></tt> libary.
You need to have of course <ttclass="docutils literal"><spanclass="pre">mpi</span></tt> and <ttclass="docutils literal"><spanclass="pre">mpi4py</span></tt> installed.
<divclass="highlight-python"><pre>This is task 0 over 2
This is task 1 over 2</pre>
</div>
<p>but if you get:</p>
<divclass="highlight-python"><pre>This is task 0 over 1
This is task 0 over 1</pre>
</div>
<p>this means that something is not working correctly, and you should check your path or <ttclass="docutils literal"><spanclass="pre">mpi</span></tt> and <ttclass="docutils literal"><spanclass="pre">mpi4py</span></tt> installation
before reading further.</p>
<p>The prevous scripts <ttclass="docutils literal"><spanclass="pre">scripts/slice.py</span></tt> can diretely be run in paralle.
This is simply obtained by calling the <ttclass="docutils literal"><spanclass="pre">mpirun</span></tt> command:</p>
<p>In this simple script, only the processus of rank 0 (the master) open the file.
The content of the file (particles) is then distributed among all the other processors.
Eeach processor recives a fraction of the particles.
Then, the selection of gas gas particles and the slice are preformed by all processors on
their local particles.
Finally, the <ttclass="docutils literal"><spanclass="pre">nb.write()</span></tt> command, run by the master, gather all particles and write the output file.</p>
<divclass="section"id="parallel-output">
<h2>Parallel output<aclass="headerlink"href="#parallel-output"title="Permalink to this headline">¶</a></h2>
<p>With <strong>pNbody</strong>, its possible to write files in parallel, i.e., each task write its own file.
We can do this in the previous script simply by adding the line <ttclass="docutils literal"><spanclass="pre">nb.set_pio('yes')</span></tt>. This
tells <strong>pNbody</strong> to write files in parallel when <ttclass="docutils literal"><spanclass="pre">nb.write()</span></tt> is called.
The content of the new scripts <ttclass="docutils literal"><spanclass="pre">scripts/slice-p1.py</span></tt> is:</p>
<p>The files have the same name than the initial name given in <ttclass="docutils literal"><spanclass="pre">Nbody()</span></tt> with an extention <ttclass="docutils literal"><spanclass="pre">.i</span></tt> where <ttclass="docutils literal"><spanclass="pre">i</span></tt>
corresponds to the processus rank. Each file contains the particles attributed to the corresponding task.</p>
</div>
<divclass="section"id="parallel-input">
<h2>Parallel input<aclass="headerlink"href="#parallel-input"title="Permalink to this headline">¶</a></h2>
<p>Now, it possible to start by reading these two files in parallel instead of asking only the master to read one file::
In our script, we add the optional argument <ttclass="docutils literal"><spanclass="pre">pio='yes'</span></tt> when creating the object with <ttclass="docutils literal"><spanclass="pre">Nbody()</span></tt>:</p>
<p>Note also that we have used <ttclass="docutils literal"><spanclass="pre">nb.set_pio('no')</span></tt>. This force at the end the file te be written only by the master.</p>
<blockquote>
<p>#!/usr/bin/env python</p>
<p>import sys
from pNbody import *</p>
<p>files = sys.argv[1:]</p>
<dlclass="docutils">
<dt>for file in files:</dt>
<dd>print “slicing”,file
nb = Nbody(file,ftype=’gadget’,pio=’yes’)
<p>the two files <ttclass="docutils literal"><spanclass="pre">gadget_z00.dat.slice.0</span></tt> and <ttclass="docutils literal"><spanclass="pre">gadget_z00.dat.slice.1</span></tt> are read
each by one task, processed but at the end only the master write the final output : <cite>gadget_z00.dat.slice.slice.new`</cite>.</p>
</div>
<divclass="section"id="more-on-parallelisme">
<h2>More on parallelisme<aclass="headerlink"href="#more-on-parallelisme"title="Permalink to this headline">¶</a></h2>
<p>Lets try two other scripts. The first one (<ttclass="docutils literal"><spanclass="pre">findmax.py</span></tt>) try to find the radial maximum distance among
all particles and the center. It illustrate the difference between using <ttclass="docutils literal"><spanclass="pre">max()</span></tt>
wich gives the local maximum (maximum among particles of the node) and <ttclass="docutils literal"><spanclass="pre">mpi.mpi_max()</span></tt>
which gives the global maximum among all particles:</p>