1

I'm trying to make a slurm file that runs a python script on tens/hundreds of input files. Each process is single-threaded and totally independent from each other and I want to speed things up by running them simultaneously.

Currently I'm doing

#SBATCH --nodes=1 --ntasks-per-node=1 --time=0:20:00
python3 xxx.py -i /path/input1

and I want to change it to something like

python3 xxx.py -i /path/*input

How should I change it so that all process could run in parallel?

2 Answers 2

1

Try the following

#SBATCH --nodes=1 --time=0:20:00
for i in {1..100}
do
    srun -n 1 python3 xxx.py -i /path/input$i &
done
wait

This will call your python script for the files ending with the name (1-100) i.e. /path/input1 till /path/input100. Also, specify the --ntasks-per-node as the maximum allowed (eg: number of cpus).

The idea here is you can do scripting inside your sbatch job script. The for loop will call the srun multiple times with different arguments (& ensures that srun will run in background without blocking). wait at the end will make the script wait till all the background jobs are finished. You can use variety of techniques to achieve the same objective.

Also you can use job array to do the same more elegantly.

Sign up to request clarification or add additional context in comments.

2 Comments

will the srun wait for the task to finish, or it will just launch 100 sruns without waiting for the individual jobs to finish?
No since we have put '&' after the 'srun' command (It will run in the background). To wait till all the background jobs are finished, we can use the 'wait' command. I have updated the answer. If you want srun to block/wait till the end of the tasks, just remove &. But then we cannot run multiple different tasks in parallel.
0

There are several ways to achieve this. Personally, I use Hydra to parameterise anything in my python file. Then for parallel tasks I use the Joblib plugin.

Here is a minimal. You can manage number of CPU and GPU, environment variables directly from the config file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.