Chris@82: /* Chris@82: * Copyright (c) 2000 Matteo Frigo Chris@82: * Copyright (c) 2000 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: #include "kernel/ifftw.h" Chris@82: #include Chris@82: Chris@82: /* GNU Coding Standards, Sec. 5.2: "Please write the comments in a GNU Chris@82: program in English, because English is the one language that nearly Chris@82: all programmers in all countries can read." Chris@82: Chris@82: ingemisco tanquam reus Chris@82: culpa rubet vultus meus Chris@82: supplicanti parce [rms] Chris@82: */ Chris@82: Chris@82: #define VALIDP(solution) ((solution)->flags.hash_info & H_VALID) Chris@82: #define LIVEP(solution) ((solution)->flags.hash_info & H_LIVE) Chris@82: #define SLVNDX(solution) ((solution)->flags.slvndx) Chris@82: #define BLISS(flags) (((flags).hash_info) & BLESSING) Chris@82: #define INFEASIBLE_SLVNDX ((1U<timelimit_impatience == 0); Chris@82: return (LEQ(a->u, b->u) && LEQ(b->l, a->l)); Chris@82: } else { Chris@82: return (LEQ(a->l, b->l) Chris@82: && a->timelimit_impatience <= b->timelimit_impatience); Chris@82: } Chris@82: } Chris@82: Chris@82: static unsigned addmod(unsigned a, unsigned b, unsigned p) Chris@82: { Chris@82: /* gcc-2.95/sparc produces incorrect code for the fast version below. */ Chris@82: #if defined(__sparc__) && defined(__GNUC__) Chris@82: /* slow version */ Chris@82: return (a + b) % p; Chris@82: #else Chris@82: /* faster version */ Chris@82: unsigned c = a + b; Chris@82: return c >= p ? c - p : c; Chris@82: #endif Chris@82: } Chris@82: Chris@82: /* Chris@82: slvdesc management: Chris@82: */ Chris@82: static void sgrow(planner *ego) Chris@82: { Chris@82: unsigned osiz = ego->slvdescsiz, nsiz = 1 + osiz + osiz / 4; Chris@82: slvdesc *ntab = (slvdesc *)MALLOC(nsiz * sizeof(slvdesc), SLVDESCS); Chris@82: slvdesc *otab = ego->slvdescs; Chris@82: unsigned i; Chris@82: Chris@82: ego->slvdescs = ntab; Chris@82: ego->slvdescsiz = nsiz; Chris@82: for (i = 0; i < osiz; ++i) Chris@82: ntab[i] = otab[i]; Chris@82: X(ifree0)(otab); Chris@82: } Chris@82: Chris@82: static void register_solver(planner *ego, solver *s) Chris@82: { Chris@82: slvdesc *n; Chris@82: int kind; Chris@82: Chris@82: if (s) { /* add s to solver list */ Chris@82: X(solver_use)(s); Chris@82: Chris@82: A(ego->nslvdesc < INFEASIBLE_SLVNDX); Chris@82: if (ego->nslvdesc >= ego->slvdescsiz) Chris@82: sgrow(ego); Chris@82: Chris@82: n = ego->slvdescs + ego->nslvdesc; Chris@82: Chris@82: n->slv = s; Chris@82: n->reg_nam = ego->cur_reg_nam; Chris@82: n->reg_id = ego->cur_reg_id++; Chris@82: Chris@82: A(strlen(n->reg_nam) < MAXNAM); Chris@82: n->nam_hash = X(hash)(n->reg_nam); Chris@82: Chris@82: kind = s->adt->problem_kind; Chris@82: n->next_for_same_problem_kind = ego->slvdescs_for_problem_kind[kind]; Chris@82: ego->slvdescs_for_problem_kind[kind] = (int)/*from unsigned*/ego->nslvdesc; Chris@82: Chris@82: ego->nslvdesc++; Chris@82: } Chris@82: } Chris@82: Chris@82: static unsigned slookup(planner *ego, char *nam, int id) Chris@82: { Chris@82: unsigned h = X(hash)(nam); /* used to avoid strcmp in the common case */ Chris@82: FORALL_SOLVERS(ego, s, sp, { Chris@82: UNUSED(s); Chris@82: if (sp->reg_id == id && sp->nam_hash == h Chris@82: && !strcmp(sp->reg_nam, nam)) Chris@82: return (unsigned)/*from ptrdiff_t*/(sp - ego->slvdescs); Chris@82: }); Chris@82: return INFEASIBLE_SLVNDX; Chris@82: } Chris@82: Chris@82: /* Compute a MD5 hash of the configuration of the planner. Chris@82: We store it into the wisdom file to make absolutely sure that Chris@82: we are reading wisdom that is applicable */ Chris@82: static void signature_of_configuration(md5 *m, planner *ego) Chris@82: { Chris@82: X(md5begin)(m); Chris@82: X(md5unsigned)(m, sizeof(R)); /* so we don't mix different precisions */ Chris@82: FORALL_SOLVERS(ego, s, sp, { Chris@82: UNUSED(s); Chris@82: X(md5int)(m, sp->reg_id); Chris@82: X(md5puts)(m, sp->reg_nam); Chris@82: }); Chris@82: X(md5end)(m); Chris@82: } Chris@82: Chris@82: /* Chris@82: md5-related stuff: Chris@82: */ Chris@82: Chris@82: /* first hash function */ Chris@82: static unsigned h1(const hashtab *ht, const md5sig s) Chris@82: { Chris@82: unsigned h = s[0] % ht->hashsiz; Chris@82: A(h == (s[0] % ht->hashsiz)); Chris@82: return h; Chris@82: } Chris@82: Chris@82: /* second hash function (for double hashing) */ Chris@82: static unsigned h2(const hashtab *ht, const md5sig s) Chris@82: { Chris@82: unsigned h = 1U + s[1] % (ht->hashsiz - 1); Chris@82: A(h == (1U + s[1] % (ht->hashsiz - 1))); Chris@82: return h; Chris@82: } Chris@82: Chris@82: static void md5hash(md5 *m, const problem *p, const planner *plnr) Chris@82: { Chris@82: X(md5begin)(m); Chris@82: X(md5unsigned)(m, sizeof(R)); /* so we don't mix different precisions */ Chris@82: X(md5int)(m, plnr->nthr); Chris@82: p->adt->hash(p, m); Chris@82: X(md5end)(m); Chris@82: } Chris@82: Chris@82: static int md5eq(const md5sig a, const md5sig b) Chris@82: { Chris@82: return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]; Chris@82: } Chris@82: Chris@82: static void sigcpy(const md5sig a, md5sig b) Chris@82: { Chris@82: b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; b[3] = a[3]; Chris@82: } Chris@82: Chris@82: /* Chris@82: memoization routines : Chris@82: */ Chris@82: Chris@82: /* Chris@82: liber scriptus proferetur Chris@82: in quo totum continetur Chris@82: unde mundus iudicetur Chris@82: */ Chris@82: struct solution_s { Chris@82: md5sig s; Chris@82: flags_t flags; Chris@82: }; Chris@82: Chris@82: static solution *htab_lookup(hashtab *ht, const md5sig s, Chris@82: const flags_t *flagsp) Chris@82: { Chris@82: unsigned g, h = h1(ht, s), d = h2(ht, s); Chris@82: solution *best = 0; Chris@82: Chris@82: ++ht->lookup; Chris@82: Chris@82: /* search all entries that match; select the one with Chris@82: the lowest flags.u */ Chris@82: /* This loop may potentially traverse the whole table, since at Chris@82: least one element is guaranteed to be !LIVEP, but all elements Chris@82: may be VALIDP. Hence, we stop after at the first invalid Chris@82: element or after traversing the whole table. */ Chris@82: g = h; Chris@82: do { Chris@82: solution *l = ht->solutions + g; Chris@82: ++ht->lookup_iter; Chris@82: if (VALIDP(l)) { Chris@82: if (LIVEP(l) Chris@82: && md5eq(s, l->s) Chris@82: && subsumes(&l->flags, SLVNDX(l), flagsp) ) { Chris@82: if (!best || LEQ(l->flags.u, best->flags.u)) Chris@82: best = l; Chris@82: } Chris@82: } else Chris@82: break; Chris@82: Chris@82: g = addmod(g, d, ht->hashsiz); Chris@82: } while (g != h); Chris@82: Chris@82: if (best) Chris@82: ++ht->succ_lookup; Chris@82: return best; Chris@82: } Chris@82: Chris@82: static solution *hlookup(planner *ego, const md5sig s, Chris@82: const flags_t *flagsp) Chris@82: { Chris@82: solution *sol = htab_lookup(&ego->htab_blessed, s, flagsp); Chris@82: if (!sol) sol = htab_lookup(&ego->htab_unblessed, s, flagsp); Chris@82: return sol; Chris@82: } Chris@82: Chris@82: static void fill_slot(hashtab *ht, const md5sig s, const flags_t *flagsp, Chris@82: unsigned slvndx, solution *slot) Chris@82: { Chris@82: ++ht->insert; Chris@82: ++ht->nelem; Chris@82: A(!LIVEP(slot)); Chris@82: slot->flags.u = flagsp->u; Chris@82: slot->flags.l = flagsp->l; Chris@82: slot->flags.timelimit_impatience = flagsp->timelimit_impatience; Chris@82: slot->flags.hash_info |= H_VALID | H_LIVE; Chris@82: SLVNDX(slot) = slvndx; Chris@82: Chris@82: /* keep this check enabled in case we add so many solvers Chris@82: that the bitfield overflows */ Chris@82: CK(SLVNDX(slot) == slvndx); Chris@82: sigcpy(s, slot->s); Chris@82: } Chris@82: Chris@82: static void kill_slot(hashtab *ht, solution *slot) Chris@82: { Chris@82: A(LIVEP(slot)); /* ==> */ A(VALIDP(slot)); Chris@82: Chris@82: --ht->nelem; Chris@82: slot->flags.hash_info = H_VALID; Chris@82: } Chris@82: Chris@82: static void hinsert0(hashtab *ht, const md5sig s, const flags_t *flagsp, Chris@82: unsigned slvndx) Chris@82: { Chris@82: solution *l; Chris@82: unsigned g, h = h1(ht, s), d = h2(ht, s); Chris@82: Chris@82: ++ht->insert_unknown; Chris@82: Chris@82: /* search for nonfull slot */ Chris@82: for (g = h; ; g = addmod(g, d, ht->hashsiz)) { Chris@82: ++ht->insert_iter; Chris@82: l = ht->solutions + g; Chris@82: if (!LIVEP(l)) break; Chris@82: A((g + d) % ht->hashsiz != h); Chris@82: } Chris@82: Chris@82: fill_slot(ht, s, flagsp, slvndx, l); Chris@82: } Chris@82: Chris@82: static void rehash(hashtab *ht, unsigned nsiz) Chris@82: { Chris@82: unsigned osiz = ht->hashsiz, h; Chris@82: solution *osol = ht->solutions, *nsol; Chris@82: Chris@82: nsiz = (unsigned)X(next_prime)((INT)nsiz); Chris@82: nsol = (solution *)MALLOC(nsiz * sizeof(solution), HASHT); Chris@82: ++ht->nrehash; Chris@82: Chris@82: /* init new table */ Chris@82: for (h = 0; h < nsiz; ++h) Chris@82: nsol[h].flags.hash_info = 0; Chris@82: Chris@82: /* install new table */ Chris@82: ht->hashsiz = nsiz; Chris@82: ht->solutions = nsol; Chris@82: ht->nelem = 0; Chris@82: Chris@82: /* copy table */ Chris@82: for (h = 0; h < osiz; ++h) { Chris@82: solution *l = osol + h; Chris@82: if (LIVEP(l)) Chris@82: hinsert0(ht, l->s, &l->flags, SLVNDX(l)); Chris@82: } Chris@82: Chris@82: X(ifree0)(osol); Chris@82: } Chris@82: Chris@82: static unsigned minsz(unsigned nelem) Chris@82: { Chris@82: return 1U + nelem + nelem / 8U; Chris@82: } Chris@82: Chris@82: static unsigned nextsz(unsigned nelem) Chris@82: { Chris@82: return minsz(minsz(nelem)); Chris@82: } Chris@82: Chris@82: static void hgrow(hashtab *ht) Chris@82: { Chris@82: unsigned nelem = ht->nelem; Chris@82: if (minsz(nelem) >= ht->hashsiz) Chris@82: rehash(ht, nextsz(nelem)); Chris@82: } Chris@82: Chris@82: #if 0 Chris@82: /* shrink the hash table, never used */ Chris@82: static void hshrink(hashtab *ht) Chris@82: { Chris@82: unsigned nelem = ht->nelem; Chris@82: /* always rehash after deletions */ Chris@82: rehash(ht, nextsz(nelem)); Chris@82: } Chris@82: #endif Chris@82: Chris@82: static void htab_insert(hashtab *ht, const md5sig s, const flags_t *flagsp, Chris@82: unsigned slvndx) Chris@82: { Chris@82: unsigned g, h = h1(ht, s), d = h2(ht, s); Chris@82: solution *first = 0; Chris@82: Chris@82: /* Remove all entries that are subsumed by the new one. */ Chris@82: /* This loop may potentially traverse the whole table, since at Chris@82: least one element is guaranteed to be !LIVEP, but all elements Chris@82: may be VALIDP. Hence, we stop after at the first invalid Chris@82: element or after traversing the whole table. */ Chris@82: g = h; Chris@82: do { Chris@82: solution *l = ht->solutions + g; Chris@82: ++ht->insert_iter; Chris@82: if (VALIDP(l)) { Chris@82: if (LIVEP(l) && md5eq(s, l->s)) { Chris@82: if (subsumes(flagsp, slvndx, &l->flags)) { Chris@82: if (!first) first = l; Chris@82: kill_slot(ht, l); Chris@82: } else { Chris@82: /* It is an error to insert an element that Chris@82: is subsumed by an existing entry. */ Chris@82: A(!subsumes(&l->flags, SLVNDX(l), flagsp)); Chris@82: } Chris@82: } Chris@82: } else Chris@82: break; Chris@82: Chris@82: g = addmod(g, d, ht->hashsiz); Chris@82: } while (g != h); Chris@82: Chris@82: if (first) { Chris@82: /* overwrite FIRST */ Chris@82: fill_slot(ht, s, flagsp, slvndx, first); Chris@82: } else { Chris@82: /* create a new entry */ Chris@82: hgrow(ht); Chris@82: hinsert0(ht, s, flagsp, slvndx); Chris@82: } Chris@82: } Chris@82: Chris@82: static void hinsert(planner *ego, const md5sig s, const flags_t *flagsp, Chris@82: unsigned slvndx) Chris@82: { Chris@82: htab_insert(BLISS(*flagsp) ? &ego->htab_blessed : &ego->htab_unblessed, Chris@82: s, flagsp, slvndx ); Chris@82: } Chris@82: Chris@82: Chris@82: static void invoke_hook(planner *ego, plan *pln, const problem *p, Chris@82: int optimalp) Chris@82: { Chris@82: if (ego->hook) Chris@82: ego->hook(ego, pln, p, optimalp); Chris@82: } Chris@82: Chris@82: #ifdef FFTW_RANDOM_ESTIMATOR Chris@82: /* a "random" estimate, used for debugging to generate "random" Chris@82: plans, albeit from a deterministic seed. */ Chris@82: Chris@82: unsigned X(random_estimate_seed) = 0; Chris@82: Chris@82: static double random_estimate(const planner *ego, const plan *pln, Chris@82: const problem *p) Chris@82: { Chris@82: md5 m; Chris@82: X(md5begin)(&m); Chris@82: X(md5unsigned)(&m, X(random_estimate_seed)); Chris@82: X(md5int)(&m, ego->nthr); Chris@82: p->adt->hash(p, &m); Chris@82: X(md5putb)(&m, &pln->ops, sizeof(pln->ops)); Chris@82: X(md5putb)(&m, &pln->adt, sizeof(pln->adt)); Chris@82: X(md5end)(&m); Chris@82: return ego->cost_hook ? ego->cost_hook(p, m.s[0], COST_MAX) : m.s[0]; Chris@82: } Chris@82: Chris@82: #endif Chris@82: Chris@82: double X(iestimate_cost)(const planner *ego, const plan *pln, const problem *p) Chris@82: { Chris@82: double cost = Chris@82: + pln->ops.add Chris@82: + pln->ops.mul Chris@82: Chris@82: #if HAVE_FMA Chris@82: + pln->ops.fma Chris@82: #else Chris@82: + 2 * pln->ops.fma Chris@82: #endif Chris@82: Chris@82: + pln->ops.other; Chris@82: if (ego->cost_hook) Chris@82: cost = ego->cost_hook(p, cost, COST_MAX); Chris@82: return cost; Chris@82: } Chris@82: Chris@82: static void evaluate_plan(planner *ego, plan *pln, const problem *p) Chris@82: { Chris@82: if (ESTIMATEP(ego) || !BELIEVE_PCOSTP(ego) || pln->pcost == 0.0) { Chris@82: ego->nplan++; Chris@82: Chris@82: if (ESTIMATEP(ego)) { Chris@82: estimate: Chris@82: /* heuristic */ Chris@82: #ifdef FFTW_RANDOM_ESTIMATOR Chris@82: pln->pcost = random_estimate(ego, pln, p); Chris@82: ego->epcost += X(iestimate_cost)(ego, pln, p); Chris@82: #else Chris@82: pln->pcost = X(iestimate_cost)(ego, pln, p); Chris@82: ego->epcost += pln->pcost; Chris@82: #endif Chris@82: } else { Chris@82: double t = X(measure_execution_time)(ego, pln, p); Chris@82: Chris@82: if (t < 0) { /* unavailable cycle counter */ Chris@82: /* Real programmers can write FORTRAN in any language */ Chris@82: goto estimate; Chris@82: } Chris@82: Chris@82: pln->pcost = t; Chris@82: ego->pcost += t; Chris@82: ego->need_timeout_check = 1; Chris@82: } Chris@82: } Chris@82: Chris@82: invoke_hook(ego, pln, p, 0); Chris@82: } Chris@82: Chris@82: /* maintain dynamic scoping of flags, nthr: */ Chris@82: static plan *invoke_solver(planner *ego, const problem *p, solver *s, Chris@82: const flags_t *nflags) Chris@82: { Chris@82: flags_t flags = ego->flags; Chris@82: int nthr = ego->nthr; Chris@82: plan *pln; Chris@82: ego->flags = *nflags; Chris@82: PLNR_TIMELIMIT_IMPATIENCE(ego) = 0; Chris@82: A(p->adt->problem_kind == s->adt->problem_kind); Chris@82: pln = s->adt->mkplan(s, p, ego); Chris@82: ego->nthr = nthr; Chris@82: ego->flags = flags; Chris@82: return pln; Chris@82: } Chris@82: Chris@82: /* maintain the invariant TIMED_OUT ==> NEED_TIMEOUT_CHECK */ Chris@82: static int timeout_p(planner *ego, const problem *p) Chris@82: { Chris@82: /* do not timeout when estimating. First, the estimator is the Chris@82: planner of last resort. Second, calling X(elapsed_since)() is Chris@82: slower than estimating */ Chris@82: if (!ESTIMATEP(ego)) { Chris@82: /* do not assume that X(elapsed_since)() is monotonic */ Chris@82: if (ego->timed_out) { Chris@82: A(ego->need_timeout_check); Chris@82: return 1; Chris@82: } Chris@82: Chris@82: if (ego->timelimit >= 0 && Chris@82: X(elapsed_since)(ego, p, ego->start_time) >= ego->timelimit) { Chris@82: ego->timed_out = 1; Chris@82: ego->need_timeout_check = 1; Chris@82: return 1; Chris@82: } Chris@82: } Chris@82: Chris@82: A(!ego->timed_out); Chris@82: ego->need_timeout_check = 0; Chris@82: return 0; Chris@82: } Chris@82: Chris@82: static plan *search0(planner *ego, const problem *p, unsigned *slvndx, Chris@82: const flags_t *flagsp) Chris@82: { Chris@82: plan *best = 0; Chris@82: int best_not_yet_timed = 1; Chris@82: Chris@82: /* Do not start a search if the planner timed out. This check is Chris@82: necessary, lest the relaxation mechanism kick in */ Chris@82: if (timeout_p(ego, p)) Chris@82: return 0; Chris@82: Chris@82: FORALL_SOLVERS_OF_KIND(p->adt->problem_kind, ego, s, sp, { Chris@82: plan *pln; Chris@82: Chris@82: pln = invoke_solver(ego, p, s, flagsp); Chris@82: Chris@82: if (ego->need_timeout_check) Chris@82: if (timeout_p(ego, p)) { Chris@82: X(plan_destroy_internal)(pln); Chris@82: X(plan_destroy_internal)(best); Chris@82: return 0; Chris@82: } Chris@82: Chris@82: if (pln) { Chris@82: /* read COULD_PRUNE_NOW_P because PLN may be destroyed Chris@82: before we use COULD_PRUNE_NOW_P */ Chris@82: int could_prune_now_p = pln->could_prune_now_p; Chris@82: Chris@82: if (best) { Chris@82: if (best_not_yet_timed) { Chris@82: evaluate_plan(ego, best, p); Chris@82: best_not_yet_timed = 0; Chris@82: } Chris@82: evaluate_plan(ego, pln, p); Chris@82: if (pln->pcost < best->pcost) { Chris@82: X(plan_destroy_internal)(best); Chris@82: best = pln; Chris@82: *slvndx = (unsigned)/*from ptrdiff_t*/(sp - ego->slvdescs); Chris@82: } else { Chris@82: X(plan_destroy_internal)(pln); Chris@82: } Chris@82: } else { Chris@82: best = pln; Chris@82: *slvndx = (unsigned)/*from ptrdiff_t*/(sp - ego->slvdescs); Chris@82: } Chris@82: Chris@82: if (ALLOW_PRUNINGP(ego) && could_prune_now_p) Chris@82: break; Chris@82: } Chris@82: }); Chris@82: Chris@82: return best; Chris@82: } Chris@82: Chris@82: static plan *search(planner *ego, const problem *p, unsigned *slvndx, Chris@82: flags_t *flagsp) Chris@82: { Chris@82: plan *pln = 0; Chris@82: unsigned i; Chris@82: Chris@82: /* relax impatience in this order: */ Chris@82: static const unsigned relax_tab[] = { Chris@82: 0, /* relax nothing */ Chris@82: NO_VRECURSE, Chris@82: NO_FIXED_RADIX_LARGE_N, Chris@82: NO_SLOW, Chris@82: NO_UGLY Chris@82: }; Chris@82: Chris@82: unsigned l_orig = flagsp->l; Chris@82: unsigned x = flagsp->u; Chris@82: Chris@82: /* guaranteed to be different from X */ Chris@82: unsigned last_x = ~x; Chris@82: Chris@82: for (i = 0; i < sizeof(relax_tab) / sizeof(relax_tab[0]); ++i) { Chris@82: if (LEQ(l_orig, x & ~relax_tab[i])) Chris@82: x = x & ~relax_tab[i]; Chris@82: Chris@82: if (x != last_x) { Chris@82: last_x = x; Chris@82: flagsp->l = x; Chris@82: pln = search0(ego, p, slvndx, flagsp); Chris@82: if (pln) break; Chris@82: } Chris@82: } Chris@82: Chris@82: if (!pln) { Chris@82: /* search [L_ORIG, U] */ Chris@82: if (l_orig != last_x) { Chris@82: last_x = l_orig; Chris@82: flagsp->l = l_orig; Chris@82: pln = search0(ego, p, slvndx, flagsp); Chris@82: } Chris@82: } Chris@82: Chris@82: return pln; Chris@82: } Chris@82: Chris@82: #define CHECK_FOR_BOGOSITY \ Chris@82: if ((ego->bogosity_hook ? \ Chris@82: (ego->wisdom_state = ego->bogosity_hook(ego->wisdom_state, p)) \ Chris@82: : ego->wisdom_state) == WISDOM_IS_BOGUS) \ Chris@82: goto wisdom_is_bogus; Chris@82: Chris@82: static plan *mkplan(planner *ego, const problem *p) Chris@82: { Chris@82: plan *pln; Chris@82: md5 m; Chris@82: unsigned slvndx; Chris@82: flags_t flags_of_solution; Chris@82: solution *sol; Chris@82: solver *s; Chris@82: Chris@82: ASSERT_ALIGNED_DOUBLE; Chris@82: A(LEQ(PLNR_L(ego), PLNR_U(ego))); Chris@82: Chris@82: if (ESTIMATEP(ego)) Chris@82: PLNR_TIMELIMIT_IMPATIENCE(ego) = 0; /* canonical form */ Chris@82: Chris@82: Chris@82: #ifdef FFTW_DEBUG Chris@82: check(&ego->htab_blessed); Chris@82: check(&ego->htab_unblessed); Chris@82: #endif Chris@82: Chris@82: pln = 0; Chris@82: Chris@82: CHECK_FOR_BOGOSITY; Chris@82: Chris@82: ego->timed_out = 0; Chris@82: Chris@82: ++ego->nprob; Chris@82: md5hash(&m, p, ego); Chris@82: Chris@82: flags_of_solution = ego->flags; Chris@82: Chris@82: if (ego->wisdom_state != WISDOM_IGNORE_ALL) { Chris@82: if ((sol = hlookup(ego, m.s, &flags_of_solution))) { Chris@82: /* wisdom is acceptable */ Chris@82: wisdom_state_t owisdom_state = ego->wisdom_state; Chris@82: Chris@82: /* this hook is mainly for MPI, to make sure that Chris@82: wisdom is in sync across all processes for MPI problems */ Chris@82: if (ego->wisdom_ok_hook && !ego->wisdom_ok_hook(p, sol->flags)) Chris@82: goto do_search; /* ignore not-ok wisdom */ Chris@82: Chris@82: slvndx = SLVNDX(sol); Chris@82: Chris@82: if (slvndx == INFEASIBLE_SLVNDX) { Chris@82: if (ego->wisdom_state == WISDOM_IGNORE_INFEASIBLE) Chris@82: goto do_search; Chris@82: else Chris@82: return 0; /* known to be infeasible */ Chris@82: } Chris@82: Chris@82: flags_of_solution = sol->flags; Chris@82: Chris@82: /* inherit blessing either from wisdom Chris@82: or from the planner */ Chris@82: flags_of_solution.hash_info |= BLISS(ego->flags); Chris@82: Chris@82: ego->wisdom_state = WISDOM_ONLY; Chris@82: Chris@82: s = ego->slvdescs[slvndx].slv; Chris@82: if (p->adt->problem_kind != s->adt->problem_kind) Chris@82: goto wisdom_is_bogus; Chris@82: Chris@82: pln = invoke_solver(ego, p, s, &flags_of_solution); Chris@82: Chris@82: CHECK_FOR_BOGOSITY; /* catch error in child solvers */ Chris@82: Chris@82: sol = 0; /* Paranoia: SOL may be dangling after Chris@82: invoke_solver(); make sure we don't accidentally Chris@82: reuse it. */ Chris@82: Chris@82: if (!pln) Chris@82: goto wisdom_is_bogus; Chris@82: Chris@82: ego->wisdom_state = owisdom_state; Chris@82: Chris@82: goto skip_search; Chris@82: } Chris@82: else if (ego->nowisdom_hook) /* for MPI, make sure lack of wisdom */ Chris@82: ego->nowisdom_hook(p); /* is in sync across all processes */ Chris@82: } Chris@82: Chris@82: do_search: Chris@82: /* cannot search in WISDOM_ONLY mode */ Chris@82: if (ego->wisdom_state == WISDOM_ONLY) Chris@82: goto wisdom_is_bogus; Chris@82: Chris@82: flags_of_solution = ego->flags; Chris@82: pln = search(ego, p, &slvndx, &flags_of_solution); Chris@82: CHECK_FOR_BOGOSITY; /* catch error in child solvers */ Chris@82: Chris@82: if (ego->timed_out) { Chris@82: A(!pln); Chris@82: if (PLNR_TIMELIMIT_IMPATIENCE(ego) != 0) { Chris@82: /* record (below) that this plan has failed because of Chris@82: timeout */ Chris@82: flags_of_solution.hash_info |= BLESSING; Chris@82: } else { Chris@82: /* this is not the top-level problem or timeout is not Chris@82: active: record no wisdom. */ Chris@82: return 0; Chris@82: } Chris@82: } else { Chris@82: /* canonicalize to infinite timeout */ Chris@82: flags_of_solution.timelimit_impatience = 0; Chris@82: } Chris@82: Chris@82: skip_search: Chris@82: if (ego->wisdom_state == WISDOM_NORMAL || Chris@82: ego->wisdom_state == WISDOM_ONLY) { Chris@82: if (pln) { Chris@82: hinsert(ego, m.s, &flags_of_solution, slvndx); Chris@82: invoke_hook(ego, pln, p, 1); Chris@82: } else { Chris@82: hinsert(ego, m.s, &flags_of_solution, INFEASIBLE_SLVNDX); Chris@82: } Chris@82: } Chris@82: Chris@82: return pln; Chris@82: Chris@82: wisdom_is_bogus: Chris@82: X(plan_destroy_internal)(pln); Chris@82: ego->wisdom_state = WISDOM_IS_BOGUS; Chris@82: return 0; Chris@82: } Chris@82: Chris@82: static void htab_destroy(hashtab *ht) Chris@82: { Chris@82: X(ifree)(ht->solutions); Chris@82: ht->solutions = 0; Chris@82: ht->nelem = 0U; Chris@82: } Chris@82: Chris@82: static void mkhashtab(hashtab *ht) Chris@82: { Chris@82: ht->nrehash = 0; Chris@82: ht->succ_lookup = ht->lookup = ht->lookup_iter = 0; Chris@82: ht->insert = ht->insert_iter = ht->insert_unknown = 0; Chris@82: Chris@82: ht->solutions = 0; Chris@82: ht->hashsiz = ht->nelem = 0U; Chris@82: hgrow(ht); /* so that hashsiz > 0 */ Chris@82: } Chris@82: Chris@82: /* destroy hash table entries. If FORGET_EVERYTHING, destroy the whole Chris@82: table. If FORGET_ACCURSED, then destroy entries that are not blessed. */ Chris@82: static void forget(planner *ego, amnesia a) Chris@82: { Chris@82: switch (a) { Chris@82: case FORGET_EVERYTHING: Chris@82: htab_destroy(&ego->htab_blessed); Chris@82: mkhashtab(&ego->htab_blessed); Chris@82: /* fall through */ Chris@82: case FORGET_ACCURSED: Chris@82: htab_destroy(&ego->htab_unblessed); Chris@82: mkhashtab(&ego->htab_unblessed); Chris@82: break; Chris@82: default: Chris@82: break; Chris@82: } Chris@82: } Chris@82: Chris@82: /* FIXME: what sort of version information should we write? */ Chris@82: #define WISDOM_PREAMBLE PACKAGE "-" VERSION " " STRINGIZE(X(wisdom)) Chris@82: static const char stimeout[] = "TIMEOUT"; Chris@82: Chris@82: /* tantus labor non sit cassus */ Chris@82: static void exprt(planner *ego, printer *p) Chris@82: { Chris@82: unsigned h; Chris@82: hashtab *ht = &ego->htab_blessed; Chris@82: md5 m; Chris@82: Chris@82: signature_of_configuration(&m, ego); Chris@82: Chris@82: p->print(p, Chris@82: "(" WISDOM_PREAMBLE " #x%M #x%M #x%M #x%M\n", Chris@82: m.s[0], m.s[1], m.s[2], m.s[3]); Chris@82: Chris@82: for (h = 0; h < ht->hashsiz; ++h) { Chris@82: solution *l = ht->solutions + h; Chris@82: if (LIVEP(l)) { Chris@82: const char *reg_nam; Chris@82: int reg_id; Chris@82: Chris@82: if (SLVNDX(l) == INFEASIBLE_SLVNDX) { Chris@82: reg_nam = stimeout; Chris@82: reg_id = 0; Chris@82: } else { Chris@82: slvdesc *sp = ego->slvdescs + SLVNDX(l); Chris@82: reg_nam = sp->reg_nam; Chris@82: reg_id = sp->reg_id; Chris@82: } Chris@82: Chris@82: /* qui salvandos salvas gratis Chris@82: salva me fons pietatis */ Chris@82: p->print(p, " (%s %d #x%x #x%x #x%x #x%M #x%M #x%M #x%M)\n", Chris@82: reg_nam, reg_id, Chris@82: l->flags.l, l->flags.u, l->flags.timelimit_impatience, Chris@82: l->s[0], l->s[1], l->s[2], l->s[3]); Chris@82: } Chris@82: } Chris@82: p->print(p, ")\n"); Chris@82: } Chris@82: Chris@82: /* mors stupebit et natura Chris@82: cum resurget creatura */ Chris@82: static int imprt(planner *ego, scanner *sc) Chris@82: { Chris@82: char buf[MAXNAM + 1]; Chris@82: md5uint sig[4]; Chris@82: unsigned l, u, timelimit_impatience; Chris@82: flags_t flags; Chris@82: int reg_id; Chris@82: unsigned slvndx; Chris@82: hashtab *ht = &ego->htab_blessed; Chris@82: hashtab old; Chris@82: md5 m; Chris@82: Chris@82: if (!sc->scan(sc, Chris@82: "(" WISDOM_PREAMBLE " #x%M #x%M #x%M #x%M\n", Chris@82: sig + 0, sig + 1, sig + 2, sig + 3)) Chris@82: return 0; /* don't need to restore hashtable */ Chris@82: Chris@82: signature_of_configuration(&m, ego); Chris@82: if (m.s[0] != sig[0] || m.s[1] != sig[1] || Chris@82: m.s[2] != sig[2] || m.s[3] != sig[3]) { Chris@82: /* invalid configuration */ Chris@82: return 0; Chris@82: } Chris@82: Chris@82: /* make a backup copy of the hash table (cache the hash) */ Chris@82: { Chris@82: unsigned h, hsiz = ht->hashsiz; Chris@82: old = *ht; Chris@82: old.solutions = (solution *)MALLOC(hsiz * sizeof(solution), HASHT); Chris@82: for (h = 0; h < hsiz; ++h) Chris@82: old.solutions[h] = ht->solutions[h]; Chris@82: } Chris@82: Chris@82: while (1) { Chris@82: if (sc->scan(sc, ")")) Chris@82: break; Chris@82: Chris@82: /* qua resurget ex favilla */ Chris@82: if (!sc->scan(sc, "(%*s %d #x%x #x%x #x%x #x%M #x%M #x%M #x%M)", Chris@82: MAXNAM, buf, ®_id, &l, &u, &timelimit_impatience, Chris@82: sig + 0, sig + 1, sig + 2, sig + 3)) Chris@82: goto bad; Chris@82: Chris@82: if (!strcmp(buf, stimeout) && reg_id == 0) { Chris@82: slvndx = INFEASIBLE_SLVNDX; Chris@82: } else { Chris@82: if (timelimit_impatience != 0) Chris@82: goto bad; Chris@82: Chris@82: slvndx = slookup(ego, buf, reg_id); Chris@82: if (slvndx == INFEASIBLE_SLVNDX) Chris@82: goto bad; Chris@82: } Chris@82: Chris@82: /* inter oves locum praesta */ Chris@82: flags.l = l; Chris@82: flags.u = u; Chris@82: flags.timelimit_impatience = timelimit_impatience; Chris@82: flags.hash_info = BLESSING; Chris@82: Chris@82: CK(flags.l == l); Chris@82: CK(flags.u == u); Chris@82: CK(flags.timelimit_impatience == timelimit_impatience); Chris@82: Chris@82: if (!hlookup(ego, sig, &flags)) Chris@82: hinsert(ego, sig, &flags, slvndx); Chris@82: } Chris@82: Chris@82: X(ifree0)(old.solutions); Chris@82: return 1; Chris@82: Chris@82: bad: Chris@82: /* ``The wisdom of FFTW must be above suspicion.'' */ Chris@82: X(ifree0)(ht->solutions); Chris@82: *ht = old; Chris@82: return 0; Chris@82: } Chris@82: Chris@82: /* Chris@82: * create a planner Chris@82: */ Chris@82: planner *X(mkplanner)(void) Chris@82: { Chris@82: int i; Chris@82: Chris@82: static const planner_adt padt = { Chris@82: register_solver, mkplan, forget, exprt, imprt Chris@82: }; Chris@82: Chris@82: planner *p = (planner *) MALLOC(sizeof(planner), PLANNERS); Chris@82: Chris@82: p->adt = &padt; Chris@82: p->nplan = p->nprob = 0; Chris@82: p->pcost = p->epcost = 0.0; Chris@82: p->hook = 0; Chris@82: p->cost_hook = 0; Chris@82: p->wisdom_ok_hook = 0; Chris@82: p->nowisdom_hook = 0; Chris@82: p->bogosity_hook = 0; Chris@82: p->cur_reg_nam = 0; Chris@82: p->wisdom_state = WISDOM_NORMAL; Chris@82: Chris@82: p->slvdescs = 0; Chris@82: p->nslvdesc = p->slvdescsiz = 0; Chris@82: Chris@82: p->flags.l = 0; Chris@82: p->flags.u = 0; Chris@82: p->flags.timelimit_impatience = 0; Chris@82: p->flags.hash_info = 0; Chris@82: p->nthr = 1; Chris@82: p->need_timeout_check = 1; Chris@82: p->timelimit = -1; Chris@82: Chris@82: mkhashtab(&p->htab_blessed); Chris@82: mkhashtab(&p->htab_unblessed); Chris@82: Chris@82: for (i = 0; i < PROBLEM_LAST; ++i) Chris@82: p->slvdescs_for_problem_kind[i] = -1; Chris@82: Chris@82: return p; Chris@82: } Chris@82: Chris@82: void X(planner_destroy)(planner *ego) Chris@82: { Chris@82: /* destroy hash table */ Chris@82: htab_destroy(&ego->htab_blessed); Chris@82: htab_destroy(&ego->htab_unblessed); Chris@82: Chris@82: /* destroy solvdesc table */ Chris@82: FORALL_SOLVERS(ego, s, sp, { Chris@82: UNUSED(sp); Chris@82: X(solver_destroy)(s); Chris@82: }); Chris@82: Chris@82: X(ifree0)(ego->slvdescs); Chris@82: X(ifree)(ego); /* dona eis requiem */ Chris@82: } Chris@82: Chris@82: plan *X(mkplan_d)(planner *ego, problem *p) Chris@82: { Chris@82: plan *pln = ego->adt->mkplan(ego, p); Chris@82: X(problem_destroy)(p); Chris@82: return pln; Chris@82: } Chris@82: Chris@82: /* like X(mkplan_d), but sets/resets flags as well */ Chris@82: plan *X(mkplan_f_d)(planner *ego, problem *p, Chris@82: unsigned l_set, unsigned u_set, unsigned u_reset) Chris@82: { Chris@82: flags_t oflags = ego->flags; Chris@82: plan *pln; Chris@82: Chris@82: PLNR_U(ego) &= ~u_reset; Chris@82: PLNR_L(ego) &= ~u_reset; Chris@82: PLNR_L(ego) |= l_set; Chris@82: PLNR_U(ego) |= u_set | l_set; Chris@82: pln = X(mkplan_d)(ego, p); Chris@82: ego->flags = oflags; Chris@82: return pln; Chris@82: } Chris@82: Chris@82: /* Chris@82: * Debugging code: Chris@82: */ Chris@82: #ifdef FFTW_DEBUG Chris@82: static void check(hashtab *ht) Chris@82: { Chris@82: unsigned live = 0; Chris@82: unsigned i; Chris@82: Chris@82: A(ht->nelem < ht->hashsiz); Chris@82: Chris@82: for (i = 0; i < ht->hashsiz; ++i) { Chris@82: solution *l = ht->solutions + i; Chris@82: if (LIVEP(l)) Chris@82: ++live; Chris@82: } Chris@82: Chris@82: A(ht->nelem == live); Chris@82: Chris@82: for (i = 0; i < ht->hashsiz; ++i) { Chris@82: solution *l1 = ht->solutions + i; Chris@82: int foundit = 0; Chris@82: if (LIVEP(l1)) { Chris@82: unsigned g, h = h1(ht, l1->s), d = h2(ht, l1->s); Chris@82: Chris@82: g = h; Chris@82: do { Chris@82: solution *l = ht->solutions + g; Chris@82: if (VALIDP(l)) { Chris@82: if (l1 == l) Chris@82: foundit = 1; Chris@82: else if (LIVEP(l) && md5eq(l1->s, l->s)) { Chris@82: A(!subsumes(&l->flags, SLVNDX(l), &l1->flags)); Chris@82: A(!subsumes(&l1->flags, SLVNDX(l1), &l->flags)); Chris@82: } Chris@82: } else Chris@82: break; Chris@82: g = addmod(g, d, ht->hashsiz); Chris@82: } while (g != h); Chris@82: Chris@82: A(foundit); Chris@82: } Chris@82: } Chris@82: } Chris@82: #endif