comparison src/fftw-3.3.3/libbench2/problem.c @ 10:37bf6b4a2645

Add FFTW3
author Chris Cannam
date Wed, 20 Mar 2013 15:35:50 +0000
parents
children
comparison
equal deleted inserted replaced
9:c0fb53affa76 10:37bf6b4a2645
1 /*
2 * Copyright (c) 2001 Matteo Frigo
3 * Copyright (c) 2001 Massachusetts Institute of Technology
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21
22 #include "config.h"
23 #include "bench.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 int always_pad_real = 0; /* by default, only pad in-place case */
30
31 typedef enum {
32 SAME, PADDED, HALFISH
33 } n_transform;
34
35 /* funny transformations for last dimension of PROBLEM_REAL */
36 static int transform_n(int n, n_transform nt)
37 {
38 switch (nt) {
39 case SAME: return n;
40 case PADDED: return 2*(n/2+1);
41 case HALFISH: return (n/2+1);
42 default: BENCH_ASSERT(0); return 0;
43 }
44 }
45
46 /* do what I mean */
47 static bench_tensor *dwim(bench_tensor *t, bench_iodim **last_iodim,
48 n_transform nti, n_transform nto,
49 bench_iodim *dt)
50 {
51 int i;
52 bench_iodim *d, *d1;
53
54 if (!FINITE_RNK(t->rnk) || t->rnk < 1)
55 return t;
56
57 i = t->rnk;
58 d1 = *last_iodim;
59
60 while (--i >= 0) {
61 d = t->dims + i;
62 if (!d->is)
63 d->is = d1->is * transform_n(d1->n, d1==dt ? nti : SAME);
64 if (!d->os)
65 d->os = d1->os * transform_n(d1->n, d1==dt ? nto : SAME);
66 d1 = d;
67 }
68
69 *last_iodim = d1;
70 return t;
71 }
72
73 static void transpose_tensor(bench_tensor *t)
74 {
75 if (!FINITE_RNK(t->rnk) || t->rnk < 2)
76 return;
77
78 t->dims[0].os = t->dims[1].os;
79 t->dims[1].os = t->dims[0].os * t->dims[0].n;
80 }
81
82 static const char *parseint(const char *s, int *n)
83 {
84 int sign = 1;
85
86 *n = 0;
87
88 if (*s == '-') {
89 sign = -1;
90 ++s;
91 } else if (*s == '+') {
92 sign = +1;
93 ++s;
94 }
95
96 BENCH_ASSERT(isdigit(*s));
97 while (isdigit(*s)) {
98 *n = *n * 10 + (*s - '0');
99 ++s;
100 }
101
102 *n *= sign;
103
104 if (*s == 'k' || *s == 'K') {
105 *n *= 1024;
106 ++s;
107 }
108
109 if (*s == 'm' || *s == 'M') {
110 *n *= 1024 * 1024;
111 ++s;
112 }
113
114 return s;
115 }
116
117 struct dimlist { bench_iodim car; r2r_kind_t k; struct dimlist *cdr; };
118
119 static const char *parsetensor(const char *s, bench_tensor **tp,
120 r2r_kind_t **k)
121 {
122 struct dimlist *l = 0, *m;
123 bench_tensor *t;
124 int rnk = 0;
125
126 L1:
127 m = (struct dimlist *)bench_malloc(sizeof(struct dimlist));
128 /* nconc onto l */
129 m->cdr = l; l = m;
130 ++rnk;
131
132 s = parseint(s, &m->car.n);
133
134 if (*s == ':') {
135 /* read input stride */
136 ++s;
137 s = parseint(s, &m->car.is);
138 if (*s == ':') {
139 /* read output stride */
140 ++s;
141 s = parseint(s, &m->car.os);
142 } else {
143 /* default */
144 m->car.os = m->car.is;
145 }
146 } else {
147 m->car.is = 0;
148 m->car.os = 0;
149 }
150
151 if (*s == 'f' || *s == 'F') {
152 m->k = R2R_R2HC;
153 ++s;
154 }
155 else if (*s == 'b' || *s == 'B') {
156 m->k = R2R_HC2R;
157 ++s;
158 }
159 else if (*s == 'h' || *s == 'H') {
160 m->k = R2R_DHT;
161 ++s;
162 }
163 else if (*s == 'e' || *s == 'E' || *s == 'o' || *s == 'O') {
164 char c = *(s++);
165 int ab;
166
167 s = parseint(s, &ab);
168
169 if (c == 'e' || c == 'E') {
170 if (ab == 0)
171 m->k = R2R_REDFT00;
172 else if (ab == 1)
173 m->k = R2R_REDFT01;
174 else if (ab == 10)
175 m->k = R2R_REDFT10;
176 else if (ab == 11)
177 m->k = R2R_REDFT11;
178 else
179 BENCH_ASSERT(0);
180 }
181 else {
182 if (ab == 0)
183 m->k = R2R_RODFT00;
184 else if (ab == 1)
185 m->k = R2R_RODFT01;
186 else if (ab == 10)
187 m->k = R2R_RODFT10;
188 else if (ab == 11)
189 m->k = R2R_RODFT11;
190 else
191 BENCH_ASSERT(0);
192 }
193 }
194 else
195 m->k = R2R_R2HC;
196
197 if (*s == 'x' || *s == 'X') {
198 ++s;
199 goto L1;
200 }
201
202 /* now we have a dimlist. Build bench_tensor, etc. */
203
204 if (k && rnk > 0) {
205 int i;
206 *k = (r2r_kind_t *) bench_malloc(sizeof(r2r_kind_t) * rnk);
207 for (m = l, i = rnk - 1; i >= 0; --i, m = m->cdr) {
208 BENCH_ASSERT(m);
209 (*k)[i] = m->k;
210 }
211 }
212
213 t = mktensor(rnk);
214 while (--rnk >= 0) {
215 bench_iodim *d = t->dims + rnk;
216 BENCH_ASSERT(l);
217 m = l; l = m->cdr;
218 d->n = m->car.n;
219 d->is = m->car.is;
220 d->os = m->car.os;
221 bench_free(m);
222 }
223
224 *tp = t;
225 return s;
226 }
227
228 /* parse a problem description, return a problem */
229 bench_problem *problem_parse(const char *s)
230 {
231 bench_problem *p;
232 bench_iodim last_iodim0 = {1,1,1}, *last_iodim = &last_iodim0;
233 bench_iodim *sz_last_iodim;
234 bench_tensor *sz;
235 n_transform nti = SAME, nto = SAME;
236 int transpose = 0;
237
238 p = (bench_problem *) bench_malloc(sizeof(bench_problem));
239 p->kind = PROBLEM_COMPLEX;
240 p->k = 0;
241 p->sign = -1;
242 p->in = p->out = 0;
243 p->inphys = p->outphys = 0;
244 p->iphyssz = p->ophyssz = 0;
245 p->in_place = 0;
246 p->destroy_input = 0;
247 p->split = 0;
248 p->userinfo = 0;
249 p->scrambled_in = p->scrambled_out = 0;
250 p->sz = p->vecsz = 0;
251 p->ini = p->outi = 0;
252 p->pstring = (char *) bench_malloc(sizeof(char) * (strlen(s) + 1));
253 strcpy(p->pstring, s);
254
255 L1:
256 switch (tolower(*s)) {
257 case 'i': p->in_place = 1; ++s; goto L1;
258 case 'o': p->in_place = 0; ++s; goto L1;
259 case 'd': p->destroy_input = 1; ++s; goto L1;
260 case '/': p->split = 1; ++s; goto L1;
261 case 'f':
262 case '-': p->sign = -1; ++s; goto L1;
263 case 'b':
264 case '+': p->sign = 1; ++s; goto L1;
265 case 'r': p->kind = PROBLEM_REAL; ++s; goto L1;
266 case 'c': p->kind = PROBLEM_COMPLEX; ++s; goto L1;
267 case 'k': p->kind = PROBLEM_R2R; ++s; goto L1;
268 case 't': transpose = 1; ++s; goto L1;
269
270 /* hack for MPI: */
271 case '[': p->scrambled_in = 1; ++s; goto L1;
272 case ']': p->scrambled_out = 1; ++s; goto L1;
273
274 default : ;
275 }
276
277 s = parsetensor(s, &sz, p->kind == PROBLEM_R2R ? &p->k : 0);
278
279 if (p->kind == PROBLEM_REAL) {
280 if (p->sign < 0) {
281 nti = p->in_place || always_pad_real ? PADDED : SAME;
282 nto = HALFISH;
283 }
284 else {
285 nti = HALFISH;
286 nto = p->in_place || always_pad_real ? PADDED : SAME;
287 }
288 }
289
290 sz_last_iodim = sz->dims + sz->rnk - 1;
291 if (*s == '*') { /* "external" vector */
292 ++s;
293 p->sz = dwim(sz, &last_iodim, nti, nto, sz_last_iodim);
294 s = parsetensor(s, &sz, 0);
295 p->vecsz = dwim(sz, &last_iodim, nti, nto, sz_last_iodim);
296 } else if (*s == 'v' || *s == 'V') { /* "internal" vector */
297 bench_tensor *vecsz;
298 ++s;
299 s = parsetensor(s, &vecsz, 0);
300 p->vecsz = dwim(vecsz, &last_iodim, nti, nto, sz_last_iodim);
301 p->sz = dwim(sz, &last_iodim, nti, nto, sz_last_iodim);
302 } else {
303 p->sz = dwim(sz, &last_iodim, nti, nto, sz_last_iodim);
304 p->vecsz = mktensor(0);
305 }
306
307 if (transpose) {
308 transpose_tensor(p->sz);
309 transpose_tensor(p->vecsz);
310 }
311
312 if (!p->in_place)
313 p->out = ((bench_real *) p->in) + (1 << 20); /* whatever */
314
315 BENCH_ASSERT(p->sz && p->vecsz);
316 BENCH_ASSERT(!*s);
317 return p;
318 }
319
320 void problem_destroy(bench_problem *p)
321 {
322 BENCH_ASSERT(p);
323 problem_free(p);
324 bench_free0(p->k);
325 bench_free0(p->pstring);
326 bench_free(p);
327 }
328