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