Skip to content

Instantly share code, notes, and snippets.

@serihiro
Last active June 3, 2022 09:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serihiro/33f8f775cd8ba524d7b20d08d170e69c to your computer and use it in GitHub Desktop.
Save serihiro/33f8f775cd8ba524d7b20d08d170e69c to your computer and use it in GitHub Desktop.
Get MPI rank in the executing shell script
#!/bin/bash
rank=`python get_mpi_rank.py`
if [ ${rank} -eq 0 ]; then
echo 'I am rank 0'
elif [ ${rank} -eq 1 ]; then
echo 'I am rank 1'
elif [ ${rank} -eq 2 ]; then
echo 'I am rank 2'
elif [ ${rank} -eq 3 ]; then
echo 'I am rank 3'
fi
# $ mpirun -np 4 executable.sh
# I am rank 1
# I am rank 3
# I am rank 2
# I am rank 0
from mpi4py import MPI
comm = MPI.COMM_WORLD
print(comm.Get_rank())
@guillefix
Copy link

the file name should be get_mpi_rank not get_my_rank

@musicpiano
Copy link

Thank you so much. Before I saw your code, I have no idea how to get the current rank of the bash script. Now I know I need to run a program inside the bash script to find its rank.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment