Coverage Report

Created: 2023-09-25 07:08

/src/fftw3/api/apiplan.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2003, 2007-14 Matteo Frigo
3
 * Copyright (c) 2003, 2007-14 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/api.h"
22
23
static planner_hook_t before_planner_hook = 0, after_planner_hook = 0;
24
25
void X(set_planner_hooks)(planner_hook_t before, planner_hook_t after)
26
0
{
27
0
     before_planner_hook = before;
28
0
     after_planner_hook = after;
29
0
}
30
31
static plan *mkplan0(planner *plnr, unsigned flags,
32
         const problem *prb, unsigned hash_info,
33
         wisdom_state_t wisdom_state)
34
572
{
35
     /* map API flags into FFTW flags */
36
572
     X(mapflags)(plnr, flags);
37
38
572
     plnr->flags.hash_info = hash_info;
39
572
     plnr->wisdom_state = wisdom_state;
40
41
     /* create plan */
42
572
     return plnr->adt->mkplan(plnr, prb);
43
572
}
44
45
static unsigned force_estimator(unsigned flags)
46
0
{
47
0
     flags &= ~(FFTW_MEASURE | FFTW_PATIENT | FFTW_EXHAUSTIVE);
48
0
     return (flags | FFTW_ESTIMATE);
49
0
}
50
51
static plan *mkplan(planner *plnr, unsigned flags,
52
        const problem *prb, unsigned hash_info)
