#!/bin/bash
# The line above this is the "shebang" line.  It must be first line in script
#-----------------------------------------------------
#	Example Submission Script: job array example
#	Runs a simple python script to show n-th element of the
#	Fibonacci sequence
#-----------------------------------------------------
#
#	Job array, with 10 elements (0 through 9)
#SBATCH --array=0-9%2
#	Sequential code, 1 core per job array element
#SBATCH -c 1
#	Request 5 minutes of walltime per job array element
#SBATCH -t 10
#	Allow other jobs to run on same node
#SBATCH --oversubscribe
#	Run on debug partition for rapid turnaround.  You will need
#	to change this (remove the line) if walltime > 15 minutes
#SBATCH --partition=debug

# Process our dot files (and in particular define the "module" command)
source /etc/profile

# Module load section
# First clear our module list 
module purge
# and reload the standard modules
module load hpcc/zaratan
# Load the desired compiler, MPI, and python modules
# NOTE: You need to use the same compiler and MPI module used
# when compiling the python (and its mpi4py package).  Module load-ing
# python first, followed by openmpi will ensure you get a compatible set
module load python

# Numbers to factor, one for each job in the array
numbers_to_factor=(
	54
	1133
	400219
	400221
	400227
	400239
	6700489
	9990123
	12240321
	76340789
	)
# Get the number to factor for this job in the array
N="${numbers_to_factor[$SLURM_ARRAY_TASK_ID]}"

# Get the full path to our python executable.  It is best
# to provide full path of our executable to , etc.
MYPYTHON=`which python`
MYPYSCRIPT="${SLURM_SUBMIT_DIR}/factorize.py"

echo "Slurm job ${SLURM_JOBID} (${SLURM_ARRAY_JOB_ID} task ${SLURM_ARRAY_TASK_ID})"
echo "Slurm job {$SLURM_JOB_NAME} running on:"
hostname
echo "Ntasks=${SLURM_NTASKS}; CPU cores per task=${SLURM_CPUS_PER_TASK}"
echo "On ${SLURM_JOB_NUM_NODES} nodes: ${SLURM_JOB_NODELIST}"
date
pwd
echo "Loaded modules are:"
module list

echo "Using python $MYPYTHON"
echo "Using python script $MYPYSCRIPT"
echo "N=${N}"

# Run the python script, saving the results to slurm-JOBNUM_TASKID.out
$MYPYTHON $MYPYSCRIPT $N
# Save the exit code from the previous command
ECODE=$?

echo "Job finished with exit code $ECODE."
date

# Exit with the cached exit code
exit $ECODE
