A simple Python wrapper for Slurm with flexibility in mind.
Project description
Simple Slurm
A simple Python wrapper for Slurm with flexibility in mind
from simple_slurm import Slurm
slurm = Slurm(
array=range(3, 12),
cpus_per_task=15,
job_name='name',
dependency=dict(after=65541, afterok=34987),
output=f'{Slurm.JOB_ARRAY_MASTER_ID}_{Slurm.JOB_ARRAY_ID}.out',
)
slurm.sbatch('python demo.py ' + Slurm.SLURM_ARRAY_TASK_ID)
The above snippet is equivalent to running the following command:
sbatch << EOF
#!/bin/sh
#SBATCH --array 3-11
#SBATCH --cpus-per-task 15
#SBATCH --dependency after:65541,afterok:34987
#SBATCH --job-name name
#SBATCH --output %A_%a.out
python demo.py \$SLURM_ARRAY_TASK_ID
EOF
See https://slurm.schedmd.com/sbatch.html for details on the commands.
To inspect the generated script
print(slurm)
before thesbatch
call
Contents
Installation instructions
From PyPI
pip install simple_slurm
From Conda
conda install -c arturo.mendoza.quispe simple_slurm
From git
pip install git+https://github.com/amq92/simple_slurm.git
Many syntaxes available
slurm = Slurm('-a', '3-11')
slurm = Slurm('--array', '3-11')
slurm = Slurm('array', '3-11')
slurm = Slurm(array='3-11')
slurm = Slurm(array=range(3, 12))
slurm.add_arguments(array=range(3, 12))
slurm.set_array(range(3, 12))
All these arguments are equivalent! It's up to you to choose the one(s) that best suits you needs.
"With great flexibility comes great responsability"
You can either keep a command-line-like syntax or a more Python-like one
slurm = Slurm()
slurm.set_dependency('after:65541,afterok:34987')
slurm.set_dependency(dict(after=65541, afterok=34987))
Each one the available arguments have their own setter method
(ex. set_dependency
).
Using configuration files
Let's define the static components of a job definition in a YAML file default.slurm
cpus_per_task: 15
job_name: 'name'
output: '%A_%a.out'
Including these options with the using the yaml
package is very simple
import yaml
from simple_slurm import Slurm
slurm = Slurm(**yaml.load(open('default.slurm')))
...
slurm.set_array(range(NUMBER_OF_SIMULATIONS))
The job can be updated according to the dynamic project needs (ex. NUMBER_OF_SIMULATIONS
).
Job dependencies
The sbatch
call prints a message if successful and returns the corresponding job_id
job_id = slurm.sbatch('python demo.py ' + Slurm.SLURM_ARRAY_TAKSK_ID)
If the job submission was successful, it prints:
Submitted batch job 34987
And returns the variable job_id = 34987
, which can be used for setting dependencies on subsequent jobs
slurm_after = Slurm(dependency=dict(afterok=job_id)))
Additional features
For convenience, Filename Patterns and Output Environment Variables are available as attributes of the Simple Slurm object.
See https://slurm.schedmd.com/sbatch.html for details on the commands.
from slurm import Slurm
slurm = Slurm(output=('{}_{}.out'.format(
Slurm.JOB_ARRAY_MASTER_ID,
Slurm.JOB_ARRAY_ID))
slurm.sbatch('python demo.py ' + slurm.SLURM_ARRAY_JOB_ID)
This example would result in output files of the form 65541_15.out
.
Here the job submission ID is 65541
, and this output file corresponds to the submission number 15
in the job array. Moreover this index is passed to the Python code demo.py
as an argument.
Note that they can be accessed either as
Slurm.<name>
orslurm.<name>
, hereslurm
is an instance of theSlurm
class.
Filename Patterns
sbatch
allows for a filename pattern to contain one or more replacement symbols.
They can be accessed with Slurm.<name>
name | value | description |
---|---|---|
JOB_ARRAY_MASTER_ID | %A | job array's master job allocation number |
JOB_ARRAY_ID | %a | job array id (index) number |
JOB_ID_STEP_ID | %J | jobid.stepid of the running job. (e.g. "128.0") |
JOB_ID | %j | jobid of the running job |
HOSTNAME | %N | short hostname. this will create a separate io file per node |
NODE_IDENTIFIER | %n | node identifier relative to current job (e.g. "0" is the first node of the running job) this will create a separate io file per node |
STEP_ID | %s | stepid of the running job |
TASK_IDENTIFIER | %t | task identifier (rank) relative to current job. this will create a separate io file per task |
USER_NAME | %u | user name |
JOB_NAME | %x | job name |
PERCENTAGE | %% | the character "%" |
DO_NOT_PROCESS | \\ | do not process any of the replacement symbols |
Output Environment Variables
The Slurm controller will set the following variables in the environment of the batch script.
They can be accessed with Slurm.<name>
.
name | description |
---|---|
SLURM_ARRAY_TASK_COUNT | total number of tasks in a job array |
SLURM_ARRAY_TASK_ID | job array id (index) number |
SLURM_ARRAY_TASK_MAX | job array's maximum id (index) number |
SLURM_ARRAY_TASK_MIN | job array's minimum id (index) number |
SLURM_ARRAY_TASK_STEP | job array's index step size |
SLURM_ARRAY_JOB_ID | job array's master job id number |
... | ... |
See https://slurm.schedmd.com/sbatch.html for a complete list.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for simple_slurm-0.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df6a2980099190b007b33d26563b5c6234b65a089fddea9e97be69ceb1c39bd9 |
|
MD5 | adeb178cf251ad8f2f51aa334eeb4d46 |
|
BLAKE2b-256 | 653b928792c879120c2ced3e93cc9c79e4ebfb76749abaa0a7136a0e5fe3d450 |