1#!/usr/bin/env bash
2# Copyright 2009 The Go Authors.  All rights reserved.
3# Use of this source code is governed by a BSD-style
4# license that can be found in the LICENSE file.
5
6set -e
7
8eval $(go tool dist env)
9O=$GOCHAR
10GC="go tool ${O}g"
11LD="go tool ${O}l"
12
13gccm=""
14case "$O" in
158)
16	gccm=-m32;;
176)
18	gccm=-m64;;
19esac
20
21PATH=.:$PATH
22
23havegccgo=false
24if which gccgo >/dev/null 2>&1
25then
26	havegccgo=true
27fi
28
29mode=run
30case X"$1" in
31X-test)
32	mode=test
33	shift
34esac
35
36gc() {
37	$GC $1.go; $LD $1.$O
38}
39
40gc_B() {
41	$GC -B $1.go; $LD $1.$O
42}
43
44runonly() {
45	if [ $mode = run ]
46	then
47		"$@"
48	fi
49}
50
51run() {
52	if [ $mode = test ]
53	then
54		if echo $1 | grep -q '^gc '
55		then
56			$1	# compile the program
57			program=$(echo $1 | sed 's/gc //')
58			shift
59			echo $program
60			$1 <fasta-1000.out > /tmp/$$
61			case $program in
62			chameneosredux)
63				# exact numbers may vary but non-numbers should match
64				grep -v '[0-9]' /tmp/$$ > /tmp/$$x
65				grep -v '[0-9]' chameneosredux.txt > /tmp/$$y
66				cmp /tmp/$$x /tmp/$$y
67				rm -f /tmp/$$ /tmp/$$x /tmp/$$y
68				;;
69			*)
70				cmp /tmp/$$ $program.txt
71				rm -f /tmp/$$
72			esac
73		fi
74		return
75	fi
76	if ! $havegccgo && echo $1 | grep -q '^gccgo '
77	then
78		return
79	fi
80	echo -n '	'$1'	'
81	$1
82	shift
83	
84	echo $((time -p $* >/dev/null) 2>&1) | awk '{print $4 "u " $6 "s " $2 "r"}'
85}
86
87fasta() {
88	runonly echo 'fasta -n 25000000'
89	run "gcc $gccm -O2 fasta.c" a.out 25000000
90	run 'gccgo -O2 fasta.go' a.out -n 25000000	#commented out until WriteString is in bufio
91	run 'gc fasta' $O.out -n 25000000
92	run 'gc_B fasta' $O.out -n 25000000
93}
94
95revcomp() {
96	runonly gcc -O2 fasta.c
97	runonly a.out 25000000 > x
98	runonly echo 'reverse-complement < output-of-fasta-25000000'
99	run "gcc $gccm -O2 reverse-complement.c" a.out < x
100	run 'gccgo -O2 reverse-complement.go' a.out < x
101	run 'gc reverse-complement' $O.out < x
102	run 'gc_B reverse-complement' $O.out < x
103	rm x
104}
105
106nbody() {
107	runonly echo 'nbody -n 50000000'
108	run "gcc $gccm -O2 nbody.c -lm" a.out 50000000
109	run 'gccgo -O2 nbody.go' a.out -n 50000000
110	run 'gc nbody' $O.out -n 50000000
111	run 'gc_B nbody' $O.out -n 50000000
112}
113
114binarytree() {
115	runonly echo 'binary-tree 15 # too slow to use 20'
116	run "gcc $gccm -O2 binary-tree.c -lm" a.out 15
117	run 'gccgo -O2 binary-tree.go' a.out -n 15
118	run 'gccgo -O2 binary-tree-freelist.go' a.out -n 15
119	run 'gc binary-tree' $O.out -n 15
120	run 'gc binary-tree-freelist' $O.out -n 15
121}
122
123fannkuch() {
124	runonly echo 'fannkuch 12'
125	run "gcc $gccm -O2 fannkuch.c" a.out 12
126	run 'gccgo -O2 fannkuch.go' a.out -n 12
127	run 'gccgo -O2 fannkuch-parallel.go' a.out -n 12
128	run 'gc fannkuch' $O.out -n 12
129	run 'gc fannkuch-parallel' $O.out -n 12
130	run 'gc_B fannkuch' $O.out -n 12
131}
132
133regexdna() {
134	runonly gcc -O2 fasta.c
135	runonly a.out 100000 > x
136	runonly echo 'regex-dna 100000'
137	run "gcc $gccm -O2 regex-dna.c -lpcre" a.out <x
138	run 'gccgo -O2 regex-dna.go' a.out <x
139	run 'gccgo -O2 regex-dna-parallel.go' a.out <x
140	run 'gc regex-dna' $O.out <x
141	run 'gc regex-dna-parallel' $O.out <x
142	run 'gc_B regex-dna' $O.out <x
143	rm x
144}
145
146spectralnorm() {
147	runonly echo 'spectral-norm 5500'
148	run "gcc $gccm -O2 spectral-norm.c -lm" a.out 5500
149	run 'gccgo -O2 spectral-norm.go' a.out -n 5500
150	run 'gc spectral-norm' $O.out -n 5500
151	run 'gc_B spectral-norm' $O.out -n 5500
152}
153
154knucleotide() {
155	runonly gcc -O2 fasta.c
156	runonly a.out 1000000 > x  # should be using 25000000
157	runonly echo 'k-nucleotide 1000000'
158	if [ $mode = run ]; then
159		run "gcc -O2 k-nucleotide.c $(pkg-config glib-2.0 --cflags --libs)" a.out <x
160	fi
161	run 'gccgo -O2 k-nucleotide.go' a.out <x
162	run 'gccgo -O2 k-nucleotide-parallel.go' a.out <x
163	run 'gc k-nucleotide' $O.out <x
164	run 'gc k-nucleotide-parallel' $O.out <x
165	run 'gc_B k-nucleotide' $O.out <x
166	rm x
167}
168
169mandelbrot() {
170	runonly echo 'mandelbrot 16000'
171	run "gcc $gccm -O2 mandelbrot.c" a.out 16000
172	run 'gccgo -O2 mandelbrot.go' a.out -n 16000
173	run 'gc mandelbrot' $O.out -n 16000
174	run 'gc_B mandelbrot' $O.out -n 16000
175}
176
177meteor() {
178	runonly echo 'meteor 2098'
179	run "gcc $gccm -O2 meteor-contest.c" a.out 2098
180	run 'gccgo -O2 meteor-contest.go' a.out -n 2098
181	run 'gc meteor-contest' $O.out -n 2098
182	run 'gc_B  meteor-contest' $O.out -n 2098
183}
184
185pidigits() {
186	runonly echo 'pidigits 10000'
187	run "gcc $gccm -O2 pidigits.c -lgmp" a.out 10000
188	run 'gccgo -O2 pidigits.go' a.out -n 10000
189	run 'gc pidigits' $O.out -n 10000
190	run 'gc_B  pidigits' $O.out -n 10000
191}
192
193threadring() {
194	runonly echo 'threadring 50000000'
195	run "gcc $gccm -O2 threadring.c -lpthread" a.out 50000000
196	run 'gccgo -O2 threadring.go' a.out -n 50000000
197	run 'gc threadring' $O.out -n 50000000
198}
199
200chameneos() {
201	runonly echo 'chameneos 6000000'
202	run "gcc $gccm -O2 chameneosredux.c -lpthread" a.out 6000000
203	run 'gccgo -O2 chameneosredux.go' a.out 6000000
204	run 'gc chameneosredux' $O.out 6000000
205}
206
207case $# in
2080)
209	run="fasta revcomp nbody binarytree fannkuch regexdna spectralnorm knucleotide mandelbrot meteor pidigits threadring chameneos"
210	;;
211*)
212	run=$*
213esac
214
215for i in $run
216do
217	$i
218	runonly echo
219done
220