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
|
/*
* Copyright (c) 1997-1999 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
*
*/
/*
* wisdom.c -- manage the wisdom
*/
#include <fftw-int.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct wisdom {
int n;
int flags;
fftw_direction dir;
enum fftw_wisdom_category category;
int istride;
int ostride;
int vector_size;
enum fftw_node_type type; /* this is the wisdom */
int signature; /* this is the wisdom */
fftw_recurse_kind recurse_kind; /* this is the wisdom */
struct wisdom *next;
};
/* list of wisdom */
static struct wisdom *wisdom_list = (struct wisdom *) 0;
int fftw_wisdom_lookup(int n, int flags, fftw_direction dir,
enum fftw_wisdom_category category,
int istride, int ostride,
enum fftw_node_type *type,
int *signature, fftw_recurse_kind *recurse_kind,
int replacep)
{
struct wisdom *p;
if (!(flags & FFTW_USE_WISDOM))
return 0; /* simply ignore if wisdom is disabled */
flags |= FFTW_MEASURE; /*
* always use (only) wisdom from
* measurements
*/
for (p = wisdom_list; p; p = p->next) {
if (p->n == n && p->flags == flags && p->dir == dir &&
p->istride == istride && p->ostride == ostride &&
p->category == category) {
/* found wisdom */
if (replacep) {
/* replace old wisdom with new */
p->type = *type;
p->signature = *signature;
p->recurse_kind = *recurse_kind;
} else {
*type = p->type;
*signature = p->signature;
*recurse_kind = p->recurse_kind;
}
return 1;
}
}
return 0;
}
void fftw_wisdom_add(int n, int flags, fftw_direction dir,
enum fftw_wisdom_category category,
int istride, int ostride,
enum fftw_node_type type,
int signature,
fftw_recurse_kind recurse_kind)
{
struct wisdom *p;
if ((flags & FFTW_NO_VECTOR_RECURSE) &&
recurse_kind == FFTW_VECTOR_RECURSE)
fftw_die("bug in planner (conflicting plan options)\n");
if (!(flags & FFTW_USE_WISDOM))
return; /* simply ignore if wisdom is disabled */
if (!(flags & FFTW_MEASURE))
return; /* only measurements produce wisdom */
if (fftw_wisdom_lookup(n, flags, dir, category, istride, ostride,
&type, &signature, &recurse_kind, 1))
return; /* wisdom overwrote old wisdom */
p = (struct wisdom *) fftw_malloc(sizeof(struct wisdom));
p->n = n;
p->flags = flags;
p->dir = dir;
p->category = category;
p->istride = istride;
p->ostride = ostride;
p->type = type;
p->signature = signature;
p->recurse_kind = recurse_kind;
/* remember this wisdom */
p->next = wisdom_list;
wisdom_list = p;
}
void fftw_forget_wisdom(void)
{
while (wisdom_list) {
struct wisdom *p;
p = wisdom_list;
wisdom_list = wisdom_list->next;
fftw_free(p);
}
}
/*
* user-visible routines, to convert wisdom into strings etc.
*/
static const char *WISDOM_FORMAT_VERSION = "FFTW-" FFTW_VERSION;
static void (*emit) (char c, void *data);
static void emit_string(const char *s, void *data)
{
while (*s)
emit(*s++, data);
}
static void emit_int(int n, void *data)
{
char buf[128];
sprintf(buf, "%d", n);
emit_string(buf, data);
}
/* dump wisdom in lisp-like format */
void fftw_export_wisdom(void (*emitter) (char c, void *), void *data)
{
struct wisdom *p;
/* install the output handler */
emit = emitter;
emit('(', data);
emit_string(WISDOM_FORMAT_VERSION, data);
for (p = wisdom_list; p; p = p->next) {
emit(' ', data); /* separator to make the output nicer */
emit('(', data);
emit_int((int) p->n, data);
emit(' ', data);
emit_int((int) p->flags, data);
emit(' ', data);
emit_int((int) p->dir, data);
emit(' ', data);
emit_int((int) p->category, data);
emit(' ', data);
emit_int((int) p->istride, data);
emit(' ', data);
emit_int((int) p->ostride, data);
emit(' ', data);
emit_int((int) p->type, data);
emit(' ', data);
emit_int((int) p->signature, data);
emit(' ', data);
emit_int((int) p->recurse_kind, data);
emit(')', data);
}
emit(')', data);
}
/* input part */
static int next_char;
static int (*get_input) (void *data);
static fftw_status input_error;
static void read_char(void *data)
{
next_char = get_input(data);
if (next_char == 0 ||
next_char == EOF)
input_error = FFTW_FAILURE;
}
/* skip blanks, newlines, tabs, etc */
static void eat_blanks(void *data)
{
while (isspace(next_char))
read_char(data);
}
static int read_int(void *data)
{
int sign = 1;
int n = 0;
eat_blanks(data);
if (next_char == '-') {
sign = -1;
read_char(data);
eat_blanks(data);
}
if (!isdigit(next_char)) {
/* error, no digit */
input_error = FFTW_FAILURE;
return 0;
}
while (isdigit(next_char)) {
n = n * 10 + (next_char - '0');
read_char(data);
}
return sign * n;
}
#define EXPECT(c) \
{ \
eat_blanks(data); \
if (input_error == FFTW_FAILURE || \
next_char != c) \
return FFTW_FAILURE; \
read_char(data); \
}
#define EXPECT_INT(n) \
{ \
n = read_int(data); \
if (input_error == FFTW_FAILURE) \
return FFTW_FAILURE; \
}
#define EXPECT_STRING(s) \
{ \
const char *s1 = s; \
while (*s1) { \
EXPECT(*s1); \
++s1; \
} \
}
fftw_status fftw_import_wisdom(int (*g) (void *), void *data)
{
int n;
int flags;
fftw_direction dir;
int dir_int;
enum fftw_wisdom_category category;
int category_int;
enum fftw_node_type type;
int recurse_kind_int;
fftw_recurse_kind recurse_kind;
int type_int;
int signature;
int istride, ostride;
get_input = g;
input_error = FFTW_SUCCESS;
read_char(data);
eat_blanks(data);
EXPECT('(');
eat_blanks(data);
EXPECT_STRING(WISDOM_FORMAT_VERSION);
eat_blanks(data);
while (next_char != ')') {
EXPECT('(');
EXPECT_INT(n);
EXPECT_INT(flags);
/* paranoid respect for enumerated types */
EXPECT_INT(dir_int);
dir = (fftw_direction) dir_int;
EXPECT_INT(category_int);
category = (enum fftw_wisdom_category) category_int;
EXPECT_INT(istride);
EXPECT_INT(ostride);
EXPECT_INT(type_int);
type = (enum fftw_node_type) type_int;
EXPECT_INT(signature);
EXPECT_INT(recurse_kind_int);
recurse_kind = (fftw_recurse_kind) recurse_kind_int;
eat_blanks(data);
EXPECT(')');
/* the wisdom has been read properly. Add it */
fftw_wisdom_add(n, flags, dir, category,
istride, ostride,
type, signature, recurse_kind);
/* prepare for next morsel of wisdom */
eat_blanks(data);
}
return FFTW_SUCCESS;
}
|