File: timer.c

package info (click to toggle)
fftw3 3.1.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 14,016 kB
  • ctags: 10,195
  • sloc: ansic: 154,845; asm: 33,960; ml: 12,962; sh: 8,943; perl: 1,392; makefile: 878; fortran: 108
file content (202 lines) | stat: -rw-r--r-- 3,963 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2001 Matteo Frigo
 * Copyright (c) 2001 Massachusetts Institute of Technology
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/* $Id: timer.c,v 1.13 2006-01-30 16:09:32 athena Exp $ */

#include "bench.h"
#include <stdio.h>

/* 
 * System-dependent timing functions:
 */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_BSDGETTIMEOFDAY
#ifndef HAVE_GETTIMEOFDAY
#define gettimeofday BSDgettimeofday
#define HAVE_GETTIMEOFDAY 1
#endif
#endif

double time_min;
int time_repeat;

#if !defined(HAVE_TIMER) && (defined(__WIN32__) || defined(_WIN32) || defined(_WINDOWS) || defined(__CYGWIN__))
#include <windows.h>
typedef LARGE_INTEGER mytime;

static mytime get_time(void)
{
     mytime tv;
     QueryPerformanceCounter(&tv);
     return tv;
}

static double elapsed(mytime t1, mytime t0)
{
     LARGE_INTEGER freq;
     QueryPerformanceFrequency(&freq);
     return ((double) (t1.QuadPart - t0.QuadPart)) /
	  ((double) freq.QuadPart);
}

#define HAVE_TIMER
#endif


#if defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_TIMER)
typedef struct timeval mytime;

static mytime get_time(void)
{
     struct timeval tv;
     gettimeofday(&tv, 0);
     return tv;
}

static double elapsed(mytime t1, mytime t0)
{
     return (double)(t1.tv_sec - t0.tv_sec) +
	  (double)(t1.tv_usec - t0.tv_usec) * 1.0E-6;
}

#define HAVE_TIMER
#endif

#ifndef HAVE_TIMER
#error "timer not defined"
#endif

static const double tmax_try = 1.0;    /* seconds */
static const int nmin = 128, nmax = 133;

static double time_one(int n)
{
     float X[16], Y[16];
     int i;
     mytime t0, t1;

     for (i = 0; i < 16; ++i)
	  X[i] = 0;

     t0 = get_time();
     for (i = 0; i < n; ++i)
	  bench_fft8(X, X+1, Y, Y+1, 2, 2);
     t1 = get_time();
     return (elapsed(t1, t0));
}

static double time_n(int n)
{
     int     i;
     double  tmin;

     tmin = time_one(n);
     for (i = 1; i < time_repeat; ++i) {
	  double t = time_one(n);
	  if (t < tmin)
	       tmin = t;
     }
     return tmin;
}

static int good_enough_p(int n, double *tp)
{
     int i;
     double t = 0.0;

     /* vary nmin and see if time scales proportionally */
     for (i = nmin; i < nmax; ++i) {
	  double t1 = time_n(n * i);

	  if (t1 <= 0)
	       return 0; /* not enough resolution */

	  if (t1 >= tmax_try) {
	       t = t1;
	       break;
	  }

	  t = (i == nmin) ? t1 : t;

	  if (t1 >= (t * (i + 0.5) / nmin))
	       return 0;

	  if (t1 <= (t * (i - 0.5) / nmin))
	       return 0;
     }

     *tp = t;
     return 1;
}

static double calibrate(void)
{
     double t = tmax_try;
     int n;

     for (n = 1; n < (1 << 20); n += n) 
	  if (good_enough_p(n, &t))
	       break;

     return t;
}


void timer_init(double tmin, int repeat)
{
     static int inited = 0;

     if (inited)
	  return;
     inited = 1;

     if (!repeat)
	  repeat = 8;
     time_repeat = repeat;

     if (tmin > 0)
	  time_min = tmin;
     else
	  time_min = calibrate();
}

static mytime t0[BENCH_NTIMERS];

void timer_start(int n)
{
     BENCH_ASSERT(n >= 0 && n < BENCH_NTIMERS);
     t0[n] = get_time();
}

double timer_stop(int n)
{
     mytime t1;
     BENCH_ASSERT(n >= 0 && n < BENCH_NTIMERS);
     t1 = get_time();
     return elapsed(t1, t0[n]);
}