Coverage Report

Created: 2023-09-25 07:08

/src/fftw3/rdft/rank-geq2.c
Line
Count
Source
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
22
/* plans for RDFT of rank >= 2 (multidimensional) */
23
24
/* FIXME: this solver cannot strictly be applied to multidimensional
25
   DHTs, since the latter are not separable...up to rnk-1 additional
26
   post-processing passes may be required.  See also:
27
28
   R. N. Bracewell, O. Buneman, H. Hao, and J. Villasenor, "Fast
29
   two-dimensional Hartley transform," Proc. IEEE 74, 1282-1283 (1986).
30
31
   H. Hao and R. N. Bracewell, "A three-dimensional DFT algorithm
32
   using the fast Hartley transform," Proc. IEEE 75(2), 264-266 (1987).
33
*/
34
35
#include "rdft/rdft.h"
36
37
typedef struct {
38
     solver super;
39
     int spltrnk;
40
     const int *buddies;
41
     size_t nbuddies;
42
} S;
43
44
typedef struct {
45
     plan_rdft super;
46
47
     plan *cld1, *cld2;
48
     const S *solver;
49
} P;
50
51
/* Compute multi-dimensional RDFT by applying the two cld plans
52
   (lower-rnk RDFTs). */
53
static void apply(const plan *ego_, R *I, R *O)
54
{
55
     const P *ego = (const P *) ego_;
56
     plan_rdft *cld1, *cld2;
57
58
     cld1 = (plan_rdft *) ego->cld1;
59
     cld1->apply(ego->cld1, I, O);
60
61
     cld2 = (plan_rdft *) ego->cld2;
62
     cld2->apply(ego->cld2, O, O);
63
}
64
65
66
static void awake(plan *ego_, enum wakefulness wakefulness)
67
{
68
     P *ego = (P *) ego_;
69
     X(plan_awake)(ego->cld1, wakefulness);
70
     X(plan_awake)(ego->cld2, wakefulness);
71
}
72
73
static void destroy(plan *ego_)
74
{
75
     P *ego = (P *) ego_;
76
     X(plan_destroy_internal)(ego->cld2);
77
     X(plan_destroy_internal)(ego->cld1);
78
}
79
80
static void print(const plan *ego_, printer *p)
81
{
82
     const P *ego = (const P *) ego_;
83
     const S *s = ego->solver;
84
     p->print(p, "(rdft-rank>=2/%d%(%p%)%(%p%))",
85
        s->spltrnk, ego->cld1, ego->cld2);
86
}
87
88
static int picksplit(const S *ego, const tensor *sz, int *rp)
89
{
90
     A(sz->rnk > 1); /* cannot split rnk <= 1 */
91
     if (!X(pickdim)(ego->spltrnk, ego->buddies, ego->nbuddies, sz, 1, rp))
92
    return 0;
93
     *rp += 1; /* convert from dim. index to rank */
94
     if (*rp >= sz->rnk) /* split must reduce rank */
95
    return 0;
96
     return 1;
97
}
98
99
static int applicable0(const solver *ego_, const problem *p_, int *rp)
100
{
101
     const problem_rdft *p = (const problem_rdft *) p_;
102
     const S *ego = (const S *)ego_;
103
     return (1
104
       && FINITE_RNK(p->sz->rnk) && FINITE_RNK(p->vecsz->rnk)
105
       && p->sz->rnk >= 2
106
       && picksplit(ego, p->sz, rp)
107
    );
108
}
109
110
/* TODO: revise this. */
111
static int applicable(const solver *ego_, const problem *p_, 
112
          const planner *plnr, int *rp)
113
{
114
     const S *ego = (const S *)ego_;
115
116
     if (!applicable0(ego_, p_, rp)) return 0;
117
118
     if (NO_RANK_SPLITSP(plnr) && (ego->spltrnk != ego->buddies[0]))
119
    return 0;
120
121
     if (NO_UGLYP(plnr)) {
122
    /* Heuristic: if the vector stride is greater than the transform
123
       sz, don't use (prefer to do the vector loop first with a
124
       vrank-geq1 plan). */
125
    const problem_rdft *p = (const problem_rdft *) p_;
126
127
    if (p->vecsz->rnk > 0 &&
128
        X(tensor_min_stride)(p->vecsz) > X(tensor_max_index)(p->sz))
129
         return 0;
130
     }
131
132
     return 1;
133
}
134
135
static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
136
{
137
     const S *ego = (const S *) ego_;
138
     const problem_rdft *p;
139
     P *pln;
140
     plan *cld1 = 0, *cld2 = 0;
141
     tensor *sz1, *sz2, *vecszi, *sz2i;
142
     int spltrnk;
143
144
     static const plan_adt padt = {
145
    X(rdft_solve), awake, print, destroy
146
     };
147
148
     if (!applicable(ego_, p_, plnr, &spltrnk))
149
          return (plan *) 0;
150
151
     p = (const problem_rdft *) p_;
152
     X(tensor_split)(p->sz, &sz1, spltrnk, &sz2);
153
     vecszi = X(tensor_copy_inplace)(p->vecsz, INPLACE_OS);
154
     sz2i = X(tensor_copy_inplace)(sz2, INPLACE_OS);
155
156
     cld1 = X(mkplan_d)(plnr, 
157
      X(mkproblem_rdft_d)(X(tensor_copy)(sz2),
158
              X(tensor_append)(p->vecsz, sz1),
159
              p->I, p->O, p->kind + spltrnk));
160
     if (!cld1) goto nada;
161
162
     cld2 = X(mkplan_d)(plnr, 
163
      X(mkproblem_rdft_d)(
164
           X(tensor_copy_inplace)(sz1, INPLACE_OS),
165
           X(tensor_append)(vecszi, sz2i),
166
           p->O, p->O, p->kind));
167
     if (!cld2) goto nada;
168
169
     pln = MKPLAN_RDFT(P, &padt, apply);
170
171
     pln->cld1 = cld1;
172
     pln->cld2 = cld2;
173
174
     pln->solver = ego;
175
     X(ops_add)(&cld1->ops, &cld2->ops, &pln->super.super.ops);
176
177
     X(tensor_destroy4)(sz2, sz1, vecszi, sz2i);
178
179
     return &(pln->super.super);
180
181
 nada:
182
     X(plan_destroy_internal)(cld2);
183
     X(plan_destroy_internal)(cld1);
184
     X(tensor_destroy4)(sz2, sz1, vecszi, sz2i);
185
     return (plan *) 0;
186
}
187
188
static solver *mksolver(int spltrnk, const int *buddies, size_t nbuddies)
189
{
190
     static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
191
     S *slv = MKSOLVER(S, &sadt);
192
     slv->spltrnk = spltrnk;
193
     slv->buddies = buddies;
194
     slv->nbuddies = nbuddies;
195
     return &(slv->super);
196
}
197
198
void X(rdft_rank_geq2_register)(planner *p)
199
1
{
200
1
     static const int buddies[] = { 1, 0, -2 };
201
1
     size_t i;
202
203
4
     for (i = 0; i < NELEM(buddies); ++i)
204
3
          REGISTER_SOLVER(p, mksolver(buddies[i], buddies, NELEM(buddies)));
205
206
     /* FIXME: Should we try more buddies?  See also dft/rank-geq2. */
207
1
}