#!/usr/bin/env bash if [[ -z $@ ]]; then BINARIES="./gluon ./neutrino ./randino ./charm" else BINARIES="$@" fi NUMGAMES=1 BINARIES="$(echo $BINARIES | tr \ \\n | sort | tr \\n \ )" if [[ ! -e competitions ]]; then mkdir competitions || exit 1 fi if [[ ! -e competitionstubs ]]; then mkdir competitionstubs || exit 1 fi find competitions/ -type f -delete find competitionstubs/ -type f -delete FIFONAME=fullcompMT_output_fifo.fifo [[ -e $FIFONAME ]] && rm $FIFONAME mkfifo fullcompMT_output_fifo.fifo function count { echo $# } function finish { printf "\x1B[$((NBINS + 5));1H" kill $TAILPID ./fullcompstats.py rm $FIFONAME } tail -F "$FIFONAME" & TAILPID=$! trap finish EXIT DATE_FMT="%Y-%m-%d %H:%M:%S" NBINS=$(count $BINARIES) #SOURCE: competition.sh, by Wilmer van der Gaast # Don't count the number of logical cores/threads. Although a competition # finishes slightly faster when using all (hyper)threads, it's only a 10-20% # improvement instead of the ~100% you'd expect. You just get threads tied # up waiting for execution units very often. Nice when testing, but it ruins # the time limits/etc. if [ -z "$num_cores" ] && [ -e /proc/cpuinfo ]; then num_cores=$(grep ^'core id\b' /proc/cpuinfo | sort | uniq | wc -l) fi if [ -z "$num_cores" ] || [ "$num_cores" -lt "1" ]; then # OS X, src: # http://stackoverflow.com/questions/1715580/how-to-discover-number-of-cores-on-mac-os-x # http://www.opensource.apple.com/source/xnu/xnu-792.13.8/libkern/libkern/sysctl.h num_cores=$(sysctl -n hw.physicalcpu || echo 0) fi if [ -z "$num_cores" ] || [ "$num_cores" -lt "1" ]; then num_cores=2 echo "Couldn't figure out number of cores, will guess $num_cores." else echo "Number of cores (w/o Hyper-Threading): $num_cores." fi function maxlen { max="${#1}" for v in "$@"; do [[ "${#v}" -gt "$max" ]] && max="${#v}" done echo $max } max_command_len=$(maxlen $BINARIES) function repstring { printf "%.0s$1" $(seq 1 $2) } function green { printf '\x1B[33m%s\x1B[0m' $1 } function red { printf '\x1B[31m%s\x1B[0m' $1 } function blue { printf '\x1B[36m%s\x1B[0m' $1 } clear #clear the screen #print the table printf "$(repstring ' ' $max_command_len)|" for p1 in $BINARIES; do printf "% -4s|" "${p1:0:4}"; done echo printf "$(repstring ' ' $max_command_len)|" for p1 in $BINARIES; do printf "% -4s|" "${p1:4:4}"; done printf "\x1B[3;1H$(repstring - $max_command_len)+$(repstring ----+ $NBINS )" ponei=1 for p1 in $BINARIES; do y=$((3 + ponei)) printf "\x1B[$y;1H$p1\x1B[$y;$((max_command_len + 1))H|$(repstring ' |' $NBINS)" ponei=$((ponei + 1)) done printf "\x1B[$((NBINS + 4));1H>competition.log\n" #prepare the actual competition ponei=1 for p1 in $BINARIES; do ptwoi=1 for p2 in $BINARIES; do if [[ $p1 == $p2 ]]; then ptwoi=$((ptwoi + 1)) continue fi p1pretty=$(echo "$p1" | sed 's/[^a-zA-Z0-9 ]//g') p2pretty=$(echo "$p2" | sed 's/[^a-zA-Z0-9 ]//g') for i in $(seq 1 $NUMGAMES); do FNAMEFORMAT="game_${p1pretty}_vs_${p2pretty}.$(printf '%03d' $i)" COMPFILE="competitionstubs/$FNAMEFORMAT.sh" CLOGFILE="competitions/$FNAMEFORMAT.txt" gotocellcmd1=$(printf '\x1B[%d;%dH' $((3+ponei)) $((max_command_len-2+5*ptwoi))) gotocellcmd2=$(printf '\x1B[%d;%dH' $((3+ptwoi)) $((max_command_len-2+5*ponei+1))) cat >"$COMPFILE" << EOF #!/usr/bin/env bash printf "%s\n%s\n" $p1 $p2 | ./competition.py -qc "$CLOGFILE" - status=$? if [[ \$status != 0 ]]; then echo "${gotocellcmd1}E${gotocellcmd2}E" >$FIFONAME echo \$(date +"$DATE_FMT") "$p1 - $p2 : ERROR $status (0-0)" >competition.log exit 1 fi lastline=\$(tail -n1 "$CLOGFILE") if [[ "\$lastline" == "P1 won" ]]; then echo "${gotocellcmd1}$(green W)${gotocellcmd2}$(red L)" >$FIFONAME echo \$(date +"$DATE_FMT") $(green "$p1") "- $p2 : WIN - LOSS (3-1)" >competition.log elif [[ "\$lastline" == "P2 won" ]]; then echo "${gotocellcmd1}$(red L)${gotocellcmd2}$(green W)" >$FIFONAME echo \$(date +"$DATE_FMT") "$p1 -" $(green $p2) ": LOSS - WIN (1-3)" >competition.log elif [[ "\$lastline" == "Tie" ]]; then echo "${gotocellcmd1}$(blue T)${gotocellcmd2}$(blue T)" >$FIFONAME echo \$(date +"$DATE_FMT") "$p1" $(green -) "$p2 : TIE (1-1)" >competition.log fi EOF chmod +x $COMPFILE done ptwoi=$((ptwoi + 1)) done ponei=$((ponei + 1)) done echo COMPETITION START >competition.log #run the competition in threads find competitionstubs -type f -name 'game_*' | xargs -P$num_cores -n1 -- bash