-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.sh
216 lines (186 loc) · 4.98 KB
/
functions.sh
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
function join_by {
paste -sd "$1" -
}
function prompt {
printf "$@" 1>&2
}
function confirm {
if $ALWAYS_YES; then
return
fi
if $TEST; then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi
read -p "Are you sure? " -n 1 -r
echo 1>&2
if [[ ! $REPLY =~ ^[Yy]$ ]] ; then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
fi
}
function make_batch_list_all {
local step="$1" collection="$2" lang="$3"
local batch_list=${COLLECTIONS[$collection]}-batches/${step}.${lang}
if [[ ! -d ${COLLECTIONS[$collection]}-batches ]]; then
mkdir ${COLLECTIONS[$collection]}-batches
fi
if $FORCE_INDEX_BATCHES || [[ ! -f ${COLLECTIONS[$collection]}-batches/${lang} ]]; then
find ${COLLECTIONS[$collection]}-shards/${lang} \
-mindepth 2 \
-maxdepth 2 \
-type d \
-regex '.*/[0-9]+/[0-9]+' \
> ${COLLECTIONS[$collection]}-batches/${lang}
fi
rm -f ${batch_list}
ln -s ${COLLECTIONS[$collection]}-batches/${lang} ${batch_list}
echo ${batch_list}
}
function batch_file_is_outdated {
local batch="$1" out_file="$2"
shift 2
for in_file in "$@"; do
for match in $batch/$in_file; do
if [[ "$match" -nt "$batch/$out_file" ]]; then
echo "$match is newer than $batch/$out_file" > /dev/stderr
return 0
fi
done
done
return 1
}
function make_batch_list_retry {
local step="$1" collection="$2" lang="$3" output_file="$4" in_files="${@:5}"
local batch_list_retry=${COLLECTIONS[$collection]}-batches/${step}.${lang}.$(date '+%Y%m%d%H%M%S')
cat `make_batch_list_all "$@"` | while read BATCH; do
output="${BATCH}/${output_file}"
# Note: $in_files not quoted here because if it would be empty, that would result in an empty argument (but an argument nontheless)
if [[ ! -e $output ]] || batch_file_is_outdated "$BATCH" "$output_file" $in_files; then
echo "$output" 1>&2
echo "$BATCH"
fi
done > $batch_list_retry
echo ${batch_list_retry}
}
function make_batch_list {
if $RETRY; then
make_batch_list_retry "$@"
else
make_batch_list_all "$@"
fi
}
function make_job_list {
local n_batches=$(< "$1" wc -l)
local n=$(( ${n_batches}%${TASKS_PER_BATCH} ? ${n_batches}/${TASKS_PER_BATCH} + 1 : ${n_batches}/${TASKS_PER_BATCH} ))
if [ "$n" -gt 0 ]; then
echo 1-${n}
fi
}
function schedule {
local options=(
${SCHEDULE_OPTIONS[@]}
--nodes 1
--ntasks ${SLURM_TASKS_PER_NODE:-$TASKS_PER_BATCH}
--verbose
--export ALL
)
local job_id=$(${SBATCH:-sbatch} "${options[@]}" "$@")
echo $(date +%Y%m%d%H%M%S) $job_id "${options[@]}" "$@" >> ./.schedule-log
echo $job_id
}
declare -g TEST=false
declare -g RETRY=false
declare -g FORCE_INDEX_BATCHES=false
declare -g ALWAYS_YES=${ALWAYS_YES:-false}
declare -g SCHEDULE_OPTIONS=(--parsable)
declare -g STEPS=""
while (( "$#" )); do
case "$1" in
-f|--force)
FORCE_INDEX_BATCHES=true
shift
;;
-r|--retry)
RETRY=true
shift
;;
-t|--test)
TEST=true
shift
;;
-y|--yes)
ALWAYS_YES=true
shift
;;
-j|--threads)
export THREADS=$2
shift 2
;;
-i|--interactive)
SBATCH=$SCRIPTS/fake-sbatch.sh
shift
;;
-t|--time)
SCHEDULE_OPTIONS=("${SCHEDULE_OPTIONS[@]}" --time "$2")
shift 2
;;
--afterany)
SCHEDULE_OPTIONS=("${SCHEDULE_OPTIONS[@]}" -d "afterany:$2")
shift 2
;;
--afterok)
SCHEDULE_OPTIONS=("${SCHEDULE_OPTIONS[@]}" -d "afterok:$2")
shift 2
;;
--aftercorr)
SCHEDULE_OPTIONS=("${SCHEDULE_OPTIONS[@]}" -d "aftercorr:$2")
shift 2
;;
--mem-per-cpu)
SCHEDULE_OPTIONS=("${SCHEDULE_OPTIONS[@]}" --mem-per-cpu $2)
shift 2
;;
--exclusive)
SCHEDULE_OPTIONS=("${SCHEDULE_OPTIONS[@]}" --exclusive)
shift
;;
--steps)
IFS='-' read -a seq_args <<< "$2"
STEPS=$(seq ${seq_args[@]})
shift 2
;;
--)
shift
break
;;
-h|--help)
echo "Available options"
echo " -j | --threads n Specify number of threads, mostly for interactive stuff."
echo " -t | --test Just run all checks, don't schedule."
echo " -r | --retry Retry batches for which no output was found."
echo " -f | --force Force (re)scanning of shards/batches/files."
echo " -t | --time t Override walltime limit for individual jobs."
echo " -i | --interactive Run jobs here and now in this process."
echo " --after job-id Run this job after prev job ended (however that happend)."
echo " --afterok job-id Run this job after prev job has finished."
echo " --aftercorr job-id Run each of the job array tasks after their counterpart."
echo " has finished."
if [[ $(type -t describe_cli_options) == "function" ]]; then
describe_cli_options;
fi
exit 0
;;
-*|--*)
# Allow each of the scheduling scripts to also define options by defining
# a "parse_cli_option" function before including functions.sh
if [[ $(type -t parse_cli_option) == "function" ]]; then
parse_cli_option "$@"
else
echo "Uknown option $1" 1>&2
exit 1
fi
;;
*)
break
;;
esac
done