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