I'm a new user of Slurm and mpi4py so I wanted to test a little code I found here : https://researchcomputing.princeton.edu/mpi4py
My python code test.py
is the following :
from mpi4py import MPI
import sys
def print_hello(rank, size, name):
msg = "Hello World! I am process {0} of {1} on {2}.\n"
sys.stdout.write(msg.format(rank, size, name))
if __name__ == "__main__":
size = MPI.COMM_WORLD.Get_size()
rank = MPI.COMM_WORLD.Get_rank()
name = MPI.Get_processor_name()
print_hello(rank, size, name)
And my bash script is :
#!/bin/bash
#SBATCH --job-name=mpi4py-test
#SBATCH --nodes=1
#SBATCH --ntasks=3
#SBATCH --cpus-per-task=1
srun python test.py
When running sbatch run.sh
I expected to get something like :
Hello World! I am process 0 of 3 on node1.
Hello World! I am process 1 of 3 on node1.
Hello World! I am process 2 of 3 on node1.
However I get :
Hello World! I am process 0 of 1 on node1.
Hello World! I am process 0 of 1 on node1.
Hello World! I am process 0 of 1 on node1.
And if I change srun python test.py
by srun mpiexec -n 3 python test.py
I get :
Hello World! I am process 0 of 3 on node1.
Hello World! I am process 2 of 3 on node1.
Hello World! I am process 1 of 3 on node1.
Hello World! I am process 1 of 3 on node1.
Hello World! I am process 0 of 3 on node1.
Hello World! I am process 2 of 3 on node1.
Hello World! I am process 0 of 3 on node1.
Hello World! I am process 1 of 3 on node1.
Hello World! I am process 2 of 3 on node1.
The process was executed 3 times but I just want it to be executed once. Thanks in advance.
It can be because either
Running
will probably get you what you want.