Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / tests / benchmark_dotprod_fff.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <stdio.h>
26 #include <sys/time.h>
27 #ifdef HAVE_SYS_RESOURCE_H
28 #include <sys/resource.h>
29 #endif
30
31 #include <unistd.h>
32 #include <gr_fir_util.h>
33 #include <gr_fir_fff.h>
34 #include <random.h>
35
36 #define TOTAL_TEST_SIZE        (40 * 1000 * 1000L)
37 #define NTAPS                                256
38 #define BLOCK_SIZE                   (50 * 1000)        /* fits in cache */
39
40 #if ((TOTAL_TEST_SIZE % BLOCK_SIZE) != 0)
41 #error "TOTAL_TEST_SIZE % BLOCK_SIZE must equal 0"
42 #endif
43
44 typedef gr_fir_fff* (*fir_maker_t)(const std::vector<float> &taps);
45 typedef gr_fir_fff  filter_t;
46
47
48 static double
49 timeval_to_double (const struct timeval *tv)
50 {
51   return (double) tv->tv_sec + (double) tv->tv_usec * 1e-6;
52 }
53
54
55 static void
56 benchmark (fir_maker_t filter_maker, const char *implementation_name)
57 {
58   int   i;
59   float coeffs[NTAPS];
60   float input[BLOCK_SIZE + NTAPS];
61   long  n;
62   float result;
63 #ifdef HAVE_SYS_RESOURCE_H
64   struct rusage rusage_start;
65   struct rusage rusage_stop;
66 #else
67   double clock_start;
68   double clock_end;
69 #endif
70   // setup coefficients and input data
71
72   for (i = 0; i < NTAPS; i++)
73     coeffs[i] = random() - RANDOM_MAX/2;
74
75   for (i = 0; i < BLOCK_SIZE + NTAPS; i++)
76     input[i] = random() - RANDOM_MAX/2;
77
78   std::vector<float> taps (&coeffs[0], &coeffs[NTAPS]);
79   filter_t *f = filter_maker (taps);
80
81   // get starting CPU usage
82 #ifdef HAVE_SYS_RESOURCE_H
83   if (getrusage (RUSAGE_SELF, &rusage_start) < 0){
84     perror ("getrusage");
85     exit (1);
86   }
87 #else
88   clock_start = (double) clock() * (1000000. / CLOCKS_PER_SEC);
89 #endif
90   // do the actual work
91
92   for (n = 0; n < TOTAL_TEST_SIZE; n += BLOCK_SIZE){
93     int j;
94     for (j = 0; j < BLOCK_SIZE; j++){
95       result = f->filter (&input[j]);
96     }
97   }
98
99   // get ending CPU usage
100 #ifdef HAVE_SYS_RESOURCE_H
101   if (getrusage (RUSAGE_SELF, &rusage_stop) < 0){
102     perror ("getrusage");
103     exit (1);
104   }
105
106   // compute results
107
108   double user =
109     timeval_to_double (&rusage_stop.ru_utime)
110     - timeval_to_double (&rusage_start.ru_utime);
111
112   double sys =
113     timeval_to_double (&rusage_stop.ru_stime)
114     - timeval_to_double (&rusage_start.ru_stime);
115
116   double total = user + sys;
117 #else
118   clock_end = (double) clock () * (1000000. / CLOCKS_PER_SEC);
119   double total = clock_end -clock_start;
120 #endif
121   double macs = NTAPS * (double) TOTAL_TEST_SIZE;
122
123   printf ("%10s: taps: %4d  input: %4g  cpu: %6.3f  taps/sec: %10.4g  \n",
124           implementation_name, NTAPS, (double) TOTAL_TEST_SIZE, total, macs / total);
125
126   delete f;
127 }
128
129 static void
130 do_all ()
131 {
132   std::vector<gr_fir_fff_info>          info;
133   gr_fir_util::get_gr_fir_fff_info (&info);     // get all known FFF implementations
134
135   for (std::vector<gr_fir_fff_info>::iterator p = info.begin ();
136        p != info.end ();
137        ++p){
138
139     benchmark (p->create, p->name);
140   }
141 }
142
143 int
144 main (int argc, char **argv)
145 {
146   do_all ();
147   return 0;
148 }