comparison src/fftw-3.3.3/api/apiplan.c @ 10:37bf6b4a2645

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