annotate src/fftw-3.3.8/rdft/direct-r2c.c @ 82:d0c2a83c1364

Add FFTW 3.3.8 source, and a Linux build
author Chris Cannam
date Tue, 19 Nov 2019 14:52:55 +0000
parents
children
rev   line source
Chris@82 1 /*
Chris@82 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@82 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@82 4 *
Chris@82 5 * This program is free software; you can redistribute it and/or modify
Chris@82 6 * it under the terms of the GNU General Public License as published by
Chris@82 7 * the Free Software Foundation; either version 2 of the License, or
Chris@82 8 * (at your option) any later version.
Chris@82 9 *
Chris@82 10 * This program is distributed in the hope that it will be useful,
Chris@82 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@82 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@82 13 * GNU General Public License for more details.
Chris@82 14 *
Chris@82 15 * You should have received a copy of the GNU General Public License
Chris@82 16 * along with this program; if not, write to the Free Software
Chris@82 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@82 18 *
Chris@82 19 */
Chris@82 20
Chris@82 21
Chris@82 22 /* direct RDFT solver, using r2c codelets */
Chris@82 23
Chris@82 24 #include "rdft/rdft.h"
Chris@82 25
Chris@82 26 typedef struct {
Chris@82 27 solver super;
Chris@82 28 const kr2c_desc *desc;
Chris@82 29 kr2c k;
Chris@82 30 int bufferedp;
Chris@82 31 } S;
Chris@82 32
Chris@82 33 typedef struct {
Chris@82 34 plan_rdft super;
Chris@82 35
Chris@82 36 stride rs, csr, csi;
Chris@82 37 stride brs, bcsr, bcsi;
Chris@82 38 INT n, vl, rs0, ivs, ovs, ioffset, bioffset;
Chris@82 39 kr2c k;
Chris@82 40 const S *slv;
Chris@82 41 } P;
Chris@82 42
Chris@82 43 /*************************************************************
Chris@82 44 Nonbuffered code
Chris@82 45 *************************************************************/
Chris@82 46 static void apply_r2hc(const plan *ego_, R *I, R *O)
Chris@82 47 {
Chris@82 48 const P *ego = (const P *) ego_;
Chris@82 49 ASSERT_ALIGNED_DOUBLE;
Chris@82 50 ego->k(I, I + ego->rs0, O, O + ego->ioffset,
Chris@82 51 ego->rs, ego->csr, ego->csi,
Chris@82 52 ego->vl, ego->ivs, ego->ovs);
Chris@82 53 }
Chris@82 54
Chris@82 55 static void apply_hc2r(const plan *ego_, R *I, R *O)
Chris@82 56 {
Chris@82 57 const P *ego = (const P *) ego_;
Chris@82 58 ASSERT_ALIGNED_DOUBLE;
Chris@82 59 ego->k(O, O + ego->rs0, I, I + ego->ioffset,
Chris@82 60 ego->rs, ego->csr, ego->csi,
Chris@82 61 ego->vl, ego->ivs, ego->ovs);
Chris@82 62 }
Chris@82 63
Chris@82 64 /*************************************************************
Chris@82 65 Buffered code
Chris@82 66 *************************************************************/
Chris@82 67 /* should not be 2^k to avoid associativity conflicts */
Chris@82 68 static INT compute_batchsize(INT radix)
Chris@82 69 {
Chris@82 70 /* round up to multiple of 4 */
Chris@82 71 radix += 3;
Chris@82 72 radix &= -4;
Chris@82 73
Chris@82 74 return (radix + 2);
Chris@82 75 }
Chris@82 76
Chris@82 77 static void dobatch_r2hc(const P *ego, R *I, R *O, R *buf, INT batchsz)
Chris@82 78 {
Chris@82 79 X(cpy2d_ci)(I, buf,
Chris@82 80 ego->n, ego->rs0, WS(ego->bcsr /* hack */, 1),
Chris@82 81 batchsz, ego->ivs, 1, 1);
Chris@82 82
Chris@82 83 if (IABS(WS(ego->csr, 1)) < IABS(ego->ovs)) {
Chris@82 84 /* transform directly to output */
Chris@82 85 ego->k(buf, buf + WS(ego->bcsr /* hack */, 1),
Chris@82 86 O, O + ego->ioffset,
Chris@82 87 ego->brs, ego->csr, ego->csi,
Chris@82 88 batchsz, 1, ego->ovs);
Chris@82 89 } else {
Chris@82 90 /* transform to buffer and copy back */
Chris@82 91 ego->k(buf, buf + WS(ego->bcsr /* hack */, 1),
Chris@82 92 buf, buf + ego->bioffset,
Chris@82 93 ego->brs, ego->bcsr, ego->bcsi,
Chris@82 94 batchsz, 1, 1);
Chris@82 95 X(cpy2d_co)(buf, O,
Chris@82 96 ego->n, WS(ego->bcsr, 1), WS(ego->csr, 1),
Chris@82 97 batchsz, 1, ego->ovs, 1);
Chris@82 98 }
Chris@82 99 }
Chris@82 100
Chris@82 101 static void dobatch_hc2r(const P *ego, R *I, R *O, R *buf, INT batchsz)
Chris@82 102 {
Chris@82 103 if (IABS(WS(ego->csr, 1)) < IABS(ego->ivs)) {
Chris@82 104 /* transform directly from input */
Chris@82 105 ego->k(buf, buf + WS(ego->bcsr /* hack */, 1),
Chris@82 106 I, I + ego->ioffset,
Chris@82 107 ego->brs, ego->csr, ego->csi,
Chris@82 108 batchsz, ego->ivs, 1);
Chris@82 109 } else {
Chris@82 110 /* copy into buffer and transform in place */
Chris@82 111 X(cpy2d_ci)(I, buf,
Chris@82 112 ego->n, WS(ego->csr, 1), WS(ego->bcsr, 1),
Chris@82 113 batchsz, ego->ivs, 1, 1);
Chris@82 114 ego->k(buf, buf + WS(ego->bcsr /* hack */, 1),
Chris@82 115 buf, buf + ego->bioffset,
Chris@82 116 ego->brs, ego->bcsr, ego->bcsi,
Chris@82 117 batchsz, 1, 1);
Chris@82 118 }
Chris@82 119 X(cpy2d_co)(buf, O,
Chris@82 120 ego->n, WS(ego->bcsr /* hack */, 1), ego->rs0,
Chris@82 121 batchsz, 1, ego->ovs, 1);
Chris@82 122 }
Chris@82 123
Chris@82 124 static void iterate(const P *ego, R *I, R *O,
Chris@82 125 void (*dobatch)(const P *ego, R *I, R *O,
Chris@82 126 R *buf, INT batchsz))
Chris@82 127 {
Chris@82 128 R *buf;
Chris@82 129 INT vl = ego->vl;
Chris@82 130 INT n = ego->n;
Chris@82 131 INT i;
Chris@82 132 INT batchsz = compute_batchsize(n);
Chris@82 133 size_t bufsz = n * batchsz * sizeof(R);
Chris@82 134
Chris@82 135 BUF_ALLOC(R *, buf, bufsz);
Chris@82 136
Chris@82 137 for (i = 0; i < vl - batchsz; i += batchsz) {
Chris@82 138 dobatch(ego, I, O, buf, batchsz);
Chris@82 139 I += batchsz * ego->ivs;
Chris@82 140 O += batchsz * ego->ovs;
Chris@82 141 }
Chris@82 142 dobatch(ego, I, O, buf, vl - i);
Chris@82 143
Chris@82 144 BUF_FREE(buf, bufsz);
Chris@82 145 }
Chris@82 146
Chris@82 147 static void apply_buf_r2hc(const plan *ego_, R *I, R *O)
Chris@82 148 {
Chris@82 149 iterate((const P *) ego_, I, O, dobatch_r2hc);
Chris@82 150 }
Chris@82 151
Chris@82 152 static void apply_buf_hc2r(const plan *ego_, R *I, R *O)
Chris@82 153 {
Chris@82 154 iterate((const P *) ego_, I, O, dobatch_hc2r);
Chris@82 155 }
Chris@82 156
Chris@82 157 static void destroy(plan *ego_)
Chris@82 158 {
Chris@82 159 P *ego = (P *) ego_;
Chris@82 160 X(stride_destroy)(ego->rs);
Chris@82 161 X(stride_destroy)(ego->csr);
Chris@82 162 X(stride_destroy)(ego->csi);
Chris@82 163 X(stride_destroy)(ego->brs);
Chris@82 164 X(stride_destroy)(ego->bcsr);
Chris@82 165 X(stride_destroy)(ego->bcsi);
Chris@82 166 }
Chris@82 167
Chris@82 168 static void print(const plan *ego_, printer *p)
Chris@82 169 {
Chris@82 170 const P *ego = (const P *) ego_;
Chris@82 171 const S *s = ego->slv;
Chris@82 172
Chris@82 173 if (ego->slv->bufferedp)
Chris@82 174 p->print(p, "(rdft-%s-directbuf/%D-r2c-%D%v \"%s\")",
Chris@82 175 X(rdft_kind_str)(s->desc->genus->kind),
Chris@82 176 /* hack */ WS(ego->bcsr, 1), ego->n,
Chris@82 177 ego->vl, s->desc->nam);
Chris@82 178
Chris@82 179 else
Chris@82 180 p->print(p, "(rdft-%s-direct-r2c-%D%v \"%s\")",
Chris@82 181 X(rdft_kind_str)(s->desc->genus->kind), ego->n,
Chris@82 182 ego->vl, s->desc->nam);
Chris@82 183 }
Chris@82 184
Chris@82 185 static INT ioffset(rdft_kind kind, INT sz, INT s)
Chris@82 186 {
Chris@82 187 return(s * ((kind == R2HC || kind == HC2R) ? sz : (sz - 1)));
Chris@82 188 }
Chris@82 189
Chris@82 190 static int applicable(const solver *ego_, const problem *p_)
Chris@82 191 {
Chris@82 192 const S *ego = (const S *) ego_;
Chris@82 193 const kr2c_desc *desc = ego->desc;
Chris@82 194 const problem_rdft *p = (const problem_rdft *) p_;
Chris@82 195 INT vl, ivs, ovs;
Chris@82 196
Chris@82 197 return (
Chris@82 198 1
Chris@82 199 && p->sz->rnk == 1
Chris@82 200 && p->vecsz->rnk <= 1
Chris@82 201 && p->sz->dims[0].n == desc->n
Chris@82 202 && p->kind[0] == desc->genus->kind
Chris@82 203
Chris@82 204 /* check strides etc */
Chris@82 205 && X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs)
Chris@82 206
Chris@82 207 && (0
Chris@82 208 /* can operate out-of-place */
Chris@82 209 || p->I != p->O
Chris@82 210
Chris@82 211 /* computing one transform */
Chris@82 212 || vl == 1
Chris@82 213
Chris@82 214 /* can operate in-place as long as strides are the same */
Chris@82 215 || X(tensor_inplace_strides2)(p->sz, p->vecsz)
Chris@82 216 )
Chris@82 217 );
Chris@82 218 }
Chris@82 219
Chris@82 220 static int applicable_buf(const solver *ego_, const problem *p_)
Chris@82 221 {
Chris@82 222 const S *ego = (const S *) ego_;
Chris@82 223 const kr2c_desc *desc = ego->desc;
Chris@82 224 const problem_rdft *p = (const problem_rdft *) p_;
Chris@82 225 INT vl, ivs, ovs, batchsz;
Chris@82 226
Chris@82 227 return (
Chris@82 228 1
Chris@82 229 && p->sz->rnk == 1
Chris@82 230 && p->vecsz->rnk <= 1
Chris@82 231 && p->sz->dims[0].n == desc->n
Chris@82 232 && p->kind[0] == desc->genus->kind
Chris@82 233
Chris@82 234 /* check strides etc */
Chris@82 235 && X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs)
Chris@82 236
Chris@82 237 && (batchsz = compute_batchsize(desc->n), 1)
Chris@82 238
Chris@82 239 && (0
Chris@82 240 /* can operate out-of-place */
Chris@82 241 || p->I != p->O
Chris@82 242
Chris@82 243 /* can operate in-place as long as strides are the same */
Chris@82 244 || X(tensor_inplace_strides2)(p->sz, p->vecsz)
Chris@82 245
Chris@82 246 /* can do it if the problem fits in the buffer, no matter
Chris@82 247 what the strides are */
Chris@82 248 || vl <= batchsz
Chris@82 249 )
Chris@82 250 );
Chris@82 251 }
Chris@82 252
Chris@82 253 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
Chris@82 254 {
Chris@82 255 const S *ego = (const S *) ego_;
Chris@82 256 P *pln;
Chris@82 257 const problem_rdft *p;
Chris@82 258 iodim *d;
Chris@82 259 INT rs, cs, b, n;
Chris@82 260
Chris@82 261 static const plan_adt padt = {
Chris@82 262 X(rdft_solve), X(null_awake), print, destroy
Chris@82 263 };
Chris@82 264
Chris@82 265 UNUSED(plnr);
Chris@82 266
Chris@82 267 if (ego->bufferedp) {
Chris@82 268 if (!applicable_buf(ego_, p_))
Chris@82 269 return (plan *)0;
Chris@82 270 } else {
Chris@82 271 if (!applicable(ego_, p_))
Chris@82 272 return (plan *)0;
Chris@82 273 }
Chris@82 274
Chris@82 275 p = (const problem_rdft *) p_;
Chris@82 276
Chris@82 277 if (R2HC_KINDP(p->kind[0])) {
Chris@82 278 rs = p->sz->dims[0].is; cs = p->sz->dims[0].os;
Chris@82 279 pln = MKPLAN_RDFT(P, &padt,
Chris@82 280 ego->bufferedp ? apply_buf_r2hc : apply_r2hc);
Chris@82 281 } else {
Chris@82 282 rs = p->sz->dims[0].os; cs = p->sz->dims[0].is;
Chris@82 283 pln = MKPLAN_RDFT(P, &padt,
Chris@82 284 ego->bufferedp ? apply_buf_hc2r : apply_hc2r);
Chris@82 285 }
Chris@82 286
Chris@82 287 d = p->sz->dims;
Chris@82 288 n = d[0].n;
Chris@82 289
Chris@82 290 pln->k = ego->k;
Chris@82 291 pln->n = n;
Chris@82 292
Chris@82 293 pln->rs0 = rs;
Chris@82 294 pln->rs = X(mkstride)(n, 2 * rs);
Chris@82 295 pln->csr = X(mkstride)(n, cs);
Chris@82 296 pln->csi = X(mkstride)(n, -cs);
Chris@82 297 pln->ioffset = ioffset(p->kind[0], n, cs);
Chris@82 298
Chris@82 299 b = compute_batchsize(n);
Chris@82 300 pln->brs = X(mkstride)(n, 2 * b);
Chris@82 301 pln->bcsr = X(mkstride)(n, b);
Chris@82 302 pln->bcsi = X(mkstride)(n, -b);
Chris@82 303 pln->bioffset = ioffset(p->kind[0], n, b);
Chris@82 304
Chris@82 305 X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
Chris@82 306
Chris@82 307 pln->slv = ego;
Chris@82 308 X(ops_zero)(&pln->super.super.ops);
Chris@82 309
Chris@82 310 X(ops_madd2)(pln->vl / ego->desc->genus->vl,
Chris@82 311 &ego->desc->ops,
Chris@82 312 &pln->super.super.ops);
Chris@82 313
Chris@82 314 if (ego->bufferedp)
Chris@82 315 pln->super.super.ops.other += 2 * n * pln->vl;
Chris@82 316
Chris@82 317 pln->super.super.could_prune_now_p = !ego->bufferedp;
Chris@82 318
Chris@82 319 return &(pln->super.super);
Chris@82 320 }
Chris@82 321
Chris@82 322 /* constructor */
Chris@82 323 static solver *mksolver(kr2c k, const kr2c_desc *desc, int bufferedp)
Chris@82 324 {
Chris@82 325 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
Chris@82 326 S *slv = MKSOLVER(S, &sadt);
Chris@82 327 slv->k = k;
Chris@82 328 slv->desc = desc;
Chris@82 329 slv->bufferedp = bufferedp;
Chris@82 330 return &(slv->super);
Chris@82 331 }
Chris@82 332
Chris@82 333 solver *X(mksolver_rdft_r2c_direct)(kr2c k, const kr2c_desc *desc)
Chris@82 334 {
Chris@82 335 return mksolver(k, desc, 0);
Chris@82 336 }
Chris@82 337
Chris@82 338 solver *X(mksolver_rdft_r2c_directbuf)(kr2c k, const kr2c_desc *desc)
Chris@82 339 {
Chris@82 340 return mksolver(k, desc, 1);
Chris@82 341 }