-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcqsub
executable file
·151 lines (133 loc) · 3.54 KB
/
cqsub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
# see README.cqsub
# $Id: cqsub 715 2009-12-09 17:29:03Z rybach $
if [ -h $0 ]; then
CFQ_SELF=$(readlink $0)
else
CFQ_SELF=$0
fi
source $(dirname $CFQ_SELF)/cftools.sh
CFQ_PREPARE=1
CFQ_CACHE=$(mktemp)
CFQ_FILES=$(mktemp)
CFQ_HOSTS=$(mktemp)
CFQ_BUFFER=$(mktemp)
CFQ_NEEDTASKID=$(mktemp)
CFQ_DETECT_ID=-9999999999
CFQ_TRYHARD=0
trap "rm -f $CFQ_FILES $CFQ_HOSTS $CFQ_NEEDTASKID $CFQ_CACHE $CFQ_BUFFER" EXIT
function getScriptFile()
{
qsub -verify $* | awk '/^script_file/ { print $2 }'
}
function getArrayTasks()
{
qsub -verify $* | awk '/^job-array tasks/ { print $3 }'
}
function readScript()
{
local scriptFile=$1
SGE_TASK_ID=$2
shift 2
rm -f $CFQ_HOSTS $CFQ_FILES
touch $CFQ_FILES
touch $CFQ_BUFFER
source $scriptFile
}
function submitJob()
{
local jobSubmitted=0
local queues=
local options=$*
local cmd
cat $CFQ_FILES | \
awk -F : '{ hosts[$1] += $3; } END { for (h in hosts) print h, hosts[h] }' | \
sort -n -k 2 -t " " -r > $CFQ_HOSTS
while read line; do
host=${line%% *}
queues="${queues},*@${host}"
size=${line##* }
if [ $CFQ_TRYHARD -eq 1 ]; then
cfq_log "try to submit to $host ($size bytes cached)"
cmd="qsub -now y -w e -q "*@$host" $options"
cfq_log "$cmd"
$cmd
if [ $? -eq 0 ]; then
cfq_log "job submitted"
jobSubmitted=1
break
fi
else
cfq_log "adding host $host ($size bytes cached)"
fi
done < $CFQ_HOSTS
if [ $jobSubmitted -eq 0 ]; then
if [ -n "${queues}" ]; then
cfq_log "submitting job with soft queue requirement"
cmd="qsub -soft -q ${queues:1} -hard $options"
cfq_log "$cmd"
$cmd
else
cfq_log "no cached files found"
cmd="qsub $options"
cfq_log "$cmd"
$cmd
fi
fi
}
function submitArrayTasks()
{
local taskDef=$(getArrayTasks $CFQ_COMMANDLINE)
if [ -z $taskDef ]; then
cfq_error "no job-array tasks defined"
exit 1
fi
local inc=${taskDef##*:}
local range=${taskDef%:*}
local taskStart=${range%-*}
local taskEnd=${range#*-}
local scriptName=
local newOpt=$(echo "$CFQ_COMMANDLINE " | sed -e "s/^\(.*\)\( -t [^ ]\+\)\(.* ${CFQ_SCRIPTFILE} .*\)\$/\1 \3/")
local j
cp $CFQ_FILES $CFQ_BUFFER
for ((j=${taskStart};j<=${taskEnd};j+=${inc})); do
cfq_log "processing task $j (${taskStart}-${taskEnd}:${inc})"
readScript $CFQ_SCRIPTFILE $j $CFQ_SCRIPT_PARAM
cat $CFQ_BUFFER >> $CFQ_FILES
submitJob -t $j $newOpt
done
}
function usage()
{
echo "usage: "$(basename $0)" [--try-hard] <options>"
echo " use this tool as replacement for qsub."
echo " all options are passed trough to qsub"
exit 1
}
if [ -z $1 ]; then
usage
exit 1
fi
echo 0 > $CFQ_NEEDTASKID
CFQ_COMMANDLINE=$*
# remove cqsub options
expr match "${CFQ_COMMANDLINE}" "\(.*--try-hard.*\)" &> /dev/null
if [ $? -eq 0 ]; then
CFQ_TRYHARD=1
tmp=$(echo $CFQ_COMMANDLINE | sed -e 's/^\(.*\)\(--try-hard \)\(.*\)$/\1 \3/')
CFQ_COMMANDLINE=$tmp
fi
CFQ_SCRIPTFILE=$(getScriptFile $CFQ_COMMANDLINE)
# remove qsub parameters
while [ $1 != $CFQ_SCRIPTFILE ]; do
shift
done
shift
CFQ_SCRIPT_PARAM=$*
readScript $CFQ_SCRIPTFILE $CFQ_DETECT_ID $CFQ_SCRIPT_PARAM
if [ $(cat $CFQ_NEEDTASKID) -eq 1 ]; then
cfq_log switching to job-array mode
submitArrayTasks
else
submitJob $CFQ_COMMANDLINE
fi