Chris@82: /* Chris@82: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@82: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@82: * Chris@82: * This program is free software; you can redistribute it and/or modify Chris@82: * it under the terms of the GNU General Public License as published by Chris@82: * the Free Software Foundation; either version 2 of the License, or Chris@82: * (at your option) any later version. Chris@82: * Chris@82: * This program is distributed in the hope that it will be useful, Chris@82: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@82: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@82: * GNU General Public License for more details. Chris@82: * Chris@82: * You should have received a copy of the GNU General Public License Chris@82: * along with this program; if not, write to the Free Software Chris@82: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@82: * Chris@82: */ Chris@82: Chris@82: Chris@82: #include "kernel/ifftw.h" Chris@82: Chris@82: #ifdef HAVE_UNISTD_H Chris@82: # include Chris@82: #endif Chris@82: Chris@82: #ifndef WITH_SLOW_TIMER Chris@82: # include "cycle.h" Chris@82: #endif Chris@82: Chris@82: #ifndef FFTW_TIME_LIMIT Chris@82: #define FFTW_TIME_LIMIT 2.0 /* don't run for more than two seconds */ Chris@82: #endif Chris@82: Chris@82: /* the following code is disabled for now, because it seems to Chris@82: require that we #include in ifftw.h to Chris@82: typedef LARGE_INTEGER crude_time, and this pulls in the whole Chris@82: Windows universe and leads to namespace conflicts (unless Chris@82: we did some hack like assuming sizeof(LARGE_INTEGER) == sizeof(long long). Chris@82: gettimeofday is provided by MinGW, which we use to cross-compile Chris@82: FFTW for Windows, and this seems to work well enough */ Chris@82: #if 0 && (defined(__WIN32__) || defined(_WIN32) || defined(_WIN64)) Chris@82: crude_time X(get_crude_time)(void) Chris@82: { Chris@82: crude_time tv; Chris@82: QueryPerformanceCounter(&tv); Chris@82: return tv; Chris@82: } Chris@82: Chris@82: static double elapsed_since(crude_time t0) Chris@82: { Chris@82: crude_time t1, freq; Chris@82: QueryPerformanceCounter(&t1); Chris@82: QueryPerformanceFrequency(&freq); Chris@82: return (((double) (t1.QuadPart - t0.QuadPart))) / Chris@82: ((double) freq.QuadPart); Chris@82: } Chris@82: Chris@82: # define TIME_MIN_SEC 1.0e-2 Chris@82: Chris@82: #elif defined(HAVE_GETTIMEOFDAY) Chris@82: crude_time X(get_crude_time)(void) Chris@82: { Chris@82: crude_time tv; Chris@82: gettimeofday(&tv, 0); Chris@82: return tv; Chris@82: } Chris@82: Chris@82: #define elapsed_sec(t1,t0) ((double)(t1.tv_sec - t0.tv_sec) + \ Chris@82: (double)(t1.tv_usec - t0.tv_usec) * 1.0E-6) Chris@82: Chris@82: static double elapsed_since(crude_time t0) Chris@82: { Chris@82: crude_time t1; Chris@82: gettimeofday(&t1, 0); Chris@82: return elapsed_sec(t1, t0); Chris@82: } Chris@82: Chris@82: # define TIME_MIN_SEC 1.0e-3 Chris@82: Chris@82: #else /* !HAVE_GETTIMEOFDAY */ Chris@82: Chris@82: /* Note that the only system where we are likely to need to fall back Chris@82: on the clock() function is Windows, for which CLOCKS_PER_SEC is 1000 Chris@82: and thus the clock wraps once every 50 days. This should hopefully Chris@82: be longer than the time required to create any single plan! */ Chris@82: crude_time X(get_crude_time)(void) { return clock(); } Chris@82: Chris@82: #define elapsed_sec(t1,t0) ((double) ((t1) - (t0)) / CLOCKS_PER_SEC) Chris@82: Chris@82: static double elapsed_since(crude_time t0) Chris@82: { Chris@82: return elapsed_sec(clock(), t0); Chris@82: } Chris@82: Chris@82: # define TIME_MIN_SEC 2.0e-1 /* from fftw2 */ Chris@82: Chris@82: #endif /* !HAVE_GETTIMEOFDAY */ Chris@82: Chris@82: double X(elapsed_since)(const planner *plnr, const problem *p, crude_time t0) Chris@82: { Chris@82: double t = elapsed_since(t0); Chris@82: if (plnr->cost_hook) Chris@82: t = plnr->cost_hook(p, t, COST_MAX); Chris@82: return t; Chris@82: } Chris@82: Chris@82: #ifdef WITH_SLOW_TIMER Chris@82: /* excruciatingly slow; only use this if there is no choice! */ Chris@82: typedef crude_time ticks; Chris@82: # define getticks X(get_crude_time) Chris@82: # define elapsed(t1,t0) elapsed_sec(t1,t0) Chris@82: # define TIME_MIN TIME_MIN_SEC Chris@82: # define TIME_REPEAT 4 /* from fftw2 */ Chris@82: # define HAVE_TICK_COUNTER Chris@82: #endif Chris@82: Chris@82: #ifdef HAVE_TICK_COUNTER Chris@82: Chris@82: # ifndef TIME_MIN Chris@82: # define TIME_MIN 100.0 Chris@82: # endif Chris@82: Chris@82: # ifndef TIME_REPEAT Chris@82: # define TIME_REPEAT 8 Chris@82: # endif Chris@82: Chris@82: static double measure(plan *pln, const problem *p, int iter) Chris@82: { Chris@82: ticks t0, t1; Chris@82: int i; Chris@82: Chris@82: t0 = getticks(); Chris@82: for (i = 0; i < iter; ++i) Chris@82: pln->adt->solve(pln, p); Chris@82: t1 = getticks(); Chris@82: return elapsed(t1, t0); Chris@82: } Chris@82: Chris@82: Chris@82: double X(measure_execution_time)(const planner *plnr, Chris@82: plan *pln, const problem *p) Chris@82: { Chris@82: int iter; Chris@82: int repeat; Chris@82: Chris@82: X(plan_awake)(pln, AWAKE_ZERO); Chris@82: p->adt->zero(p); Chris@82: Chris@82: start_over: Chris@82: for (iter = 1; iter; iter *= 2) { Chris@82: double tmin = 0; Chris@82: int first = 1; Chris@82: crude_time begin = X(get_crude_time)(); Chris@82: Chris@82: /* repeat the measurement TIME_REPEAT times */ Chris@82: for (repeat = 0; repeat < TIME_REPEAT; ++repeat) { Chris@82: double t = measure(pln, p, iter); Chris@82: Chris@82: if (plnr->cost_hook) Chris@82: t = plnr->cost_hook(p, t, COST_MAX); Chris@82: if (t < 0) Chris@82: goto start_over; Chris@82: Chris@82: if (first || t < tmin) Chris@82: tmin = t; Chris@82: first = 0; Chris@82: Chris@82: /* do not run for too long */ Chris@82: if (X(elapsed_since)(plnr, p, begin) > FFTW_TIME_LIMIT) Chris@82: break; Chris@82: } Chris@82: Chris@82: if (tmin >= TIME_MIN) { Chris@82: X(plan_awake)(pln, SLEEPY); Chris@82: return tmin / (double) iter; Chris@82: } Chris@82: } Chris@82: goto start_over; /* may happen if timer is screwed up */ Chris@82: } Chris@82: Chris@82: #else /* no cycle counter */ Chris@82: Chris@82: double X(measure_execution_time)(const planner *plnr, Chris@82: plan *pln, const problem *p) Chris@82: { Chris@82: UNUSED(plnr); Chris@82: UNUSED(p); Chris@82: UNUSED(pln); Chris@82: return -1.0; Chris@82: } Chris@82: Chris@82: #endif