📄 test_main.c
字号:
/* * paranoid test to see if time is monotonic. If not, you are * really in trouble */void test_timer_paranoid(void){ fftw_time start_t, end_t; double sec; int i; start_t = fftw_get_time(); /* waste some time */ for (i = 0; i < 10000; ++i) hack_sum_i = i; end_t = fftw_get_time(); sec = fftw_time_to_sec(fftw_time_diff(end_t, start_t)); if (sec < 0.0) negative_time();}/* compute useful numbers */static int fib(int n){ if (n < 2) return n; else { int x, y; x = fib(n - 1); y = fib(n - 2); return x + y; }}static int hack_fib;void test_timer(void){ double times[32], acc, min_time = 10000.00; unsigned long iters, iter; fftw_time begin, end, start; double t, tmax, tmin; int last = 0, i, repeat; please_wait(); test_timer_paranoid(); start = fftw_get_time(); for (i = 0; i < 32; i++) { iters = 1 << i; tmin = 1.0E10; tmax = -1.0E10; for (repeat = 0; repeat < FFTW_TIME_REPEAT; ++repeat) { begin = fftw_get_time(); for (iter = 0; iter < iters; ++iter) { hack_fib = fib(10); } end = fftw_get_time(); t = fftw_time_to_sec(fftw_time_diff(end, begin)); if (t < tmin) tmin = t; if (t > tmax) tmax = t; /* do not run for too long */ t = fftw_time_to_sec(fftw_time_diff(end, start)); if (t > FFTW_TIME_LIMIT) break; } if (tmin < 0.0) negative_time(); times[i] = tmin; WHEN_VERBOSE(2, my_printf("Number of iterations = 2^%d = %lu, time = %g, " "time/iter = %g\n", i, iters, times[i], times[i] / iters)); WHEN_VERBOSE(2, my_printf(" (out of %d tries, tmin = %g, tmax = %g)\n", FFTW_TIME_REPEAT, tmin, tmax)); last = i; if (times[i] > 10.0) break; } /* * at this point, `last' is the last valid element in the * `times' array. */ for (i = 0; i <= last; ++i) if (times[i] > 0.0 && times[i] < min_time) min_time = times[i]; WHEN_VERBOSE(1, my_printf("\nMinimum resolvable time interval = %g seconds.\n\n", min_time)); for (acc = 0.1; acc > 0.0005; acc *= 0.1) { double t_final; t_final = times[last] / (1 << last); for (i = last; i >= 0; --i) { double t_cur, error; iters = 1 << i; t_cur = times[i] / iters; error = (t_cur - t_final) / t_final; if (error < 0.0) error = -error; if (error > acc) break; } ++i; WHEN_VERBOSE(1, my_printf("Minimum time for %g%% consistency = %g seconds.\n", acc * 100.0, times[i])); } WHEN_VERBOSE(1, my_printf("\nMinimum time used in FFTW timing (FFTW_TIME_MIN)" " = %g seconds.\n", FFTW_TIME_MIN));}/************************************************* * help *************************************************/#ifdef HAVE_GETOPT_LONG# define WHEN_LONG_OPTIONS(x) x#else# define WHEN_LONG_OPTIONS(x) ""#endifstatic void usage(int exit_when_done){ my_printf("Usage: %s_test [options]\n", fftw_prefix); my_printf(WHEN_LONG_OPTIONS(" --speed=<n>\n") " -s <n> : test speed for size n\n"); my_printf(WHEN_LONG_OPTIONS("\n --correctness=<n>\n") " -c <n> : test correctness for size n\n"); my_printf(WHEN_LONG_OPTIONS("\n --random=<rank>\n") " -r <rank> : test correctness for random sizes " "(does not terminate)\n"); my_printf(WHEN_LONG_OPTIONS("\n --all=<rank>\n") " -a <rank> : test correctness for all sizes " "(does not terminate)\n"); my_printf(WHEN_LONG_OPTIONS("\n --fields=<n>\n") " -f <n> : n fields ('howmany' param) in speed tests\n"); my_printf(WHEN_LONG_OPTIONS("\n --planner=<rank>\n") " -p <rank> : test planner\n"); my_printf(WHEN_LONG_OPTIONS("\n --measure\n") " -m : use FFTW_MEASURE in correctness tests\n"); my_printf(WHEN_LONG_OPTIONS("\n --estimate\n") " -e : use FFTW_ESTIMATE in speed tests\n"); my_printf(WHEN_LONG_OPTIONS("\n --wisdom=<file>\n") " -w <file> : use wisdom & read/write it from/to file\n"); my_printf(WHEN_LONG_OPTIONS("\n --timer\n") " -t : test timer resolution\n"); my_printf(WHEN_LONG_OPTIONS("\n --x-repeat=<n>\n") " -x <n> : run non-terminating tests (-r, -a) only n times\n"); my_printf(WHEN_LONG_OPTIONS("\n --paranoid\n") " -P : enable paranoid tests\n"); my_printf(WHEN_LONG_OPTIONS("\n --verbose\n") " -v : verbose output for subsequent options\n"); my_printf(WHEN_LONG_OPTIONS("\n --version\n") " -V : print FFTW version information\n"); my_printf(WHEN_LONG_OPTIONS("\n --help\n") " -h : this help\n");#ifndef HAVE_GETOPT my_printf("(When run with no arguments, an interactive mode is used.)\n");#endif if (exit_when_done) exit(EXIT_FAILURE);}char wfname[128];void handle_option(char opt, char *optarg){ FILE *wf; struct size sz; int rank, n; switch (opt) { case 's': sz = parse_size(optarg); if (!sz.is_nd) test_speed(sz.narray[0]); else test_speed_nd(sz); break; case 'c': sz = parse_size(optarg); if (!sz.is_nd) test_correctness(sz.narray[0]); else testnd_correctness_aux(sz); break; case 'p': rank = atoi(optarg); test_planner(rank); break; case 'P': paranoid = 1; enter_paranoid_mode(); break; case 'r': rank = atoi(optarg); test_all_random(rank); break; case 'a': rank = atoi(optarg); if (rank == 0) test_all(); else testnd_all(rank); break; case 't': test_timer(); break; case 'f': n = atoi(optarg); CHECK(n > 0, "-f requires a positive integer argument"); howmany_fields = n; break; case 'm': measure_flag = FFTW_MEASURE; break; case 'e': speed_flag = FFTW_ESTIMATE; break; case 'N': no_vector_flag = FFTW_NO_VECTOR_RECURSE; break; case 'w': wisdom_flag = FFTW_USE_WISDOM; strcpy(wfname, optarg); wf = fopen(wfname, "r"); if (wf == 0) { my_printf("Couldn't open wisdom file \"%s\".\n", wfname); my_printf("This file will be created upon completion.\n"); } else { CHECK(FFTW_SUCCESS == fftw_import_wisdom_from_file(wf), "invalid wisdom file format"); fclose(wf); } break; case '1': only_one_speed_test = 1; break; case 'v': verbose++; break; case 'V': my_printf("%s\n", fftw_version); my_printf("%s test program, compiled in %s precision.\n", fftw_prefix, sizeof(fftw_real) == sizeof(double) ? "double" : (sizeof(fftw_real) == sizeof(float) ? "single" : "unknown")); my_printf( "\nCopyright (C) Massachusetts Institute of Technology.\n" "FFTW comes with ABSOLUTELY NO WARRANTY. This is free software, and\n" "you are welcome to redistribute it under the terms of the GNU\n" "General Public License. For more information, see the file COPYING or\n" "the GNU web site at http://www.gnu.org.\n" "\nFor more information regarding FFTW, or to download the latest version,\n" "see the FFTW home page at http://www.fftw.org.\n"); break; case 'x': n = atoi(optarg); CHECK(n > 0, "-x requires a positive integer argument"); max_iterations = n; break; case 'h': usage(FALSE); break; default: usage(TRUE); } /* every test must free all the used FFTW memory */ if (!(wisdom_flag & FFTW_USE_WISDOM) && chk_mem_leak) fftw_check_memory_leaks();}short askuser(const char *s){ char line[200] = "", c; int i, count = 0; do { if (count++ > 0) my_printf("Invalid response. Please enter \"y\" or \"n\".\n"); my_printf("%s (y/n) ", s); /* skip blank lines */ while (line[0] == 0 || line[0] == '\n') fgets(line, 200, stdin); for (i = 0; line[i] && (line[i] == ' ' || line[i] == '\t'); ++i); c = line[i]; } while (c != 'n' && c != 'N' && c != 'y' && c != 'Y'); return (c == 'y' || c == 'Y');}/* Standard function to get the next command-line argument for the program. Returns the option character (or -1 if there are no more options), and the option argument (if any) in argval, which is an array of length at least argval_maxlen. The test programs need to implement a function get_option with the same arguments as this one, which will typically just call default_get_option. The reason we need to put this in a separate function is that the MPI test programs can't rely on all of the processes having direct access to the program arguments--they need to pass them as explicit messages from the master process. Sigh. */int default_get_option(int argc, char **argv, char *argval, int argval_maxlen){ int c = -1; if (argc <= 1) usage(TRUE);#ifdef HAVE_GETOPT { const char short_options[] = "s:c:w:f:p:Pa:r:tvVmehx:N1"; extern char *optarg; extern int optind; # if defined(HAVE_GETOPT_LONG) && defined(HAVE_GETOPT_H) { int option_index; const struct option long_options[] = { {"speed", 1, 0, 's'}, {"correctness", 1, 0, 'c'}, {"wisdom", 1, 0, 'w'}, {"fields", 1, 0, 'f'}, {"planner", 1, 0, 'p'}, {"paranoid", 0, 0, 'P'}, {"all", 1, 0, 'a'}, {"random", 1, 0, 'r'}, {"timer", 0, 0, 't'}, {"verbose", 0, 0, 'v'}, {"version", 0, 0, 'V'}, {"measure", 0, 0, 'm'}, {"estimate", 0, 0, 'e'}, {"help", 0, 0, 'h'}, {"x-repeat", 1, 0, 'x'}, {"no-vector-recurse", 0, 0, 'N'}, {"only-one-speed-test", 0, 0, '1'}, {0, 0, 0, 0} }; c = getopt_long(argc, argv, short_options, long_options, &option_index); }# else /* not HAVE_GETOPT_LONG */ c = getopt(argc, argv, short_options);# endif /* not HAVE_GETOPT_LONG */ if (c == -1 && argc != optind) usage(TRUE); /* there were invalid args; print usage info */ if (optarg) { strncpy(argval, optarg, argval_maxlen - 1); argval[argval_maxlen - 1] = 0; } else argval[0] = 0; }#endif /* HAVE_GETOPT */ return c;}int main(int argc, char *argv[]){#ifdef DETERMINISTIC srand(1123);#else srand((unsigned int) time(NULL));#endif test_init(&argc, &argv); /* * To parse the command line, we use getopt, but this does not seem * to be in the ANSI standard (it is only available on UNIX, * apparently). */#ifndef HAVE_GETOPT if (argc > 1) my_printf("Sorry, command-line arguments are not available on\n" "this system. Run fftw_test with no arguments to\n" "use it in interactive mode.\n"); if (argc <= 1) { int n = 0; char s[128] = ""; usage(FALSE); my_printf("\n"); if (askuser("Perform random correctness tests (non-terminating)?")) handle_option('r', "0"); if (askuser("Verbose output?")) handle_option('v', ""); if (askuser("Paranoid test?")) handle_option('P', ""); if (askuser("Use/test wisdom?")) { my_printf(" Enter wisdom file name to use: "); fgets(s, 128, stdin); handle_option('w', s); } if (askuser("Test correctness?")) { if (askuser(" -- for all sizes?")) handle_option('a', ""); else { my_printf(" Enter n: "); fgets(s, 128, stdin); handle_option('c', s); } } if (askuser("Test speed?")) { my_printf(" Enter n: "); fgets(s, 128, stdin); handle_option('s', s); } if (askuser("Test planner?")) handle_option('p', ""); if (askuser("Test timer?")) handle_option('t', ""); }#else /* * read command-line args using getopt * facility */ { char option_arg[128]; int c; while ((c = get_option(argc, argv, option_arg, 128)) != EOF) handle_option((int) c, option_arg); }#endif if (wisdom_flag & FFTW_USE_WISDOM) { char *ws; FILE *wf; ws = fftw_export_wisdom_to_string(); CHECK(ws != 0, "error exporting wisdom to string"); my_printf("\nAccumulated wisdom:\n %s\n", ws); fftw_forget_wisdom(); CHECK(FFTW_SUCCESS == fftw_import_wisdom_from_string(ws), "unexpected error reading in wisdom from string"); fftw_free(ws); if (io_okay) { wf = fopen(wfname, "w"); CHECK(wf != 0, "error creating wisdom file"); fftw_export_wisdom_to_file(wf); fclose(wf); } } /* make sure to dispose of wisdom before checking for memory leaks */ fftw_forget_wisdom(); fftw_check_memory_leaks(); if (io_okay) fftw_print_max_memory_usage(); test_finish(); return EXIT_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -