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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
#!/usr/bin/python
# Copyright (C) 2014-2015 Free Software Foundation, Inc.
# This file is part of the GNU C Library.
#
# The GNU C Library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# The GNU C Library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with the GNU C Library; if not, see
# <http://www.gnu.org/licenses/>.
"""Benchmark program generator script
This script takes a function name as input and generates a program using
an input file located in the benchtests directory. The name of the
input file should be of the form foo-inputs where 'foo' is the name of
the function.
"""
from __future__ import print_function
import sys
import os
import itertools
# Macro definitions for functions that take no arguments. For functions
# that take arguments, the STRUCT_TEMPLATE, ARGS_TEMPLATE and
# VARIANTS_TEMPLATE are used instead.
DEFINES_TEMPLATE = '''
#define CALL_BENCH_FUNC(v, i) %(func)s ()
#define NUM_VARIANTS (1)
#define NUM_SAMPLES(v) (1)
#define VARIANT(v) FUNCNAME "()"
'''
# Structures to store arguments for the function call. A function may
# have its inputs partitioned to represent distinct performance
# characteristics or distinct flavors of the function. Each such
# variant is represented by the _VARIANT structure. The ARGS structure
# represents a single set of arguments.
STRUCT_TEMPLATE = '''
#define CALL_BENCH_FUNC(v, i) %(func)s (%(func_args)s)
struct args
{
%(args)s
double timing;
};
struct _variants
{
const char *name;
int count;
struct args *in;
};
'''
# The actual input arguments.
ARGS_TEMPLATE = '''
struct args in%(argnum)d[%(num_args)d] = {
%(args)s
};
'''
# The actual variants, along with macros defined to access the variants.
VARIANTS_TEMPLATE = '''
struct _variants variants[%(num_variants)d] = {
%(variants)s
};
#define NUM_VARIANTS %(num_variants)d
#define NUM_SAMPLES(i) (variants[i].count)
#define VARIANT(i) (variants[i].name)
'''
# Epilogue for the generated source file.
EPILOGUE = '''
#define RESULT(__v, __i) (variants[(__v)].in[(__i)].timing)
#define RESULT_ACCUM(r, v, i, old, new) \\
((RESULT ((v), (i))) = (RESULT ((v), (i)) * (old) + (r)) / ((new) + 1))
#define BENCH_FUNC(i, j) ({%(getret)s CALL_BENCH_FUNC (i, j);})
#define FUNCNAME "%(func)s"
#include "bench-skeleton.c"'''
def gen_source(func, directives, all_vals):
"""Generate source for the function
Generate the C source for the function from the values and
directives.
Args:
func: The function name
directives: A dictionary of directives applicable to this function
all_vals: A dictionary input values
"""
# The includes go in first.
for header in directives['includes']:
print('#include <%s>' % header)
for header in directives['include-sources']:
print('#include "%s"' % header)
# Print macros. This branches out to a separate routine if
# the function takes arguments.
if not directives['args']:
print(DEFINES_TEMPLATE % {'func': func})
outargs = []
else:
outargs = _print_arg_data(func, directives, all_vals)
# Print the output variable definitions if necessary.
for out in outargs:
print(out)
# If we have a return value from the function, make sure it is
# assigned to prevent the compiler from optimizing out the
# call.
if directives['ret']:
print('static %s volatile ret;' % directives['ret'])
getret = 'ret = '
else:
getret = ''
# Test initialization.
if directives['init']:
print('#define BENCH_INIT %s' % directives['init'])
print(EPILOGUE % {'getret': getret, 'func': func})
def _print_arg_data(func, directives, all_vals):
"""Print argument data
This is a helper function for gen_source that prints structure and
values for arguments and their variants and returns output arguments
if any are found.
Args:
func: Function name
directives: A dictionary of directives applicable to this function
all_vals: A dictionary input values
Returns:
Returns a list of definitions for function arguments that act as
output parameters.
"""
# First, all of the definitions. We process writing of
# CALL_BENCH_FUNC, struct args and also the output arguments
# together in a single traversal of the arguments list.
func_args = []
arg_struct = []
outargs = []
for arg, i in zip(directives['args'], itertools.count()):
if arg[0] == '<' and arg[-1] == '>':
pos = arg.rfind('*')
if pos == -1:
die('Output argument must be a pointer type')
outargs.append('static %s out%d __attribute__((used));' % (arg[1:pos], i))
func_args.append(' &out%d' % i)
else:
arg_struct.append(' %s volatile arg%d;' % (arg, i))
func_args.append('variants[v].in[i].arg%d' % i)
print(STRUCT_TEMPLATE % {'args' : '\n'.join(arg_struct), 'func': func,
'func_args': ', '.join(func_args)})
# Now print the values.
variants = []
for (k, vals), i in zip(all_vals.items(), itertools.count()):
out = [' {%s, 0},' % v for v in vals]
# Members for the variants structure list that we will
# print later.
variants.append(' {"%s", %d, in%d},' % (k, len(vals), i))
print(ARGS_TEMPLATE % {'argnum': i, 'num_args': len(vals),
'args': '\n'.join(out)})
# Print the variants and the last set of macros.
print(VARIANTS_TEMPLATE % {'num_variants': len(all_vals),
'variants': '\n'.join(variants)})
return outargs
def _process_directive(d_name, d_val):
"""Process a directive.
Evaluate the directive name and value passed and return the
processed value. This is a helper function for parse_file.
Args:
d_name: Name of the directive
d_val: The string value to process
Returns:
The processed value, which may be the string as it is or an object
that describes the directive.
"""
# Process the directive values if necessary. name and ret don't
# need any processing.
if d_name.startswith('include'):
d_val = d_val.split(',')
elif d_name == 'args':
d_val = d_val.split(':')
# Return the values.
return d_val
def parse_file(func):
"""Parse an input file
Given a function name, open and parse an input file for the function
and get the necessary parameters for the generated code and the list
of inputs.
Args:
func: The function name
Returns:
A tuple of two elements, one a dictionary of directives and the
other a dictionary of all input values.
"""
all_vals = {}
# Valid directives.
directives = {
'name': '',
'args': [],
'includes': [],
'include-sources': [],
'ret': '',
'init': ''
}
try:
with open('%s-inputs' % func) as f:
for line in f:
# Look for directives and parse it if found.
if line.startswith('##'):
try:
d_name, d_val = line[2:].split(':', 1)
d_name = d_name.strip()
d_val = d_val.strip()
directives[d_name] = _process_directive(d_name, d_val)
except (IndexError, KeyError):
die('Invalid directive: %s' % line[2:])
# Skip blank lines and comments.
line = line.split('#', 1)[0].rstrip()
if not line:
continue
# Otherwise, we're an input. Add to the appropriate
# input set.
cur_name = directives['name']
all_vals.setdefault(cur_name, [])
all_vals[cur_name].append(line)
except IOError as ex:
die("Failed to open input file (%s): %s" % (ex.filename, ex.strerror))
return directives, all_vals
def die(msg):
"""Exit with an error
Prints an error message to the standard error stream and exits with
a non-zero status.
Args:
msg: The error message to print to standard error
"""
print('%s\n' % msg, file=sys.stderr)
sys.exit(os.EX_DATAERR)
def main(args):
"""Main function
Use the first command line argument as function name and parse its
input file to generate C source that calls the function repeatedly
for the input.
Args:
args: The command line arguments with the program name dropped
Returns:
os.EX_USAGE on error and os.EX_OK on success.
"""
if len(args) != 1:
print('Usage: %s <function>' % sys.argv[0])
return os.EX_USAGE
directives, all_vals = parse_file(args[0])
gen_source(args[0], directives, all_vals)
return os.EX_OK
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
|