https://athena.brynmawr.edu/wiki/index.php?title=Tutorial_on_using_Python_and_MPI&feed=atom&action=historyTutorial on using Python and MPI - Revision history2024-03-28T08:57:39ZRevision history for this page on the wikiMediaWiki 1.26.4https://athena.brynmawr.edu/wiki/index.php?title=Tutorial_on_using_Python_and_MPI&diff=5&oldid=prevMrice admin: Created page with "= Hello MPI in Python = This tutorial will demonstrate how to use [http://athena.brynmawr.edu Bryn Mawr College's Athena Cluster] with Python and MPI, the Message Passing Int..."2017-09-11T18:37:53Z<p>Created page with "= Hello MPI in Python = This tutorial will demonstrate how to use [http://athena.brynmawr.edu Bryn Mawr College's Athena Cluster] with Python and MPI, the Message Passing Int..."</p>
<p><b>New page</b></p><div>= Hello MPI in Python =<br />
<br />
This tutorial will demonstrate how to use [http://athena.brynmawr.edu Bryn Mawr College's Athena Cluster] with Python and MPI, the Message Passing Interface.<br />
<br />
Here are the steps, briefly, for running a MPI Python Script on Athena<br />
<br />
# log into athena.brynmawr.edu<br />
# find out which /data path you have a shared directory (either /data1, or /data2) <br />
#* You should now have access to a $DATA variable<br />
#* echo $DATA<br />
# create a python program in your /dataN/userid/ directory<br />
# create a PBS script<br />
# submit the PBS script<br />
# check on status<br />
# see the results<br />
<br />
We will now go through each step in detail.<br />
<br />
== Step 1: Log into Athena ==<br />
<br />
If you do not have an Athena account, please email ... to request one. It can take up to one business day. <br />
<br />
After you have your account, you can log into the account from anywhere. You can use ssh on Linux and Mac, or putty on Windows.<br />
<br />
You can use the X-forwarding flag, -X, if you have X running:<br />
<br />
<pre><br />
ssh -X dblank@athena.brynmawr.edu<br />
</pre><br />
<br />
Using X-forwarding will allow you to use graphical programs, such as gedit.<br />
<br />
== Step 2: Where is my /data? ==<br />
<br />
Next, you need to find out where your /data directory (either /data1 or /data2) has been located. <br />
<br />
You should now have access to a $DATA variable:<br />
<br />
<pre><br />
echo $DATA<br />
</pre><br />
<br />
Another way to find your data directory is to list the files in the directories:<br />
<br />
<pre><br />
[dblank@powerwulf ~]$ ls /data1<br />
dblank gdavis n001.cluster.com n004.cluster.com pbrodfue<br />
dmtpc josh n002.cluster.com n005.cluster.com powerwulf.brynmawr.edu<br />
emaccorma mrice n003.cluster.com n006.cluster.com xwang<br />
[dblank@powerwulf ~]$ ls /data2<br />
dblank gdavis n001.cluster.com n004.cluster.com pbrodfue<br />
emaccorma josh n002.cluster.com n005.cluster.com powerwulf.brynmawr.edu<br />
galaxy mrice n003.cluster.com n006.cluster.com xwang<br />
[dblank@powerwulf ~]$ <br />
</pre><br />
<br />
== Step 3: Create a Python script ==<br />
<br />
Next, you will want to create a Python script in your /data directory.<br />
<br />
First, change to your directory:<br />
<br />
<pre><br />
cd /data1/dblank<br />
</pre><br />
<br />
Next, use a text editor (emacs, vi, gedit, or nano) to create a file in your /data directory. We use the /data directory because it is available to all of the nodes.<br />
<br />
Here is a sample program: <br />
<br />
<pre><br />
# More tutorials available at:<br />
# http://mpi4py.scipy.org/docs/usrman/tutorial.html<br />
<br />
from mpi4py import MPI<br />
import platform<br />
<br />
comm = MPI.COMM_WORLD<br />
rank = comm.Get_rank()<br />
size = comm.Get_size()<br />
machine = platform.node()<br />
<br />
print("Hello MPI from %s %d of %d" % (machine, rank, size))<br />
</pre><br />
<br />
Save the script to a file, such as: /data1/dblank/hellompi.py<br />
<br />
rank and size are two useful values from MPI. See http://mpi4py.scipy.org/docs/usrman/tutorial.html for more examples.<br />
<br />
== Step 4: Submit PBS script ==<br />
<br />
To submit a Python script, you will need a shell program to give to qsub. Here is a sample:<br />
<br />
<pre><br />
#!/bin/bash<br />
<br />
#PBS -q default<br />
#PBS -l nodes=6:ppn=12<br />
<br />
mpiexec -machinefile /opt/machinelist -n 10 python /data1/dblank/hellompi.py<br />
<br />
echo "end"<br />
</pre><br />
<br />
Save this in a file, such as /home/dblank/pbs/hellompi.sh.<br />
<br />
PBS is a language that allows you to set many aspects of your running program. For more details see ...<br />
<br />
In this example, we tell mpiexec to choose from the machines listed in /opt/machinelist (all of the nodes) and to create 10 instances of our running code across those machines.<br />
<br />
NOTE: the program will start in your home directory, so by default you will be in /home/dblank/. Use full pathnames to /data1/ for shared data.<br />
<br />
Finally you will submit this script to qsum, like so:<br />
<br />
<pre><br />
qsub hellompi.sh<br />
</pre><br />
<br />
NOTE: the results of qsub will appear here. Therefore, you may want to move your shell script to a folder before beginning.<br />
<br />
== Step 5: Check status ==<br />
<br />
When you start your program a number of things will happen:<br />
<br />
# your program will be scheduled to run<br />
# when qsub runs your script, the script be submitted to each of the machines, perhaps a number of times<br />
<br />
You can run "qstat -a" to see the status of your program.<br />
<br />
== Step 6: Get Results ==<br />
<br />
Finally, any output from your program will be saved in a file with the same name as your shell script, with a dot, the letter "o", and job number appended.<br />
<br />
<pre><br />
Hello MPI from n002.cluster.com 1 of 10<br />
Hello MPI from n002.cluster.com 7 of 10<br />
Hello MPI from n001.cluster.com 6 of 10<br />
Hello MPI from n001.cluster.com 0 of 10<br />
Hello MPI from n004.cluster.com 3 of 10<br />
Hello MPI from n004.cluster.com 9 of 10<br />
Hello MPI from n003.cluster.com 2 of 10<br />
Hello MPI from n003.cluster.com 8 of 10<br />
Hello MPI from n006.cluster.com 5 of 10<br />
Hello MPI from n005.cluster.com 4 of 10<br />
end<br />
</pre><br />
<br />
== Step 7: Where to go from here? ==<br />
<br />
* http://mpi4py.scipy.org/docs/usrman/tutorial.html<br />
* http://pympi.sourceforge.net/examples.html - slightly different from MPI4PY, but still Python and MPI ideas.<br />
* https://github.com/jbornschein/mpi4py-examples</div>Mrice admin