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