53
572
{
54
572
     plan *pln;
55
56
572
     pln = mkplan0(plnr, flags, prb, hash_info, WISDOM_NORMAL);
57
58
572
     if (plnr->wisdom_state == WISDOM_NORMAL && !pln) {
59
    /* maybe the planner failed because of inconsistent wisdom;
60
       plan again ignoring infeasible wisdom */
61
0
    pln = mkplan0(plnr, force_estimator(flags), prb,
62
0
      hash_info, WISDOM_IGNORE_INFEASIBLE);
63
0
     }
64
65
572
     if (plnr->wisdom_state == WISDOM_IS_BOGUS) {
66
    /* if the planner detected a wisdom inconsistency,
67
       forget all wisdom and plan again */
68
0
    plnr->adt->forget(plnr, FORGET_EVERYTHING);
69
70
0
    A(!pln);
71
0
    pln = mkplan0(plnr, flags, prb, hash_info, WISDOM_NORMAL);
72
73
0
    if (plnr->wisdom_state == WISDOM_IS_BOGUS) {
74
         /* if it still fails, plan without wisdom */
75
0
         plnr->adt->forget(plnr, FORGET_EVERYTHING);
76
77
0
         A(!pln);
78
0
         pln = mkplan0(plnr, force_estimator(flags),
79
0
           prb, hash_info, WISDOM_IGNORE_ALL);
80
0
    }
81
0
     }
82
83
572
     return pln;
84
572
}
85
86
apiplan *X(mkapiplan)(int sign, unsigned flags, problem *prb)
87
286
{
88
286
     apiplan *p = 0;
89
286
     plan *pln;
90
286
     unsigned flags_used_for_planning;
91
286
     planner *plnr;
92
286
     static const unsigned int pats[] = {FFTW_ESTIMATE, FFTW_MEASURE,
93
286
                                         FFTW_PATIENT, FFTW_EXHAUSTIVE};
94
286
     int pat, pat_max;
95
286
     double pcost = 0;
96
97
286
     if (before_planner_hook)
98
0
          before_planner_hook();
99
100
286
     plnr = X(the_planner)();
101
102
286
     if (flags & FFTW_WISDOM_ONLY) {
103
    /* Special mode that returns a plan only if wisdom is present,
104
       and returns 0 otherwise.  This is now documented in the manual,
105
       as a way to detect whether wisdom is available for a problem. */
106
0
    flags_used_for_planning = flags;
107
0
    pln = mkplan0(plnr, flags, prb, 0, WISDOM_ONLY);
108
286
     } else {
109
286
    pat_max = flags & FFTW_ESTIMATE ? 0 :
110
286
         (flags & FFTW_EXHAUSTIVE ? 3 :
111
0
    (flags & FFTW_PATIENT ? 2 : 1));
112
286
    pat = plnr->timelimit >= 0 ? 0 : pat_max;
113
114
286
    flags &= ~(FFTW_ESTIMATE | FFTW_MEASURE |
115
286
         FFTW_PATIENT | FFTW_EXHAUSTIVE);
116
117
286
    plnr->start_time = X(get_crude_time)();
118
119
    /* plan at incrementally increasing patience until we run
120
       out of time */
121
572
    for (pln = 0, flags_used_for_planning = 0; pat <= pat_max; ++pat) {
122
286
         plan *pln1;
123
286
         unsigned tmpflags = flags | pats[pat];
124
286
         pln1 = mkplan(plnr, tmpflags, prb, 0u);
125
126
286
         if (!pln1) {
127
        /* don't bother continuing if planner failed or timed out */
128
0
        A(!pln || plnr->timed_out);
129
0
        break;
130
0
         }
131
132
286
         X(plan_destroy_internal)(pln);
133
286
         pln = pln1;
134
286
         flags_used_for_planning = tmpflags;
135
286
         pcost = pln->pcost;
136
286
    }
137
286
     }
138
139
286
     if (pln) {
140
    /* build apiplan */
141
286
    p = (apiplan *) MALLOC(sizeof(apiplan), PLANS);
142
286
    p->prb = prb;
143
286
    p->refcount = 1u;
144
286
    p->sign = sign; /* cache for execute_dft */
145
146
    /* re-create plan from wisdom, adding blessing */
147
286
    p->pln = mkplan(plnr, flags_used_for_planning, prb, BLESSING);
148
149
    /* record pcost from most recent measurement for use in X(cost) */
150
286
    p->pln->pcost = pcost;
151
152
286
    if (sizeof(trigreal) > sizeof(R)) {
153
         /* this is probably faster, and we have enough trigreal
154
      bits to maintain accuracy */
155
0
         X(plan_awake)(p->pln, AWAKE_SQRTN_TABLE);
156
286
    } else {
157
         /* more accurate */
158
286
         X(plan_awake)(p->pln, AWAKE_SINCOS);
159
286
    }
160
161
    /* we don't use pln for p->pln, above, since by re-creating the
162
       plan we might use more patient wisdom from a timed-out mkplan */
163
286
    X(plan_destroy_internal)(pln);
164
286
     } else
165
0
    X(problem_destroy)(prb);
166
167
     /* discard all information not necessary to reconstruct the plan */
168
286
     plnr->adt->forget(plnr, FORGET_ACCURSED);
169
170
#ifdef FFTW_RANDOM_ESTIMATOR
171
     X(random_estimate_seed)++; /* subsequent "random" plans are distinct */
172
#endif
173
174
286
     if (after_planner_hook)
175
0
          after_planner_hook();
176
177
286
     return p;
178
286
}
179
180
X(plan) X(copy_plan)(X(plan) p)
181
0
{
182
0
     if (p) {
183
0
          if (before_planner_hook)
184
0
               before_planner_hook();
185
186
0
          p->refcount++;
187
188
0
          if (after_planner_hook)
189
0
               after_planner_hook();
190
0
     }
191
0
     return p;
192
0
}
193
194
void X(destroy_plan)(X(plan) p)
195
286
{
196
286
     if (p) {
197
286
          if (before_planner_hook)
198
0
               before_planner_hook();
199
200
286
          if (p->refcount-- == 1u) {
201
286
               X(plan_awake)(p->pln, SLEEPY);
202
286
               X(plan_destroy_internal)(p->pln);
203
286
               X(problem_destroy)(p->prb);
204
286
               X(ifree)(p);
205
286
          }
206
207
286
          if (after_planner_hook)
208
0
               after_planner_hook();
209
286
     }
210
286
}
211
212
int X(alignment_of)(R *p)
213
0
{
214
0
     return X(ialignment_of(p));
215
0
}