forked from FFTW/fftw3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dft-vrank-geq1.c
224 lines (190 loc) · 5.84 KB
/
dft-vrank-geq1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Copyright (c) 2003, 2007-14 Matteo Frigo
* Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "threads/threads.h"
typedef struct {
solver super;
int vecloop_dim;
const int *buddies;
size_t nbuddies;
} S;
typedef struct {
plan_dft super;
plan **cldrn;
INT its, ots;
int nthr;
const S *solver;
} P;
typedef struct {
INT its, ots;
R *ri, *ii, *ro, *io;
plan **cldrn;
} PD;
static void *spawn_apply(spawn_data *d)
{
PD *ego = (PD *) d->data;
INT its = ego->its;
INT ots = ego->ots;
int thr_num = d->thr_num;
plan_dft *cld = (plan_dft *) ego->cldrn[thr_num];
cld->apply((plan *) cld,
ego->ri + thr_num * its, ego->ii + thr_num * its,
ego->ro + thr_num * ots, ego->io + thr_num * ots);
return 0;
}
static void apply(const plan *ego_, R *ri, R *ii, R *ro, R *io)
{
const P *ego = (const P *) ego_;
PD d;
d.its = ego->its;
d.ots = ego->ots;
d.cldrn = ego->cldrn;
d.ri = ri; d.ii = ii; d.ro = ro; d.io = io;
X(spawn_loop)(ego->nthr, ego->nthr, spawn_apply, (void*) &d);
}
static void awake(plan *ego_, enum wakefulness wakefulness)
{
P *ego = (P *) ego_;
int i;
for (i = 0; i < ego->nthr; ++i)
X(plan_awake)(ego->cldrn[i], wakefulness);
}
static void destroy(plan *ego_)
{
P *ego = (P *) ego_;
int i;
for (i = 0; i < ego->nthr; ++i)
X(plan_destroy_internal)(ego->cldrn[i]);
X(ifree)(ego->cldrn);
}
static void print(const plan *ego_, printer *p)
{
const P *ego = (const P *) ego_;
const S *s = ego->solver;
int i;
p->print(p, "(dft-thr-vrank>=1-x%d/%d", ego->nthr, s->vecloop_dim);
for (i = 0; i < ego->nthr; ++i)
if (i == 0 || (ego->cldrn[i] != ego->cldrn[i-1] &&
(i <= 1 || ego->cldrn[i] != ego->cldrn[i-2])))
p->print(p, "%(%p%)", ego->cldrn[i]);
p->putchr(p, ')');
}
static int pickdim(const S *ego, const tensor *vecsz, int oop, int *dp)
{
return X(pickdim)(ego->vecloop_dim, ego->buddies, ego->nbuddies,
vecsz, oop, dp);
}
static int applicable0(const solver *ego_, const problem *p_,
const planner *plnr, int *dp)
{
const S *ego = (const S *) ego_;
const problem_dft *p = (const problem_dft *) p_;
return (1
&& plnr->nthr > 1
&& FINITE_RNK(p->vecsz->rnk)
&& p->vecsz->rnk > 0
&& pickdim(ego, p->vecsz, p->ri != p->ro, dp)
);
}
static int applicable(const solver *ego_, const problem *p_,
const planner *plnr, int *dp)
{
const S *ego = (const S *)ego_;
if (!applicable0(ego_, p_, plnr, dp)) return 0;
/* fftw2 behavior */
if (NO_VRANK_SPLITSP(plnr) && (ego->vecloop_dim != ego->buddies[0]))
return 0;
return 1;
}
static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
{
const S *ego = (const S *) ego_;
const problem_dft *p;
P *pln;
problem *cldp;
int vdim;
iodim *d;
plan **cldrn = (plan **) 0;
int i, nthr;
INT its, ots, block_size;
tensor *vecsz = 0;
static const plan_adt padt = {
X(dft_solve), awake, print, destroy
};
if (!applicable(ego_, p_, plnr, &vdim))
return (plan *) 0;
p = (const problem_dft *) p_;
d = p->vecsz->dims + vdim;
block_size = (d->n + plnr->nthr - 1) / plnr->nthr;
nthr = (int)((d->n + block_size - 1) / block_size);
plnr->nthr = (plnr->nthr + nthr - 1) / nthr;
its = d->is * block_size;
ots = d->os * block_size;
cldrn = (plan **)MALLOC(sizeof(plan *) * nthr, PLANS);
for (i = 0; i < nthr; ++i) cldrn[i] = (plan *) 0;
vecsz = X(tensor_copy)(p->vecsz);
for (i = 0; i < nthr; ++i) {
vecsz->dims[vdim].n =
(i == nthr - 1) ? (d->n - i*block_size) : block_size;
cldp = X(mkproblem_dft)(p->sz, vecsz,
p->ri + i*its, p->ii + i*its,
p->ro + i*ots, p->io + i*ots);
cldrn[i] = X(mkplan_d)(plnr, cldp);
if (!cldrn[i]) goto nada;
}
X(tensor_destroy)(vecsz);
pln = MKPLAN_DFT(P, &padt, apply);
pln->cldrn = cldrn;
pln->its = its;
pln->ots = ots;
pln->nthr = nthr;
pln->solver = ego;
X(ops_zero)(&pln->super.super.ops);
pln->super.super.pcost = 0;
for (i = 0; i < nthr; ++i) {
X(ops_add2)(&cldrn[i]->ops, &pln->super.super.ops);
pln->super.super.pcost += cldrn[i]->pcost;
}
return &(pln->super.super);
nada:
if (cldrn) {
for (i = 0; i < nthr; ++i)
X(plan_destroy_internal)(cldrn[i]);
X(ifree)(cldrn);
}
X(tensor_destroy)(vecsz);
return (plan *) 0;
}
static solver *mksolver(int vecloop_dim, const int *buddies, size_t nbuddies)
{
static const solver_adt sadt = { PROBLEM_DFT, mkplan, 0 };
S *slv = MKSOLVER(S, &sadt);
slv->vecloop_dim = vecloop_dim;
slv->buddies = buddies;
slv->nbuddies = nbuddies;
return &(slv->super);
}
void X(dft_thr_vrank_geq1_register)(planner *p)
{
/* FIXME: Should we try other vecloop_dim values? */
static const int buddies[] = { 1, -1 };
size_t i;
for (i = 0; i < NELEM(buddies); ++i)
REGISTER_SOLVER(p, mksolver(buddies[i], buddies, NELEM(buddies)));
}