forked from JaneliaSciComp/ray-janelia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathray-janelia.sh
executable file
·212 lines (180 loc) · 5.82 KB
/
ray-janelia.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
#!/bin/bash
#
# Run a Ray cluster job on the Janelia LSF cluster.
#
# Adapted from https://github.com/IBMSpectrumComputing/ray-integration
#
# Parameters:
# -c : user command to run on the cluster (if not specified, the cluster will be left running)
# -n : name of conda environment to use for running ray and user workload
# -m : size of shared object store (bytes). defaults to 4GB if not specified
#
# Example usage:
#
# bsub -o std%J.out -e std%J.out -n 20 -R "span[ptile=4]" bash -i $PWD/ray-janelia.sh \
# -c "python /path/to/job.py --option" -n "ray-python" -m 20000000000
#
# This will allocate 20 slots on the cluster, divide them into 20/4=5 nodes, and run the job.py
# python script using the ray-python conda environment.
#
# location of conda executable to use
CONDA=conda
# Timeout (in seconds) whenever we're waiting on the cluster to converge to a new state
TIMEOUT_SEC=180
# How often to check cluster state
SLEEP_DELAY_SEC=1
# Shut down the cluster by killing the job id
function shutdown_cluster()
{
echo "Shutting down the cluster"
bkill $LSB_JOBID
}
# Wait for the cluster at address $1 to have $2 nodes available, with timeout.
function wait_for_nodes()
{
local _address=$1
local _num_nodes=$2
start_time="$(date -u +%s)"
status_cmd="ray status --address $_address"
echo "Waiting for $_num_nodes nodes to be ready on cluster $_address"
while true; do
# from https://stackoverflow.com/questions/12321469/retry-a-bash-command-with-timeout
current_time="$(date -u +%s)"
elapsed_seconds=$(($current_time-$start_time))
if [ $elapsed_seconds -gt $TIMEOUT_SEC ]; then
echo "Cluster timeout after $TIMEOUT_SEC seconds"
shutdown_cluster
exit 1
fi
STATUS_OUTPUT=$($status_cmd)
STATUS_RC=$?
if [ $STATUS_RC -ne 0 ]; then
echo "Cluster status command failed with exit code $STATUS_RC"
shutdown_cluster
exit 1
fi
num_ready=$(echo "$STATUS_OUTPUT" | awk '/Healthy:/{ f = 1; next } /Pending:/{ f = 0 } f' | wc -l)
if [ $_num_nodes -eq $num_ready ]; then
echo "Cluster is ready with $num_ready nodes"
return 0
else
echo "$num_ready cluster nodes are ready (waiting for $_num_nodes)"
fi
sleep $SLEEP_DELAY_SEC
done
}
# Find an available network port to use
function get_free_port()
{
CHECK="do while"
while [[ ! -z $CHECK ]]; do
port=$(( ( RANDOM % 40000 ) + 19999 ))
CHECK=$(netstat -a | grep $port)
done
echo $port
}
while getopts ":c:n:m:" option;do
case "${option}" in
c) c=${OPTARG}
user_command=$c
;;
n) n=${OPTARG}
conda_env=$n
;;
m) m=${OPTARG}
object_store_mem=$m
;;
*) echo "Did not supply the correct arguments"
;;
esac
done
#use bash -i to activate conda env when the script is launched
#or use the below syntax.
if [ -z "$conda_env" ]
then
echo "Use -n to provide the name of a conda environment with ray installed."
else
eval "$($CONDA shell.bash hook)"
conda activate $conda_env
echo "Activated conda environment: $conda_env"
fi
hosts=()
for host in `cat $LSB_DJOB_HOSTFILE | uniq`
do
echo "Adding host: $host"
hosts+=($host)
done
echo
port=$(get_free_port)
echo "Head node will use port: " $port
export port
dashboard_port=$(get_free_port)
echo "Dashboard will use port: " $dashboard_port
echo
IFS=' ' read -r -a array <<< "$LSB_MCPU_HOSTS"
declare -A cpus_for_node
i=0
len=${#array[@]}
while [ $i -lt $len ]
do
key=${array[$i]}
value=${array[$i+1]}
cpus_for_node[$key]+=$value
i=$((i=i+2))
done
echo
if [ -z "$CUDA_VISIBLE_DEVICES" ]
then
num_gpu_for_head=0
else
num_gpu_for_head=$(echo $CUDA_VISIBLE_DEVICES | awk -F "," "{print NF}")
fi
num_gpu_for_worker=0
export head_node=${hosts[0]}
cluster_address="$head_node:$port"
client_server_port=10001
echo "Starting Ray head node on $head_node"
if [ -z $object_store_mem ]
then
echo " Using default object store mem of 4GB. Make sure your cluster has more than 4GB of memory."
object_store_mem=4000000000
fi
echo " The object store memory: $object_store_mem bytes"
num_cpu_for_head=${cpus_for_node[$head_node]}
command_launch="blaunch -z $head_node ray start --head --port $port --ray-client-server-port $client_server_port --dashboard-host 0.0.0.0 --dashboard-port $dashboard_port --min-worker-port 18999 --max-worker-port 19999 --num-cpus $num_cpu_for_head --num-gpus $num_gpu_for_head --object-store-memory $object_store_mem"
echo $command_launch
$command_launch &
# Wait for the head node to start up
wait_for_nodes "$cluster_address" 1
echo "The dashboard is now available at http://$head_node:$dashboard_port"
workers=("${hosts[@]:1}")
echo "Starting workers: ${workers[*]}"
#run ray on worker nodes and connect to head
for host in "${workers[@]}"
do
echo "Starting worker on $host using master node $head_node"
num_cpu=${cpus_for_node[$host]}
command_for_worker="blaunch -z $host ray start --address $cluster_address --num-cpus $num_cpu --num-gpus $num_gpu_for_worker --object-store-memory $object_store_mem"
echo $command_for_worker
$command_for_worker &
done
# Wait for the head node to start up
num_nodes=${#hosts[@]}
wait_for_nodes "$cluster_address" "$num_nodes"
# Run the user command if specified
if [ -n "$user_command" ]; then
echo "Running user workload"
echo $user_command
$user_command
retVal=$?
if [ $retVal -ne 0 ]; then
echo "Error: user command exited with code $retVal"
echo "Cluster will keep running to allow debugging"
exit $retVal
else
echo "Done"
shutdown_cluster
fi
else
echo "Ray cluster with $num_nodes nodes is now running at ray://$head_node:$client_server_port with a dashboard at http://$head_node:$dashboard_port/"
fi