View Full Version : Python Scripts and MPI Python

07-12-2010, 02:44 PM
A user recently brought to my attention a case where he was using python scripts to invoke C++ codes in an MPI job using SWIG an mpi4py. If you know all those terms, then you're halfway there!

He hadn't been able to get TotalView to work on his code, and it turned out to be a bit tricky, but not too hard.

My first try was to do:

mpirun -tv -np 4 python test2.py

where test2.py was:
from example import *
from mpi4py import MPI

print "rank = ", MPI.COMM_WORLD.Get_rank()

print cube(3)
print cube(42)
print cube(8)

cube itself was found in example.c:
#include <stdlib.h>

int cube (int n)
return n * n * n;

which has a wrapper in example.i:
%module example
extern int cube( int n );
%include example.c

which then uses swig to build more python related stuff:

swig -python example.i

and then gets built into stuff you can call from python:

gcc -g -fPIC -c example.c example_wrap.c -I/usr/include/pythonX.X -I/usr/lib/pythonX.x
gcc -g -shared -o _example.so example.o example_wrap.o

Then you can run (one hopes) python test2.py and have it all work correctly. The problem when trying TotalView as above with

mpirun -tv -np 4 python test2.py

was that it seemed to pick up the wrong version of python and thus, to make thing work, I changed this to

mpirun -tv -np 4 /usr/bin/python ./test2.py

which then seemed to pick up something. It wasn't clear where the example.c code was coming in, so I set a breakpoint on 'cube' and that took. Hitting 'Go' and answering the basic questions did get me eventually to break on 'cube'.

Another method is to use


by itself, and then enter

/usr/bin/python in the New Program window as the target to debug. With this method, you would then go to Arguments, and enter the script test2.py as a command line argument. Finally click on Parallel, and chose
OpenMPI (or your preferred MPI system) and the number of tasks you want to run.

Again, after you start, you want to set a breakpoint in 'cube' unless you have run things before and saved the breakpoint.


09-11-2012, 01:07 PM
Hey Pete-

I recently got a copy of the student edition of TotalView and I'm having some problems running a python script that calls c++ shared objects created using swig (same as above).

First, I tried your first option:

mpirun -tv -np 2 /usr/bin/python ./Parallel.py

where Parallel.py is the script I wish to run.

TotalView fires up and brings me to the startup parameters window, on which I click okay. Then I press Go and TotalView starts outputting to the screen, but gets held up. It says that there is only one process (even though np=2). The script, Parallel.py, has print statements in it, but none show up in the output.

Next, I tried your second option:

totalview &

This opens TotalView and brings me to the new program window, where I ender /usr/bin/python as the target to debug. In the Arguments tab, I write ./Parallel.py as a command line argument. In the Parallel tab, I choose MPICH and select 2 tasks on 2 nodes.

When I click Go, TotalView says that my parallel configuration does not pass the basic sanity test. If I click to continue anyway, Totalview opens again and I revieve a warning saying that TotalView is already running on this machine.

To simlify things, I attempted to run just a parallel python script, using mpi4py. I got the same results when I tried both of the above paths.

Could you please help?


09-12-2012, 06:51 AM
After a while of tinkering, I have it working now. I needed to update some header files in MPICH2 (those associated with debugging). I also downloaded the mpi4py debugging header files.

Everything works fine now.