aboutsummaryrefslogtreecommitdiff
path: root/fullcompTabMT.sh
blob: c592c00e5dcf2af34b7856144ecf33a964350997 (plain)
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
#!/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