annotate src/fftw-3.3.8/api/apiplan.c @ 82:d0c2a83c1364

Add FFTW 3.3.8 source, and a Linux build
author Chris Cannam
date Tue, 19 Nov 2019 14:52:55 +0000
parents
children
rev   line source
Chris@82 1 /*
Chris@82 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@82 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@82 4 *
Chris@82 5 * This program is free software; you can redistribute it and/or modify
Chris@82 6 * it under the terms of the GNU General Public License as published by
Chris@82 7 * the Free Software Foundation; either version 2 of the License, or
Chris@82 8 * (at your option) any later version.
Chris@82 9 *
Chris@82 10 * This program is distributed in the hope that it will be useful,
Chris@82 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@82 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@82 13 * GNU General Public License for more details.
Chris@82 14 *
Chris@82 15 * You should have received a copy of the GNU General Public License
Chris@82 16 * along with this program; if not, write to the Free Software
Chris@82 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@82 18 *
Chris@82 19 */
Chris@82 20
Chris@82 21 #include "api/api.h"
Chris@82 22
Chris@82 23 static planner_hook_t before_planner_hook = 0, after_planner_hook = 0;
Chris@82 24
Chris@82 25 void X(set_planner_hooks)(planner_hook_t before, planner_hook_t after)
Chris@82 26 {
Chris@82 27 before_planner_hook = before;
Chris@82 28 after_planner_hook = after;
Chris@82 29 }
Chris@82 30
Chris@82 31 static plan *mkplan0(planner *plnr, unsigned flags,
Chris@82 32 const problem *prb, unsigned hash_info,
Chris@82 33 wisdom_state_t wisdom_state)
Chris@82 34 {
Chris@82 35 /* map API flags into FFTW flags */
Chris@82 36 X(mapflags)(plnr, flags);
Chris@82 37
Chris@82 38 plnr->flags.hash_info = hash_info;
Chris@82 39 plnr->wisdom_state = wisdom_state;
Chris@82 40
Chris@82 41 /* create plan */
Chris@82 42 return plnr->adt->mkplan(plnr, prb);
Chris@82 43 }
Chris@82 44
Chris@82 45 static unsigned force_estimator(unsigned flags)
Chris@82 46 {
Chris@82 47 flags &= ~(FFTW_MEASURE | FFTW_PATIENT | FFTW_EXHAUSTIVE);
Chris@82 48 return (flags | FFTW_ESTIMATE);
Chris@82 49 }
Chris@82 50
Chris@82 51 static plan *mkplan(planner *plnr, unsigned flags,
Chris@82 52 const problem *prb, unsigned hash_info)
Chris@82 53 {
Chris@82 54 plan *pln;
Chris@82 55
Chris@82 56 pln = mkplan0(plnr, flags, prb, hash_info, WISDOM_NORMAL);
Chris@82 57
Chris@82 58 if (plnr->wisdom_state == WISDOM_NORMAL && !pln) {
Chris@82 59 /* maybe the planner failed because of inconsistent wisdom;
Chris@82 60 plan again ignoring infeasible wisdom */
Chris@82 61 pln = mkplan0(plnr, force_estimator(flags), prb,
Chris@82 62 hash_info, WISDOM_IGNORE_INFEASIBLE);
Chris@82 63 }
Chris@82 64
Chris@82 65 if (plnr->wisdom_state == WISDOM_IS_BOGUS) {
Chris@82 66 /* if the planner detected a wisdom inconsistency,
Chris@82 67 forget all wisdom and plan again */
Chris@82 68 plnr->adt->forget(plnr, FORGET_EVERYTHING);
Chris@82 69
Chris@82 70 A(!pln);
Chris@82 71 pln = mkplan0(plnr, flags, prb, hash_info, WISDOM_NORMAL);
Chris@82 72
Chris@82 73 if (plnr->wisdom_state == WISDOM_IS_BOGUS) {
Chris@82 74 /* if it still fails, plan without wisdom */
Chris@82 75 plnr->adt->forget(plnr, FORGET_EVERYTHING);
Chris@82 76
Chris@82 77 A(!pln);
Chris@82 78 pln = mkplan0(plnr, force_estimator(flags),
Chris@82 79 prb, hash_info, WISDOM_IGNORE_ALL);
Chris@82 80 }
Chris@82 81 }
Chris@82 82
Chris@82 83 return pln;
Chris@82 84 }
Chris@82 85
Chris@82 86 apiplan *X(mkapiplan)(int sign, unsigned flags, problem *prb)
Chris@82 87 {
Chris@82 88 apiplan *p = 0;
Chris@82 89 plan *pln;
Chris@82 90 unsigned flags_used_for_planning;
Chris@82 91 planner *plnr;
Chris@82 92 static const unsigned int pats[] = {FFTW_ESTIMATE, FFTW_MEASURE,
Chris@82 93 FFTW_PATIENT, FFTW_EXHAUSTIVE};
Chris@82 94 int pat, pat_max;
Chris@82 95 double pcost = 0;
Chris@82 96
Chris@82 97 if (before_planner_hook)
Chris@82 98 before_planner_hook();
Chris@82 99
Chris@82 100 plnr = X(the_planner)();
Chris@82 101
Chris@82 102 if (flags & FFTW_WISDOM_ONLY) {
Chris@82 103 /* Special mode that returns a plan only if wisdom is present,
Chris@82 104 and returns 0 otherwise. This is now documented in the manual,
Chris@82 105 as a way to detect whether wisdom is available for a problem. */
Chris@82 106 flags_used_for_planning = flags;
Chris@82 107 pln = mkplan0(plnr, flags, prb, 0, WISDOM_ONLY);
Chris@82 108 } else {
Chris@82 109 pat_max = flags & FFTW_ESTIMATE ? 0 :
Chris@82 110 (flags & FFTW_EXHAUSTIVE ? 3 :
Chris@82 111 (flags & FFTW_PATIENT ? 2 : 1));
Chris@82 112 pat = plnr->timelimit >= 0 ? 0 : pat_max;
Chris@82 113
Chris@82 114 flags &= ~(FFTW_ESTIMATE | FFTW_MEASURE |
Chris@82 115 FFTW_PATIENT | FFTW_EXHAUSTIVE);
Chris@82 116
Chris@82 117 plnr->start_time = X(get_crude_time)();
Chris@82 118
Chris@82 119 /* plan at incrementally increasing patience until we run
Chris@82 120 out of time */
Chris@82 121 for (pln = 0, flags_used_for_planning = 0; pat <= pat_max; ++pat) {
Chris@82 122 plan *pln1;
Chris@82 123 unsigned tmpflags = flags | pats[pat];
Chris@82 124 pln1 = mkplan(plnr, tmpflags, prb, 0u);
Chris@82 125
Chris@82 126 if (!pln1) {
Chris@82 127 /* don't bother continuing if planner failed or timed out */
Chris@82 128 A(!pln || plnr->timed_out);
Chris@82 129 break;
Chris@82 130 }
Chris@82 131
Chris@82 132 X(plan_destroy_internal)(pln);
Chris@82 133 pln = pln1;
Chris@82 134 flags_used_for_planning = tmpflags;
Chris@82 135 pcost = pln->pcost;
Chris@82 136 }
Chris@82 137 }
Chris@82 138
Chris@82 139 if (pln) {
Chris@82 140 /* build apiplan */
Chris@82 141 p = (apiplan *) MALLOC(sizeof(apiplan), PLANS);
Chris@82 142 p->prb = prb;
Chris@82 143 p->sign = sign; /* cache for execute_dft */
Chris@82 144
Chris@82 145 /* re-create plan from wisdom, adding blessing */
Chris@82 146 p->pln = mkplan(plnr, flags_used_for_planning, prb, BLESSING);
Chris@82 147
Chris@82 148 /* record pcost from most recent measurement for use in X(cost) */
Chris@82 149 p->pln->pcost = pcost;
Chris@82 150
Chris@82 151 if (sizeof(trigreal) > sizeof(R)) {
Chris@82 152 /* this is probably faster, and we have enough trigreal
Chris@82 153 bits to maintain accuracy */
Chris@82 154 X(plan_awake)(p->pln, AWAKE_SQRTN_TABLE);
Chris@82 155 } else {
Chris@82 156 /* more accurate */
Chris@82 157 X(plan_awake)(p->pln, AWAKE_SINCOS);
Chris@82 158 }
Chris@82 159
Chris@82 160 /* we don't use pln for p->pln, above, since by re-creating the
Chris@82 161 plan we might use more patient wisdom from a timed-out mkplan */
Chris@82 162 X(plan_destroy_internal)(pln);
Chris@82 163 } else
Chris@82 164 X(problem_destroy)(prb);
Chris@82 165
Chris@82 166 /* discard all information not necessary to reconstruct the plan */
Chris@82 167 plnr->adt->forget(plnr, FORGET_ACCURSED);
Chris@82 168
Chris@82 169 #ifdef FFTW_RANDOM_ESTIMATOR
Chris@82 170 X(random_estimate_seed)++; /* subsequent "random" plans are distinct */
Chris@82 171 #endif
Chris@82 172
Chris@82 173 if (after_planner_hook)
Chris@82 174 after_planner_hook();
Chris@82 175
Chris@82 176 return p;
Chris@82 177 }
Chris@82 178
Chris@82 179 void X(destroy_plan)(X(plan) p)
Chris@82 180 {
Chris@82 181 if (p) {
Chris@82 182 if (before_planner_hook)
Chris@82 183 before_planner_hook();
Chris@82 184
Chris@82 185 X(plan_awake)(p->pln, SLEEPY);
Chris@82 186 X(plan_destroy_internal)(p->pln);
Chris@82 187 X(problem_destroy)(p->prb);
Chris@82 188 X(ifree)(p);
Chris@82 189
Chris@82 190 if (after_planner_hook)
Chris@82 191 after_planner_hook();
Chris@82 192 }
Chris@82 193 }
Chris@82 194
Chris@82 195 int X(alignment_of)(R *p)
Chris@82 196 {
Chris@82 197 return X(ialignment_of(p));
Chris@82 198 }