forked from FFTW/fftw3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify-lib.c
393 lines (335 loc) · 9.13 KB
/
verify-lib.c
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Copyright (c) 2002 Matteo Frigo
* Copyright (c) 2002 Steven G. Johnson
*
* 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: verify-lib.c,v 1.5 2002-09-22 13:49:09 athena Exp $ */
#include "verify.h"
#include <math.h>
#include <stdlib.h>
/*
* Utility functions:
*/
static double dabs(double x) { return (x < 0.0) ? -x : x; }
static double dmax(double x, double y) { return (x > y) ? x : y; }
static double dmin(double x, double y) { return (x < y) ? x : y; }
static double norm2(double x, double y) { return dmax(dabs(x), dabs(y)); }
static double aerror(C *a, C *b, uint n)
{
if (n > 0) {
/* compute the relative Linf error */
double e = 0.0, mag = 0.0;
uint i;
for (i = 0; i < n; ++i) {
e = dmax(e, norm2(a[i].r - b[i].r, a[i].i - b[i].i));
mag = dmax(mag,
dmin(norm2(a[i].r, a[i].i),
norm2(b[i].r, b[i].i)));
}
e /= mag;
#ifdef HAVE_ISNAN
A(!isnan(e));
#endif
return e;
} else
return 0.0;
}
#ifdef HAVE_DRAND48
# if defined(HAVE_DECL_DRAND48) && !HAVE_DECL_DRAND48
extern double drand48(void);
# endif
double mydrand(void)
{
return drand48() - 0.5;
}
#else
double mydrand(void)
{
double d = rand();
return (d / (double) RAND_MAX) - 0.5;
}
#endif
void arand(C *a, uint n)
{
uint i;
/* generate random inputs */
for (i = 0; i < n; ++i) {
a[i].r = mydrand();
a[i].i = mydrand();
}
}
/* make array real */
void mkreal(C *A, uint n)
{
uint i;
for (i = 0; i < n; ++i) {
A[i].i = 0.0;
}
}
static void assign_conj(C *Ac, C *A, uint rank, const iodim *dim, int size)
{
if (rank == 0) {
Ac->r = A->r;
Ac->i = -A->i;
}
else {
uint i, n0 = dim[0].n;
rank -= 1;
dim += 1;
size /= n0;
assign_conj(Ac, A, rank, dim, size);
for (i = 1; i < n0; ++i)
assign_conj(Ac + (n0 - i) * size, A + i * size, rank, dim,size);
}
}
/* make array hermitian */
void mkhermitian(C *A, uint rank, const iodim *dim)
{
if (rank == 0)
A->i = 0.0;
else {
uint i, n0 = dim[0].n, size;
rank -= 1;
dim += 1;
mkhermitian(A, rank, dim);
for (i = 0, size = 1; i < rank; ++i)
size *= dim[i].n;
for (i = 1; 2*i < n0; ++i)
assign_conj(A + (n0 - i) * size, A + i * size, rank, dim, size);
if (2*i == n0)
mkhermitian(A + i*size, rank, dim);
}
}
/* C = A + B */
void aadd(C *c, C *a, C *b, uint n)
{
uint i;
for (i = 0; i < n; ++i) {
c[i].r = a[i].r + b[i].r;
c[i].i = a[i].i + b[i].i;
}
}
/* C = A - B */
void asub(C *c, C *a, C *b, uint n)
{
uint i;
for (i = 0; i < n; ++i) {
c[i].r = a[i].r - b[i].r;
c[i].i = a[i].i - b[i].i;
}
}
/* B = rotate left A (complex) */
void arol(C *b, C *a, uint n, uint nb, uint na)
{
uint i, ib, ia;
for (ib = 0; ib < nb; ++ib) {
for (i = 0; i < n - 1; ++i)
for (ia = 0; ia < na; ++ia)
b[(ib * n + i) * na + ia] =
a[(ib * n + i + 1) * na + ia];
for (ia = 0; ia < na; ++ia)
b[(ib * n + n - 1) * na + ia] = a[ib * n * na + ia];
}
}
void aphase_shift(C *b, C *a, uint n, uint nb, uint na, double sign)
{
uint j, jb, ja;
for (jb = 0; jb < nb; ++jb)
for (j = 0; j < n; ++j) {
trigreal c = X(cos2pi)(j, n);
trigreal s = sign * X(sin2pi)(j, n);
for (ja = 0; ja < na; ++ja) {
uint k = (jb * n + j) * na + ja;
b[k].r = a[k].r * c - a[k].i * s;
b[k].i = a[k].r * s + a[k].i * c;
}
}
}
/* A = alpha * A (complex, in place) */
void ascale(C *a, C alpha, uint n)
{
uint i;
for (i = 0; i < n; ++i) {
C x = a[i];
a[i].r = x.r * alpha.r - x.i * alpha.i;
a[i].i = x.r * alpha.i + x.i * alpha.r;
}
}
double acmp(C *a, C *b, uint n, const char *test, double tol)
{
double d = aerror(a, b, n);
if (d > tol) {
fprintf(stderr, "Found relative error %e (%s)\n", d, test);
{
uint i;
for (i = 0; i < n; ++i)
fprintf(stderr,
"%8d %16.12f %16.12f %16.12f %16.12f\n", i,
(double) a[i].r, (double) a[i].i,
(double) b[i].r, (double) b[i].i);
}
exit(EXIT_FAILURE);
}
return d;
}
/*
* Implementation of the FFT tester described in
*
* Funda Ergün. Testing multivariate linear functions: Overcoming the
* generator bottleneck. In Proceedings of the Twenty-Seventh Annual
* ACM Symposium on the Theory of Computing, pages 407-416, Las Vegas,
* Nevada, 29 May--1 June 1995.
*/
static void impulse0(void (*dofft)(void *nfo, C *in, C *out),
void *nfo,
uint n, uint vecn,
C *inA, C *inB, C *inC,
C *outA, C *outB, C *outC,
C *tmp, uint rounds, double tol)
{
uint N = n * vecn;
uint j;
dofft(nfo, inA, tmp);
acmp(tmp, outA, N, "impulse 1", tol);
for (j = 0; j < rounds; ++j) {
arand(inB, N);
asub(inC, inA, inB, N);
dofft(nfo, inB, outB);
dofft(nfo, inC, outC);
aadd(tmp, outB, outC, N);
acmp(tmp, outA, N, "impulse", tol);
}
}
void impulse(void (*dofft)(void *nfo, C *in, C *out),
void *nfo,
uint n, uint vecn,
C *inA, C *inB, C *inC,
C *outA, C *outB, C *outC,
C *tmp, uint rounds, double tol)
{
uint N = n * vecn;
C pls;
uint i;
/* check that the unit impulse is transformed properly */
pls.r = 1.0;
pls.i = 0.0;
for (i = 0; i < N; ++i) {
/* pls */
inA[i].r = inA[i].i = 0.0;
outA[i] = pls;
}
for (i = 0; i < vecn; ++i)
inA[i * n] = pls;
impulse0(dofft, nfo, n, vecn, inA, inB, inC, outA, outB, outC,
tmp, rounds, tol);
pls.r = n;
for (i = 0; i < vecn; ++i)
inA[i * n] = pls;
impulse0(dofft, nfo, n, vecn, outA, inB, inC, inA, outB, outC,
tmp, rounds, tol);
}
void linear(void (*dofft)(void *nfo, C *in, C *out),
void *nfo, int realp,
uint n, C *inA, C *inB, C *inC, C *outA,
C *outB, C *outC, C *tmp, uint rounds, double tol)
{
uint j;
for (j = 0; j < rounds; ++j) {
C alpha, beta;
alpha.r = mydrand();
alpha.i = realp ? 0.0 : mydrand();
beta.r = mydrand();
beta.i = realp ? 0.0 : mydrand();
arand(inA, n);
arand(inB, n);
dofft(nfo, inA, outA);
dofft(nfo, inB, outB);
ascale(outA, alpha, n);
ascale(outB, beta, n);
aadd(tmp, outA, outB, n);
ascale(inA, alpha, n);
ascale(inB, beta, n);
aadd(inC, inA, inB, n);
dofft(nfo, inC, outC);
acmp(outC, tmp, n, "linear", tol);
}
}
void tf_shift(void (*dofft)(void *nfo, C *in, C *out),
void *nfo, int realp, const tensor *sz,
uint n, uint vecn,
C *inA, C *inB, C *outA, C *outB, C *tmp,
uint rounds, double tol, int which_shift)
{
double sign;
uint nb, na, dim, N = n * vecn;
uint i, j;
sign = -1.0;
/* test 3: check the time-shift property */
/* the paper performs more tests, but this code should be fine too */
nb = 1;
na = n;
/* check shifts across all SZ dimensions */
for (dim = 0; dim < sz->rnk; ++dim) {
uint ncur = sz->dims[dim].n;
na /= ncur;
for (j = 0; j < rounds; ++j) {
arand(inA, N);
if (which_shift == TIME_SHIFT) {
for (i = 0; i < vecn; ++i) {
if (realp) mkreal(inA + i * n, n);
arol(inB + i * n, inA + i * n, ncur, nb, na);
}
dofft(nfo, inA, outA);
dofft(nfo, inB, outB);
for (i = 0; i < vecn; ++i)
aphase_shift(tmp + i * n, outB + i * n, ncur,
nb, na, sign);
acmp(tmp, outA, N, "time shift", tol);
} else {
for (i = 0; i < vecn; ++i) {
if (realp)
mkhermitian(inA + i * n, sz->rnk, sz->dims);
aphase_shift(inB + i * n, inA + i * n, ncur,
nb, na, -sign);
}
dofft(nfo, inA, outA);
dofft(nfo, inB, outB);
for (i = 0; i < vecn; ++i)
arol(tmp + i * n, outB + i * n, ncur, nb, na);
acmp(tmp, outA, N, "freq shift", tol);
}
}
nb *= ncur;
}
}
/* Make a copy of the size tensor, with the same dimensions, but with
the strides corresponding to a "packed" row-major array with the
given stride. */
tensor *verify_pack(const tensor *sz, int s)
{
tensor *x = X(tensor_copy)(sz);
if (FINITE_RNK(x->rnk) && x->rnk > 0) {
uint i;
x->dims[x->rnk - 1].is = s;
x->dims[x->rnk - 1].os = s;
for (i = x->rnk - 1; i > 0; --i) {
x->dims[i - 1].is = x->dims[i].is * x->dims[i].n;
x->dims[i - 1].os = x->dims[i].os * x->dims[i].n;
}
}
return x;
}