Skip to content

Instantly share code, notes, and snippets.

@nmpowell
Last active July 22, 2016 14:48
Show Gist options
  • Save nmpowell/50be670cd0c0604ba4f4ad49a7941f64 to your computer and use it in GitHub Desktop.
Save nmpowell/50be670cd0c0604ba4f4ad49a7941f64 to your computer and use it in GitHub Desktop.
Some Sun Grid Engine shortcuts to include in .bashrc
# .bashrc
# simple qsub: assings 8GB memory for 1 hour. Call with: $ nq8 script.sh
alias nq8='qsub -l h_rt=01:00:00 -l tmem=8.0G -l h_vmem=8.0G -l vf=4.0 -l s_stack=10240 -R y -j y -S /bin/csh -b y -cwd -V'
# Shell function to echo the current time along with status updates
echoStatusTime() {
STATUS_TEXT=$1
NOW=$(date +%T)
DATE=$(date +%F)
echo "[${DATE} ${NOW}] ${STATUS_TEXT}"
}
function qdm() {
# Delete a numbered range of jobs
delfirst=$1
dellast=$2
deldiff=$[dellast - delfirst]
echo "deleting multiple queued jobs: ${delfirst} through ${dellast} ..."
for i in `seq 0 ${deldiff}`; do delthisnum=$[delfirst + i]; qdel ${delthisnum}; done
echo "... completed $[deldiff + 1] deletions."
}
# Shell function to check and make directories if they don't already exist
makeDirs() {
# Check directories
for dirToMake in "$@"
do
if [ ! -d ${dirToMake} ]
then
mkdir ${dirToMake}
echoStatusTime "Created directory, ${dirToMake}"
fi
# if it still doesn't exist...
if [ ! -d ${dirToMake} ]
then
echoStatusTime "Unable to create the ${dirToMake} folder!"
fi
done
sleep 0
}
# Compute a script's total runtime with this and completeFunc
time_start=`date +%s`
completeFunc() {
time_end=`date +%s`
time_diff=$(( ${time_end} - ${time_start} ))
echo ""
echo "*******************************************************************************"
echoStatusTime "Complete. Took ${time_diff} seconds total."
echo "*******************************************************************************"
echo ""
}
# Generate a random number for qsubFunc which is consistent for the whole run of the calling script. This doesn't need to be referenced in the calling script; it is appended to job names in qsubFunc. Hopefully this means simultaneous runs of the same script won't conflict.
randNum=$[ ( $RANDOM % 1000 ) ]
logDir=/home/${USER}
# Function to generate a qsub. This can be run and submitted in one line with eg. `qsubFunc 2 12 echo Hello world`. The user can specify the name of the job and the name of a previous job to wait for. Subsequent qsubFuncs can be run with $4 == $name_string to wait for _this_ job. If the logDir hasn't been created then output will be into the current directory.
#
# Eg. Test 1: > qsubFunc 2 8 "name_one" # sets up the function
# > ${QSUB_CMD} echo "Hello world" # runs it
# Test 2: > `qsubFunc 2 8 "name_two" ${wait_job_name}` echo "Goodbye" # runs dependent job.
# If nothing depends on run 1 then it may also be called in one line with `quotes` as run 2. The `quotes` cause ${this_name} to be unavailable.
#
# ${timeNow} is now included below: there's no need for the job -Name to have it ($3 cannot begin with a number anyway) and it complicates matters if ${wait_job_name} doesn't include it anyway. So using it for the -output file name alone. This also cleans up scripts using this function. The general format for -Name should be -N script_purpose_image_name_${timeNow} as image names could begin with a number as well.
# NB wait_job_name seems to be persistent between scripts. Another issue is that, in loops, if the first qsub in the loop has no wait_job_name but is skipped (if the output already exists, say), then the second qsub (with a wait_job_name) will just wait for the last job in the previous loop iteration. Have to clear wait_job_name="" explicitly at the beginning of the loop or use specific job names, in this case.
qsubFunc() {
hrs=$1
mem=$2
# Needs to be reset otherwise it is persistent between jobs!
unset hold_name_string
# Set default number of cores to use and wait for (more: longer wait)
numCores=4
if [ $# -gt 2 ]
then
name_string="-N $3_${randNum}"
# subsequent jobs can use this variable to wait for this job (if this function is called outside of ``s). If this is used, _${randNum} will be appended by hold_name_string, so no need to add it here.
wait_job_name="$3"
if [ -d "${logDir}" ]
then
timeNow=`date +"%F-%H%M%S"`
outfile_name_string="-o ${logDir}/${timeNow}_${3}.txt"
error_name_string="-e ${logDir}/${timeNow}_${3}_error.txt"
fi
if [ $# -gt 3 ]
then
hold_name_string="-hold_jid $4_${randNum}"
fi
fi
# Floating-point division of memory between cores
memresult=$(echo "$mem / $numCores" | bc -l)
mem=${memresult:0:4}
QSUB_CMD="qsub -l h_rt=0${hrs}:00:00 -l tmem=${mem}G -l h_vmem=${mem}G -l s_stack=10240 -R y -j y -S /bin/csh -b y -wd /home/${USER} -pe smp ${numCores} -V ${hold_name_string} ${outfile_name_string} ${name_string}"
echo ${QSUB_CMD}
}
# To submit Matlab scripts, for example, see matlab_np.sh (also in Gist)
# qsubFunc 2 10 "job_name"
# ${QSUB_CMD} ${SCRIPTS_DIR}/matlab_np.sh matlab_scipt_name argument1 arg2 arg3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment