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