I've coded a python script that can be either launched in stand alone way or with mpi support.
python myscript.py
vs
mpirun -np 2 python myscript.py
How can I know inside the script in which way the script was launched to do some conditional operations?
Mpiexec is a replacement program for the script mpirun, which is part of the mpich package. It is used to initialize a parallel job from within a PBS batch or interactive environment. Mpiexec uses the task manager library of PBS to spawn copies of the executable on the nodes in a PBS allocation.
Interface options The use of -m mpi4py to execute Python code on the command line resembles that of the Python interpreter. mpiexec -n numprocs python -m mpi4py pyfile [arg] ... mpiexec -n numprocs python -m mpi4py -m mod [arg] ... mpiexec -n numprocs python -m mpi4py -c cmd [arg] ...
MPI for Python provides Python bindings for the Message Passing Interface (MPI) standard, allowing Python applications to exploit multiple processors on workstations, clusters and supercomputers. This package builds on the MPI specification and provides an object oriented interface resembling the MPI-2 C++ bindings.
Do you care whether it was run using MPI or whether it is run on one MPI rank? For compiled MPI code, running just the program will still start it under MPI, but with only one rank; and so you would probably just initialize MPI and check the size of MPI_COMM_WORLD
. It could be that you trying to avoid initializing MPI (or even needing to have an MPI implementation available) if you are running without MPI, though. If so, you will probably need to check for particular environment variables, which appear to be implementation-specific. For Open MPI, the list is at http://www.open-mpi.org/faq/?category=running#mpi-environmental-variables. For MPICH, various sources mention PMI_RANK
and PMI_SIZE
as commonly being set; Microsoft MPI documents that it sets those. They may be specific to particular MPICH versions or configurations. There is a list of variables to check at http://www.roguewave.com/portals/0/products/threadspotter/docs/2012.1/linux/manual_html/apas03.html that might be useful as well.
If you are on Unix, you can analize the output of:
import os
print os.popen("ps -p %d -oargs=" % os.getpid()).read().strip()
play with getpid()
and getppid()
(for the parent). For a portable solution you need external libraries like psutil:
import psutil, os
p = psutil.Process(os.getppid())
print p.name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With