Made networking work hopefully
[progcomp2013.git] / run.sh
1 #!/bin/bash
2
3 # This script runs a single round of the competition
4
5 . vars
6
7 agents=""
8
9 function sanity_swarm
10 {
11         # Ideally swarm shouldn't crash
12         # But I'm a shit programmer
13         # So I'll leave this in
14         kill -0 $1 2>&1 >> /dev/null
15         if [ "$?" != "0" ]; then
16                 echo "Swarm $pid is no longer running! $2" 1>&2
17                 exit 1
18         fi
19 }
20
21 # Copy messages to a log file
22 if [ "$BASH_ARGV" != "_worker_" ]; then
23         # Get the round number
24         mkdir -p $results_dir
25         cd $results_dir
26         round=$(ls | grep "round" | sed -e "s:round::g" | sort -n | head --lines=1)
27         if [ "$round" == "" ]; then round=0; fi
28         round=$(( $round + 1 ))
29         mkdir -p round$round
30         cd $root_dir
31         exec $0 "$@" _worker_ 2>&1 | tee $results_dir/round$round/run.log
32         exit $?
33 else
34         cd $results_dir
35         round=$(ls | grep "round" | sed -e "s:round::g" | sort -n | head --lines=1)
36 fi
37
38 echo "Start at $(date)"
39
40 cd $root_dir
41 # Setup the swarmF
42 if [ "$swarm_hosts" != "" ]; then
43         swarm_pid=$(swarm --daemon -l daemon.log:100)
44         if [ "$?" != "0" ]; then
45                 echo "Couldn't start swarm daemon!" 1>&2
46                 exit 1
47         fi
48         
49         for h in $swarm_hosts; do
50                 if [ "$h" != "local" ]; then
51                         swarm -c "#ABSORB $h#"
52                 fi
53         done
54         sanity_swarm $swarm_pid
55         swarm -c "#.*# cd $root_dir; mkdir -p $results_dir/round$round" -l +wrapper.log:100
56 fi
57
58
59
60 count=0
61 cd $root_dir/$agent_dir
62 for f in $(ls); do
63         if [ -d $f ]; then
64                 info_file=$(ls $f | grep -w "info")
65                 if [ "$info_file" == "" ]; then
66                         echo "Skipping agent $f (missing info file)"
67                 else
68                         count=$(( $count + 1 ))
69                         agents="$agents $f"
70                 fi
71         fi
72 done
73 echo "Found $count agents in $agent_dir/"
74
75
76
77 # Add all the games to swarm
78 cd $root_dir
79
80 if [ "$agents" == "" ]; then
81         echo "No agents to run round $round with" 1>&2
82         rm -rf "$results_dir/round$round"
83         exit 1
84 fi
85
86 echo "Start round $round"
87
88 game=0
89
90 get_number="echo \$name | tr ':' ' ' | awk '{print \$2}'"
91 for a in $agents; do
92         runa="$agent_dir/$a/$(head --lines=1 $agent_dir/$a/info)"
93         for b in $agents; do
94                 if [ "$a" == "$b" ]; then continue; fi
95                 for ((i=1;i<=$games_per_pair;++i)); do
96                         runb="$agent_dir/$b/$(head --lines=1 $agent_dir/$b/info)"
97                         f="${a}_${b}_${i}"
98
99                         game=$(( $game + 1))    
100                         l="$results_dir/round$round/$f.log"
101                         err="$results_dir/round$round/$f.err"
102
103                         echo "Game #$game: $a .vs. $b ($i of $games_per_pair)"
104                         if [ "$swarm_hosts" != "" ]; then
105                                 sanity_swarm $swarm_pid
106                                 swarm -c "#OUTPUT $f.result#"
107                                 swarm -c "$qchess --no-graphics \"$runa\" \"$runb\" --log=$l --log=@web/current\$($get_number).log 2>$err" -l +wrapper.log:100
108                         else
109                                 $qchess --no-graphics "$runa" "$runb" --log=$l --log=@web/current.log 1> $f.result 2> $err
110                                 if [ "$(wc -l $err | awk '{print $1}')" == "0" ]; then rm $err; fi
111                         fi
112                 done
113         done
114 done
115
116
117
118 if [ "$swarm_hosts" != "" ]; then
119         echo "Waiting for games to finish"
120         sanity_swarm $swarm_pid
121         swarm -c "#BARRIER BLOCK#" # Wait for all games to finish
122
123         #Copy over log files (start before updating scores as the scp may take some time)
124         for h in "local $swarm_hosts"; do
125                 cmd="
126                 if [ \"\$(hostname)\" != \"$webserver\" ]; then
127                         cd $root_dir/$results_dir/round$round;
128                         for i in *.log *.err; do
129                                 if [ \"\$(wc -l \$i | awk '{print \$1}')\" != 0 ]; then
130                                         scp \$i $webserver/$root_dir/$results_dir/round$round/\$i
131                                 fi
132                         done
133                 fi"
134                 sanity_swarm $swarm_pid
135                 swarm -c "#$h:.* &# $cmd" # Execute once on each host
136         done
137 fi
138
139 echo "Completed $games games with $count agents"
140
141 # A bash function. Yes, they do exist.
142 function update_score
143 {
144         if [ -e $agent_dir/$1/score.dat ]; then
145                 score=$(tail --lines=1 $agent_dir/$1/score.dat | awk '{print $1}')
146         else
147                 score=0
148         fi
149         
150
151         score=$(( $score + $3 ))
152         echo "$3 $score $2 $4 $5" >> $agent_dir/$1/score.dat
153         return $score
154 }
155
156 # Go through results
157 for f in *.result; do
158         log=round$round/$(echo $f.log | sed -e "s:.result::g")
159         white=$(echo $f | tr '_' '\t' | awk '{print $1}')
160         black=$(echo $f | tr '_' '\t' | awk '{print $2}')
161         if [ "$(cat $f)" == "white" ]; then
162                 update_score $white $black $win_score WIN $log
163                 update_score $black $white $loss_score LOSS $log
164         elif [ "$(cat $f)" == "black" ]; then
165                 update_score $white $black $loss_score LOSS $log
166                 update_score $black $white $win_score WIN $log
167         elif [ "$(cat $f)" == "DRAW" ]; then
168                 update_score $white $black $draw_score DRAW $log
169                 update_score $black $white $draw_score DRAW $log
170         else
171                 echo "Unrecognised result \"$(cat $f)\" in $f" 1>&2
172         fi
173
174         rm $f
175 done
176
177 echo "Updated scores"
178
179 #Close the swarm
180 if [ "$swarm_hosts" != "" ]; then
181         swarm -c "#BARRIER BLOCK#"
182         swarm -c "#.*# exit"
183 fi
184
185 echo "Finished at $(date)"
186

UCC git Repository :: git.ucc.asn.au