Bsoft  1.9
Bernard's software package
putils.c
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 1997-1999, 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 
21 /*
22  * putils.c -- plan utilities shared by planner.c and rplanner.c
23  */
24 
25 /* $Id: putils.c,v 1.21 2003/03/16 23:43:46 stevenj Exp $ */
26 #include "fftw-int.h"
27 #include <stdlib.h>
28 #include <stdio.h>
29 
30 int fftw_node_cnt = 0;
31 int fftw_plan_cnt = 0;
32 
33 /*
34  * These two constants are used for the FFTW_ESTIMATE flag to help
35  * create a heuristic plan. They don't affect FFTW_MEASURE.
36  */
37 #define NOTW_OPTIMAL_SIZE 32
38 #define TWIDDLE_OPTIMAL_SIZE 12
39 
40 #define IS_POWER_OF_TWO(n) (((n) & ((n) - 1)) == 0)
41 
42 /* constructors --- I wish I had ML */
44 {
46  fftw_malloc(sizeof(fftw_plan_node));
47  p->refcnt = 0;
48  fftw_node_cnt++;
49  return p;
50 }
51 
53 {
54  ++p->refcnt;
55 }
56 
58 {
60 
61  p->type = config->type;
62  p->nodeu.notw.size = size;
63  p->nodeu.notw.codelet = (fftw_notw_codelet *) config->codelet;
64  p->nodeu.notw.codelet_desc = config;
65  return p;
66 }
67 
70 {
72 
73  p->type = config->type;
74  p->nodeu.real2hc.size = size;
75  p->nodeu.real2hc.codelet = (fftw_real2hc_codelet *) config->codelet;
76  p->nodeu.real2hc.codelet_desc = config;
77  return p;
78 }
79 
82 {
84 
85  p->type = config->type;
86  p->nodeu.hc2real.size = size;
87  p->nodeu.hc2real.codelet = (fftw_hc2real_codelet *) config->codelet;
88  p->nodeu.hc2real.codelet_desc = config;
89  return p;
90 }
91 
95  int flags)
96 {
98 
99  p->type = config->type;
100  p->nodeu.twiddle.size = config->size;
101  p->nodeu.twiddle.codelet = (fftw_twiddle_codelet *) config->codelet;
102  p->nodeu.twiddle.recurse = recurse;
103  p->nodeu.twiddle.codelet_desc = config;
104  fftw_use_node(recurse);
105  if (flags & FFTW_MEASURE)
106  p->nodeu.twiddle.tw = fftw_create_twiddle(n, config);
107  else
108  p->nodeu.twiddle.tw = 0;
109  return p;
110 }
111 
113  const fftw_codelet_desc *config,
115  int flags)
116 {
118 
119  p->type = config->type;
120  p->nodeu.hc2hc.size = config->size;
121  p->nodeu.hc2hc.dir = dir;
122  p->nodeu.hc2hc.codelet = (fftw_hc2hc_codelet *) config->codelet;
123  p->nodeu.hc2hc.recurse = recurse;
124  p->nodeu.hc2hc.codelet_desc = config;
125  fftw_use_node(recurse);
126  if (flags & FFTW_MEASURE)
127  p->nodeu.hc2hc.tw = fftw_create_twiddle(n, config);
128  else
129  p->nodeu.hc2hc.tw = 0;
130  return p;
131 }
132 
136  int flags)
137 {
139 
140  p->type = FFTW_GENERIC;
141  p->nodeu.generic.size = size;
142  p->nodeu.generic.codelet = codelet;
143  p->nodeu.generic.recurse = recurse;
144  fftw_use_node(recurse);
145 
146  if (flags & FFTW_MEASURE)
148  (const fftw_codelet_desc *) 0);
149  else
150  p->nodeu.generic.tw = 0;
151  return p;
152 }
153 
158  int flags)
159 {
161 
162  if (size % 2 == 0 || (n / size) % 2 == 0)
163  fftw_die("invalid size for rgeneric codelet\n");
164 
165  p->type = FFTW_RGENERIC;
166  p->nodeu.rgeneric.size = size;
167  p->nodeu.rgeneric.dir = dir;
168  p->nodeu.rgeneric.codelet = codelet;
169  p->nodeu.rgeneric.recurse = recurse;
170  fftw_use_node(recurse);
171 
172  if (flags & FFTW_MEASURE)
174  (const fftw_codelet_desc *) 0);
175  else
176  p->nodeu.rgeneric.tw = 0;
177  return p;
178 }
179 
180 /*
181  * Note that these two Rader-related things must go here, rather than
182  * in rader.c, in order that putils.c (and rplanner.c) won't depend
183  * upon rader.c.
184  */
185 
187 
188 static void fftw_destroy_rader(fftw_rader_data * d)
189 {
190  if (d) {
191  d->refcount--;
192  if (d->refcount <= 0) {
193  fftw_rader_data *cur = fftw_rader_top, *prev = NULL;
194 
195  while (cur && cur != d) {
196  prev = cur;
197  cur = cur->next;
198  }
199  if (!cur)
200  fftw_die("invalid Rader data pointer\n");
201 
202  if (prev)
203  prev->next = d->next;
204  else
205  fftw_rader_top = d->next;
206 
208  fftw_free(d->omega);
209  fftw_free(d->cdesc);
210  fftw_free(d);
211  }
212  }
213 }
214 
215 static void destroy_tree(fftw_plan_node *p)
216 {
217  if (p) {
218  --p->refcnt;
219  if (p->refcnt == 0) {
220  switch (p->type) {
221  case FFTW_NOTW:
222  case FFTW_REAL2HC:
223  case FFTW_HC2REAL:
224  break;
225 
226  case FFTW_TWIDDLE:
227  if (p->nodeu.twiddle.tw)
229  destroy_tree(p->nodeu.twiddle.recurse);
230  break;
231 
232  case FFTW_HC2HC:
233  if (p->nodeu.hc2hc.tw)
235  destroy_tree(p->nodeu.hc2hc.recurse);
236  break;
237 
238  case FFTW_GENERIC:
239  if (p->nodeu.generic.tw)
241  destroy_tree(p->nodeu.generic.recurse);
242  break;
243 
244  case FFTW_RADER:
245  if (p->nodeu.rader.tw)
247  if (p->nodeu.rader.rader_data)
248  fftw_destroy_rader(p->nodeu.rader.rader_data);
249  destroy_tree(p->nodeu.rader.recurse);
250  break;
251 
252  case FFTW_RGENERIC:
253  if (p->nodeu.rgeneric.tw)
255  destroy_tree(p->nodeu.rgeneric.recurse);
256  break;
257  }
258 
259  fftw_free(p);
260  fftw_node_cnt--;
261  }
262  }
263 }
264 
265 /* create a plan with twiddle factors, and other bells and whistles */
267  fftw_plan_node *root, int flags,
268  enum fftw_node_type wisdom_type,
269  int wisdom_signature,
270  fftw_recurse_kind recurse_kind, int vector_size)
271 {
272  fftw_plan p = (fftw_plan) fftw_malloc(sizeof(struct fftw_plan_struct));
273 
274  p->n = n;
275  p->dir = dir;
276  p->flags = flags;
277  fftw_use_node(root);
278  p->root = root;
279  p->cost = 0.0;
280  p->wisdom_type = wisdom_type;
281  p->wisdom_signature = wisdom_signature;
282  p->recurse_kind = recurse_kind;
283  p->vector_size = vector_size;
284  if (recurse_kind == FFTW_VECTOR_RECURSE && vector_size > 1)
285  fftw_die("invalid vector-recurse plan attempted\n");
286  p->next = (fftw_plan) 0;
287  p->refcnt = 0;
288  fftw_plan_cnt++;
289  return p;
290 }
291 
292 /*
293  * complete with twiddle factors (because nodes don't have
294  * them when FFTW_ESTIMATE is set)
295  */
297 {
298  int r;
299  switch (p->type) {
300  case FFTW_NOTW:
301  case FFTW_REAL2HC:
302  case FFTW_HC2REAL:
303  break;
304 
305  case FFTW_TWIDDLE:
306  r = p->nodeu.twiddle.size;
307  if (!p->nodeu.twiddle.tw)
308  p->nodeu.twiddle.tw =
309  fftw_create_twiddle(n, p->nodeu.twiddle.codelet_desc);
310  fftw_complete_twiddle(p->nodeu.twiddle.recurse, n / r);
311  break;
312 
313  case FFTW_HC2HC:
314  r = p->nodeu.hc2hc.size;
315  if (!p->nodeu.hc2hc.tw)
316  p->nodeu.hc2hc.tw =
317  fftw_create_twiddle(n, p->nodeu.hc2hc.codelet_desc);
318  fftw_complete_twiddle(p->nodeu.hc2hc.recurse, n / r);
319  break;
320 
321  case FFTW_GENERIC:
322  r = p->nodeu.generic.size;
323  if (!p->nodeu.generic.tw)
324  p->nodeu.generic.tw =
325  fftw_create_twiddle(n, (const fftw_codelet_desc *) 0);
326  fftw_complete_twiddle(p->nodeu.generic.recurse, n / r);
327  break;
328 
329  case FFTW_RADER:
330  r = p->nodeu.rader.size;
331  if (!p->nodeu.rader.tw)
332  p->nodeu.rader.tw =
333  fftw_create_twiddle(n, p->nodeu.rader.rader_data->cdesc);
334  fftw_complete_twiddle(p->nodeu.rader.recurse, n / r);
335  break;
336 
337  case FFTW_RGENERIC:
338  r = p->nodeu.rgeneric.size;
339  if (!p->nodeu.rgeneric.tw)
340  p->nodeu.rgeneric.tw =
341  fftw_create_twiddle(n, (const fftw_codelet_desc *) 0);
342  fftw_complete_twiddle(p->nodeu.rgeneric.recurse, n / r);
343  break;
344 
345  }
346 }
347 
349 {
350  ++p->refcnt;
351 }
352 
354 {
355  --p->refcnt;
356 
357  if (p->refcnt == 0) {
358  destroy_tree(p->root);
359  fftw_plan_cnt--;
360  fftw_free(p);
361  }
362 }
363 
364 /* end of constructors */
365 
366 /* management of plan tables */
368 {
369  *table = (fftw_plan) 0;
370 }
371 
372 void fftw_insert(fftw_plan *table, fftw_plan this_plan)
373 {
374  fftw_use_plan(this_plan);
375  this_plan->next = *table;
376  *table = this_plan;
377 }
378 
379 fftw_plan fftw_lookup(fftw_plan *table, int n, int flags, int vector_size)
380 {
381  fftw_plan p;
382 
383  for (p = *table; p &&
384  (p->n != n || p->flags != flags || p->vector_size != vector_size);
385  p = p->next);
386 
387  return p;
388 }
389 
391 {
392  fftw_plan p, q;
393 
394  for (p = *table; p; p = q) {
395  q = p->next;
397  }
398 }
399 
401 {
402  int k;
403 
404  switch (p->type) {
405  case FFTW_NOTW:
406  k = p->nodeu.notw.size;
407  goto common1;
408 
409  case FFTW_REAL2HC:
410  k = p->nodeu.real2hc.size;
411  goto common1;
412 
413  case FFTW_HC2REAL:
414  k = p->nodeu.hc2real.size;
415  common1:
416  return 1.0 + 0.1 * (k - NOTW_OPTIMAL_SIZE) *
417  (k - NOTW_OPTIMAL_SIZE);
418 
419  case FFTW_TWIDDLE:
420  k = p->nodeu.twiddle.size;
421  return 1.0 + 0.1 * (k - TWIDDLE_OPTIMAL_SIZE) *
423  + fftw_estimate_node(p->nodeu.twiddle.recurse);
424 
425  case FFTW_HC2HC:
426  k = p->nodeu.hc2hc.size;
427  return 1.0 + 0.1 * (k - TWIDDLE_OPTIMAL_SIZE) *
429  + fftw_estimate_node(p->nodeu.hc2hc.recurse);
430 
431  case FFTW_GENERIC:
432  k = p->nodeu.generic.size;
433  return 10.0 + k * k
434  + fftw_estimate_node(p->nodeu.generic.recurse);
435 
436  case FFTW_RADER:
437  k = p->nodeu.rader.size;
438  return 10.0 + 10 * k
439  + fftw_estimate_node(p->nodeu.rader.recurse);
440 
441  case FFTW_RGENERIC:
442  k = p->nodeu.rgeneric.size;
443  return 10.0 + k * k
444  + fftw_estimate_node(p->nodeu.rgeneric.recurse);
445  }
446  return 1.0E20;
447 }
448 
449 /* pick the better of two plans and destroy the other one. */
451 {
452  if (!p1)
453  return p2;
454 
455  if (!p2)
456  return p1;
457 
458  if (p1->cost > p2->cost) {
460  return p2;
461  } else {
463  return p1;
464  }
465 }
466 
467 /* find the smallest prime factor of n */
468 int fftw_factor(int n)
469 {
470  int r;
471 
472  /* try 2 */
473  if ((n & 1) == 0)
474  return 2;
475 
476  /* try odd numbers up to sqrt(n) */
477  for (r = 3; r * r <= n; r += 2)
478  if (n % r == 0)
479  return r;
480 
481  /* n is prime */
482  return n;
483 }
484 
485 static void print_node(FILE *f, fftw_plan_node *p, int indent)
486 {
487  if (p) {
488  switch (p->type) {
489  case FFTW_NOTW:
490  fprintf(f, "%*sFFTW_NOTW %d\n", indent, "",
491  p->nodeu.notw.size);
492  break;
493  case FFTW_REAL2HC:
494  fprintf(f, "%*sFFTW_REAL2HC %d\n", indent, "",
495  p->nodeu.real2hc.size);
496  break;
497  case FFTW_HC2REAL:
498  fprintf(f, "%*sFFTW_HC2REAL %d\n", indent, "",
499  p->nodeu.hc2real.size);
500  break;
501  case FFTW_TWIDDLE:
502  fprintf(f, "%*sFFTW_TWIDDLE %d\n", indent, "",
503  p->nodeu.twiddle.size);
504  print_node(f, p->nodeu.twiddle.recurse, indent);
505  break;
506  case FFTW_HC2HC:
507  fprintf(f, "%*sFFTW_HC2HC %d\n", indent, "",
508  p->nodeu.hc2hc.size);
509  print_node(f, p->nodeu.hc2hc.recurse, indent);
510  break;
511  case FFTW_GENERIC:
512  fprintf(f, "%*sFFTW_GENERIC %d\n", indent, "",
513  p->nodeu.generic.size);
514  print_node(f, p->nodeu.generic.recurse, indent);
515  break;
516  case FFTW_RADER:
517  fprintf(f, "%*sFFTW_RADER %d\n", indent, "",
518  p->nodeu.rader.size);
519 
520  fprintf(f, "%*splan for size %d convolution:\n",
521  indent + 4, "", p->nodeu.rader.size - 1);
522  print_node(f, p->nodeu.rader.rader_data->plan->root,
523  indent + 6);
524 
525  print_node(f, p->nodeu.rader.recurse, indent);
526  break;
527  case FFTW_RGENERIC:
528  fprintf(f, "%*sFFTW_RGENERIC %d\n", indent, "",
529  p->nodeu.rgeneric.size);
530  print_node(f, p->nodeu.rgeneric.recurse, indent);
531  break;
532  }
533  }
534 }
535 
536 void fftw_fprint_plan(FILE *f, fftw_plan p)
537 {
538 
539  fprintf(f, "plan: (cost = %e)\n", p->cost);
541  fprintf(f, "(vector recursion)\n");
542  else if (p->vector_size > 1)
543  fprintf(f, "(vector-size %d)\n", p->vector_size);
544  print_node(f, p->root, 0);
545 }
546 
548 {
549  fftw_fprint_plan(stdout, p);
550 }
551 
553 {
554  return(sizeof(fftw_real));
555 }
struct fftw_rader_data_struct * next
Definition: fftw.h:183
int fftw_plan_cnt
Definition: putils.c:31
fftw_twiddle * fftw_create_twiddle(int n, const fftw_codelet_desc *d)
Definition: twiddle.c:175
void fftw_fprint_plan(FILE *f, fftw_plan p)
Definition: putils.c:536
void fftw_complete_twiddle(fftw_plan_node *p, int n)
Definition: putils.c:296
double cost
Definition: fftw.h:280
fftw_complex * omega
Definition: fftw.h:180
union fftw_plan_node_struct::@0 nodeu
void( fftw_twiddle_codelet)(fftw_complex *, const fftw_complex *, int, int, int)
Definition: fftw.h:95
void fftw_make_empty_table(fftw_plan *table)
Definition: putils.c:367
fftw_plan_node * fftw_make_node_hc2hc(int n, fftw_direction dir, const fftw_codelet_desc *config, fftw_plan_node *recurse, int flags)
Definition: putils.c:112
struct fftw_plan_node_struct::@0::@4 rader
void( fftw_hc2hc_codelet)(fftw_real *, const fftw_complex *, int, int, int)
Definition: fftw.h:107
void( fftw_real2hc_codelet)(const fftw_real *, fftw_real *, fftw_real *, int, int, int)
Definition: fftw.h:101
size_t fftw_sizeof_fftw_real(void)
Definition: putils.c:552
fftw_plan_node * fftw_make_node_hc2real(int size, const fftw_codelet_desc *config)
Definition: putils.c:80
struct fftw_plan_struct * next
Definition: fftw.h:278
fftw_plan fftw_lookup(fftw_plan *table, int n, int flags, int vector_size)
Definition: putils.c:379
void fftw_free(void *p)
Definition: malloc.c:178
void fftw_die(const char *s)
Definition: malloc.c:192
void fftw_use_plan(fftw_plan p)
Definition: putils.c:348
fftw_recurse_kind recurse_kind
Definition: fftw.h:281
struct fftw_plan_struct * plan
Definition: fftw.h:179
void( fftw_generic_codelet)(fftw_complex *, const fftw_complex *, int, int, int, int)
Definition: fftw.h:98
#define FFTW_MEASURE
Definition: fftw.h:289
struct fftw_plan_node_struct::@0::@5 real2hc
fftw_plan_node * root
Definition: fftw.h:279
fftw_node_type
Definition: fftw.h:120
fftw_notw_codelet * codelet
Definition: fftw.h:199
void( fftw_notw_codelet)(const fftw_complex *, fftw_complex *, int, int)
Definition: fftw.h:93
void( fftw_hc2real_codelet)(const fftw_real *, const fftw_real *, fftw_real *, int, int, int)
Definition: fftw.h:104
fftw_plan fftw_make_plan(int n, fftw_direction dir, fftw_plan_node *root, int flags, enum fftw_node_type wisdom_type, int wisdom_signature, fftw_recurse_kind recurse_kind, int vector_size)
Definition: putils.c:266
fftw_rader_data * fftw_rader_top
Definition: putils.c:186
void * fftw_malloc(size_t n)
Definition: malloc.c:160
fftw_plan fftw_pick_better(fftw_plan p1, fftw_plan p2)
Definition: putils.c:450
fftw_recurse_kind
Definition: fftw.h:266
fftw_codelet_desc * cdesc
Definition: fftw.h:184
fftw_direction dir
Definition: fftw.h:246
enum fftw_node_type type
Definition: fftw.h:131
struct fftw_plan_node_struct::@0::@7 hc2hc
fftw_plan_node * fftw_make_node_notw(int size, const fftw_codelet_desc *config)
Definition: putils.c:57
enum fftw_node_type type
Definition: fftw.h:193
void fftw_print_plan(fftw_plan p)
Definition: putils.c:547
struct fftw_plan_node_struct::@0::@6 hc2real
fftw_plan_node * fftw_make_node(void)
Definition: putils.c:43
void fftw_destroy_table(fftw_plan *table)
Definition: putils.c:390
void(* codelet)()
Definition: fftw.h:128
struct fftw_plan_node_struct::@0::@8 rgeneric
fftw_plan_node * fftw_make_node_rgeneric(int n, int size, fftw_direction dir, fftw_rgeneric_codelet *codelet, fftw_plan_node *recurse, int flags)
Definition: putils.c:154
int vector_size
Definition: fftw.h:282
struct fftw_plan_node_struct::@0::@2 twiddle
double fftw_estimate_node(fftw_plan_node *p)
Definition: putils.c:400
void fftw_insert(fftw_plan *table, fftw_plan this_plan)
Definition: putils.c:372
int fftw_factor(int n)
Definition: putils.c:468
#define NOTW_OPTIMAL_SIZE
Definition: putils.c:37
void( fftw_rgeneric_codelet)(fftw_real *, const fftw_complex *, int, int, int, int)
Definition: fftw.h:110
fftw_direction dir
Definition: fftw.h:274
fftw_plan_node * fftw_make_node_generic(int n, int size, fftw_generic_codelet *codelet, fftw_plan_node *recurse, int flags)
Definition: putils.c:133
#define TWIDDLE_OPTIMAL_SIZE
Definition: putils.c:38
struct fftw_plan_struct * fftw_plan
Definition: fftw.h:285
int fftw_node_cnt
Definition: putils.c:30
void fftw_destroy_plan_internal(fftw_plan p)
Definition: putils.c:353
float fftw_real
Definition: fftw.h:49
fftw_plan_node * fftw_make_node_real2hc(int size, const fftw_codelet_desc *config)
Definition: putils.c:68
struct fftw_plan_node_struct::@0::@1 notw
struct fftw_plan_node_struct * recurse
Definition: fftw.h:208
enum fftw_node_type wisdom_type
Definition: fftw.h:277
struct fftw_plan_node_struct::@0::@3 generic
fftw_direction
Definition: fftw.h:63
int wisdom_signature
Definition: fftw.h:276
void fftw_use_node(fftw_plan_node *p)
Definition: putils.c:52
void fftw_destroy_twiddle(fftw_twiddle *tw)
Definition: twiddle.c:201
fftw_plan_node * fftw_make_node_twiddle(int n, const fftw_codelet_desc *config, fftw_plan_node *recurse, int flags)
Definition: putils.c:92