1#!/usr/bin/env python
2
3from __future__ import print_function
4from __future__ import unicode_literals
5from gnuradio import gr
6from gnuradio import blocks
7import argparse
8from volk_test_funcs import *
9
10######################################################################
11
12def float_to_char(N):
13    op = blocks.float_to_char()
14    tb = helper(N, op, gr.sizeof_float, gr.sizeof_char, 1, 1)
15    return tb
16
17######################################################################
18
19def float_to_int(N):
20    op = blocks.float_to_int()
21    tb = helper(N, op, gr.sizeof_float, gr.sizeof_int, 1, 1)
22    return tb
23
24######################################################################
25
26def float_to_short(N):
27    op = blocks.float_to_short()
28    tb = helper(N, op, gr.sizeof_float, gr.sizeof_short, 1, 1)
29    return tb
30
31######################################################################
32
33def short_to_float(N):
34    op = blocks.short_to_float()
35    tb = helper(N, op, gr.sizeof_short, gr.sizeof_float, 1, 1)
36    return tb
37
38######################################################################
39
40def short_to_char(N):
41    op = blocks.short_to_char()
42    tb = helper(N, op, gr.sizeof_short, gr.sizeof_char, 1, 1)
43    return tb
44
45######################################################################
46
47def char_to_short(N):
48    op = blocks.char_to_short()
49    tb = helper(N, op, gr.sizeof_char, gr.sizeof_short, 1, 1)
50
51######################################################################
52
53def char_to_float(N):
54    op = blocks.char_to_float()
55    tb = helper(N, op, gr.sizeof_char, gr.sizeof_float, 1, 1)
56
57#####################################################################
58
59def int_to_float(N):
60    op = blocks.int_to_float()
61    tb = helper(N, op, gr.sizeof_int, gr.sizeof_float, 1, 1)
62    return tb
63
64######################################################################
65
66def complex_to_float(N):
67    op = blocks.complex_to_float()
68    tb = helper(N, op, gr.sizeof_gr_complex, gr.sizeof_float, 1, 2)
69    return tb
70
71######################################################################
72
73def complex_to_real(N):
74    op = blocks.complex_to_real()
75    tb = helper(N, op, gr.sizeof_gr_complex, gr.sizeof_float, 1, 1)
76    return tb
77
78######################################################################
79
80def complex_to_imag(N):
81    op = blocks.complex_to_imag()
82    tb = helper(N, op, gr.sizeof_gr_complex, gr.sizeof_float, 1, 1)
83    return tb
84
85######################################################################
86
87def complex_to_mag(N):
88    op = blocks.complex_to_mag()
89    tb = helper(N, op, gr.sizeof_gr_complex, gr.sizeof_float, 1, 1)
90    return tb
91
92######################################################################
93
94def complex_to_mag_squared(N):
95    op = blocks.complex_to_mag_squared()
96    tb = helper(N, op, gr.sizeof_gr_complex, gr.sizeof_float, 1, 1)
97    return tb
98
99######################################################################
100
101
102def run_tests(func, N, iters):
103    print("Running Test: {0}".format(func.__name__))
104    try:
105        tb = func(N)
106        t = timeit(tb, iters)
107        res = format_results(func.__name__, t)
108        return res
109    except AttributeError:
110        print("\tCould not run test. Skipping.")
111        return None
112
113def main():
114    avail_tests = [float_to_char,
115                   float_to_int,
116                   float_to_short,
117                   short_to_float,
118                   short_to_char,
119                   char_to_short,
120                   char_to_float,
121                   int_to_float,
122                   complex_to_float,
123                   complex_to_real,
124                   complex_to_imag,
125                   complex_to_mag,
126                   complex_to_mag_squared]
127
128    desc='Time an operation to compare with other implementations. \
129          This program runs a simple GNU Radio flowgraph to test a \
130          particular math function, mostly to compare the  \
131          Volk-optimized implementation versus a regular \
132          implementation. The results are stored to an SQLite database \
133          that can then be read by volk_plot.py to plot the differences.'
134    parser = argparse.ArgumentParser(description=desc)
135    parser.add_argument('-L', '--label', type=str,
136                        required=True, default=None,
137                        help='Label of database table [default: %(default)s]')
138    parser.add_argument('-D', '--database', type=str,
139                        default="volk_results.db",
140                        help='Database file to store data in [default: %(default)s]')
141    parser.add_argument('-N', '--nitems', type=float,
142                        default=1e9,
143                        help='Number of items per iterations [default: %(default)s]')
144    parser.add_argument('-I', '--iterations', type=int,
145                        default=20,
146                        help='Number of iterations [default: %(default)s]')
147    parser.add_argument('--tests', type=int, nargs='*',
148                        choices=list(range(len(avail_tests))),
149                        help='A list of tests to run; can be a single test or a \
150                              space-separated list.')
151    parser.add_argument('--list', action='store_true',
152                        help='List the available tests')
153    parser.add_argument('--all', action='store_true',
154                        help='Run all tests')
155    args = parser.parse_args()
156
157    if(args.list):
158        print("Available Tests to Run:")
159        print("\n".join(["\t{0}: {1}".format(i,f.__name__) for i,f in enumerate(avail_tests)]))
160        sys.exit(0)
161
162    N = int(args.nitems)
163    iters = args.iterations
164    label = args.label
165
166    conn = create_connection(args.database)
167    new_table(conn, label)
168
169    if args.all:
170        tests = list(range(len(avail_tests)))
171    else:
172        tests = args.tests
173
174    for test in tests:
175        res = run_tests(avail_tests[test], N, iters)
176        if res is not None:
177            replace_results(conn, label, N, iters, res)
178
179if __name__ == "__main__":
180    try:
181        main()
182    except KeyboardInterrupt:
183        pass
184