-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.sh
More file actions
executable file
·302 lines (267 loc) · 10.6 KB
/
generate.sh
File metadata and controls
executable file
·302 lines (267 loc) · 10.6 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env bash
# THIS SCRIPT GENERATES SBATCH SCRIPTS FROM TEMPLATE
# FILE `run.sh.template`.
# THE OUTPUT FILE FORMAT IS `%benchsuite%-%benchname%.sh`
# Format:
# - BENCHSUITE = ''
# - BENCHNAME = ''
# - MODE=AUTO optional, choices: AUTO, MANUAL, MAKE; default is AUTO
# - COMPILER='' set teh compiler to be used
# - TIMELIMIT=MINUTES optional, defaults to 60 minutes
# - SOURCES = () note, in the case of MAKE, the first source is assumend to be
# a directory containing the Makefile. Any further list directories
# are only copied over, not traversed!
# - INPUTS = () optional, if give all data is copied to temp
# - TARGETS = () optional, defaults to TARGETS = ('seq')
# - VARIENTS = () optional, defaults to VARIENTS = ('default')
# - BUILDFLAGS = () is converted to BUILDFLAGS_default
# or BUILDFLAGS_varient = ()
# - RUNFLAGS = '' optional
# - REPRANGE = () optional, contains a start, end, and increment value (think seq)
# - STDINS = '' optional
shopt -s nullglob
FORCE=false
OVERRIDE=false
PROFDIR="${PWD}"
RDIR="${PWD}"
# FIXME we want to point this to something more generic
TDIR="sac-template"
TARGETS=()
VARIANTS=()
VERBOSITY=0
DOSYSINFO=false
readonly _batchtypes=('slurm' 'pbs')
BATCHSYS=${_batchtypes[0]}
readonly VERSION='0.7.0'
while getopts "vhfib:d:V:r:t:T:" flag; do
case $flag in
b)
if [[ "${_batchtypes[*]}" =~ ${OPTARG} ]]; then
BATCHSYS="${OPTARG}"
else
echo "Value \`${OPTARG}' is not valid batch system type!" >&2
exit 12
fi
;;
d)
PROFDIR="${OPTARG}"
;;
f)
FORCE=true
;;
i)
DOSYSINFO=true
break
;;
r)
RDIR="$(readlink -f -- "${OPTARG}")"
;;
t)
TDIR="$(readlink -f -- "${OPTARG}")"
;;
T) # restrict targets
TARGETS+=("${OPTARG}")
OVERRIDE=true
;;
v) # defaults to level 3 (warn)
(( VERBOSITY += 1 ))
;;
V) # restrict varients
VARIANTS+=("${OPTARG}")
OVERRIDE=true
;;
h)
;&
?)
echo "Usage: $0 [-h|-f|-v...] [-b bsys] [-r dir] [-t dir] [-T target] [-V variant] [-d dir]" >&2
echo "version: ${VERSION}" >&2
echo "" >&2
echo "Generate batch system compatible scripts based upon profile files and a script template" >&2
echo "" >&2
echo "More help:" >&2
printf "%5s %s\\n" "-b" "batch system to generate for (default: slurm, choices: slurm, pbs)" >&2
printf "%5s %s\\n" "-d" "directory will profile file(s)" >&2
printf "%5s %s\\n" "-f" "force overwrite of existing sbatch scripts" >&2
printf "%5s %s\\n" "-h" "this help message and exit" >&2
printf "%5s %s\\n" "-i" "display system info and exit" >&2
printf "%5s %s\\n" "-r" "root directory (where bench-runner files are)" >&2
printf "%5s %s\\n" "-t" "directory with the sbatch template" >&2
printf "%5s %s\\n" "-T" "restrict profile generation to specified target" >&2
printf "%5s %s\\n" "-v" "increase verbosity (specify multiple times for higher verbosity)" >&2
printf "%5s %s\\n" "-V" "restrict profile generation to specified variant" >&2
exit 0
;;
esac
done
if [ ! -d "${RDIR}/modules" ]; then
echo "Can't find shell modules! Exiting..." >&2
exit 1
fi
# get external functions
for s in ${RDIR}/modules/*.bash; do
# shellcheck source=/dev/null
source "${s}"
done
# initiat logging
loginit $VERBOSITY
if [ "${DOSYSINFO}" = true ]; then
sysinfo
exit 0
fi
if [ ! -d "${RDIR}/${TDIR}" ]; then
critical "Template dir could not be found! Exiting..."
exit 1
fi
# again we through generics out of the window...woo!
if [ ${#TARGETS[@]} -eq 0 ]; then
TARGETS=('seq')
fi
if [ ${#VARIANTS[@]} -eq 0 ]; then
VARIANTS=('default')
fi
if [ ! -d "${PROFDIR}" ]; then
critical "Profile dir could not be found! Exiting..."
exit 1
fi
PROFILES=( "${PROFDIR}"/*.profile )
# FIXME again we ignore generics for the moment
#COMPILER=$(command -v sac2c_p) || critical "Unable to locate sac2c_p binary, exiting..."
if [ ${#PROFILES[@]} -eq 0 ]; then
critical "No profiles found! Exiting..."
exit 1
fi
# we compile the bash modules into a single file
BMODF="$(mktemp)"
cat "${RDIR}"/modules/*.bash > "${BMODF}"
for profile in "${PROFILES[@]}"; do
declare -A CURRENTPROFILE
# shellcheck disable=SC2162
while IFS='=' read key value; do
CURRENTPROFILE[$key]=$value
debug "[$key] = $value"
done < <(sed -e 's/\(^#.*\|[^\\]#.*\)//' -e 's/\&/\\\\&/g' -e 's/\\$/\\\\n\\/' -e '/^\s*$/d' "$profile")
# remove handle comments (we can still
# escape (\#) hash symbol and ampersand)
BUILD_MANUAL=false
BUILD_MAKE=false
skip=false
FULL_NAME="${CURRENTPROFILE[BENCHSUITE]//\'}-${CURRENTPROFILE[BENCHNAME]//\'}"
SCRIPT_NAME="${FULL_NAME}.run.sh"
# the timelimit is set to two and a half days (in minutes)
TIMELIMIT="3880"
# get time limit
if [ -n "${CURRENTPROFILE[TIMELIMIT]}" ]; then
TIMELIMIT="${CURRENTPROFILE[TIMELIMIT]//\'}"
fi
# get build mode
if [ "x${CURRENTPROFILE[MODE]//\'}" = "xMANUAL" ]; then
BUILD_MANUAL=true
CURRENTPROFILE[MODE]="'MANUAL'"
elif [ "x${CURRENTPROFILE[MODE]//\'}" = "xMAKE" ]; then
BUILD_MAKE=true
CURRENTPROFILE[MODE]="'MAKE'"
CURRENTPROFILE[COMPILER]="'make'"
else
CURRENTPROFILE[MODE]="'AUTO'"
fi
# check that we have the compiler set
if [ -z "${CURRENTPROFILE[COMPILER]}" ]; then
# we'll go back to generics later
error "Can't generate script without \`COMPILER' being defined. Skipping '${SCRIPT_NAME}'"
skip=true
#CURRENTPROFILE[COMPILER]="'${COMPILER}'"
fi
# check that we have build for MANUAL mode
if [ -z "${CURRENTPROFILE[BUILD]}" ] && [ "$BUILD_MANUAL" = true ]; then
error "Can't generate script with manual build without \`BUILD' function. Skipping '${SCRIPT_NAME}'"
skip=true
elif [ -n "${CURRENTPROFILE[BUILD]}" ] && [ "$BUILD_MANUAL" = false ]; then
warn "Profile specifies BUILD but is in AUTO mode!"
unset 'CURRENTPROFILE[BUILD]'
fi
# check that we have run for MANUAL mode
if [ -z "${CURRENTPROFILE[RUN]}" ] && [ "$BUILD_MANUAL" = true ]; then
error "Can't generate script with manual build without \`RUN' function. Skipping '${SCRIPT_NAME}'"
skip=true
elif [ -n "${CURRENTPROFILE[RUN]}" ] && [ "$BUILD_MANUAL" = false ]; then
warn "Profile specifies RUN but is in AUTO mode!"
unset 'CURRENTPROFILE[RUN]'
fi
# check that we have REPRANGE to define the range, default is 5 iterations
if [ -z "${CURRENTPROFILE[REPRANGE]}" ]; then
CURRENTPROFILE[REPRANGE]="('1' '5' '1')"
fi
if [ "$skip" = false ]; then
if [ "${BUILD_MANUAL}" = true ]; then
# we unset most key-value pairs
IFS=',' read -r -a varients <<< "$(sed -e "s/' /',/g" -e "s/[()]//g" <<< "${CURRENTPROFILE[VARIENTS]}")"
for varient in "${varients[@]}"; do
unset "CURRENTPROFILE[BUILDFLAGS_${varient//\'}]"
done
unset 'CURRENTPROFILE[TARGETS]'
unset 'CURRENTPROFILE[VARIENTS]'
else
# set default target
if [ -z "${CURRENTPROFILE[TARGETS]}" ] || [ "$OVERRIDE" = true ]; then
_t=$(printf "'%s' " "${TARGETS[@]}")
# we need to remove the trailing space
CURRENTPROFILE[TARGETS]="(${_t% })"
fi
# set default varient
if [ -z "${CURRENTPROFILE[VARIENTS]}" ] || [ "$OVERRIDE" = true ]; then
_t=$(printf "'%s' " "${VARIANTS[@]}")
# we need to remove the trailing space
CURRENTPROFILE[VARIENTS]="(${_t% })"
fi
IFS=',' read -r -a varients <<< "$(sed -e "s/' /',/g" -e "s/[()]//g" <<< "${CURRENTPROFILE[VARIENTS]}")"
IFS=',' read -r -a targets <<< "$(sed -e "s/' /',/g" -e "s/[()]//g" <<< "${CURRENTPROFILE[TARGETS]}")"
# copy build flags
for varient in "${varients[@]}"; do
if [ -z "${CURRENTPROFILE[BUILDFLAGS_${varient//\'}]}" ]; then
CURRENTPROFILE[BUILDFLAGS_${varient//\'}]="${CURRENTPROFILE[BUILDFLAGS]}"
fi
if [ "${#targets[@]}" -gt 1 ]; then
IFS=',' read -r -a buildflags <<< "$(sed -e "s/' /',/g" -e "s/[()]//g" <<< "${CURRENTPROFILE[BUILDFLAGS_${varient//\'}]}")"
for (( i=0; i<${#targets[@]}; i++ )); do
if [ -z "${buildflags[${i}]}" ]; then
buildflags[${i}]="${buildflags[0]}"
fi
done
CURRENTPROFILE[BUILDFLAGS_${varient//\'}]="$(printf "(%s)" "${buildflags[*]}")"
fi
done
# remove default buildflag
unset 'CURRENTPROFILE[BUILDFLAGS]'
fi
PROFILEOUT=""
for key in "${!CURRENTPROFILE[@]}"; do
if [ "x$key" = "xBUILD" ] || [ "x$key" = "xRUN" ]; then
# output function and body
PROFILEOUT+=$(printf "%s(){%s}\\\\n" "${key,,}" "${CURRENTPROFILE[$key]//\'}")
else
# output key-value pair as is
PROFILEOUT+=$(printf "%s=%s\\\\n" "$key" "${CURRENTPROFILE[$key]}")
fi
done
# generate sbatch script
if [ ! -f "${SCRIPT_NAME}" ] || [ "${FORCE}" = true ]; then
sed -e "/## BATCH/ r ${RDIR}/${TDIR}/${BATCHSYS}.in" -e "/## ENVMODULES/ r ${RDIR}/${TDIR}/envmodules.in" \
-e "/## GLOBALS/ r ${RDIR}/${TDIR}/globals.in" -e "/## BASHMODULES/ r ${BMODF}" \
-e "/## SCRIPT/ r ${RDIR}/${TDIR}/script.sh.in" "${RDIR}/basic-template.sh.in" |\
sed -e "s:@NAME@:${FULL_NAME}:" -e "s:@TIMELIMIT@:${TIMELIMIT}:" \
-e "s:@PROFILE@:${PROFILEOUT}:" -e "s:@RUNNERDIR@:${RDIR}:" -e "s:@PWD@:${PWD}:" > "${SCRIPT_NAME}"
chmod +x -- "${SCRIPT_NAME}"
else
inf "Not updating ${SCRIPT_NAME}"
fi
fi
# clear variables
unset _t
unset CURRENTPROFILE
unset PROFILEOUT
unset targets
unset varients
unset buildflags
done
# delete temp file
[ -f "${BMODF}" ] && rm "${BMODF}"