-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·176 lines (149 loc) · 4.83 KB
/
run.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
#!/bin/bash
set -euo pipefail
run_local() {
echo "Starting server"
ds="cifar10"
python3 runner.py --server --rounds 60 --epochs 2 --data=$ds &
sleep 5 # Sleep for 3s to give the server enough time to start
num_clients=10
poison_list="1"
heterogeneity_list="1 2 3"
for i in `seq 0 $((num_clients-1))`; do
echo "Starting client $i"
if exists_in_list "$poison_list" " " $i; then
if exists_in_list "$heterogeneity_list" " " $i; then
python3 runner.py --client --total-clients=$num_clients --client-index $i --server-address 127.0.0.1 --is-poisoned --is-noniid --data=$ds &
else
python3 runner.py --client --total-clients=$num_clients --client-index $i --server-address 127.0.0.1 --data=$ds &
fi
else
if exists_in_list "$heterogeneity_list" " " $i; then
python3 runner.py --client --total-clients=$num_clients --client-index $i --server-address 127.0.0.1 --is-noniid --data=$ds &
else
python3 runner.py --client --total-clients=$num_clients --client-index $i --server-address 127.0.0.1 --data=$ds &
fi
fi
done
# This will allow you to use CTRL+C to stop all background processes
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM
# Wait for all background processes to complete
wait
}
function exists_in_list() {
LIST=$1
DELIMITER=$2
VALUE=$3
LIST_WHITESPACES=`echo $LIST | tr "$DELIMITER" " "`
for x in $LIST_WHITESPACES; do
if [ "$x" = "$VALUE" ]; then
return 0
fi
done
return 1
}
run_remote() {
update="$1"
if [[ ! -f config.sh ]]; then
echo 'no config file found'
exit 1
fi
source config.sh
server_ssh="${server%:*}"
server_ip="${server#*@}"
server_port="${server_ip#*:}"
server_ip="${server_ip%:*}"
if ! ping -c1 -W1 "${server_ip#*@}" &>/dev/null; then
echo 'CONNECT TO THE VPN!'
exit 1
fi
echo "=== SERVER $server ==="
if [[ "$update" == true ]]; then
zip fl.zip *.py
scp fl.zip "$server_ssh":~
ssh "$server_ssh" "
cd ~ && mkdir -p fl &&
unzip -o fl.zip -d fl && cd fl &&
if [[ ! -d data ]]; then
echo '--- DOWNLOADING DATASET ON SERVER $server_ssh'
wget -q https://datashare.ed.ac.uk/download/DS_10283_3192.zip &&
unzip DS_10283_3192.zip && rm DS_10283_3192.zip &&
mkdir -p data &&
tar -C data -x -z -f CINIC-10.tar.gz && rm CINIC-10.tar.gz;
fi &&
if ! command -v docker; then
echo 'install docker on server $server_ssh' &&
exit 1 ;
fi
"
fi
ssh "$server_ssh" -- "
if pgrep -f ^docker\ .*\ role=server; then
pkill --signal 9 -f ^docker\ .*\ role=server;
fi;
sleep 2;
tmux new-session -d -- 'docker run -v ~/fl/:/app/ -p $server_port:$server_port -e role=server --rm $docker_image; bash'
"
sleep 10
i=0
for client in "${clients[@]}" "${poisoned_clients[@]}"; do
echo "=== CLIENT $i $client ==="
if [[ "$update" == true ]]; then
echo "--- PREPARING CLIENT $i $client ---"
scp fl.zip "$client":~
ssh "$client" "
cd ~ && mkdir -p fl &&
unzip -o fl.zip -d fl && cd fl &&
if [[ ! -d data ]]; then
echo '--- DOWNLOADING DATASET ON CLIENT $i $client'
wget -q https://datashare.ed.ac.uk/download/DS_10283_3192.zip &&
unzip DS_10283_3192.zip && rm DS_10283_3192.zip &&
mkdir -p data &&
tar -C data -x -z -f CINIC-10.tar.gz && rm CINIC-10.tar.gz;
fi &&
if ! command -v docker; then
echo 'install docker on client $i $client' &&
exit 1;
fi
" &
fi
wait
is_poisoned=false
if [[ " ${poisoned_clients[@] } " == *" $client "* ]]; then
is_poisoned=true
fi
echo "--- RUNNING FL CLIENT ON $i $client ---"
ssh "$client" "
if pgrep -f ^docker\ .*\ role=client; then
pkill --signal 9 -f ^docker\ .*\ role=client;
fi;
sleep 2;
tmux new-session -d -- 'docker run -v ~/fl/:/app $([[ $is_poisoned == true ]] && echo -- '-e is_poisoned true') -e role=client -e server_address=$server_ip -e client_index=$i -e total_clients=${#clients[@]} --rm -it $docker_image; bash'
"
(( i++ )) || :
done
}
local=true
update=false
debug=false
while [[ "$#" -gt 0 ]]; do
case "$1" in
--local)
local=true
;;
--update)
update=true
;;
--debug)
debug=true
;;
esac
shift
done
if [[ "$debug" == true ]]; then
set -x
fi
if [[ "$local" == true ]]; then
run_local
else
run_remote "$update"
fi