| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
HOSTS= |
| 4 |
CPUS=2 |
| 5 |
CORES=4 |
| 6 |
THREADS=1 |
| 7 |
GPUS= |
| 8 |
HOST_PREFIX="node-" |
| 9 |
YAML= |
| 10 |
CREATE_PROPERTIES=1 |
| 11 |
USE_THREADS= |
| 12 |
DO_GPU= |
| 13 |
CPUSETS= |
| 14 |
CPUTOPO= |
| 15 |
GPUDEVICES= |
| 16 |
GPUTOPO= |
| 17 |
DO_REPAIR= |
| 18 |
DEBUG= |
| 19 |
|
| 20 |
usage() { |
| 21 |
cat <<EOF |
| 22 |
Usage: |
| 23 |
$0 -H <# of hosts> [other options] |
| 24 |
|
| 25 |
Generate commands to add new resources to OAR database |
| 26 |
|
| 27 |
Options: |
| 28 |
-T, --use-threads use the thread resource property |
| 29 |
-H, --hosts <#> # of hosts |
| 30 |
-C, --cpus <#> # of cpu per host |
| 31 |
-c, --cores <#> # of core per cpu |
| 32 |
-t, --threads <#> # of threads per core |
| 33 |
-g, --gpus <#> # of gpus per host |
| 34 |
--host0 <#> first host id to use |
| 35 |
--cpu0 <#> first cpu id to use |
| 36 |
--core0 <#> first core id to use |
| 37 |
--thread0 <#> first thread id to use |
| 38 |
--gpu0 <#> first gpu id to use |
| 39 |
--cpusets <#> # of cpusets/host (useful for fake setups only) |
| 40 |
--cputopo <list|RR> topology of the cpus: ordered list of the cpuset ids or RR for round robin |
| 41 |
--gpudevices <#> # of gpu devices/host (useful for fake setups only) |
| 42 |
--gpudevice-prefix <str> path prefix to use for the GPU device (e.g.: /dev/nvidia) |
| 43 |
--gputopo <list> topology of the gpus: ordered list of the gpudevice ids |
| 44 |
--host-prefix <str> hostname prefix (default: "node-") |
| 45 |
--host-suffix <str> hostname suffix (e.g. ".domain") |
| 46 |
-a, --auto-offset guess next host/cpu/core/thread/gpu ids |
| 47 |
-p, --no-create-properties do not generate oarproperty commands |
| 48 |
-A, --append <str> append a text string (extra properties) |
| 49 |
-R, --repair do not create, repair existing properties (fix cpu/gpu topologie) |
| 50 |
-o, --write-to <file> write commands to file |
| 51 |
-Y, --yaml generate YAML output |
| 52 |
-h, --help display this message |
| 53 |
--debug enable debugging messages |
| 54 |
|
| 55 |
EOF |
| 56 |
} |
| 57 |
|
| 58 |
die() { |
| 59 |
cat <<EOF 1>&2 |
| 60 |
Error: $1 |
| 61 |
|
| 62 |
EOF |
| 63 |
exit 1 |
| 64 |
} |
| 65 |
|
| 66 |
debug() { |
| 67 |
[ -n "$DEBUG" ] && echo "DEBUG: $@" 1>&2 |
| 68 |
} |
| 69 |
|
| 70 |
LONG_OPTS="hosts:,cpus:,cores:,threads:,gpus:,host-prefix:,host-suffix:,gpudevice-prefix:,host0:,cpu0:,core0:,thread0:,gpu0:,cpuset:,cpusets:,cputopo:,gpudevices:,gputopo:,append:,use-threads,write-to:,auto-offset,yaml,no-create-properties,repair,help,debug" |
| 71 |
SHORT_OPTS="H:C:c:t:g:P:S:A:o:TRaYph" |
| 72 |
args=$(getopt -l $LONG_OPTS -o $SHORT_OPTS -q -- "$@") |
| 73 |
[ $? -gt 0 ] && die "Syntax error, $(getopt -l $LONG_OPTS -o $SHORT_OPTS -Q -- "$@" 2>&1)." |
| 74 |
|
| 75 |
eval set -- "$args" |
| 76 |
|
| 77 |
while [ $# -ge 1 ]; do |
| 78 |
case "$1" in |
| 79 |
--) |
| 80 |
# No more options left. |
| 81 |
shift |
| 82 |
break |
| 83 |
;; |
| 84 |
-H|--hosts) |
| 85 |
HOSTS=$2 |
| 86 |
shift |
| 87 |
;; |
| 88 |
-C|--cpus) |
| 89 |
CPUS=$2 |
| 90 |
shift |
| 91 |
;; |
| 92 |
-c|--cores) |
| 93 |
CORES=$2 |
| 94 |
shift |
| 95 |
;; |
| 96 |
-t|--threads) |
| 97 |
THREADS=$2 |
| 98 |
shift |
| 99 |
;; |
| 100 |
-g|--gpus) |
| 101 |
GPUS=$2 |
| 102 |
shift |
| 103 |
;; |
| 104 |
-P|--host-prefix) |
| 105 |
HOST_PREFIX=$2 |
| 106 |
shift |
| 107 |
;; |
| 108 |
-S|--host-suffix) |
| 109 |
HOST_SUFFIX=$2 |
| 110 |
shift |
| 111 |
;; |
| 112 |
--gpudevice-prefix) |
| 113 |
GPUDEVICE_PREFIX=$2 |
| 114 |
shift |
| 115 |
;; |
| 116 |
--host0) |
| 117 |
HOST0=$2 |
| 118 |
shift |
| 119 |
;; |
| 120 |
--cpu0) |
| 121 |
CPU0=$2 |
| 122 |
shift |
| 123 |
;; |
| 124 |
--core0) |
| 125 |
CORE0=$2 |
| 126 |
shift |
| 127 |
;; |
| 128 |
--thread0) |
| 129 |
THREAD0=$2 |
| 130 |
shift |
| 131 |
;; |
| 132 |
--gpu0) |
| 133 |
GPU0=$2 |
| 134 |
shift |
| 135 |
;; |
| 136 |
--cpuset|--cpusets) # --cpuset kept for backward compat. |
| 137 |
CPUSETS=$2 |
| 138 |
shift |
| 139 |
;; |
| 140 |
--cputopo) |
| 141 |
CPUTOPO=$2 |
| 142 |
shift |
| 143 |
;; |
| 144 |
--gpudevices) |
| 145 |
GPUDEVICES=$2 |
| 146 |
shift |
| 147 |
;; |
| 148 |
--gputopo) |
| 149 |
GPUTOPO=$2 |
| 150 |
shift |
| 151 |
;; |
| 152 |
-A|--append) |
| 153 |
APPEND=$2 |
| 154 |
shift |
| 155 |
;; |
| 156 |
-o|--write-to) |
| 157 |
|
| 158 |
WRITE_TO=$2 |
| 159 |
shift |
| 160 |
;; |
| 161 |
-T|--use-threads) |
| 162 |
USE_THREADS=1 |
| 163 |
;; |
| 164 |
-a|--auto-offset) |
| 165 |
AUTO_OFFSET=1 |
| 166 |
;; |
| 167 |
-p|--no-create-properties) |
| 168 |
CREATE_PROPERTIES= |
| 169 |
;; |
| 170 |
-R|--repair) |
| 171 |
DO_REPAIR=1 |
| 172 |
;; |
| 173 |
-Y|--yaml) |
| 174 |
YAML=1 |
| 175 |
;; |
| 176 |
--debug) |
| 177 |
DEBUG=1 |
| 178 |
;; |
| 179 |
-h|--help) |
| 180 |
usage |
| 181 |
exit 0 |
| 182 |
;; |
| 183 |
*) |
| 184 |
usage 1>&2 |
| 185 |
exit 1 |
| 186 |
;; |
| 187 |
esac |
| 188 |
shift |
| 189 |
done |
| 190 |
|
| 191 |
[ -n "$1" ] && die "Syntax error, unknown argument: $1" |
| 192 |
|
| 193 |
[ -n "$HOSTS" ] && [ $HOSTS -gt 0 ] || die "Syntax error, need a # of host." |
| 194 |
|
| 195 |
[ -n "$DO_REPAIR" ] && [ -n "$CPUSETS" -o -n "$GPUDEVICES" ] && die "Error: cannot use -R/--repair with --cpusets/-C and --gpudevices/-G." |
| 196 |
|
| 197 |
if [ -n "$WRITE_TO" ]; then |
| 198 |
if [ -e "$WRITE_TO" ]; then |
| 199 |
echo -n > $WRITE_TO |
| 200 |
fi |
| 201 |
exec 1> >(tee -a $WRITE_TO) |
| 202 |
fi |
| 203 |
|
| 204 |
debug "HOSTS=$HOSTS" |
| 205 |
debug "CPUS=$CPUS" |
| 206 |
debug "CORES=$CORES" |
| 207 |
debug "THREADS=$THREADS" |
| 208 |
debug "GPUS=$GPUS" |
| 209 |
|
| 210 |
CPUSETS=${CPUSETS:-$((CPUS*CORES*THREADS))} |
| 211 |
debug "CPUSETS=$CPUSETS" |
| 212 |
|
| 213 |
declare -a CPUTOPO |
| 214 |
if [ -n "$CPUTOPO" ]; then |
| 215 |
if [ "$CPUTOPO" == "RR" ]; then |
| 216 |
for ((i=0;i<$CPUSETS;i++)); do |
| 217 |
# Magic formula to compute the round-robin distribution for Xeon CPUs cores and HyperThreads optionaly. |
| 218 |
# Core physical ids are distributed in round-robin on physical cpus. |
| 219 |
# Same if HyperThreading is activated: the 1st hardware threads is distributed in round-robin |
| 220 |
# and the second hardware threads as well but with an offset of +cpus*cores. |
| 221 |
# This should match lstopo output for at least some Xeon CPUs ! |
| 222 |
# WARNING: it's Euclidian arithmetic. |
| 223 |
CPUTOPO[i]=$((i/THREADS*CPUS%CPUSETS/THREADS*THREADS%(CPUSETS/THREADS) + i*CPUS/CPUSETS + i%THREADS*CPUS*CORES)) |
| 224 |
done |
| 225 |
else |
| 226 |
CPUTOPO=(${CPUTOPO//,/ }) |
| 227 |
if [ ${#CPUTOPO[@]} -ne $CPUSETS ]; then |
| 228 |
die "Bad definition of CPU topology, ${#CPUTOPO[@]} elements (${CPUTOPO[*]}) instead of $CPUSETS." |
| 229 |
fi |
| 230 |
fi |
| 231 |
else |
| 232 |
CPUTOPO=($(seq 0 $((CPUSETS-1)))) |
| 233 |
fi |
| 234 |
debug "CPUTOPO=${CPUTOPO[@]}" |
| 235 |
|
| 236 |
if [ -n "$GPUS" ] && [ $GPUS -gt 0 ]; then |
| 237 |
if [ $GPUS -eq 1 ]; then |
| 238 |
DO_GPUS="single" |
| 239 |
# Singe GPU: fix GPUS=$CPUS to that the loops are ok |
| 240 |
GPUS=$CPUS |
| 241 |
GPUDEVICES=1 |
| 242 |
elif [ $GPUS -lt $CPUS ] || [ $((GPUS % CPUS)) -ne 0 ] || [ $CORES -lt $((GPUS / CPUS)) ] || [ $((CORES % (GPUS / CPUS))) -ne 0 ]; then |
| 243 |
die "Cannot manage the requested GPU configuration ($GPUS GPUs for $CPUS CPUs and $CORES cores per CPU)." |
| 244 |
else |
| 245 |
DO_GPUS="multi" |
| 246 |
GPUDEVICES=${GPUDEVICES:-$GPUS} |
| 247 |
fi |
| 248 |
else |
| 249 |
# DO_GPUS not set, but fix GPUS=$CPUS to that the loops are ok |
| 250 |
GPUS=$CPUS |
| 251 |
GPUDEVICES=1 |
| 252 |
fi |
| 253 |
debug "DO_GPUS=$DO_GPUS => GPUS=$GPUS" |
| 254 |
debug "GPUDEVICES=$GPUDEVICES" |
| 255 |
|
| 256 |
declare -a GPUTOPO |
| 257 |
if [ -n "$GPUTOPO" ]; then |
| 258 |
GPUTOPO=(${GPUTOPO//,/ }) |
| 259 |
if [ ${#GPUTOPO[@]} -ne $GPUDEVICES ]; then |
| 260 |
die "Bad definition of GPU topology, ${#GPUTOPO[@]} elements (${GPUTOPO[*]}) instead of $GPUDEVICES." |
| 261 |
fi |
| 262 |
else |
| 263 |
GPUTOPO=($(seq 0 $((GPUDEVICES-1)))) |
| 264 |
fi |
| 265 |
debug "GPUTOPO=${GPUTOPO[@]}" |
| 266 |
|
| 267 |
if [ -n "$AUTO_OFFSET" ]; then |
| 268 |
[ -z "$HOST0" ] && echo "# Warning: guessing a new hostname is not really reliable because of a sort issue for non-numeric properties. Please double-check." |
| 269 |
HOST0=${HOST0:-$(($(oarnodesetting --last-property-value host | perl -pe 's/^[^\d]*(\d+).*/$1/') + 1))} |
| 270 |
debug "First host=$HOST0" |
| 271 |
CPU0=${CPU0:-$(($(oarnodesetting --last-property-value cpu) + 1))} |
| 272 |
debug "First cpu=$CPU0" |
| 273 |
CORE0=${CORE0:-$(($(oarnodesetting --last-property-value core) + 1))} |
| 274 |
debug "First core=$CORE0" |
| 275 |
if [ -n "$USE_THREADS" ]; then |
| 276 |
THREAD0=${THREAD0:-$(($(oarnodesetting --last-property-value thread) + 1))} |
| 277 |
debug "First thread=$THREAD0" |
| 278 |
fi |
| 279 |
if [ -n "$GPUS" ]; then |
| 280 |
GPU0=${GPU0:-$(($(oarnodesetting --last-property-value gpu) + 1))} |
| 281 |
debug "First gpu=$GPU0" |
| 282 |
fi |
| 283 |
else |
| 284 |
HOST0=${HOST0:-1} |
| 285 |
CPU0=${CPU0:-1} |
| 286 |
CORE0=${CORE0:-1} |
| 287 |
THREAD0=${THREAD0:-1} |
| 288 |
GPU0=${GPU0:-1} |
| 289 |
fi |
| 290 |
|
| 291 |
host=1 |
| 292 |
thread=0 |
| 293 |
core=0 |
| 294 |
cpu=0 |
| 295 |
gpu=0 |
| 296 |
# _gpu will be used in the loop instead of _gpu in order to manage the DO_GPUS=single case |
| 297 |
_gpu=0 |
| 298 |
|
| 299 |
if [ -z "$USE_THREADS" ]; then |
| 300 |
# Force #threads=1 for the loop below to work. |
| 301 |
THREADS=1 |
| 302 |
fi |
| 303 |
|
| 304 |
if [ -n "$CREATE_PROPERTIES" -a -z "$YAML" -a -z "$DO_REPAIR" ]; then |
| 305 |
cat <<EOF |
| 306 |
oarproperty -l | grep -q -e "^host$" || oarproperty -c -a host |
| 307 |
oarproperty -l | grep -q -e "^cpu$" || oarproperty -a cpu |
| 308 |
oarproperty -l | grep -q -e "^core$" || oarproperty -a core |
| 309 |
EOF |
| 310 |
if [ -n "$USE_THREADS" ]; then |
| 311 |
cat <<EOF |
| 312 |
oarproperty -l | grep -q -e "^thread$" || oarproperty -a thread |
| 313 |
EOF |
| 314 |
fi |
| 315 |
if [ -n "$DO_GPUS" ]; then |
| 316 |
cat <<EOF |
| 317 |
oarproperty -l | grep -q -e "^gpu$" || oarproperty -a gpu |
| 318 |
oarproperty -l | grep -q -e "^gpudevice$" || oarproperty -c -a gpudevice |
| 319 |
EOF |
| 320 |
fi |
| 321 |
fi |
| 322 |
|
| 323 |
if [ -n "$YAML" ]; then |
| 324 |
echo "---" |
| 325 |
fi |
| 326 |
|
| 327 |
while [ $host -le $HOSTS ]; do |
| 328 |
debug "Host loop $host <= $HOSTS" |
| 329 |
hostname="$HOST_PREFIX$((host+HOST0-1))$HOST_SUFFIX" |
| 330 |
cpuset=0 |
| 331 |
gpudevice=0 |
| 332 |
while [ $cpu -lt $((CPUS*host)) ]; do |
| 333 |
debug "CPU loop $cpu < $CPUS * $host" |
| 334 |
while [ $_gpu -lt $(((GPUS/CPUS) * (cpu+1))) ]; do |
| 335 |
debug "GPU loop $_gpu < ($GPUS/$CPUS) * ($cpu+1)" |
| 336 |
while [ $core -lt $((CORES/(GPUS/CPUS) * (_gpu+1))) ]; do |
| 337 |
debug "Core loop $core < $CORES/($GPUS/$CPUS) * ($_gpu+1)" |
| 338 |
while [ $thread -lt $((THREADS * (core+1))) ]; do |
| 339 |
debug "Thread loop $thread < $THREADS * ($core+1)" |
| 340 |
if [ -n "$YAML" ]; then |
| 341 |
if [ -z "$USE_THREADS" ]; then |
| 342 |
cat <<EOF |
| 343 |
- network_address: $hostname |
| 344 |
host: $hostname |
| 345 |
cpu: $((cpu+CPU0)) |
| 346 |
core: $((core+CORE0)) |
| 347 |
cpuset: ${CPUTOPO[$cpuset]} |
| 348 |
EOF |
| 349 |
else |
| 350 |
cat <<EOF |
| 351 |
- network_address: $hostname |
| 352 |
host: $hostname |
| 353 |
cpu: $((cpu+CPU0)) |
| 354 |
core: $((core+CORE0)) |
| 355 |
thread: $((thread+THREAD0)) |
| 356 |
cpuset: ${CPUTOPO[$cpuset]} |
| 357 |
EOF |
| 358 |
fi |
| 359 |
if [ -n "$DO_GPUS" ]; then |
| 360 |
cat <<EOF |
| 361 |
gpu: $((gpu+GPU0)) |
| 362 |
gpudevice: ${GPUTOPO[$gpudevice]} |
| 363 |
EOF |
| 364 |
fi |
| 365 |
echo -e "$APPEND" |
| 366 |
else |
| 367 |
if [ -z "$DO_REPAIR" ]; then |
| 368 |
if [ -z "$USE_THREADS" ]; then |
| 369 |
echo "oarnodesetting -a -h $hostname -p host=$hostname -p cpu=$((cpu+CPU0)) -p core=$((core+CORE0)) -p cpuset=${CPUTOPO[$cpuset]} ${DO_GPUS:+-p gpu=$((gpu+GPU0)) -p gpudevice=$GPUDEVICE_PREFIX${GPUTOPO[$gpudevice]} }$APPEND" |
| 370 |
else |
| 371 |
echo "oarnodesetting -a -h $hostname -p host=$hostname -p cpu=$((cpu+CPU0)) -p core=$((core+CORE0)) -p thread=$((thread+THREAD0)) -p cpuset=${CPUTOPO[$cpuset]} ${DO_GPUS:+-p gpu=$((gpu+GPU0)) -p gpudevice=$GPUDEVICE_PREFIX${GPUTOPO[$gpudevice]} }$APPEND" |
| 372 |
fi |
| 373 |
else |
| 374 |
if [ -z "$USE_THREADS" ]; then |
| 375 |
echo "oarnodesetting --sql \"network_address = '$hostname' and cpuset = '${CPUTOPO[$cpuset]}'\" -p host=$hostname -p cpu=$((cpu+CPU0)) -p core=$((core+CORE0)) ${DO_GPUS:+-p gpu=$((gpu+GPU0)) -p gpudevice=$GPUDEVICE_PREFIX${GPUTOPO[$gpudevice]} }$APPEND" |
| 376 |
else |
| 377 |
echo "oarnodesetting --sql \"network_address = '$hostname' and cpuset = '${CPUTOPO[$cpuset]}'\" -p host=$hostname -p cpu=$((cpu+CPU0)) -p core=$((core+CORE0)) -p thread=$((thread+THREAD0)) ${DO_GPUS:+-p gpu=$((gpu+GPU0)) -p gpudevice=$GPUDEVICE_PREFIX${GPUTOPO[$gpudevice]} }$APPEND" |
| 378 |
fi |
| 379 |
fi |
| 380 |
fi |
| 381 |
((thread++)) |
| 382 |
cpuset=$(((cpuset+1) % CPUSETS)) |
| 383 |
done |
| 384 |
((core++)) |
| 385 |
done |
| 386 |
((_gpu++)) |
| 387 |
if [ "$DO_GPUS" == "multi" ]; then |
| 388 |
gpu=_gpu |
| 389 |
gpudevice=$(((gpudevice+1) % GPUDEVICES)) |
| 390 |
fi |
| 391 |
done |
| 392 |
((cpu++)) |
| 393 |
done |
| 394 |
if [ "$DO_GPUS" == "single" ]; then |
| 395 |
((gpu++)) |
| 396 |
fi |
| 397 |
((host++)) |
| 398 |
done |