-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconvolve.h
1871 lines (1588 loc) · 50.4 KB
/
convolve.h
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Hybrid dealiased convolution routines.
Copyright (C) 2024 John C. Bowman and Noel Murasko, Univ. of Alberta
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <cfloat>
#include <climits>
#include <list>
#include "Complex.h"
#include "fftw++.h"
#include "utils.h"
#include "Array.h"
#ifndef __convolve_h__
#define __convolve_h__ 1
namespace fftwpp {
extern const double twopi;
extern bool showOptTimes;
extern bool showRoutines;
// Constants used for initialization and testing.
const Complex I(0.0,1.0);
size_t nextfftsize(size_t m);
class fftBase;
typedef void (fftBase::*FFTcall)(Complex *f, Complex *F, size_t r,
Complex *W);
typedef void (fftBase::*FFTPad)(Complex *W);
class Indices
{
public:
fftBase *fft;
size_t *index;
size_t size,maxsize;
size_t r;
size_t offset;
Indices() : index(NULL), maxsize(0), offset(0) {}
void copy(Indices *indices, size_t size0) {
size=indices ? indices->size : size0;
if(size > maxsize) {
if(maxsize > 0)
delete [] index;
index=new size_t[size];
maxsize=size;
}
if(indices)
for(size_t d=1; d < size; ++d)
index[d]=indices->index[d];
}
~Indices() {
if(maxsize > 0)
delete [] index;
}
};
typedef void multiplier(Complex **F, size_t n,
Indices *indices, size_t threads);
// Multiplication routines for binary convolutions that take two inputs.
multiplier multNone,multBinary,realMultBinary,multcorrelation;
class Application : public ThreadBase
{
public:
size_t A;
size_t B;
multiplier *mult;
bool overwrite;
bool verbose;
size_t m;
size_t D;
ptrdiff_t I;
size_t maxthreads;
Application(size_t A, size_t B, multiplier *mult,
size_t threads=fftw::maxthreads, bool overwrite=true,
bool verbose=false, size_t m=0, size_t D=0, ptrdiff_t I=-1) :
ThreadBase(threads), A(A), B(B), mult(mult), overwrite(overwrite),
verbose(verbose), m(m), D(D), I(I)
{
maxthreads=threads;
}
Application(size_t A, size_t B, multiplier *mult, Application &parent,
size_t m=0, size_t D=0, ptrdiff_t I=-1) :
ThreadBase(1), A(A), B(B), mult(mult), overwrite(parent.overwrite),
verbose(parent.verbose), m(m), D(D), I(I)
{
maxthreads=parent.maxthreads;
}
};
class fftBase : public ThreadBase {
public:
size_t L; // number of unpadded Complex data values
size_t M; // minimum number of padded Complex data values
size_t C; // number of FFTs to compute in parallel
size_t S; // stride between successive elements
size_t m;
size_t p;
size_t q;
size_t n; // number of residues
size_t R; // number of residue blocks
size_t dr; // r increment
size_t D; // number of residues stored in F at a time
size_t D0; // remainder
size_t Cm,Sm;
size_t l; // block size of a single FFT
size_t b; // total block size, including stride
bool inplace;
Application app;
bool centered;
bool overwrite;
FFTcall Forward,Backward;
FFTPad Pad;
protected:
Complex *Zetaqp;
Complex *Zetaqp0;
Complex *Zetaqm;
Complex *ZetaqmS;
Complex *Zetaqm0;
Complex *ZetaqmS0;
public:
void checkParameters();
static void parameters(size_t L, size_t M, size_t m, bool centered,
size_t &p, size_t& n, size_t& q);
void common();
void initZetaqm(size_t q, size_t m);
class OptBase {
public:
size_t counter;
size_t m,D;
bool inplace;
bool mForced;
bool DForced;
typedef std::list<size_t> mList;
mList mlist;
double threshold;
double T;
virtual double time(size_t L, size_t M, Application &app,
size_t C, size_t S, size_t m,
size_t D, bool inplace)=0;
virtual bool valid(size_t m, size_t p, size_t q, size_t n, size_t D, size_t S)=0;
virtual size_t maxD(size_t n)=0;
virtual bool real() {return false;}
// Called by the optimizer to record the time to complete an application
// for a given value of m.
void check(size_t L, size_t M,
size_t C, size_t S, size_t m,
size_t p, size_t q, size_t n, size_t D,
bool inplace, Application& app, bool useTimer);
// Determine the optimal m value for padding L data values to
// size >= M for an application app.
// If Explicit=true, we only consider m >= M.
// centered must be true for all centered and Hermitian routines.
void scan(size_t L, size_t M, Application& app,
size_t C, size_t S, bool Explicit=false,
bool centered=false);
// A function called by opt to iterate over m and D values
// and call check.
// We compute p=ceilquotient(L,m0).
// If p <= 2, ubound is the maximum number maximum number of
// m values that are checked.
// If p > 2, ubound is the maximum size of m values that are
// checked.
// nextInnerSize is the function for determining the next m
// value when p > 2.
void optloop(size_t& m0, size_t L, size_t M,
Application& app, size_t C, size_t S,
bool centered, size_t ubound, bool useTimer,
bool Explicit, size_t(*nextInnerSize)(size_t)=NULL);
// The default optimizer routine. Used by scan to iterate and check
// different m values for a given geometry and application.
// 'minInner' is the minimum size of FFT we consider for the inner routines.
// 'itmax' is the maximum number of iterations done by optloop
// (when p <= 2).
void opt(size_t L, size_t M, Application& app,
size_t C, size_t S, size_t minInner,
size_t itmax, bool Explicit, bool centered,
bool useTimer=true);
};
void invalid () {
std::cerr << "Invalid parameters: " << std::endl
<< "m=" << m << " p=" << p << " q=" << q
<< " n=" << n << " " << " D=" << D << " S=" << S
<< std::endl;
exit(-1);
}
fftBase(size_t L, size_t M, Application& app,
size_t C=1, size_t S=0, bool centered=false) :
ThreadBase(app.threads), L(L), M(M), C(C), S(S == 0 ? C : S),
app(app), centered(centered) {
checkParameters();
}
fftBase(size_t L, size_t M, Application &app, size_t C, size_t S,
size_t m, size_t D, bool inplace,
bool centered=false) :
ThreadBase(app.threads), L(L), M(M), C(C), S(S == 0 ? C : S), m(m),
D(D), inplace(inplace), app(app), centered(centered) {
checkParameters();
this->app.D=D;
}
virtual ~fftBase();
void padNone(Complex *W) {}
virtual void padSingle(Complex *W) {}
virtual void padMany(Complex *W) {}
void pad(Complex *W) {
if(W)
(this->*Pad)(W);
}
void forward(Complex *f, Complex *F, size_t r=0, Complex *W=NULL) {
(this->*Forward)(f,F,r,W);
}
void backward(Complex *f, Complex *F, size_t r=0, Complex *W=NULL) {
(this->*Backward)(f,F,r,W);
}
virtual void forwardExplicit(Complex *f, Complex *F, size_t,
Complex *W) {};
virtual void forwardExplicitMany(Complex *f, Complex *F, size_t,
Complex *W) {};
virtual void forwardExplicitFast(Complex *f, Complex *F, size_t,
Complex *W) {}
virtual void forwardExplicitManyFast(Complex *f, Complex *F, size_t,
Complex *W) {}
virtual void forwardExplicitSlow(Complex *f, Complex *F, size_t r,
Complex *W) {}
virtual void forwardExplicitManySlow(Complex *f, Complex *F, size_t r,
Complex *W) {}
virtual void backwardExplicit(Complex *F, Complex *f, size_t,
Complex *W) {};
virtual void backwardExplicitMany(Complex *F, Complex *f, size_t,
Complex *W) {};
virtual void backwardExplicitFast(Complex *F, Complex *f, size_t,
Complex *W) {}
virtual void backwardExplicitManyFast(Complex *F, Complex *f, size_t,
Complex *W) {}
virtual void backwardExplicitSlow(Complex *F, Complex *f, size_t r,
Complex *W) {}
virtual void backwardExplicitManySlow(Complex *F, Complex *f, size_t r,
Complex *W) {}
// Return transformed index for residue r at position i
virtual size_t index(size_t r, size_t i) {
if(q == 1) return i;
size_t s=i%m;
size_t u;
size_t P;
if(D > 1 && ((centered && p % 2 == 0) || p <= 2)) {
P=utils::ceilquotient(p,2);
u=(i/m)%P;
size_t offset=r == 0 && i >= P*m && D0 % 2 == 1 ? 1 : 0;
double incr=(i+P*m*offset)/(2*P*m);
r += incr;
if(i/(P*m)-2*incr+offset == 1) {
if((!centered && p == 2) || (r > 0 && u == 0))
s=s > 0 ? s-1 : m-1;
if(r == 0)
r=n/2;
else {
r=n-r;
u=u > 0 ? u-1 : P-1;
}
}
} else {
u=(i/m)%p;
r += i/(p*m);
}
return q*s+n*u+r;
}
size_t Index(size_t r, size_t I) {
size_t i=I/S;
return S*(index(r,i)-i)+I;
}
virtual void forward1(Complex *f, Complex *F0, size_t r0, Complex *W)
{}
virtual void forward1All(Complex *f, Complex *F0, size_t r0,
Complex *W) {}
virtual void forward1Many(Complex *f, Complex *F, size_t r, Complex *W) {}
virtual void forward1ManyAll(Complex *f, Complex *F, size_t r,
Complex *W) {}
virtual void forward2(Complex *f, Complex *F0, size_t r0, Complex *W) {}
virtual void forward2All(Complex *f, Complex *F0, size_t r0,
Complex *W) {}
virtual void forward2Many(Complex *f, Complex *F, size_t r,
Complex *W) {}
virtual void forward2ManyAll(Complex *f, Complex *F, size_t r,
Complex *W) {}
virtual void forwardInner(Complex *f, Complex *F0, size_t r0,
Complex *W) {}
virtual void forwardInnerAll(Complex *f, Complex *F0, size_t r0,
Complex *W) {}
virtual void forwardInnerMany(Complex *f, Complex *F, size_t r,
Complex *W) {}
virtual void forwardInnerManyAll(Complex *f, Complex *F, size_t r,
Complex *W) {}
virtual void backward1(Complex *F0, Complex *f, size_t r0, Complex *W)
{}
virtual void backward1All(Complex *F0, Complex *f, size_t r0,
Complex *W) {}
virtual void backward1Many(Complex *F, Complex *f, size_t r,
Complex *W) {}
virtual void backward1ManyAll(Complex *F, Complex *f, size_t r,
Complex *W) {}
virtual void backward2(Complex *F0, Complex *f, size_t r0,
Complex *W) {}
virtual void backward2All(Complex *F0, Complex *f, size_t r0,
Complex *W) {}
virtual void backward2Many(Complex *F, Complex *f, size_t r,
Complex *W) {}
virtual void backward2ManyAll(Complex *F, Complex *f, size_t r,
Complex *W) {}
virtual void backwardInner(Complex *F0, Complex *f, size_t r0,
Complex *W) {}
virtual void backwardInnerAll(Complex *F0, Complex *f, size_t r0,
Complex *W) {}
virtual void backwardInnerMany(Complex *F, Complex *f, size_t r,
Complex *W) {}
virtual void backwardInnerManyAll(Complex *F, Complex *f, size_t r,
Complex *W) {}
size_t normalization() {
return M;
}
virtual size_t paddedSize() {
return m*q;
}
virtual bool conjugates() {
return D > 1 && (p <= 2 || (centered && p % 2 == 0));
}
virtual size_t residueBlocks() {
return conjugates() ? utils::ceilquotient(n,2) : n;
}
size_t Dr() {return conjugates() ? D/2 : D;}
virtual size_t increment(size_t r) {
return r > 0 ? dr : (conjugates() ? utils::ceilquotient(D0,2) : D0);
}
size_t nloops() {
size_t count=0;
for(size_t r=0; r < R; r += increment(r))
++count;
return count;
}
bool loop2() {
return nloops() == 2 && app.A > app.B && !overwrite;
}
// Input data length
virtual size_t inputLength() {
return L;
}
// Number of doubles in an input word
virtual size_t wordSize() {
return 2;
}
// Number of doubles in input array
virtual size_t doubles() {
return wordSize()*S*inputLength();
}
// Number of complex words in FFT output buffer
virtual size_t outputSize() {
return b*D;
}
// Number of complex FFT outputs per residue per copy
virtual size_t blocksize(size_t r) {
return l;
}
// Number of complex FFT outputs per iteration per copy
virtual size_t noutputs(size_t r) {
return blocksize(r)*(r == 0 ? D0 : D);
}
// Number of complex FFT outputs per iteration, including stride.
virtual size_t span(size_t r) {
return S*noutputs(r);
}
// Number of complex words in input accumulation array
size_t workSizeV() {
return nloops() == 1 || loop2() ? 0 : utils::ceilquotient(doubles(),2);
}
virtual size_t workSizeW() {
return inplace ? 0 : outputSize();
}
size_t repad() {
return !inplace && L < m;
}
virtual double time()=0;
double report() {
double median=time()*1.0e-9;
std::cout << "median=" << median << std::endl;
return median;
}
};
typedef double timer(fftBase *fft, double& threshold);
timer timePad;
static const struct centeredConstructor {} noOptimize={};
class fftPad : public fftBase {
protected:
fft1d *fftm1;
fft1d *ifftm1;
mfft1d *fftm,*fftm0;
mfft1d *ifftm,*ifftm0;
mfft1d *fftp;
mfft1d *ifftp;
public:
static bool valid(size_t m, size_t p, size_t q , size_t n, size_t D, size_t S) {
if(q == 1) return D == 1;
return D == 1 || (S == 1 && ((D < n && D % 2 == 0) || D == n));
}
class Opt : public OptBase {
public:
Opt() {}
Opt(size_t L, size_t M, Application& app,
size_t C, size_t S, bool Explicit=false) {
scan(L,M,app,C,S,Explicit);
}
bool valid(size_t m, size_t p, size_t q, size_t n, size_t D, size_t S) {
return fftPad::valid(m,p,q,n,D,S);
}
size_t maxD(size_t n) {
return n;
}
double time(size_t L, size_t M, Application &app, size_t C, size_t S,
size_t m, size_t D, bool inplace) {
fftPad fft(L,M,app,C,S,m,D,inplace);
double threshold=DBL_MAX;
return timePad(&fft,threshold);
}
};
// Normal entry point.
// Compute C ffts of length L with stride S >= C and distance 1
// padded to at least M
fftPad(size_t L, size_t M, Application& app,
size_t C=1, size_t S=0, bool Explicit=false) :
fftBase(L,M,app,C,S) {
Opt opt=Opt(L,M,app,C,this->S,Explicit);
m=opt.m;
D=opt.D;
inplace=opt.inplace;
if(Explicit)
M=m;
parameters(L,M,m,centered,p,n,q);
init();
}
// Compute an fft padded to N=m*q >= M >= L
fftPad(size_t L, size_t M, Application &app, size_t C, size_t S,
size_t m, size_t D, bool inplace) :
fftBase(L,M,app,C,S,m,D,inplace) {
Opt opt;
parameters(L,M,m,centered,p,n,q);
if(q > 1 && !opt.valid(m,p,q,n,D,this->S)) invalid();
init();
}
// Centered entry points: defer optimization
fftPad(size_t L, size_t M, Application &app, size_t C, size_t S,
centeredConstructor) :
fftBase(L,M,app,C,S,true) {}
fftPad(size_t L, size_t M, Application &app, size_t C, size_t S,
size_t m, size_t D, bool inplace, centeredConstructor) :
fftBase(L,M,app,C,S,m,D,inplace,true) {}
~fftPad();
void init();
double time() {
double threshold=DBL_MAX;
return timePad(this,threshold);
}
// Explicitly pad to m.
void padSingle(Complex *W);
// Explicitly pad C FFTs to m.
void padMany(Complex *W);
void forwardExplicit(Complex *f, Complex *F, size_t, Complex *W);
void forwardExplicitMany(Complex *f, Complex *F, size_t, Complex *W);
void backwardExplicit(Complex *F, Complex *f, size_t, Complex *W);
void backwardExplicitMany(Complex *F, Complex *f, size_t, Complex *W);
// p=1 && C=1
void forward1(Complex *f, Complex *F0, size_t r0, Complex *W);
void forward1All(Complex *f, Complex *F0, size_t r0, Complex *W);
void forward1Many(Complex *f, Complex *F, size_t r, Complex *W);
void forward1ManyAll(Complex *f, Complex *F, size_t r, Complex *W);
void forward2(Complex *f, Complex *F0, size_t r0, Complex *W);
void forward2All(Complex *f, Complex *F0, size_t r0, Complex *W);
void forward2Many(Complex *f, Complex *F, size_t r, Complex *W);
void forward2ManyAll(Complex *f, Complex *F, size_t r, Complex *W);
void forwardInner(Complex *f, Complex *F0, size_t r0, Complex *W);
void forwardInnerMany(Complex *f, Complex *F, size_t r, Complex *W);
// Compute an inverse fft of length N=m*q unpadded back
// to size m*p >= L.
// input and output arrays must be distinct
// Input F destroyed
void backward1(Complex *F0, Complex *f, size_t r0, Complex *W);
void backward1All(Complex *F0, Complex *f, size_t r0, Complex *W);
void backward1Many(Complex *F, Complex *f, size_t r, Complex *W);
void backward1ManyAll(Complex *F, Complex *f, size_t r, Complex *W);
void backward2(Complex *F0, Complex *f, size_t r0, Complex *W);
void backward2All(Complex *F0, Complex *f, size_t r0, Complex *W);
void backward2Many(Complex *F, Complex *f, size_t r, Complex *W);
void backward2ManyAll(Complex *F, Complex *f, size_t r, Complex *W);
void backwardInner(Complex *F0, Complex *f, size_t r0, Complex *W);
void backwardInnerMany(Complex *F, Complex *f, size_t r, Complex *W);
};
class fftPadCentered : public fftPad {
Complex *ZetaShift;
public:
class Opt : public OptBase {
public:
Opt() {}
Opt(size_t L, size_t M, Application& app,
size_t C, size_t S, bool Explicit=false) {
scan(L,M,app,C,S,Explicit,true);
}
bool valid(size_t m, size_t p, size_t q , size_t n, size_t D, size_t S) {
return (q == 1 || p%2 == 0) && fftPad::valid(m,p,q,n,D,S);
}
size_t maxD(size_t n) {
return n;
}
double time(size_t L, size_t M, Application &app, size_t C, size_t S,
size_t m, size_t D, bool inplace) {
fftPadCentered fft(L,M,app,C,S,m,D,inplace);
double threshold=DBL_MAX;
return timePad(&fft,threshold);
}
};
// Normal entry point.
// Compute C ffts of length L and distance 1 padded to at least M
fftPadCentered(size_t L, size_t M, Application& app,
size_t C=1, size_t S=0, bool Explicit=false) :
fftPad(L,M,app,C,S,noOptimize) {
Opt opt=Opt(L,M,app,C,this->S,Explicit);
m=opt.m;
D=opt.D;
inplace=opt.inplace;
if(Explicit)
M=m;
parameters(L,M,m,centered,p,n,q);
fftPad::init();
init();
}
// Compute an fft padded to N=m*q >= M >= L
fftPadCentered(size_t L, size_t M, Application &app, size_t C, size_t S,
size_t m, size_t D, bool inplace) :
fftPad(L,M,app,C,S,m,D,inplace,noOptimize) {
Opt opt;
parameters(L,M,m,centered,p,n,q);
if(q > 1 && !opt.valid(m,p,q,n,D,this->S)) invalid();
fftPad::init();
init();
}
~fftPadCentered() {
if(ZetaShift)
utils::deleteAlign(ZetaShift);
}
bool conjugates() {return D > 1 && (p == 1 || p % 2 == 0);}
void init();
double time() {
double threshold=DBL_MAX;
return timePad(this,threshold);
}
void forwardExplicitFast(Complex *f, Complex *F, size_t r, Complex *W);
void forwardExplicitManyFast(Complex *f, Complex *F, size_t r,
Complex *W);
void forwardExplicitSlow(Complex *f, Complex *F, size_t r, Complex *W);
void forwardExplicitManySlow(Complex *f, Complex *F, size_t r,
Complex *W);
void backwardExplicitFast(Complex *F, Complex *f, size_t r,
Complex *W);
void backwardExplicitManyFast(Complex *F, Complex *f, size_t r,
Complex *W);
void backwardExplicitSlow(Complex *F, Complex *f, size_t r,
Complex *W);
void backwardExplicitManySlow(Complex *F, Complex *f, size_t r,
Complex *W);
void initShift();
void forward2(Complex *f, Complex *F0, size_t r0, Complex *W);
void forward2All(Complex *f, Complex *F0, size_t r0, Complex *W);
void forward2Many(Complex *f, Complex *F, size_t r, Complex *W);
void forward2ManyAll(Complex *f, Complex *F, size_t r, Complex *W);
void forwardInner(Complex *f, Complex *F0, size_t r0, Complex *W);
void forwardInnerAll(Complex *f, Complex *F0, size_t r0, Complex *W);
void forwardInnerMany(Complex *f, Complex *F0, size_t r0, Complex *W);
void forwardInnerManyAll(Complex *f, Complex *F0, size_t r0,
Complex *W);
void backward2(Complex *F0, Complex *f, size_t r0, Complex *W);
void backward2All(Complex *F0, Complex *f, size_t r0, Complex *W);
void backward2Many(Complex *F, Complex *f, size_t r, Complex *W);
void backward2ManyAll(Complex *F, Complex *f, size_t r, Complex *W);
void backwardInner(Complex *F0, Complex *f, size_t r0, Complex *W);
void backwardInnerAll(Complex *F0, Complex *f, size_t r0, Complex *W);
void backwardInnerMany(Complex *F0, Complex *f, size_t r0, Complex *W);
void backwardInnerManyAll(Complex *F0, Complex *f, size_t r0,
Complex *W);
};
class fftPadHermitian : public fftBase {
size_t e;
size_t B; // Work block size
crfft1d *crfftm1;
rcfft1d *rcfftm1;
mcrfft1d *crfftm;
mrcfft1d *rcfftm;
mfft1d *fftp;
mfft1d *ifftp;
public:
class Opt : public OptBase {
public:
Opt() {}
Opt(size_t L, size_t M, Application& app,
size_t C, size_t, bool Explicit=false) {
scan(L,M,app,C,C,Explicit,true);
}
bool valid(size_t m, size_t p, size_t q , size_t n, size_t D, size_t C) {
return (D == 1 && q == 1) || (D == 2 && p%2 == 0 && (p == 2 || C == 1));
}
size_t maxD(size_t n) {
return n;
}
double time(size_t L, size_t M, Application &app, size_t C, size_t,
size_t m, size_t D, bool inplace) {
fftPadHermitian fft(L,M,app,C,m,D,inplace);
return timePad(&fft,threshold);
}
};
// Normal entry point.
fftPadHermitian(size_t L, size_t M, Application& app,
size_t C=1, bool Explicit=false) :
fftBase(L,M,app,C,C,true) {
Opt opt=Opt(L,M,app,C,C,Explicit);
m=opt.m;
D=opt.D;
inplace=opt.inplace;
if(Explicit)
M=m;
parameters(L,M,m,centered,p,n,q);
init();
}
fftPadHermitian(size_t L, size_t M, Application &app, size_t C,
size_t m, size_t D, bool inplace) :
fftBase(L,M,app,C,C,m,D,inplace,true) {
Opt opt;
parameters(L,M,m,centered,p,n,q);
if(q > 1 && !opt.valid(m,p,q,n,D,C)) invalid();
init();
}
~fftPadHermitian();
void init();
double time() {
double threshold=DBL_MAX;
return timePad(this,threshold);
}
void forwardExplicit(Complex *f, Complex *F, size_t, Complex *W);
void forwardExplicitMany(Complex *f, Complex *F, size_t, Complex *W);
void forward2(Complex *f, Complex *F0, size_t r0, Complex *W);
void forward2Many(Complex *f, Complex *F, size_t r, Complex *W);
void forwardInner(Complex *f, Complex *F0, size_t r0, Complex *W);
void backwardExplicit(Complex *F, Complex *f, size_t, Complex *W);
void backwardExplicitMany(Complex *F, Complex *f, size_t, Complex *W);
void backward2(Complex *F0, Complex *f, size_t r0, Complex *W);
void backward2Many(Complex *F, Complex *f, size_t r, Complex *W);
void backwardInner(Complex *F0, Complex *f, size_t r0, Complex *W);
// Length of input array
size_t inputLength() {
return utils::ceilquotient(L,2);
}
// Number of real FFT outputs per residue per copy
size_t blocksize(size_t) {
return m*(q == 1 ? 1 : p/2);
}
// Number of real FFT outputs per iteration per copy
size_t noutputs(size_t) {
return blocksize(0);
}
// Number of complex FFT outputs per iteration, including stride.
size_t span(size_t) {
return 2*b;
}
size_t workSizeW() {
return inplace ? 0 : B*D;
}
};
class fftPadReal : public fftBase {
size_t e;
rcfft1d *rcfftm1;
crfft1d *crfftm1;
mrcfft1d *rcfftm;
mcrfft1d *crfftm;
mfft1d *fftm,*fftm0,*fftmr0,*fftmp2;
mfft1d *ifftm,*ifftm0,*ifftmr0,*ifftmp2;
mfft1d *ffth;
mfft1d *iffth;
mrcfft1d *rcfftp;
mcrfft1d *crfftp;
mfft1d *fftp;
mfft1d *ifftp;
mfft1d *fftp2;
mfft1d *ifftp2;
Complex *V;
public:
class Opt : public OptBase {
public:
Opt() {}
Opt(size_t L, size_t M, Application& app,
size_t C, size_t S, bool Explicit=false) {
scan(L,M,app,C,S,Explicit);
}
bool valid(size_t m, size_t p, size_t q, size_t n, size_t D, size_t S) {
return (n%2 == 1 || (p%2 == 0 || p <= 2)) && (q%2 == 1 || m%2 == 0) &&
(D == 1 || (S == 1 && ((D < (n-1)/2 && D % 2 == 0) || D == (n-1)/2)));
}
size_t maxD(size_t n) {
return n > 2 ? (n-1)/2 : 1;
}
bool real() {return true;}
double time(size_t L, size_t M, Application &app, size_t C, size_t S,
size_t m, size_t D, bool inplace) {
fftPadReal fft(L,M,app,C,S,m,D,inplace);
double threshold=DBL_MAX;
return timePad(&fft,threshold);
}
};
// Normal entry point.
// Compute C ffts of length L with stride S >= C and distance 1
// padded to at least M
fftPadReal(size_t L, size_t M, Application& app,
size_t C=1, size_t S=0, bool Explicit=false) :
fftBase(L,M,app,C,S) {
Opt opt=Opt(L,M,app,C,this->S,Explicit);
m=opt.m;
D=opt.D;
inplace=opt.inplace;
if(Explicit)
M=m;
parameters(L,M,m,centered,p,n,q);
init();
}
// Compute an fft padded to N=m*q >= M >= L
fftPadReal(size_t L, size_t M, Application &app, size_t C, size_t S,
size_t m, size_t D, bool inplace) :
fftBase(L,M,app,C,S,m,D,inplace) {
Opt opt;
parameters(L,M,m,centered,p,n,q);
if(q > 1 && !opt.valid(m,p,q,n,D,this->S)) invalid();
init();
}
~fftPadReal();
void init();
double time() {
double threshold=DBL_MAX;
return timePad(this,threshold);
}
// Explicitly pad to m.
void padSingle(Complex *W);
// Explicitly pad C FFTs to m.
void padMany(Complex *W);
void forwardExplicit(Complex *f, Complex *F, size_t, Complex *W);
void backwardExplicit(Complex *F, Complex *f, size_t, Complex *W);
void forwardExplicitMany(Complex *f, Complex *F, size_t, Complex *W);
void backwardExplicitMany(Complex *F, Complex *f, size_t, Complex *W);
// p=1 && C=1
void forward1(Complex *f, Complex *F, size_t r0, Complex *W);
void backward1(Complex *F, Complex *f, size_t r0, Complex *W);
void forward1Many(Complex *f, Complex *F, size_t r, Complex *W);
void backward1Many(Complex *F, Complex *f, size_t r, Complex *W);
void forward2Many(Complex *f, Complex *F, size_t r, Complex *W);
void backward2Many(Complex *F, Complex *f, size_t r, Complex *W);
// p=2 && C=1
void forward2(Complex *f, Complex *F, size_t r0, Complex *W);
void backward2(Complex *F, Complex *f, size_t r0, Complex *W);
void forwardInner(Complex *f, Complex *F, size_t r0, Complex *W);
void backwardInner(Complex *F, Complex *f, size_t r0, Complex *W);
void forwardInnerMany(Complex *f, Complex *F, size_t r, Complex *W);
void backwardInnerMany(Complex *F, Complex *f, size_t r, Complex *W);
// Number of doubles in an input word
size_t wordSize() {
return 1;
}
size_t outputSize() {
if(n == 2) return p > 2 ? (p/2+1)*m*S : e*S;
return b*D;
}
bool conjugates() {
return false;
}
size_t residueBlocks() {
return utils::ceilquotient(n+1,2);
}
size_t increment(size_t r) {
return r > 1 ? D : r == 1 ? D0 : 1;
}
// Number of complex FFT outputs per residue per copy
size_t blocksize(size_t r) {
if(r == 0) return p > 2 ? (p % 2 ? (p/2+1)*m : (p/2)*m+e-1): e;
if(2*r == n) return p > 2 ? (p/2)*m : e-1;
return l;
}
// Number of complex FFT outputs per iteration per copy
size_t noutputs(size_t r) {
if(r == 0) return p > 2 ? (p % 2 ? (p/2+1)*m : (p/2)*m+e-1): e;
return blocksize(r)*(2*r == n ? 1 : r == 1 ? D0 : D);
}
// Return transformed index for residue r at position i
size_t index(size_t r, size_t i) {
if(q == 1) return i;
if(D > 1) {
std::cerr << "Indexing for D > 1 not yet implemented for real transforms"
<< std::endl;
exit(-1);
}
size_t s=i%m;
if(p <= 2) {
if(r == 0) {
return q*i;
} else if(2*r == q) {
return q*m-(q*2*i+r);
}
} else { // Inner loop
size_t u=(i/m)%p;
if(r == 0) {
if(2*u == p)
return q*m-(q*2*s+u*n);
return s == 0 ? u*n : q*m-(q*s-u*n);
} else if(2*r == n)
return q*m-(q*s+2*u*n+r);
}
return q*(m-s)-r;
}
};
class Convolution : public ThreadBase {
public:
fftBase *fft;
size_t L;
size_t A;
size_t B;
multiplier *mult;
double scale;
protected:
size_t q;
size_t n;
size_t R;
size_t r;
Complex **F,**Fp;
Complex *FpB;
Complex **V;
Complex *W;
Complex *H;
Complex *W0;