Coverage Report

Created: 2025-11-16 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fftw3/dft/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 DFT of rank >= 2 (multidimensional) */
23
24
#include "dft/dft.h"
25
26
typedef struct {
27
     solver super;
28
     int spltrnk;
29
     const int *buddies;
30
     size_t nbuddies;
31
} S;
32
33
typedef struct {
34
     plan_dft super;
35
36
     plan *cld1, *cld2;
37
     const S *solver;
38
} P;
39
40
/* Compute multi-dimensional DFT by applying the two cld plans
41
   (lower-rnk DFTs). */
42
static void apply(const plan *ego_, R *ri, R *ii, R *ro, R *io)
43
0
{
44
0
     const P *ego = (const P *) ego_;
45
0
     plan_dft *cld1, *cld2;
46
47
0
     cld1 = (plan_dft *) ego->cld1;
48
0
     cld1->apply(ego->cld1, ri, ii, ro, io);
49
50
0
     cld2 = (plan_dft *) ego->cld2;
51
0
     cld2->apply(ego->cld2, ro, io, ro, io);
52
0
}
53
54
55
static void awake(plan *ego_, enum wakefulness wakefulness)
56
0
{
57
0
     P *ego = (P *) ego_;
58
0
     X(plan_awake)(ego->cld1, wakefulness);
59
0
     X(plan_awake)(ego->cld2, wakefulness);
60
0
}
61
62
static void destroy(plan *ego_)
63
0
{
64
0
     P *ego = (P *) ego_;
65
0
     X(plan_destroy_internal)(ego->cld2);
66
0
     X(plan_destroy_internal)(ego->cld1);
67
0
}
68
69
static void print(const plan *ego_, printer *p)
70
0
{
71
0
     const P *ego = (const P *) ego_;
72
0
     const S *s = ego->solver;
73
0
     p->print(p, "(dft-rank>=2/%d%(%p%)%(%p%))",
74
0
        s->spltrnk, ego->cld1, ego->cld2);
75
0
}
76
77
static int picksplit(const S *ego, const tensor *sz, int *rp)
78
0
{
79
0
     A(sz->rnk > 1); /* cannot split rnk <= 1 */
80
0
     if (!X(pickdim)(ego->spltrnk, ego->buddies, ego->nbuddies, sz, 1, rp))
81
0
    return 0;
82
0
     *rp += 1; /* convert from dim. index to rank */
83
0
     if (*rp >= sz->rnk) /* split must reduce rank */
84
0
    return 0;
85
0
     return 1;
86
0
}
87
88
static int applicable0(const solver *ego_, const problem *p_, int *rp)
89
4.21k
{
90
4.21k
     const problem_dft *p = (const problem_dft *) p_;
91
4.21k
     const S *ego = (const S *)ego_;
92
4.21k
     return (1
93
4.21k
       && FINITE_RNK(p->sz->rnk) && FINITE_RNK(p->vecsz->rnk)
94
3.73k
       && p->sz->rnk >= 2
95
0
       && picksplit(ego, p->sz, rp)
96
4.21k
    );
97
4.21k
}
98
99
/* TODO: revise this. */
100
static int applicable(const solver *ego_, const problem *p_, 
101
          const planner *plnr, int *rp)
102
4.21k
{
103
4.21k
     const S *ego = (const S *)ego_;
104
4.21k
     const problem_dft *p = (const problem_dft *) p_;
105
106
4.21k
     if (!applicable0(ego_, p_, rp)) return 0;
107
108
0
     if (NO_RANK_SPLITSP(plnr) && (ego->spltrnk != ego->buddies[0])) return 0;
109
110
     /* Heuristic: if the vector stride is greater than the transform
111
        sz, don't use (prefer to do the vector loop first with a
112
        vrank-geq1 plan). */
113
0
     if (NO_UGLYP(plnr))
114
0
    if (p->vecsz->rnk > 0 &&
115
0
        X(tensor_min_stride)(p->vecsz) > X(tensor_max_index)(p->sz))
116
0
         return 0;
117
118
0
     return 1;
119
0
}
120
121
static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
122
4.21k
{
123
4.21k
     const S *ego = (const S *) ego_;
124
4.21k
     const problem_dft *p;
125
4.21k
     P *pln;
126
4.21k
     plan *cld1 = 0, *cld2 = 0;
127
4.21k
     tensor *sz1, *sz2, *vecszi, *sz2i;
128
4.21k
     int spltrnk;
129
130
4.21k
     static const plan_adt padt = {
131
4.21k
    X(dft_solve), awake, print, destroy
132
4.21k
     };
133
134
4.21k
     if (!applicable(ego_, p_, plnr, &spltrnk))
135
4.21k
          return (plan *) 0;
136
137
0
     p = (const problem_dft *) p_;
138
0
     X(tensor_split)(p->sz, &sz1, spltrnk, &sz2);
139
0
     vecszi = X(tensor_copy_inplace)(p->vecsz, INPLACE_OS);
140
0
     sz2i = X(tensor_copy_inplace)(sz2, INPLACE_OS);
141
142
0
     cld1 = X(mkplan_d)(plnr, 
143
0
      X(mkproblem_dft_d)(X(tensor_copy)(sz2),
144
0
             X(tensor_append)(p->vecsz, sz1),
145
0
             p->ri, p->ii, p->ro, p->io));
146
0
     if (!cld1) goto nada;
147
148
0
     cld2 = X(mkplan_d)(plnr, 
149
0
      X(mkproblem_dft_d)(
150
0
           X(tensor_copy_inplace)(sz1, INPLACE_OS),
151
0
           X(tensor_append)(vecszi, sz2i),
152
0
           p->ro, p->io, p->ro, p->io));
153
0
     if (!cld2) goto nada;
154
155
0
     pln = MKPLAN_DFT(P, &padt, apply);
156
157
0
     pln->cld1 = cld1;
158
0
     pln->cld2 = cld2;
159
160
0
     pln->solver = ego;
161
0
     X(ops_add)(&cld1->ops, &cld2->ops, &pln->super.super.ops);
162
163
0
     X(tensor_destroy4)(sz1, sz2, vecszi, sz2i);
164
165
0
     return &(pln->super.super);
166
167
0
 nada:
168
0
     X(plan_destroy_internal)(cld2);
169
0
     X(plan_destroy_internal)(cld1);
170
0
     X(tensor_destroy4)(sz1, sz2, vecszi, sz2i);
171
0
     return (plan *) 0;
172
0
}
173
174
static solver *mksolver(int spltrnk, const int *buddies, size_t nbuddies)
175
6
{
176
6
     static const solver_adt sadt = { PROBLEM_DFT, mkplan, 0 };
177
6
     S *slv = MKSOLVER(S, &sadt);
178
6
     slv->spltrnk = spltrnk;
179
6
     slv->buddies = buddies;
180
6
     slv->nbuddies = nbuddies;
181
6
     return &(slv->super);
182
6
}
183
184
void X(dft_rank_geq2_register)(planner *p)
185
1
{
186
1
     static const int buddies[] = { 1, 0, -2 };
187
1
     size_t i;
188
189
4
     for (i = 0; i < NELEM(buddies); ++i)
190
3
          REGISTER_SOLVER(p, mksolver(buddies[i], buddies, NELEM(buddies)));
191
192
     /* FIXME:
193
194
        Should we try more buddies? 
195
196
        Another possible variant is to swap cld1 and cld2 (or rather,
197
        to swap their problems; they are not interchangeable because
198
        cld2 must be in-place).  In past versions of FFTW, however, I
199
        seem to recall that such rearrangements have made little or no
200
        difference.
201
     */
202
1
}