Example 3. Another sbatch example
Sbatch file
This is the run.sh
.
#!/bin/bash
#SBATCH --time=00:50:00
#SBATCH --ntasks=10 # 10 tasks per job
#SBATCH --cpus-per-task=1
#SBATCH --mem=15000M # Total memory for the job (1500M * 10 tasks)
#SBATCH --partition=serc
#SBATCH --array=0-43 # Array from index 0 to 43, creating 44 jobs in total
#SBATCH -o /scratch/users/bastudil/ignore/%j_%a_slurm.out # Separate STDOUT for each array task
#----------------
# Load any modules or environments with preinstalled requirements
module load python/3.9.0
source "/home/users/bastudil/myproject_env/bin/activate"
#----------------
# Define the custom array of PIDs
# myPID=(0 1 2 3 4 5 6 7 8 9 10)
myPID=()
for i in {0..2000}; do
myPID+=($i) # Adds each number from 0 to 2000 into the array
done
#----------------
# Loop through each task manually to ensure each gets a unique PID
for task_id in $(seq 0 $((SLURM_NTASKS - 1))); do
# Calculate a unique index for each task within the array job
index=$((SLURM_ARRAY_TASK_ID * SLURM_NTASKS + task_id))
# Check to ensure index is within bounds of myPID array
if (( index < ${#myPID[@]} )); then
#----------------
# Current pid
pid=${myPID[$index]}
#----------------
# Log detailed information about the job and task
now1=$(date +"%T")
echo "Start time for job $SLURM_ARRAY_TASK_ID, task $task_id (manually assigned). PID $pid: $now1"
#----------------
# filenames
python_console="/scratch/users/bastudil/ignore/${SLURM_JOB_ID}_${pid}_python_console.log"
python_error="/scratch/users/bastudil/ignore/${SLURM_JOB_ID}_${pid}_python_error.log"
python_pyinstrument="/scratch/users/bastudil/ignore/${SLURM_JOB_ID}_${pid}_python_pyinstrument.html"
#----------------
# Run the Python script with srun and pass the unique PID
# srun --ntasks=1 --exclusive python3 run.py -myPID "$pid" &
srun --ntasks=1 --exclusive run.py -myPID "$pid" >> "$python_console" 2>> "$python_error" & # prints console to specific files
# srun --ntasks=1 --exclusive pyinstrument -o $python_pyinstrument run.py -myPID "$pid" >> "$python_console" 2>> "$python_error" & # with profiling
fi
done
# Wait for all background tasks to complete
wait
Python file
This is the run.py
# You are in python now :)
import openseespy.opensees as ops
import os, sys
import time
import itertools
def yourcode():
# input arguments
Arg1 = sys.argv[1]
Arg2 = sys.argv[2]
# your python code here
# You are welcome
def main():
print("run.py started at: ",datetime.datetime.now())
yourcode()
print("run.py ended at: ",datetime.datetime.now())
if __name__ == "__main__":
main()