summaryrefslogtreecommitdiff
path: root/gnuradio-core/src/tests/benchmark_dotprod.cc
blob: 344821c569ce608918510605f7539a0d97871157 (plain)
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
/* -*- c++ -*- */
/*
 * Copyright 2002 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * GNU Radio 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <sys/time.h>
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif

#include <unistd.h>
#include <gr_fir_util.h>
#include <gr_fir_fff.h>
#include <random.h>

#define TOTAL_TEST_SIZE	       (40 * 1000 * 1000L)
#define	NTAPS		                     256
#define	BLOCK_SIZE	             (50 * 1000)	/* fits in cache */

#if ((TOTAL_TEST_SIZE % BLOCK_SIZE) != 0)
#error "TOTAL_TEST_SIZE % BLOCK_SIZE must equal 0"
#endif

typedef gr_fir_fff* (*fir_maker_t)(const std::vector<float> &taps);
typedef gr_fir_fff  filter_t;


static double
timeval_to_double (const struct timeval *tv)
{
  return (double) tv->tv_sec + (double) tv->tv_usec * 1e-6;
}


static void
benchmark (fir_maker_t filter_maker, const char *implementation_name)
{
  int	i;
  float coeffs[NTAPS];
  float input[BLOCK_SIZE + NTAPS];
  long	n;
  float	result;
#ifdef HAVE_SYS_RESOURCE_H
  struct rusage	rusage_start;
  struct rusage	rusage_stop;
#else
  double clock_start;
  double clock_end;
#endif
  // setup coefficients and input data

  for (i = 0; i < NTAPS; i++)
    coeffs[i] = random() - RANDOM_MAX/2;

  for (i = 0; i < BLOCK_SIZE + NTAPS; i++)
    input[i] = random() - RANDOM_MAX/2;

  std::vector<float> taps (&coeffs[0], &coeffs[NTAPS]);
  filter_t *f = filter_maker (taps);

  // get starting CPU usage
#ifdef HAVE_SYS_RESOURCE_H
  if (getrusage (RUSAGE_SELF, &rusage_start) < 0){
    perror ("getrusage");
    exit (1);
  }
#else
  clock_start = (double) clock() * (1000000. / CLOCKS_PER_SEC);
#endif
  // do the actual work

  for (n = 0; n < TOTAL_TEST_SIZE; n += BLOCK_SIZE){
    int	j;
    for (j = 0; j < BLOCK_SIZE; j++){
      result = f->filter (&input[j]);
    }
  }

  // get ending CPU usage
#ifdef HAVE_SYS_RESOURCE_H
  if (getrusage (RUSAGE_SELF, &rusage_stop) < 0){
    perror ("getrusage");
    exit (1);
  }

  // compute results

  double user =
    timeval_to_double (&rusage_stop.ru_utime)
    - timeval_to_double (&rusage_start.ru_utime);

  double sys =
    timeval_to_double (&rusage_stop.ru_stime)
    - timeval_to_double (&rusage_start.ru_stime);

  double total = user + sys;
#else
  clock_end = (double) clock () * (1000000. / CLOCKS_PER_SEC);
  double total = clock_end -clock_start;
#endif
  double macs = NTAPS * (double) TOTAL_TEST_SIZE;

  printf ("%10s: taps: %4d  input: %4g  cpu: %6.3f  taps/sec: %10.4g  \n",
	  implementation_name, NTAPS, (double) TOTAL_TEST_SIZE, total, macs / total);

  delete f;
}

static void
do_all ()
{
  std::vector<gr_fir_fff_info>		info;
  gr_fir_util::get_gr_fir_fff_info (&info);	// get all known FFF implementations

  for (std::vector<gr_fir_fff_info>::iterator p = info.begin ();
       p != info.end ();
       ++p){

    benchmark (p->create, p->name);
  }
}

int
main (int argc, char **argv)
{
  do_all ();
  return 0;
}