annotate src/fftw-3.3.5/tools/fftw-wisdom.c @ 141:1b5b6dfd0d0e

Add updated build of PortAudio for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 03 Jan 2017 15:10:52 +0000
parents 7867fa7e1b6b
children
rev   line source
cannam@127 1 /* Re-use libbench2 and the test program, but override bench_main so that
cannam@127 2 we can have different command-line syntax. */
cannam@127 3 #include "my-getopt.h"
cannam@127 4 #include "bench.h"
cannam@127 5
cannam@127 6 #include <stdio.h>
cannam@127 7 #include <stdlib.h>
cannam@127 8 #include <ctype.h>
cannam@127 9 #include <fftw3.h>
cannam@127 10 #include <string.h>
cannam@127 11 #include <time.h>
cannam@127 12
cannam@127 13 #if defined(HAVE_THREADS) || defined(HAVE_OPENMP)
cannam@127 14 # define HAVE_SMP
cannam@127 15 extern int threads_ok;
cannam@127 16 #endif
cannam@127 17
cannam@127 18 #define CONCAT(prefix, name) prefix ## name
cannam@127 19 #if defined(BENCHFFT_SINGLE)
cannam@127 20 #define FFTW(x) CONCAT(fftwf_, x)
cannam@127 21 #elif defined(BENCHFFT_LDOUBLE)
cannam@127 22 #define FFTW(x) CONCAT(fftwl_, x)
cannam@127 23 #elif defined(BENCHFFT_QUAD)
cannam@127 24 #define FFTW(x) CONCAT(fftwq_, x)
cannam@127 25 #else
cannam@127 26 #define FFTW(x) CONCAT(fftw_, x)
cannam@127 27 #endif
cannam@127 28
cannam@127 29 /* from bench.c: */
cannam@127 30 extern unsigned the_flags;
cannam@127 31 extern int usewisdom;
cannam@127 32 extern int nthreads;
cannam@127 33
cannam@127 34 /* dummy routines to replace those in hook.c */
cannam@127 35 void install_hook(void) {}
cannam@127 36 void uninstall_hook(void) {}
cannam@127 37
cannam@127 38 int verbose;
cannam@127 39
cannam@127 40 static void do_problem(bench_problem *p)
cannam@127 41 {
cannam@127 42 if (verbose)
cannam@127 43 printf("Planning transform: %s\n", p->pstring);
cannam@127 44 /* BENCH_ASSERT(can_do(p)); */
cannam@127 45 problem_alloc(p);
cannam@127 46 setup(p);
cannam@127 47 done(p);
cannam@127 48 }
cannam@127 49
cannam@127 50 static void add_problem(const char *pstring,
cannam@127 51 bench_problem ***p, int *ip, int *np)
cannam@127 52 {
cannam@127 53 if (*ip >= *np) {
cannam@127 54 *np = *np * 2 + 1;
cannam@127 55 *p = (bench_problem **) realloc(*p, sizeof(bench_problem *) * *np);
cannam@127 56 }
cannam@127 57 (*p)[(*ip)++] = problem_parse(pstring);
cannam@127 58 }
cannam@127 59
cannam@127 60 static int sz(const bench_problem *p)
cannam@127 61 {
cannam@127 62 return tensor_sz(p->sz) * tensor_sz(p->vecsz);
cannam@127 63 }
cannam@127 64
cannam@127 65 static int prob_size_cmp(const void *p1_, const void *p2_)
cannam@127 66 {
cannam@127 67 const bench_problem * const *p1 = (const bench_problem * const *) p1_;
cannam@127 68 const bench_problem * const *p2 = (const bench_problem * const *) p2_;
cannam@127 69 return (sz(*p1) - sz(*p2));
cannam@127 70 }
cannam@127 71
cannam@127 72 static struct my_option options[] =
cannam@127 73 {
cannam@127 74 {"help", NOARG, 'h'},
cannam@127 75 {"version", NOARG, 'V'},
cannam@127 76 {"verbose", NOARG, 'v'},
cannam@127 77
cannam@127 78 {"canonical", NOARG, 'c'},
cannam@127 79 {"time-limit", REQARG, 't'},
cannam@127 80
cannam@127 81 {"output-file", REQARG, 'o'},
cannam@127 82
cannam@127 83 {"impatient", NOARG, 'i'},
cannam@127 84 {"measure", NOARG, 'm'},
cannam@127 85 {"estimate", NOARG, 'e'},
cannam@127 86 {"exhaustive", NOARG, 'x'},
cannam@127 87
cannam@127 88 {"no-system-wisdom", NOARG, 'n'},
cannam@127 89 {"wisdom-file", REQARG, 'w'},
cannam@127 90
cannam@127 91 #ifdef HAVE_SMP
cannam@127 92 {"threads", REQARG, 'T'},
cannam@127 93 #endif
cannam@127 94
cannam@127 95 /* options to restrict configuration to rdft-only, etcetera? */
cannam@127 96
cannam@127 97 {0, NOARG, 0}
cannam@127 98 };
cannam@127 99
cannam@127 100 static void help(FILE *f, const char *program_name)
cannam@127 101 {
cannam@127 102 fprintf(
cannam@127 103 f,
cannam@127 104 "Usage: %s [options] [sizes]\n"
cannam@127 105 " Create wisdom (pre-planned/optimized transforms) for specified sizes,\n"
cannam@127 106 " writing wisdom to stdout (or to a file, using -o).\n"
cannam@127 107 "\nOptions:\n"
cannam@127 108 " -h, --help: print this help\n"
cannam@127 109 " -V, --version: print version/copyright info\n"
cannam@127 110 " -v, --verbose: verbose output\n"
cannam@127 111 " -c, --canonical: plan/optimize canonical set of sizes\n"
cannam@127 112 " -t <h>, --time-limit=<h>: time limit in hours (default: 0, no limit)\n"
cannam@127 113 " -o FILE, --output-file=FILE: output to FILE instead of stdout\n"
cannam@127 114 " -m, --measure: plan in MEASURE mode (PATIENT is default)\n"
cannam@127 115 " -e, --estimate: plan in ESTIMATE mode (not recommended)\n"
cannam@127 116 " -x, --exhaustive: plan in EXHAUSTIVE mode (may be slow)\n"
cannam@127 117 " -n, --no-system-wisdom: don't read /etc/fftw/ system wisdom file\n"
cannam@127 118 " -w FILE, --wisdom-file=FILE: read wisdom from FILE (stdin if -)\n"
cannam@127 119 #ifdef HAVE_SMP
cannam@127 120 " -T N, --threads=N: plan with N threads\n"
cannam@127 121 #endif
cannam@127 122 "\nSize syntax: <type><inplace><direction><geometry>\n"
cannam@127 123 " <type> = c/r/k for complex/real(r2c,c2r)/r2r\n"
cannam@127 124 " <inplace> = i/o for in/out-of place\n"
cannam@127 125 " <direction> = f/b for forward/backward, omitted for k transforms\n"
cannam@127 126 " <geometry> = <n1>[x<n2>[x...]], e.g. 10x12x14\n"
cannam@127 127 " -- for k transforms, after each dimension is a <kind>:\n"
cannam@127 128 " <kind> = f/b/h/e00/e01/e10/e11/o00/o01/o10/o11\n"
cannam@127 129 " for R2HC/HC2R/DHT/REDFT00/.../RODFT11\n"
cannam@127 130 , program_name);
cannam@127 131 }
cannam@127 132
cannam@127 133 /* powers of two and ten up to 2^20, for now */
cannam@127 134 static char canonical_sizes[][32] = {
cannam@127 135 "1", "2", "4", "8", "16", "32", "64", "128", "256", "512", "1024",
cannam@127 136 "2048", "4096", "8192", "16384", "32768", "65536", "131072",
cannam@127 137 "262144", "524288", "1048576",
cannam@127 138
cannam@127 139 "10", "100", "1000", "10000", "100000", "1000000",
cannam@127 140
cannam@127 141 "2x2", "4x4", "8x8", "10x10", "16x16", "32x32", "64x64", "100x100",
cannam@127 142 "128x128", "256x256", "512x512", "1000x1000", "1024x1024",
cannam@127 143
cannam@127 144 "2x2x2", "4x4x4", "8x8x8", "10x10x10", "16x16x16", "32x32x32",
cannam@127 145 "64x64x64", "100x100x100"
cannam@127 146 };
cannam@127 147
cannam@127 148 #define NELEM(array)(sizeof(array) / sizeof((array)[0]))
cannam@127 149
cannam@127 150 int bench_main(int argc, char *argv[])
cannam@127 151 {
cannam@127 152 int c;
cannam@127 153 unsigned i;
cannam@127 154 int impatient = 0;
cannam@127 155 int system_wisdom = 1;
cannam@127 156 int canonical = 0;
cannam@127 157 double hours = 0;
cannam@127 158 FILE *output_file;
cannam@127 159 char *output_fname = 0;
cannam@127 160 bench_problem **problems = 0;
cannam@127 161 int nproblems = 0, iproblem = 0;
cannam@127 162 time_t begin;
cannam@127 163
cannam@127 164 verbose = 0;
cannam@127 165 usewisdom = 0;
cannam@127 166
cannam@127 167 bench_srand(1);
cannam@127 168 #ifdef HAVE_SMP
cannam@127 169 /* do not configure FFTW with threads, unless the
cannam@127 170 user requests -T */
cannam@127 171 threads_ok = 0;
cannam@127 172 #endif
cannam@127 173
cannam@127 174 while ((c = my_getopt(argc, argv, options)) != -1) {
cannam@127 175 switch (c) {
cannam@127 176 case 'h':
cannam@127 177 help(stdout, argv[0]);
cannam@127 178 exit(EXIT_SUCCESS);
cannam@127 179 break;
cannam@127 180
cannam@127 181 case 'V':
cannam@127 182 printf("fftw-wisdom tool for FFTW version " VERSION ".\n");
cannam@127 183 printf(
cannam@127 184 "\n"
cannam@127 185 "Copyright (c) 2003, 2007-14 Matteo Frigo\n"
cannam@127 186 "Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology\n"
cannam@127 187 "\n"
cannam@127 188 "This program is free software; you can redistribute it and/or modify\n"
cannam@127 189 "it under the terms of the GNU General Public License as published by\n"
cannam@127 190 "the Free Software Foundation; either version 2 of the License, or\n"
cannam@127 191 "(at your option) any later version.\n"
cannam@127 192 "\n"
cannam@127 193 "This program is distributed in the hope that it will be useful,\n"
cannam@127 194 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
cannam@127 195 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
cannam@127 196 "GNU General Public License for more details.\n"
cannam@127 197 "\n"
cannam@127 198 "You should have received a copy of the GNU General Public License\n"
cannam@127 199 "along with this program; if not, write to the Free Software\n"
cannam@127 200 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n"
cannam@127 201 );
cannam@127 202 exit(EXIT_SUCCESS);
cannam@127 203 break;
cannam@127 204
cannam@127 205 case 'v':
cannam@127 206 verbose = 1;
cannam@127 207 break;
cannam@127 208
cannam@127 209 case 'c':
cannam@127 210 canonical = 1;
cannam@127 211 break;
cannam@127 212
cannam@127 213 case 't':
cannam@127 214 hours = atof(my_optarg);
cannam@127 215 break;
cannam@127 216
cannam@127 217 case 'o':
cannam@127 218 if (output_fname)
cannam@127 219 bench_free(output_fname);
cannam@127 220
cannam@127 221 if (!strcmp(my_optarg, "-"))
cannam@127 222 output_fname = 0;
cannam@127 223 else {
cannam@127 224 output_fname = (char *) bench_malloc(sizeof(char) *
cannam@127 225 (strlen(my_optarg) + 1));
cannam@127 226 strcpy(output_fname, my_optarg);
cannam@127 227 }
cannam@127 228 break;
cannam@127 229
cannam@127 230 case 'm':
cannam@127 231 case 'i':
cannam@127 232 impatient = 1;
cannam@127 233 break;
cannam@127 234
cannam@127 235 case 'e':
cannam@127 236 the_flags |= FFTW_ESTIMATE;
cannam@127 237 break;
cannam@127 238
cannam@127 239 case 'x':
cannam@127 240 the_flags |= FFTW_EXHAUSTIVE;
cannam@127 241 break;
cannam@127 242
cannam@127 243 case 'n':
cannam@127 244 system_wisdom = 0;
cannam@127 245 break;
cannam@127 246
cannam@127 247 case 'w': {
cannam@127 248 FILE *w = stdin;
cannam@127 249 if (strcmp(my_optarg, "-") && !(w = fopen(my_optarg, "r"))) {
cannam@127 250 fprintf(stderr,
cannam@127 251 "fftw-wisdom: error opening \"%s\": ", my_optarg);
cannam@127 252 perror("");
cannam@127 253 exit(EXIT_FAILURE);
cannam@127 254 }
cannam@127 255 if (!FFTW(import_wisdom_from_file)(w)) {
cannam@127 256 fprintf(stderr, "fftw_wisdom: error reading wisdom "
cannam@127 257 "from \"%s\"\n", my_optarg);
cannam@127 258 exit(EXIT_FAILURE);
cannam@127 259 }
cannam@127 260 if (w != stdin)
cannam@127 261 fclose(w);
cannam@127 262 break;
cannam@127 263 }
cannam@127 264
cannam@127 265 #ifdef HAVE_SMP
cannam@127 266 case 'T':
cannam@127 267 nthreads = atoi(my_optarg);
cannam@127 268 if (nthreads < 1) nthreads = 1;
cannam@127 269 threads_ok = 1;
cannam@127 270 BENCH_ASSERT(FFTW(init_threads)());
cannam@127 271 break;
cannam@127 272 #endif
cannam@127 273
cannam@127 274 case '?':
cannam@127 275 /* `my_getopt' already printed an error message. */
cannam@127 276 cleanup();
cannam@127 277 return EXIT_FAILURE;
cannam@127 278
cannam@127 279 default:
cannam@127 280 abort ();
cannam@127 281 }
cannam@127 282 }
cannam@127 283
cannam@127 284 if (!impatient)
cannam@127 285 the_flags |= FFTW_PATIENT;
cannam@127 286
cannam@127 287 if (system_wisdom)
cannam@127 288 if (!FFTW(import_system_wisdom)() && verbose)
cannam@127 289 fprintf(stderr, "fftw-wisdom: system-wisdom import failed\n");
cannam@127 290
cannam@127 291 if (canonical) {
cannam@127 292 for (i = 0; i < NELEM(canonical_sizes); ++i) {
cannam@127 293 unsigned j;
cannam@127 294 char types[][8] = {
cannam@127 295 "cof", "cob", "cif", "cib", "rof", "rob", "rif", "rib"
cannam@127 296 };
cannam@127 297
cannam@127 298 for (j = 0; j < NELEM(types); ++j) {
cannam@127 299 char ps[64];
cannam@127 300 if (!strchr(canonical_sizes[i],'x')
cannam@127 301 || !strchr(types[j],'o')) {
cannam@127 302 #ifdef HAVE_SNPRINTF
cannam@127 303 snprintf(ps, sizeof(ps), "%s%s", types[j], canonical_sizes[i]);
cannam@127 304 #else
cannam@127 305 sprintf(ps, "%s%s", types[j], canonical_sizes[i]);
cannam@127 306 #endif
cannam@127 307 add_problem(ps, &problems, &iproblem, &nproblems);
cannam@127 308 }
cannam@127 309 }
cannam@127 310 }
cannam@127 311 }
cannam@127 312
cannam@127 313 while (my_optind < argc) {
cannam@127 314 if (!strcmp(argv[my_optind], "-")) {
cannam@127 315 char s[1025];
cannam@127 316 while (1 == fscanf(stdin, "%1024s", s))
cannam@127 317 add_problem(s, &problems, &iproblem, &nproblems);
cannam@127 318 }
cannam@127 319 else
cannam@127 320 add_problem(argv[my_optind], &problems, &iproblem, &nproblems);
cannam@127 321 ++my_optind;
cannam@127 322 }
cannam@127 323
cannam@127 324 nproblems = iproblem;
cannam@127 325 qsort(problems, nproblems, sizeof(bench_problem *), prob_size_cmp);
cannam@127 326
cannam@127 327 if (!output_fname)
cannam@127 328 output_file = stdout;
cannam@127 329 else
cannam@127 330 if (!(output_file = fopen(output_fname, "w"))) {
cannam@127 331 fprintf(stderr,
cannam@127 332 "fftw-wisdom: error creating \"%s\"", output_fname);
cannam@127 333 perror("");
cannam@127 334 exit(EXIT_FAILURE);
cannam@127 335 }
cannam@127 336
cannam@127 337 begin = time((time_t*)0);
cannam@127 338 for (iproblem = 0; iproblem < nproblems; ++iproblem) {
cannam@127 339 if (hours <= 0
cannam@127 340 || hours > (time((time_t*)0) - begin) / 3600.0)
cannam@127 341 do_problem(problems[iproblem]);
cannam@127 342 problem_destroy(problems[iproblem]);
cannam@127 343
cannam@127 344 }
cannam@127 345 free(problems);
cannam@127 346
cannam@127 347 if (verbose && hours > 0
cannam@127 348 && hours < (time((time_t*)0) - begin) / 3600.0)
cannam@127 349 fprintf(stderr, "EXCEEDED TIME LIMIT OF %g HOURS.\n", hours);
cannam@127 350
cannam@127 351 FFTW(export_wisdom_to_file)(output_file);
cannam@127 352 if (output_file != stdout)
cannam@127 353 fclose(output_file);
cannam@127 354 if (output_fname)
cannam@127 355 bench_free(output_fname);
cannam@127 356
cannam@127 357 cleanup();
cannam@127 358
cannam@127 359 return EXIT_SUCCESS;
cannam@127 360 }