How do I find the number of cores available to MPI(4PY)?
Motivation
My Python program spawns MPI instances hierarchically.
The first spawn always happens and creates 4 instances. It doesn't make sense to increase this number due to the structure of my computations, so I hardcoded it.
Depending on the command line options to the main program, each of the 4 instances then call external Python software that scales almost linearly.
I call this external software using
N=3
child=MPI.COMM_SELF.Spawn(sys.executable,args=[`external.py`],maxprocs=N)
At the moment, I use N=3
so that the 4 instances of the first spawn each spawn 3 instances of the external program, which yields a total of 12 instances, matching the number of cores on my workstation.
However, for portability, I would like to do
N_avail = <MPI.N_CORES> #on my workstation: N_avail=12
N = N_avail/MPI.COMM_WORLD.Get_size() #on my workstation: N=12/4=3
so that the number of available cores needn't be hardcoded.
Is this possible, and does it make sense?
Notes
I had hoped that not specifying maxprocs would do the job, just as mpirun
with out -np
spawns as many instances as available cores. However, Spawn
then defaults to maxprocs=1
.
The call of the external library is blocking, which is why I don't (wouldn't) subtract the 4 instances from the first spawn from N_avail
.
I can't just use multiprocessing.cpu_count()
, since this would only give me the cores on the current node (in a cluster setting). I am planning to run my code on a cluster using a SLURM scheduler.
$SLURM_NTASKS
to figure out how many slots were allocated. – Nephew