Chris@42: /* Chris@42: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@42: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@42: * Chris@42: * This program is free software; you can redistribute it and/or modify Chris@42: * it under the terms of the GNU General Public License as published by Chris@42: * the Free Software Foundation; either version 2 of the License, or Chris@42: * (at your option) any later version. Chris@42: * Chris@42: * This program is distributed in the hope that it will be useful, Chris@42: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@42: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@42: * GNU General Public License for more details. Chris@42: * Chris@42: * You should have received a copy of the GNU General Public License Chris@42: * along with this program; if not, write to the Free Software Chris@42: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@42: * Chris@42: */ Chris@42: Chris@42: #include "api.h" Chris@42: Chris@42: static planner_hook_t before_planner_hook = 0, after_planner_hook = 0; Chris@42: Chris@42: void X(set_planner_hooks)(planner_hook_t before, planner_hook_t after) Chris@42: { Chris@42: before_planner_hook = before; Chris@42: after_planner_hook = after; Chris@42: } Chris@42: Chris@42: static plan *mkplan0(planner *plnr, unsigned flags, Chris@42: const problem *prb, unsigned hash_info, Chris@42: wisdom_state_t wisdom_state) Chris@42: { Chris@42: /* map API flags into FFTW flags */ Chris@42: X(mapflags)(plnr, flags); Chris@42: Chris@42: plnr->flags.hash_info = hash_info; Chris@42: plnr->wisdom_state = wisdom_state; Chris@42: Chris@42: /* create plan */ Chris@42: return plnr->adt->mkplan(plnr, prb); Chris@42: } Chris@42: Chris@42: static unsigned force_estimator(unsigned flags) Chris@42: { Chris@42: flags &= ~(FFTW_MEASURE | FFTW_PATIENT | FFTW_EXHAUSTIVE); Chris@42: return (flags | FFTW_ESTIMATE); Chris@42: } Chris@42: Chris@42: static plan *mkplan(planner *plnr, unsigned flags, Chris@42: const problem *prb, unsigned hash_info) Chris@42: { Chris@42: plan *pln; Chris@42: Chris@42: pln = mkplan0(plnr, flags, prb, hash_info, WISDOM_NORMAL); Chris@42: Chris@42: if (plnr->wisdom_state == WISDOM_NORMAL && !pln) { Chris@42: /* maybe the planner failed because of inconsistent wisdom; Chris@42: plan again ignoring infeasible wisdom */ Chris@42: pln = mkplan0(plnr, force_estimator(flags), prb, Chris@42: hash_info, WISDOM_IGNORE_INFEASIBLE); Chris@42: } Chris@42: Chris@42: if (plnr->wisdom_state == WISDOM_IS_BOGUS) { Chris@42: /* if the planner detected a wisdom inconsistency, Chris@42: forget all wisdom and plan again */ Chris@42: plnr->adt->forget(plnr, FORGET_EVERYTHING); Chris@42: Chris@42: A(!pln); Chris@42: pln = mkplan0(plnr, flags, prb, hash_info, WISDOM_NORMAL); Chris@42: Chris@42: if (plnr->wisdom_state == WISDOM_IS_BOGUS) { Chris@42: /* if it still fails, plan without wisdom */ Chris@42: plnr->adt->forget(plnr, FORGET_EVERYTHING); Chris@42: Chris@42: A(!pln); Chris@42: pln = mkplan0(plnr, force_estimator(flags), Chris@42: prb, hash_info, WISDOM_IGNORE_ALL); Chris@42: } Chris@42: } Chris@42: Chris@42: return pln; Chris@42: } Chris@42: Chris@42: apiplan *X(mkapiplan)(int sign, unsigned flags, problem *prb) Chris@42: { Chris@42: apiplan *p = 0; Chris@42: plan *pln; Chris@42: unsigned flags_used_for_planning; Chris@42: planner *plnr; Chris@42: static const unsigned int pats[] = {FFTW_ESTIMATE, FFTW_MEASURE, Chris@42: FFTW_PATIENT, FFTW_EXHAUSTIVE}; Chris@42: int pat, pat_max; Chris@42: double pcost = 0; Chris@42: Chris@42: if (before_planner_hook) Chris@42: before_planner_hook(); Chris@42: Chris@42: plnr = X(the_planner)(); Chris@42: Chris@42: if (flags & FFTW_WISDOM_ONLY) { Chris@42: /* Special mode that returns a plan only if wisdom is present, Chris@42: and returns 0 otherwise. This is now documented in the manual, Chris@42: as a way to detect whether wisdom is available for a problem. */ Chris@42: flags_used_for_planning = flags; Chris@42: pln = mkplan0(plnr, flags, prb, 0, WISDOM_ONLY); Chris@42: } else { Chris@42: pat_max = flags & FFTW_ESTIMATE ? 0 : Chris@42: (flags & FFTW_EXHAUSTIVE ? 3 : Chris@42: (flags & FFTW_PATIENT ? 2 : 1)); Chris@42: pat = plnr->timelimit >= 0 ? 0 : pat_max; Chris@42: Chris@42: flags &= ~(FFTW_ESTIMATE | FFTW_MEASURE | Chris@42: FFTW_PATIENT | FFTW_EXHAUSTIVE); Chris@42: Chris@42: plnr->start_time = X(get_crude_time)(); Chris@42: Chris@42: /* plan at incrementally increasing patience until we run Chris@42: out of time */ Chris@42: for (pln = 0, flags_used_for_planning = 0; pat <= pat_max; ++pat) { Chris@42: plan *pln1; Chris@42: unsigned tmpflags = flags | pats[pat]; Chris@42: pln1 = mkplan(plnr, tmpflags, prb, 0u); Chris@42: Chris@42: if (!pln1) { Chris@42: /* don't bother continuing if planner failed or timed out */ Chris@42: A(!pln || plnr->timed_out); Chris@42: break; Chris@42: } Chris@42: Chris@42: X(plan_destroy_internal)(pln); Chris@42: pln = pln1; Chris@42: flags_used_for_planning = tmpflags; Chris@42: pcost = pln->pcost; Chris@42: } Chris@42: } Chris@42: Chris@42: if (pln) { Chris@42: /* build apiplan */ Chris@42: p = (apiplan *) MALLOC(sizeof(apiplan), PLANS); Chris@42: p->prb = prb; Chris@42: p->sign = sign; /* cache for execute_dft */ Chris@42: Chris@42: /* re-create plan from wisdom, adding blessing */ Chris@42: p->pln = mkplan(plnr, flags_used_for_planning, prb, BLESSING); Chris@42: Chris@42: /* record pcost from most recent measurement for use in X(cost) */ Chris@42: p->pln->pcost = pcost; Chris@42: Chris@42: if (sizeof(trigreal) > sizeof(R)) { Chris@42: /* this is probably faster, and we have enough trigreal Chris@42: bits to maintain accuracy */ Chris@42: X(plan_awake)(p->pln, AWAKE_SQRTN_TABLE); Chris@42: } else { Chris@42: /* more accurate */ Chris@42: X(plan_awake)(p->pln, AWAKE_SINCOS); Chris@42: } Chris@42: Chris@42: /* we don't use pln for p->pln, above, since by re-creating the Chris@42: plan we might use more patient wisdom from a timed-out mkplan */ Chris@42: X(plan_destroy_internal)(pln); Chris@42: } else Chris@42: X(problem_destroy)(prb); Chris@42: Chris@42: /* discard all information not necessary to reconstruct the plan */ Chris@42: plnr->adt->forget(plnr, FORGET_ACCURSED); Chris@42: Chris@42: #ifdef FFTW_RANDOM_ESTIMATOR Chris@42: X(random_estimate_seed)++; /* subsequent "random" plans are distinct */ Chris@42: #endif Chris@42: Chris@42: if (after_planner_hook) Chris@42: after_planner_hook(); Chris@42: Chris@42: return p; Chris@42: } Chris@42: Chris@42: void X(destroy_plan)(X(plan) p) Chris@42: { Chris@42: if (p) { Chris@42: X(plan_awake)(p->pln, SLEEPY); Chris@42: X(plan_destroy_internal)(p->pln); Chris@42: X(problem_destroy)(p->prb); Chris@42: X(ifree)(p); Chris@42: } Chris@42: } Chris@42: Chris@42: int X(alignment_of)(R *p) Chris@42: { Chris@42: return X(ialignment_of(p)); Chris@42: }