-
Notifications
You must be signed in to change notification settings - Fork 64
/
deploy-distributed-on-slurm.sh
executable file
·628 lines (524 loc) · 22.2 KB
/
deploy-distributed-on-slurm.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#!/bin/bash
# Copyright 2021 The DAPHNE Consortium
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script allows the user to deploy DAPHNE through SLURM, including:
# 1 - compilation of the Singularity image,
# 2 - compilation of the daphne main and worker codes within the Singularity image
# 3 - packaging compiled daphne codes
# 4 - packaging compiled daphne codes with user payload as a payload package
# 5 - uploading the payload package to an HPC platform
# 6 - obtaining the list of PEERS from SLURM
# 7 - executing daphne main and worker binaries on SLURM PEERS
# 8 - collection of logs from daphne execution
# 9 - cleanup of workers and payload deployment
# The difference of this script from deploy-distributed-on-slurm.sh is that
# while packaging and executing on a target HPC platform, it is tailored to
# the communication required with SLURM and the target HPC platform.
#
# Specific description of functionality differences with deploy-distributed-on-slurm.sh:
#
# - the build of the daphne main and worker node executables is executed
# through a Singularity container that is built on the utilized HPC,
# while the function "deploy" in deployDistributed.sh sends and builds
# executables on each node, which might cause overwrite if the workers use same
# mounted user storage (e.g. distributed storage attached as home directory)
#
# - the list of PEERS is not defined by the user but obtained from SLURM
# (in deployDistributed.sh, the user supplies PEERS as an argument)
#
# - the support for single request deployment, run, and cleanup is provided
#
# - an expanded format of parameters, commands, and arguments
#
#*****************************************************************************
# SSH Configurations to access the HPC platform
#*****************************************************************************
SSH_LOGIN_NODE_HOSTNAME=localhost
SSH_IDENTITY_FILE=~/.ssh/id_rsa.pub
SSH_USERNAME=$USER
SSH_PORT=22
#*****************************************************************************
# DAPHNE deployment default parameters
#*****************************************************************************
NUMCORES=128
DAPHNE_SCRIPT_AND_PARAMS=/dev/stdin
#*****************************************************************************
# Parameterization of the HPC platform specifics (optional)
#*****************************************************************************
PORTRANGE_BEGIN=50000
PATH_TO_DEPLOY_BUILD=./DaphneDistributedWorker
PEERS= # the "PEERS" variable is obtained using SLURM below
#*****************************************************************************
# DO (not) EDIT BELOW THIS LINE
#*****************************************************************************
SSH_COMMAND="ssh ${SSH_IDENTITY_FILE:+-i $SSH_IDENTITY_FILE} ${SSH_PORT:+-p $SSH_PORT}"
SCP_COMMAND="scp ${SSH_IDENTITY_FILE:+-i $SSH_IDENTITY_FILE} ${SSH_PORT:+-P $SSH_PORT}"
# Stop immediately if any command fails.
set -e
# ****************************************************************************
# Builds the image for a Singularity container, then transfers it to the
# target platform.
# ****************************************************************************
function BuildAndTransferSingularityContainerImage {
./deploy/build-daphne-singularity-image.sh
$SCP_COMMAND daphne.sif $SSH_USERNAME@$SSH_LOGIN_NODE_HOSTNAME:$PATH_TO_DEPLOY_BUILD
}
# ****************************************************************************
# Builds daphne code and puts it in a time-stamped build directory.
# The build is called using the daphne singularity container "daphne.sif"
# created from the DAPHNE Docker image.
# The daphne and DistributedWorker are both built.
# ****************************************************************************
function BuildWithSingularityContainer {
time singularity $SINGULARITY_ARG exec daphne.sif ./build.sh
time singularity $SINGULARITY_ARG exec daphne.sif ./build.sh --target DistributedWorker
TIME_BUILT=$(date +%F-%T)
mv build build_${TIME_BUILT}
mv bin bin${TIME_BUILT}
mv lib lib${TIME_BUILT}
}
# ****************************************************************************
# Packaging of files (payload) to be sent to a remote machine:
# - daphne built (bin,lib) and
# - user code (all *.daphne scripts in the current working directory).
# The step of packaging using same compilation (build_*) is hence reusable.
# ****************************************************************************
function PackageBuiltDaphnePayload {
echo "Packaging latest files for DaphneDistributedWorker deployment..."
(
tar cvzf build.tgz bin lib
cp $0 deploy-distributed-on-slurm.sh
chmod 755 deploy-distributed-on-slurm.sh
tar cvzf daphne-package.tgz build.tgz *.daphne deploy-distributed-on-slurm.sh
) | awk '{printf("\r%-100s ", substr($0, -1, 100));}END{print "";}'
}
# ****************************************************************************
# Deploys: copies the package to the target platform using SCP and extracts it.
# The target deployment platform (HPC) is configured in SSH/SCP Configuration.
# All distributed workers are expected to be able to access an extracted package.
# For transfer, scp is used, since rsync might already be used for "daphne.sif".
# ****************************************************************************
function TransferPackageToTargetPlatform {
$SSH_COMMAND $SSH_USERNAME@$SSH_LOGIN_NODE_HOSTNAME mkdir -p $PATH_TO_DEPLOY_BUILD
$SCP_COMMAND daphne-package.tgz $SSH_USERNAME@$SSH_LOGIN_NODE_HOSTNAME:$PATH_TO_DEPLOY_BUILD
$SSH_COMMAND $SSH_USERNAME@$SSH_LOGIN_NODE_HOSTNAME <<EOF
cd $PATH_TO_DEPLOY_BUILD
tar xvf daphne-package.tgz
exit
EOF
}
#*****************************************************************************
# Remotely start workers on remote machines (argument "workers", on login node for SLURM and containers).
# This script itself is expected to be present at the target platform,
# packaged with PackageBuiltDaphnePayload and transferred with TransferPackageToTargetPlatform.
#*****************************************************************************
function RemotelyStartWorkers {
# forward the parameters to remote invocation
PARAMS_REMOTE="$PARAMS"
if [[ -n "$SRUN_ARG" ]]; then
PARAMS_REMOTE="-R=\"$SRUN_ARG\" $PARAMS_REMOTE"
fi
if [[ -n "$SINGULARITY_ARG" ]]; then
PARAMS_REMOTE="-G=\"$SINGULARITY_ARG\" $PARAMS_REMOTE"
fi
if [[ -n "$NUMCORES" ]]; then
PARAMS_REMOTE="-n $NUMCORES $PARAMS_REMOTE"
fi
if [[ -n "$PORTRANGE_BEGIN" ]]; then
PARAMS_REMOTE="-p $PORTRANGE_BEGIN $PARAMS_REMOTE"
fi
$SSH_COMMAND $SSH_USERNAME@$SSH_LOGIN_NODE_HOSTNAME <<EOF
cd $PATH_TO_DEPLOY_BUILD
./deploy-distributed-on-slurm.sh workers -d . $PARAMS_REMOTE
exit
EOF
}
#*****************************************************************************
# Start workers on remote machines through SLURM using Singularity containers.
# This can be called from RemotelyStartWorkers.
#*****************************************************************************
function StartWorkersInContainersOnSLURM {
mkdir -p logs
rm -f logs/* 2>/dev/null # clean workerlist and other content
srun -J DAPHNEworkers $SRUN_ARG -n $NUMCORES \
bash -c 'singularity '$SINGULARITY_ARG' exec daphne.sif \
bin/DistributedWorker $(hostname):$(( 50000 + SLURM_LOCALID )) \
> logs/OUTPUT.$(hostname):$(( 50000 + SLURM_LOCALID )) \
2>&1 \
& echo $! > logs/PID.$(hostname):$(( 50000 + SLURM_LOCALID ))' &
}
#*****************************************************************************
# Get worker's status
#*****************************************************************************
function WorkersStatus {
WORKERS=$(cd logs; echo OUTPUT.* | sed -e 's/OUTPUT.//g' -e 's/ /,/g')
echo "WORKERS list: $WORKERS"
[ $(cd logs; ls -1 OUTPUT.* 2>/dev/null | wc -l) -ge $NUMCORES ] && echo All up.
echo -e "\nInfo about the DAPHNE bin/ dir is:"
cat bin/git_source_status_info
}
#*****************************************************************************
# Waits for all workers to be run through SLURM.
#*****************************************************************************
function WaitAllWorkersUp {
until [ $(cd logs; ls -1 OUTPUT.* 2>/dev/null | wc -l) -ge $NUMCORES ]
do
echo -n .
sleep 1
done
echo -e "\nSuccessfully spawned N new distributed worker daemons (see queue below), N=" $NUMCORES
squeue -u $(whoami) # print the generated worker list
}
#*****************************************************************************
# Kill workers
#*****************************************************************************
function KillWorkersOnSLURM {
scancel -n DAPHNEworkers
}
#*****************************************************************************
# Run one request (.daphne script) on an already deployed distributed platform.
#*****************************************************************************
function RunOneRequest {
WORKERS=$(cd logs; echo OUTPUT.* | sed -e 's/OUTPUT.//g' -e 's/ /,/g')
time srun $SRUN_ARG \
singularity $SINGULARITY_ARG exec daphne.sif bash -c \
"DISTRIBUTED_WORKERS=${WORKERS} bin/daphne $ARGS_CS $DAPHNE_SCRIPT_AND_PARAMS"
}
#*****************************************************************************
# Run one request (.daphne script) on an already deployed distributed platform.
#*****************************************************************************
function DeploymentClean {
$SSH_COMMAND $SSH_USERNAME@$SSH_LOGIN_NODE_HOSTNAME <<EOF
rm -rf $PATH_TO_DEPLOY_BUILD
exit
EOF
}
# ****************************************************************************
# A more general deploy function, evoking all necessary steps to deploy a
# working DAPHNE deployment workers set: builds with Singularity, packages,
# copies the package, executes, and cleans a deployment at the HPC platform.
# ****************************************************************************
function DeployEverythingHere {
# building locally or at target, but supporting transfer over OpenSSH
BuildAndTransferSingularityContainerImage
BuildWithSingularityContainer
PackageBuiltDaphnePayload
TransferPackageToTargetPlatform
# starting remotely at the platform
RemotelyStartWorkers
# for evocation of the command at target site below here:
WaitAllWorkersUp
WorkersStatus
RunOneRequest
KillWorkersOnSLURM
# cleanup using OpenSSH
DeploymentClean
}
#*****************************************************************************
#*****************************************************************************
# General functions below as in deployDistributed.sh, tailored for this script,
# with added format for commands and additional parameters and arguments.
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
# Help message
#*****************************************************************************
function PrintHelp {
echo "Usage: $0 <options> <command>"
echo ""
echo "Start the DAPHNE distributed deployment on remote machines using SLURM."
echo ""
echo "These are the options (short and long formats available):"
echo " -h, --help Print this help message and exit."
echo " -i SSH_IDENTITY_FILE Specify OpenSSH identity file (default: private key in ~/.ssh/id_rsa.pub)."
echo " -u, --user SSH_USERNAME Specify OpenSSH username (default: \$USER)."
echo " -l, --login SSH_LOGIN_NODE_HOSTNAME Specify OpenSSH login name hostname (default: localhost)."
echo " -d, --pathToBuild A path to deploy or where the build is already deployed (default ~/DaphneDistributedWorker can be specified in the script)."
echo " -n, --numcores Specify number of workers (cores) to use to deploy DAPHNE workers (default: 128)."
echo " -p, --port Specify DAPHNE deployed port range begin (default: 50000)."
echo " --args ARGS_CS Specify arguments of a DAPHNE SCRIPT in a comma-separated format."
echo " -S, --ssh-arg=S Specify additional arguments S for ssh client (default command: $SSH_COMMAND)."
echo " -C, --scp-arg=C Specify additional arguments C for scp client (default command: $SCP_COMMAND)."
echo " -R, --srun-arg=R Specify additional arguments R for srun client."
echo " -G, --singularity-arg=G Specify additional arguments G for singularity client."
echo ""
echo "These are the commands that can be executed:"
echo " singularity Compile the Singularity SIF image for DAPHNE (and transfer it to the target platform)."
echo " build Compile DAPHNE codes (daphne, DistributedWorker) using the Singularity image for DAPHNE."
echo " It should only be invoked from the code base root directory."
echo " It could also be invoked on a target platform after a transfer."
echo " package Create the package image with *.daphne scripts and a compressed bin,lib directories."
echo " transfer Transfers (uploads) a package to the target platform."
echo " start Run workers on remote machines through login node (deploys this script and runs workers)."
echo " workers Run workers on current login node through SLURM."
echo " status Get distributed workers' status."
echo " wait Waits until all workers are up."
echo " stop Stops all distributed workers."
echo " run [SCRIPT [ARGs]] Run one request on the deployed platform by processing one DAPHNE SCRIPT file (default: /dev/stdin)"
echo " using optional arguments (ARGs in script format)."
echo " clean Cleans (deletes) the package on the target platform."
echo " deploy Deploys everything in one sweep: singularity=>build=>package=>transfer=>start=>wait=>run=>clean."
echo ""
echo ""
echo "The default connection to the target platform (HPC) login node is through OpenSSH, configured by default in ~/.ssh (see: man ssh_config)."
echo ""
echo "The default ports for worker peers begin at 50000 (PORTRANGE_BEGIN) and the list of PEERS is generated as:"
echo "PEERS = ( WORKER1_IP:PORTRANGE_BEGIN, WORKER1_IP:PORTRANGE_BEGIN+1, ..., WORKER2_IP:PORTRANGE_BEGIN, WORKER2_IP:PORTRANGE_BEGIN+1, ... )"
echo ""
echo "Logs can be found at [pathToBuild]/logs."
echo ""
echo ""
echo "Examples:"
echo " $0 singularity && $0 build && $0 package Builds the Singularity image and uses it to compile the daphne binaries, then packages it."
echo " $0 --login HPC --user hpc -i ~/.ssh/hpc.pub transfer Transfers a package to the target platform through OpenSSH, using login node HPC, user hpc, and identify key hpc.pub."
echo " $0 -l HPC start Using login node HPC, accesses the target platform and starts workers on remote machines."
echo " $0 -l HPC -n 1024 run example-time.daphne Runs one request (script called example-time.daphne) on the deployment using 1024 cores, login node HPC, and default OpenSSH configuration."
echo " $0 run Executes one request (DAPHNE script input from standard input) at a running deployed platform, using default singularity/srun configurations."
echo " $0 deploy -n 10 Deploys once at the target platform through OpenSSH using default login node (localhost), then cleans."
echo " $0 workers -R=\"-t 120 --mem-per-cpu=10G --cpu-bind=cores --cpus-per-task=2\" Starts workers at a running deployed platform using custom srun arguments (2 hours dual-core with 10G memory)."
echo " $0 run -R=\"--time=30 --cpu-bind=cores --nodes=1 --ntasks-per-node=1 --cpus-per-task=1\" Executes a request with custom srun arguments (30 minutes single-core)."
echo " cat example.daphne | $0 run Example request job from a pipe."
}
#*****************************************************************************
# Parse arguments
#*****************************************************************************
PARAMS=""
while (( "$#" )); do
case "$1" in
-h|--help)
PrintHelp
shift 1
exit 0
;;
-i)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
SSH_IDENTITY_FILE=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
PrintHelp
exit 1
fi
;;
-u|--user)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
SSH_USERNAME=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
PrintHelp
exit 1
fi
;;
-l|--login)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
SSH_LOGIN_NODE_HOSTNAME=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
PrintHelp
exit 1
fi
;;
-d|--pathToBuild)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
PATH_TO_DEPLOY_BUILD=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
PrintHelp
exit 1
fi
;;
-n|--numcores)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
NUMCORES=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
PrintHelp
exit 1
fi
;;
-p|--port)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
PORTRANGE_BEGIN=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
PrintHelp
exit 1
fi
;;
--args)
if [ -n "$2" ]; then
ARGS_CS="--args $2 "
shift 2
else
echo "Error: Argument for $1 is missing" >&2
PrintHelp
exit 1
fi
;;
-S=-*|--ssh-arg=-)
SSH_ARG="${1:3}"
shift 1
;;
-C=-*|--scp-arg=-)
SCP_ARG="${1:3}"
shift 1
;;
-R=-*|--srun-arg=-)
SRUN_ARG="${1:3}"
shift 1
;;
-G=-*|--singularity-arg=-)
SINGULARITY_ARG="${1:3}"
shift 1
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"
# parse commands
PARAMS=""
while (( "$#" )); do
case "$1" in
singularity)
SINGULARITY_BUILD_AND_TRANSFER_COMMAND=TRUE
shift 1
;;
build)
BUILD_WITH_CONTAINER_COMMAND=TRUE
shift 1
;;
package)
PACKAGE_SCRIPTS_AND_BUILD_COMMAND=TRUE
shift 1
;;
transfer)
TRANSFER_PACKAGE_COMMAND=TRUE
shift 1
;;
start)
START_WORKERS_COMMAND=TRUE
shift 1
;;
workers)
START_WORKERS_SLURM_COMMAND=TRUE
shift 1
;;
status)
WORKERS_STATUS_COMMAND=TRUE
shift 1
;;
wait)
WAIT_WORKERS_COMMAND=TRUE
shift 1
;;
stop)
KILL_WORKERS_COMMAND=TRUE
shift 1
;;
run)
RUN_ONE_REQUEST_COMMAND=TRUE
shift 1
;;
clean)
CLEAN_PLATFORM_COMMAND=TRUE
shift 1
;;
deploy)
DEPLOY_COMMAND=TRUE
shift 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"
if [[ ! -n $PATH_TO_DEPLOY_BUILD ]]; then
echo "Missing: please specifcy where the build is located."
exit 1
fi
if [[ ! -n $SSH_IDENTITY_FILE ]]; then
echo "Missing: please configure the identity file for OpenSSH client (argument -i or inside the script)."
exit 1
fi
# reevaluate the commands
SSH_COMMAND="ssh ${SSH_IDENTITY_FILE:+-i $SSH_IDENTITY_FILE} ${SSH_PORT:+-p $SSH_PORT} $SSH_ARG"
SCP_COMMAND="scp ${SSH_IDENTITY_FILE:+-i $SSH_IDENTITY_FILE} ${SSH_PORT:+-P $SSH_PORT} $SCP_ARG"
if [[ -n $PARAMS ]]; then
DAPHNE_SCRIPT_AND_PARAMS="$PARAMS"
fi
if [[ -n $SINGULARITY_BUILD_AND_TRANSFER_COMMAND ]]; then
BuildAndTransferSingularityContainerImage
exit 0
fi
if [[ -n $BUILD_WITH_CONTAINER_COMMAND ]]; then
BuildWithSingularityContainer
exit 0
fi
if [[ -n $PACKAGE_SCRIPTS_AND_BUILD_COMMAND ]]; then
PackageBuiltDaphnePayload
exit 0
fi
if [[ -n $TRANSFER_PACKAGE_COMMAND ]]; then
TransferPackageToTargetPlatform
exit 0
fi
if [[ -n $START_WORKERS_COMMAND ]]; then
RemotelyStartWorkers
exit 0
fi
if [[ -n $START_WORKERS_SLURM_COMMAND ]]; then
StartWorkersInContainersOnSLURM
exit 0
fi
if [[ -n $WORKERS_STATUS_COMMAND ]]; then
WorkersStatus
exit 0
fi
if [[ -n $WAIT_WORKERS_COMMAND ]]; then
WaitAllWorkersUp
exit 0
fi
if [[ -n $KILL_WORKERS_COMMAND ]]; then
KillWorkersOnSLURM
exit 0
fi
if [[ -n $RUN_ONE_REQUEST_COMMAND ]]; then
RunOneRequest
exit 0
fi
if [[ -n $CLEAN_PLATFORM_COMMAND ]]; then
DeploymentClean
exit 0
fi
if [[ -n $DEPLOY_COMMAND ]]; then
DeployEverythingHere
exit 0
fi
PrintHelp