-
Notifications
You must be signed in to change notification settings - Fork 0
/
perf.sh
executable file
·279 lines (249 loc) · 6.88 KB
/
perf.sh
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env bash
# Usage: ./perf.sh acos
set -e
export LC_ALL=C.UTF-8
S=20 # trial
N=100000 # count
M=${PERF_REPEAT:-500} # repeat
if [ -z "$CORE_MATH_QUIET" ]; then
{ read -r -d '' prog_end || true; } <<EOF
END {
s = 0;
nout = int(5*i/100);
for(k = 1; k < i-nout; k++){
d = a[k] - a[0];
s += d*d;
}
rms = sqrt(s/(i-nout-1));
nmd = a[int(i/2)] - a[0];
printf "Ntrial = %d ; Min = %.3f + %.3f clc/call; Median-Min = %.3f clc/call; Max = %.3f clc/call;\n", i, a[0], rms, nmd, a[i-1];
}
EOF
else
{ read -r -d '' prog_end || true; } <<EOF
END {
s = 0;
nout = int(5*i/100);
for(k = 1; k < i-nout; k++){
d = a[k] - a[0];
s += d*d;
}
rms = sqrt(s/(i-nout-1));
nmd = a[int(i/2)] - a[0];
printf "%.3f\n", a[0];
}
EOF
fi
prog_bar () {
local T=$1 i=$2
local p=$(( i * 100 / T ))
local j=$(( T - i ))
printf "[%s%s] %3d %%\r" "${str_hsh:0:${i}}" "${str_dot:0:${j}}" $p
if (( i == T )); then
echo
fi
}
collect_perf_stat () {
echo -n "" > $LOG_FILE
if [ -z "$CORE_MATH_QUIET" ]; then
prog_bar $S 0
fi
local i=1
while [ $i -le $S ]; do
perf stat -e cycles -x " " $CORE_MATH_LAUNCHER ./perf $PERF_ARGS &>> $LOG_FILE
if [ -z "$CORE_MATH_QUIET" ]; then
prog_bar $S $i
fi
i=$(( i + 1 ))
done
}
process_perf_stat () {
sort -g -k 1 $LOG_FILE | awk "/cycles/{a[i++]=\$1/(${N}*${M});} ${prog_end}"
}
perform_perf_stat () {
perf stat -e cycles -x " " $CORE_MATH_LAUNCHER ./perf $PERF_ARGS 2>&1 | awk "/cycles/{print \$1/(${N}*${M})}"
}
proc_perf () {
if [ -z "$CORE_MATH_SIMPLE_STAT" ]; then
collect_perf_stat
process_perf_stat
else
perform_perf_stat
fi
}
collect_rdtsc_stat () {
echo -n "" > $LOG_FILE
if [ -z "$CORE_MATH_QUIET" ]; then
prog_bar $S 0
fi
local i=1
while [ $i -le $S ]; do
$CORE_MATH_LAUNCHER ./perf $PERF_ARGS &>> $LOG_FILE
if [ -z "$CORE_MATH_QUIET" ]; then
prog_bar $S $i
fi
i=$(( i + 1 ))
done
}
process_rdtsc_stat () {
sort -g $LOG_FILE | awk "{a[i++]=\$1;} ${prog_end}"
}
perform_rdtsc_stat () {
$CORE_MATH_LAUNCHER ./perf $PERF_ARGS
}
proc_rdtsc () {
if [ -z "$CORE_MATH_SIMPLE_STAT" ]; then
collect_rdtsc_stat
process_rdtsc_stat
else
perform_rdtsc_stat
fi
}
has_symbol () {
[ "$(nm "$LIBM" | while read a b c; do if [ "$c" = "$f" ]; then echo OK; return; fi; done | wc -l)" -ge 1 ]
}
RANDOMS_FILE="$(mktemp /tmp/core-math.XXXXXX)"
LOG_FILE="$(mktemp /tmp/core-math.XXXXXX)"
# trap "rm -f $RANDOMS_FILE $LOG_FILE" 0
f=$1
u="$(echo src/binary*/*/$f.c)"
if [ -z "${HOME_PATH}" ]; then
HOME_PATH="/home/lnt/experiment"
fi
shift
while [ -n "$1" ]; do
case "$1" in
--skip_core_math ) SKIP_CORE_MATH=TRUE; shift ;;
--skip_system_libc ) SKIP_SYSTEM_LIBC=TRUE; shift ;;
--show_perf_error ) SHOW_PERF_ERROR=TRUE; shift ;;
--latency ) LATENCY_MODE=TRUE; PERF_ARGS="--latency"; shift ;;
--rdtsc ) CORE_MATH_PERF_MODE="rdtsc"; shift ;;
--perf ) CORE_MATH_PERF_MODE="perf"; shift ;;
--llvm ) export LIBM="${HOME_PATH}/llvm-project/build/projects/libc/lib/libllvmlibc.a"; shift ;;
--llvm2 ) export LIBM="${HOME_PATH}/llvm/llvm-project/build/projects/libc/lib/libllvmlibc.a"; shift ;;
--musl ) export CC="musl-gcc"; shift ;;
--simple_stat ) CORE_MATH_SIMPLE_STAT=TRUE; shift ;;
--quiet ) CORE_MATH_QUET=TRUE; shift ;;
--fast ) N=10000; shift ;;
* ) break ;;
esac
done
if [ -z "$CORE_MATH_PERF_MODE" ]; then
if [ -z "$CORE_MATH_QUIET" ]; then
echo 'CORE_MATH_PERF_MODE (perf or rdtsc) environment variable is not set. The default is rdtsc.'
fi
CORE_MATH_PERF_MODE=rdtsc
fi
if [ -f "$u" ]; then
dir="${u%/*}"
else
echo "Unknown function: $f"
exit 2
fi
# for clang we might want to add -ffp-contract=on to enable FMA
if [ "$CFLAGS" == "" ]; then
CFLAGS="-O3 -march=native"
ROUNDING_MATH="-frounding-math"
if [ "$CC" == "icx" ]; then
# for icx we need to add -fp-model=strict for full IEEE 754 support
# and we don't need -frounding-math (which does slow down acoshf for example)
CFLAGS="-O3 -fp-model=strict"
ROUNDING_MATH=
fi
fi
export CFLAGS ROUNDING_MATH
if [ -n "$LIBM" ]; then
if [ -z "$CORE_MATH_QUIET" ]; then
echo "LIBC-location: $LIBM"
fi
BACKUP_LIBM="$LIBM"
unset LIBM
fi
if [ -z "$CORE_MATH_QUIET" ]; then
make -s -C src/generic/support clean
make -s -C src/generic/support all
$CORE_MATH_LAUNCHER src/generic/support/glibc_version >&2
str_hsh=""
str_dot=""
i=1
while [ $i -le $S ]; do
str_hsh="${str_hsh}#"
str_dot="${str_dot}."
i=$(( i + 1 ))
done
fi
cd $dir
make -s clean
make -s perf
# prepare random arguments for performance test
./perf --file ${RANDOMS_FILE} --count ${N} --reference
PERF_ARGS="${PERF_ARGS} --file ${RANDOMS_FILE} --count ${N} --repeat ${M}"
if [ "$CORE_MATH_PERF_MODE" = rdtsc ]; then
PERF_ARGS="${PERF_ARGS} --rdtsc"
fi
if [ -z "$SKIP_CORE_MATH" ]; then
if [ -z "$CORE_MATH_QUIET" ]; then
if [ -z "$LATENCY_MODE" ]; then
echo "-- CORE-MATH reciprocal throughput --"
else
echo "-- CORE-MATH latency --"
fi
fi
if [ "$CORE_MATH_PERF_MODE" = perf ]; then
proc_perf
elif [ "$CORE_MATH_PERF_MODE" = rdtsc ]; then
proc_rdtsc
fi
fi
CFLAGS="${CFLAGS} -DSKIP_C_FUNC_REDEF=1"
export CFLAGS
if [ -z "$SKIP_SYSTEM_LIBC" ]; then
make -s clean
if ! (make -s perf &> /dev/null); then
if [ -z "$CORE_MATH_QUIET" ]; then
echo "$f is not present in system libc; skipping"
else
echo "N/A"
fi
else
if [ -z "$CORE_MATH_QUIET" ]; then
if [ -z "$LATENCY_MODE" ]; then
echo "-- System LIBC reciprocal throughput --"
else
echo "-- System LIBC latency --"
fi
fi
PERF_ARGS="${PERF_ARGS} --libc"
if [ "$CORE_MATH_PERF_MODE" = perf ]; then
proc_perf
elif [ "$CORE_MATH_PERF_MODE" = rdtsc ]; then
proc_rdtsc
fi
fi
fi
if [ -n "$BACKUP_LIBM" ]; then
export LIBM="$BACKUP_LIBM"
CFLAGS="${CFLAGS} -DSKIP_C_FUNC_REDEF=1"
export CFLAGS
if has_symbol; then
PERF_ARGS="${PERF_ARGS} --libc"
make -s clean
make -s perf
if [ -z "$CORE_MATH_QUIET" ]; then
if [ -z "$LATENCY_MODE" ]; then
echo "-- LIBC reciprocal throughput --"
else
echo "-- LIBC latency --"
fi
fi
if [ "$CORE_MATH_PERF_MODE" = perf ]; then
proc_perf
elif [ "$CORE_MATH_PERF_MODE" = rdtsc ]; then
proc_rdtsc
fi
elif [ -z "$CORE_MATH_QUIET" ]; then
echo "$f is not present in $LIBM; skipping"
else
echo "N/A"
fi
fi