Skip to content

Instantly share code, notes, and snippets.

@kaspermunch
Created October 19, 2020 11:55
Show Gist options
  • Select an option

  • Save kaspermunch/64e11cd21e3953295e149e75bee51733 to your computer and use it in GitHub Desktop.

Select an option

Save kaspermunch/64e11cd21e3953295e149e75bee51733 to your computer and use it in GitHub Desktop.
How to easily use multiprocessing in Python (on a SLURM cluster)
import os
from multiprocessing import Pool, cpu_count
# function you want to run in parallel:
def myfunction(a, b):
return a + b
# list of tuples to serve as arguments to function:
args = [(1, 2), (9, 11), (6, 2)]
# number of cores you have allocated for your slurm task:
number_of_cores = int(os.environ['SLURM_CPUS_PER_TASK'])
# number_of_cores = cpu_count() # if not on the cluster you should do this instead
# multiprocssing pool to distribute tasks to:
with Pool(number_of_cores) as pool:
# distribute computations and collect results:
results = pool.starmap(myfunction, args)
@afqbrito
Copy link

Thank you, very much!
It's save my day (or week, or ...)

@nmendiboure
Copy link

Thank you. I wonder how to manage that number of cpus properly in the case of multiple nodes allocation and multiple tasks with a number of cpu per task ?

For instance in slurm :
#SBATCH --ntasks=4
#SBATCH --nodes=4
#SBATCH --cpus-per-task=5

Thus a total of 4*5 = 20 cpus.

@kaspermunch
Copy link
Author

kaspermunch commented Jun 17, 2024 via email

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