-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgadi_jupyter
executable file
·342 lines (282 loc) · 8.4 KB
/
gadi_jupyter
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
#!/bin/bash
# Scott Wales 20190522
print_help() {
cat <<EOF
Run a Jupyter notebook on Gadi's compute nodes, presenting the interface in a
browser on the local machine
You can set a default username in your local ~/.ssh/config, e.g.
Host gadi.nci.org.au
User abc123
Your default project is set in the file on Gadi ~/.config/gadi-login.conf
General Options:
-h: Print help
-l USER: NCI username
-L HOST: NCI login node (default 'gadi.nci.org.au')
-e ENVIRON: Conda environment
-d: Debug mode
-D: Profile mode
Queue Options:
-q QUEUE: Queue name
-n NCPU: Use NCPU cpus
-g NGPU: Use NGPU gpus (only on some queues)
-m MEM: Memory allocation (default 4*NCPU GB)
-t TIME: Walltime limit (default 1 hour)
-J JOBFS: Jobfs allocation (default 100 GB)
-P PROJ: Submit job under project PROJ
-s STORAGE: PBS Storage flags (e.g. 'scratch/w35+gdata/v45')
If not specified, all available storage is requested
EOF
}
set -eu
# Internal defaults
USER=''
PROJECT=''
LOGINNODE='gadi.nci.org.au'
QUEUE='normalbw'
NCPUS='1'
NGPUS=''
WALLTIME=1:00:00
JOBFS=100gb
MEM=''
CONDA_ENV=analysis3
DEBUG=""
STORAGE=""
STRACE=""
# Handle arguments
optspec="hl:L:q:n:m:t:J:P:e:ds:g:D"
while getopts "$optspec" optchar; do
case "${optchar}" in
h)
print_help
exit 2
;;
l)
USER="${OPTARG}"
;;
L)
LOGINNODE="${OPTARG}"
;;
q)
QUEUE="${OPTARG}"
;;
n)
NCPUS="${OPTARG}"
;;
g)
NGPUS="${OPTARG}"
;;
t)
WALLTIME="${OPTARG}"
;;
J)
JOBFS="${OPTARG}"
;;
m)
MEM="${OPTARG}"
;;
P)
PROJECT="${OPTARG}"
;;
e)
CONDA_ENV="${OPTARG}"
;;
s)
STORAGE="${OPTARG}"
;;
d)
DEBUG=true
;;
D)
DEBUG=true
STRACE="strace --follow-forks -e trace=%file --syscall-times --absolute-timestamps=precision:us --output=\$WORKDIR/strace"
;;
*)
print_help
exit 2
;;
esac
done
# This gets evaluated on Gadi in the SSH script
WORKDIR=/scratch/${PROJECT:-\$PROJECT}/\$USER/tmp/runjp
# Check for agent
set +e
ssh-add -l &> /dev/null
AGENT=$?
set -e
if [ $AGENT -eq 2 ]; then
# Restart with an agent
ssh-agent "$0" "$@"
exit
fi
if [ $AGENT -eq 1 ]; then
# No keys saved
ssh-add
fi
SSH='ssh -x -oBatchMode=yes'
if [ -n "$USER" ]; then
SSH="${SSH} -l ${USER}"
fi
if [ $NCPUS -gt 48 ]; then
echo "WARNING: Using more than one node with Dask needs extra setup and is not supported by this script"
fi
if [ $NCPUS -gt 8 ]; then
echo "WARNING: Using a large number of CPUs in an interactive session can waste lots of NCI compute time. Try keeping the number of CPUs under 8."
read -p "Proceed? [y/N] " -n 1 cpu_confirm
echo
if [[ ! "$cpu_confirm" =~ ^[Yy]$ ]]; then
exit
fi
fi
if [ -z "$MEM" ]; then
MEM_PER_CPU=4
MEM="$(( NCPUS * MEM_PER_CPU ))gb"
fi
SUBMITOPTS="-N jupyter-lab ${PROJECT:+-P '$PROJECT'} -q '$QUEUE' -l 'ncpus=${NCPUS},mem=${MEM},walltime=${WALLTIME},jobfs=${JOBFS}'"
if [ -n "$NGPUS" ]; then
SUBMITOPTS="$SUBMITOPTS -l 'ngpus=$NGPUS'"
fi
echo "Starting notebook on ${LOGINNODE}..."
if [ -n "$DEBUG" ]; then
echo "DEBUG: Connecting using: '$SSH $LOGINNODE'"
fi
# Check connection
$SSH "$LOGINNODE" echo "Working directory: $WORKDIR"
echo "qsub ${SUBMITOPTS}"
# Kill the job if this top-level script is cancelled while the job is still in the queue
trap "{ echo 'Stopping queued job... (Ctrl-C will leave job in the queue)' ; $SSH \"$LOGINNODE\" > /dev/null <<< 'qdel \$(cat $WORKDIR/jobid)' ; }" EXIT
message=$(
$SSH -q "$LOGINNODE" <<EOF | tail -n 1
set -eu
WORKDIR="$WORKDIR"
mkdir -p "\$WORKDIR"
# Check if a hh5 member
if [ ! -d /g/data/hh5 ]; then
echo "ERROR: \$USER is not a member of hh5. Join at https://my.nci.org.au/mancini/project/hh5/join" >&2
echo "x x x x ERROR"
exit
fi
# Check if already running
if [ -f "\$WORKDIR/jobid" ] && [ "\$(qstat -x \$(cat "\$WORKDIR/jobid") 2> /dev/null | sed -n '\$s/\S\+\s\+jupyter-lab\s.*\s[QR]\s\+\S\+\s*$/\0/p')" ] ; then
while [ ! -f "\$WORKDIR/message" ]; do
sleep 5
done
cat "\$WORKDIR/message" | sed 's/$/ RECONNECT/'
exit
fi
rm -f "\$WORKDIR/message"
cat > "\$WORKDIR/runjp.sh" <<EOQ
#!/bin/bash
module purge
eval "\\\$(/g/data/hh5/public/apps/miniconda3/bin/conda shell.bash hook)"
conda activate "${CONDA_ENV}"
set -eu
# Jupyter security token
TOKEN=\\\$(uuidgen)
# Find a remote port https://unix.stackexchange.com/a/132524
PORT=\\\$(python -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()')
# Write message file with info for the local connection
echo "\\\$HOSTNAME \\\$TOKEN \\\$PBS_JOBID \\\$PORT" > "\$WORKDIR/message"
echo "runjp log dir \$WORKDIR"
cat "\$WORKDIR/message"
export DASK_LABEXTENSION__FACTORY__MODULE=dask.distributed
export DASK_LABEXTENSION__FACTORY__CLASS=LocalCluster
export DASK_LABEXTENSION__FACTORY__KWARGS__MEMORY_LIMIT=3900MB
export DASK_LABEXTENSION__FACTORY__KWARGS__LOCAL_DIRECTORY=\\\$PBS_JOBFS/dask-worker-space
export DASK_LABEXTENSION__DEFAULT__WORKERS=\\\$PBS_NCPUS
export DASK_DISTRIBUTED__DASHBOARD__LINK="/proxy/{port}/status"
export DASK_TEMPORARY_DIRECTORY="\\\$TMPDIR"
$STRACE jupyter lab --NotebookApp.token="\\\$TOKEN" --no-browser --ip="\\\$HOSTNAME" --port "\\\$PORT" --port-retries=0
EOQ
# Required for conda
storage="gdata/hh5"
if [ -n "$STORAGE" ]; then
storage="$STORAGE+\$storage"
else
for p in \$(id -nG); do
if [ \$p = "access.dev" ]; then continue; fi
if [ \$p = "access.admin" ]; then continue; fi
if [ -d "/scratch/\$p" ]; then storage="scratch/\$p+\$storage"; fi
if [ -d "/g/data/\$p" ]; then storage="gdata/\$p+\$storage"; fi
done
fi
qsub $SUBMITOPTS -W umask=0022 -l "storage=\$storage" -j oe -o "\$WORKDIR/pbs.log" "\$WORKDIR/runjp.sh" > "\$WORKDIR/jobid"
# Wait for the message file to appear, then return to the local process
while [ ! -f "\$WORKDIR/message" ]; do
sleep 5
done
cat "\$WORKDIR/message" | sed 's/$/ NEW/'
EOF
)
if [ -n "$DEBUG" ]; then
echo "DEBUG: Remote Message: '$message'"
fi
# Grab info from the PBS job
read jobhost token jobid remote_port type <<< "$message"
if [ "$type" = "ERROR" ]; then
echo
echo "Stopping due to error"
exit
fi
if [ "$type" = "RECONNECT" ]; then
echo
echo "Existing jupyterlab found, reconnecting to that instead"
fi
# Find a local port
for local_port in {8888..9000}; do
if ! echo > /dev/tcp/127.0.0.1/${local_port} ; then
break
fi 2> /dev/null
done
echo
echo "Notebook running as PBS job ${jobid}"
echo
echo "Starting tunnel..."
if [ -n "$DEBUG" ]; then
echo "DEBUG:" $SSH -N -L "${local_port}:$jobhost:${remote_port}" "$LOGINNODE"
fi
$SSH -q -N -L "${local_port}:$jobhost:${remote_port}" "$LOGINNODE" &
tunnelid=$!
# Shut everything down on exit
trap "{ echo 'Closing connections... (Ctrl-C will leave job in the queue)' ; kill $tunnelid ; $SSH "$LOGINNODE" qdel $jobid ; }" EXIT
URL="http://localhost:${local_port}/lab?token=${token}"
# Wait for startup then open a browser
if which curl > /dev/null; then
until curl $URL &> /dev/null; do echo -n '.'; sleep 1; done
echo
else
sleep 5
fi
cat << EOF
Start a Dask cluster in your notebook using the Dask panel of Jupyterlab, or by
running (needs kernel analysis3-20.01 or later):
---------------------------------------------------------------
import climtas.nci
climtas.nci.GadiClient()
---------------------------------------------------------------
Opening ${URL}
EOF
set +e
if [ -f "/mnt/c/Windows/system32/cmd.exe" ]; then
# Windows subsystem for linux
/mnt/c/Windows/system32/cmd.exe /c start "$URL"
elif which xdg-open > /dev/null; then
# Linux
xdg-open "$URL"
elif which open > /dev/null; then
# OSX
open "$URL"
elif which explorer > /dev/null; then
# Windows
explorer "$URL"
else
echo
echo "----"
echo "Can't find a way to open the URL automatically, please copy the URL above into your browser"
fi
set -e
echo
$SSH "$LOGINNODE" /g/data/hh5/public/apps/nci_scripts/qmonitor $jobid
# Move the cursor past the progress bars
echo
echo
echo