Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / tests / benchmark_dotprod_ccc.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 2, 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 #include <unistd.h>
31 #include <gr_fir_util.h>
32 #include <gr_fir_ccc.h>
33 #include <random.h>
34
35 #define TOTAL_TEST_SIZE        (40 * 1000 * 1000L)
36 #define NTAPS                                256
37 #define BLOCK_SIZE                   (50 * 1000)        /* fits in cache */
38
39 #if ((TOTAL_TEST_SIZE % BLOCK_SIZE) != 0)
40 #error "TOTAL_TEST_SIZE % BLOCK_SIZE must equal 0"
41 #endif
42
43 typedef gr_fir_ccc* (*fir_maker_t)(const std::vector<gr_complex> &taps);
44 typedef gr_fir_ccc  filter_t;
45
46
47 static double
48 timeval_to_double (const struct timeval *tv)
49 {
50   return (double) tv->tv_sec + (double) tv->tv_usec * 1e-6;
51 }
52
53
54 static void
55 benchmark (fir_maker_t filter_maker, const char *implementation_name)
56 {
57   int   i;
58   gr_complex   coeffs[NTAPS];
59   gr_complex   input[BLOCK_SIZE + NTAPS];
60   long  n;
61   gr_complex    result;
62 #ifdef HAVE_SYS_RESOURCE_H
63   struct rusage rusage_start;
64   struct rusage rusage_stop;
65 #else
66   double clock_start;
67   double clock_end;
68 #endif
69
70
71   // setup coefficients and input data
72
73   for (i = 0; i < NTAPS; i++)
74     coeffs[i] = gr_complex(random() - RANDOM_MAX/2, random() - RANDOM_MAX/2);
75
76   for (i = 0; i < BLOCK_SIZE + NTAPS; i++)
77     input[i] = gr_complex(random() - RANDOM_MAX/2, random() - RANDOM_MAX/2);
78
79   std::vector<gr_complex> taps (&coeffs[0], &coeffs[NTAPS]);
80   filter_t *f = filter_maker (taps);
81
82   // get starting CPU usage
83 #ifdef HAVE_SYS_RESOURCE_H
84   if (getrusage (RUSAGE_SELF, &rusage_start) < 0){
85     perror ("getrusage");
86     exit (1);
87   }
88 #else
89   clock_start= (double) clock() * (1000000. / CLOCKS_PER_SEC);
90 #endif
91   // do the actual work
92
93   for (n = 0; n < TOTAL_TEST_SIZE; n += BLOCK_SIZE){
94     int j;
95     for (j = 0; j < BLOCK_SIZE; j++){
96       result = f->filter ((gr_complex*)&input[j]);
97     }
98   }
99
100   // get ending CPU usage
101 #ifdef HAVE_SYS_RESOURCE_H
102   if (getrusage (RUSAGE_SELF, &rusage_stop) < 0){
103     perror ("getrusage");
104     exit (1);
105   }
106
107   // compute results
108
109   double user =
110     timeval_to_double (&rusage_stop.ru_utime)
111     - timeval_to_double (&rusage_start.ru_utime);
112
113   double sys =
114     timeval_to_double (&rusage_stop.ru_stime)
115     - timeval_to_double (&rusage_start.ru_stime);
116
117   double total = user + sys;
118 #else
119   clock_end = (double) clock() * (1000000. / CLOCKS_PER_SEC);
120   double total = clock_end - clock_start;
121 #endif
122
123   double macs = NTAPS * (double) TOTAL_TEST_SIZE;
124
125   printf ("%10s: taps: %4d  input: %4g  cpu: %6.3f  taps/sec: %10.4g  \n",
126           implementation_name, NTAPS, (double) TOTAL_TEST_SIZE, total, macs / total);
127
128   delete f;
129 }
130
131 static void
132 do_all ()
133 {
134   std::vector<gr_fir_ccc_info>          info;
135   gr_fir_util::get_gr_fir_ccc_info (&info);     // get all known CCC implementations
136
137   for (std::vector<gr_fir_ccc_info>::iterator p = info.begin ();
138        p != info.end () ;
139        ++p){
140     benchmark (p->create, p->name);
141   }
142 }
143
144 int
145 main (int argc, char **argv)
146 {
147   do_all ();
148   return 0;
149 }