Search code examples
pythonbashimportglobsubmission

Using Python to Submit Multiple Jobs to a Supercomputer


I am currently using a supercomputer for various quantum mechanical calculations. There are a large number of files I need to submit at any given time (280 separate submisions). I am trying to use python to automate this process but have little experience with using python to run a separate submission script. The submission script I am using (named subOrca_mpi.sh) is:

#!/bin/bash
string=".inp"
root="${1%$string}"

#if [ -z $1 ]
#then
#  echo 'Usage: subGamess.sh <jobinput>'
#  exit 1
#fi

#if [ ! -f ./$root.inp ]
#then
#  echo '<jobinput> does not look like an Orca input \(*.inp\)'
#  exit 1
#fi

if [ -f $root.slurm ]
then
   rm $root.slurm
fi

echo '#!/bin/bash'>>$root.slurm
echo '#SBATCH --job-name='$root>>$root.slurm
echo '#SBATCH --output='$root'.out'>>$root.slurm
echo '#SBATCH --nodes=1'>>$root.slurm
echo '#SBATCH --ntasks-per-node=12 '>>$root.slurm
echo '#SBATCH --time=0-48:00:00 '>>$root.slurm
echo '#SBATCH --cluster=smp'>>$root.slurm

echo 'cd $SBATCH_O_WORKDIR'>>$root.slurm
echo 'module purge'>>$root.slurm
echo 'module load orca/3.0.3'>>$root.slurm

echo 'files=('>>$root.slurm
echo  $root'.inp'>>$root.slurm
echo ')'>>$root.slurm
echo 'for i in ${files[@]}; do'>>$root.slurm
echo '     sbcast $SLURM_SUBMIT_DIR/$i $SLURM_SCRATCH/$i'>>$root.slurm
echo 'done'>>$root.slurm

echo 'export LD_LIBRARY_PATH=/usr/lib64/openmpi-
1.10/lib:$LD_LIBRARY_PATH'>>$root.slurm
echo 'export PATH=/usr/lib64/openmpi-1.10/bin:$PATH'>>$root.slurm

echo 'cd $SLURM_SCRATCH'>>$root.slurm

echo '$(which orca) '$root'.inp'>>$root.slurm
echo 'cp $SLURM_SCRATCH/*.{gbw,prop} $SLURM_SUBMIT_DIR'>>$root.slurm

sbatch $root.slurm

exit

From what I've been told, I will need to import os, but I'm essentially lost from there. The only code I have thus far is:

import os
import glob
def orcasubmit():
    for filename in glob.glob('*.inp'):
        #execute subOrca_mpi.sh for all input files in given folder

orcasubmit()

Any input would be incredibly helpful.


Solution

  • You've two alternatives here. The first one, as you've probably guessed, is using os.system. You can do that like this:

    for filename in glob.glob('*.inp'):
        os.system('./' + filename) # assuming these files lie in the same directory
    

    Alternatively, you may use the subprocess.Popen module, and I recommend this one.

    from subprocess import Popen
    for filename in glob.glob('*.inp'):
        process = Popen(['/bin/bash', filename])