aboutsummaryrefslogtreecommitdiff
path: root/fullcompMT.sh
blob: 93b44862119a0787a7d975a8d240282848075e08 (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
#!/usr/bin/env bash
if [[ -z $@ ]]; then
	BINARIES="./gluon ./neutrino ./randino ./charm"
else
	BINARIES="$@"
fi
NUMGAMES=10

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

tail -F "$FIFONAME" &
TAILPID=$!
trap "kill $TAILPID; ./fullcompstats.py; rm $FIFONAME" EXIT


DATE_FMT="%Y-%m-%d %H:%M:%S"



#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



if [[ -t 1 ]]; then
	function green {
		printf '\x1B[33m%s\x1B[0m' $1
	}
else
	function green {
		printf '%s' $1
	}
fi



for p1 in $BINARIES; do
	for p2 in $BINARIES; do
		[[ $p1 == $p2 ]] && continue
		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"
			cat >"$COMPFILE" << EOF
#!/usr/bin/env bash
printf "%s\n%s\n" $p1 $p2 | ./competition.py -qc "$CLOGFILE" -
status=$?
if [[ \$status != 0 ]]; then
	echo \$(date +"$DATE_FMT") "$p1 - $p2 : ERROR $status     (0-0)" >$FIFONAME
	exit 1
fi
lastline=\$(tail -n1 "$CLOGFILE")
if [[ "\$lastline" == "P1 won" ]]; then
	echo \$(date +"$DATE_FMT") $(green "$p1") "- $p2 : WIN - LOSS (3-1)" >$FIFONAME
elif [[ "\$lastline" == "P2 won" ]]; then
	echo \$(date +"$DATE_FMT") "$p1 -" $(green $p2) ": LOSS - WIN (1-3)" >$FIFONAME
elif [[ "\$lastline" == "Tie" ]]; then
	echo \$(date +"$DATE_FMT") "$p1" $(green -) "$p2 :    TIE     (1-1)" >$FIFONAME
fi
EOF
			chmod +x $COMPFILE
		done
	done
done

find competitionstubs -type f -name 'game_*' | xargs -P$num_cores -n1 -- bash