-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.sh
186 lines (152 loc) · 4.4 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
177
178
179
180
181
182
183
184
185
#!/bin/bash
# This script runs a single round of the competition
. vars
agents=""
function sanity_swarm
{
# Ideally swarm shouldn't crash
# But I'm a shit programmer
# So I'll leave this in
kill -0 $1 2>&1 >> /dev/null
if [ "$?" != "0" ]; then
echo "Swarm $pid is no longer running! $2" 1>&2
exit 1
fi
}
# Copy messages to a log file
if [ "$BASH_ARGV" != "_worker_" ]; then
# Get the round number
mkdir -p $results_dir
cd $results_dir
round=$(ls | grep "round" | sed -e "s:round::g" | sort -n | head --lines=1)
if [ "$round" == "" ]; then round=0; fi
round=$(( $round + 1 ))
mkdir -p round$round
cd $root_dir
exec $0 "$@" _worker_ 2>&1 | tee $results_dir/round$round/run.log
exit $?
else
cd $results_dir
round=$(ls | grep "round" | sed -e "s:round::g" | sort -n | head --lines=1)
fi
echo "Start at $(date)"
cd $root_dir
# Setup the swarmF
if [ "$swarm_hosts" != "" ]; then
swarm_pid=$(swarm --daemon -l daemon.log:100)
if [ "$?" != "0" ]; then
echo "Couldn't start swarm daemon!" 1>&2
exit 1
fi
for h in $swarm_hosts; do
if [ "$h" != "local" ]; then
swarm -c "#ABSORB $h#"
fi
done
sanity_swarm $swarm_pid
swarm -c "#.*# cd $root_dir; mkdir -p $results_dir/round$round" -l +wrapper.log:100
fi
count=0
cd $root_dir/$agent_dir
for f in $(ls); do
if [ -d $f ]; then
info_file=$(ls $f | grep -w "info")
if [ "$info_file" == "" ]; then
echo "Skipping agent $f (missing info file)"
else
count=$(( $count + 1 ))
agents="$agents $f"
fi
fi
done
echo "Found $count agents in $agent_dir/"
# Add all the games to swarm
cd $root_dir
if [ "$agents" == "" ]; then
echo "No agents to run round $round with" 1>&2
rm -rf "$results_dir/round$round"
exit 1
fi
echo "Start round $round"
game=0
get_number="echo \$name | tr ':' ' ' | awk '{print \$2}'"
for a in $agents; do
runa="$agent_dir/$a/$(head --lines=1 $agent_dir/$a/info)"
for b in $agents; do
if [ "$a" == "$b" ]; then continue; fi
for ((i=1;i<=$games_per_pair;++i)); do
runb="$agent_dir/$b/$(head --lines=1 $agent_dir/$b/info)"
f="${a}_${b}_${i}"
game=$(( $game + 1))
l="$results_dir/round$round/$f.log"
err="$results_dir/round$round/$f.err"
echo "Game #$game: $a .vs. $b ($i of $games_per_pair)"
if [ "$swarm_hosts" != "" ]; then
sanity_swarm $swarm_pid
swarm -c "#OUTPUT $f.result#"
swarm -c "$qchess --no-graphics \"$runa\" \"$runb\" --log=$l --log=@web/current\$($get_number).log 2>$err" -l +wrapper.log:100
else
$qchess --no-graphics "$runa" "$runb" --log=$l --log=@web/current.log 1> $f.result 2> $err
if [ "$(wc -l $err | awk '{print $1}')" == "0" ]; then rm $err; fi
fi
done
done
done
if [ "$swarm_hosts" != "" ]; then
echo "Waiting for games to finish"
sanity_swarm $swarm_pid
swarm -c "#BARRIER BLOCK#" # Wait for all games to finish
#Copy over log files (start before updating scores as the scp may take some time)
for h in "local $swarm_hosts"; do
cmd="
if [ \"\$(hostname)\" != \"$webserver\" ]; then
cd $root_dir/$results_dir/round$round;
for i in *.log *.err; do
if [ \"\$(wc -l \$i | awk '{print \$1}')\" != 0 ]; then
scp \$i $webserver/$root_dir/$results_dir/round$round/\$i
fi
done
fi"
sanity_swarm $swarm_pid
swarm -c "#$h:.* &# $cmd" # Execute once on each host
done
fi
echo "Completed $games games with $count agents"
# A bash function. Yes, they do exist.
function update_score
{
if [ -e $agent_dir/$1/score.dat ]; then
score=$(tail --lines=1 $agent_dir/$1/score.dat | awk '{print $1}')
else
score=0
fi
score=$(( $score + $3 ))
echo "$3 $score $2 $4 $5" >> $agent_dir/$1/score.dat
return $score
}
# Go through results
for f in *.result; do
log=round$round/$(echo $f.log | sed -e "s:.result::g")
white=$(echo $f | tr '_' '\t' | awk '{print $1}')
black=$(echo $f | tr '_' '\t' | awk '{print $2}')
if [ "$(cat $f)" == "white" ]; then
update_score $white $black $win_score WIN $log
update_score $black $white $loss_score LOSS $log
elif [ "$(cat $f)" == "black" ]; then
update_score $white $black $loss_score LOSS $log
update_score $black $white $win_score WIN $log
elif [ "$(cat $f)" == "DRAW" ]; then
update_score $white $black $draw_score DRAW $log
update_score $black $white $draw_score DRAW $log
else
echo "Unrecognised result \"$(cat $f)\" in $f" 1>&2
fi
rm $f
done
echo "Updated scores"
#Close the swarm
if [ "$swarm_hosts" != "" ]; then
swarm -c "#BARRIER BLOCK#"
swarm -c "#.*# exit"
fi
echo "Finished at $(date)"