forked from preda/gpuowl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpuowl.cl
1238 lines (991 loc) · 30.5 KB
/
gpuowl.cl
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
// gpuOwl, an OpenCL Mersenne primality test.
// Copyright (C) Mihai Preda.
// The data is organized in pairs of words in a matrix WIDTH x HEIGHT.
// The pair (a, b) is sometimes interpreted as the complex value a + i*b.
// The order of words is column-major (i.e. transposed from the usual row-major matrix order).
#pragma OPENCL FP_CONTRACT ON
#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
#endif
// Common type names C++ - OpenCL.
typedef uint u32;
typedef ulong u64;
#include "shared.h"
// Expected defines: EXP the exponent.
// WIDTH, SMALL_HEIGHT, MIDDLE.
#define BIG_HEIGHT (SMALL_HEIGHT * MIDDLE)
#define ND (WIDTH * BIG_HEIGHT)
#define NWORDS (ND * 2u)
#if WIDTH == 1024 || WIDTH == 256
#define NW 4
#else
#define NW 8
#endif
#if SMALL_HEIGHT == 1024 || SMALL_HEIGHT == 256
#define NH 4
#else
#define NH 8
#endif
#define G_W (WIDTH / NW)
#define G_H (SMALL_HEIGHT / NH)
// Used in bitlen() and weighting.
#define STEP (NWORDS - (EXP % NWORDS))
uint extra(uint k) { return ((ulong) STEP) * k % NWORDS; }
// Is the word at pos a big word (BASE_BITLEN+1 bits)? (vs. a small, BASE_BITLEN bits word).
bool isBigWord(uint k) { return extra(k) + STEP < NWORDS; }
// { return extra(k) < extra(k + 1); }
// Number of bits for the word at pos.
uint bitlen(uint k) { return EXP / NWORDS + isBigWord(k); }
// Propagate carry this many pairs of words.
#define CARRY_LEN 16
typedef double T;
typedef double2 T2;
typedef int Word;
typedef int2 Word2;
typedef long Carry;
T2 U2(T a, T b) { return (T2)(a, b); }
// complex mul
T2 mul(T2 a, T2 b) { return U2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
// complex square
T2 sq(T2 a) { return U2((a.x + a.y) * (a.x - a.y), 2 * a.x * a.y); }
T2 mul_t4(T2 a) { return U2(a.y, -a.x); } // mul(a, U2( 0, -1)); }
T2 mul_t8(T2 a) { return U2(a.y + a.x, a.y - a.x) * M_SQRT1_2; } // mul(a, U2( 1, -1)) * (T)(M_SQRT1_2); }
T2 mul_3t8(T2 a) { return U2(a.x - a.y, a.x + a.y) * - M_SQRT1_2; } // mul(a, U2(-1, -1)) * (T)(M_SQRT1_2); }
T shl1(T a, uint k) { return a * (1 << k); }
T2 shl(T2 a, uint k) { return U2(shl1(a.x, k), shl1(a.y, k)); }
T2 swap(T2 a) { return U2(a.y, a.x); }
T2 conjugate(T2 a) { return U2(a.x, -a.y); }
void bar() { barrier(CLK_LOCAL_MEM_FENCE); }
Word lowBits(int u, uint bits) { return (u << (32 - bits)) >> (32 - bits); }
Word carryStep(Carry x, Carry *carry, int bits) {
x += *carry;
Word w = lowBits(x, bits);
*carry = (x - w) >> bits;
return w;
}
// Simpler version of signbit(a).
uint signBit(double a) { return ((uint *)&a)[1] >> 31; }
uint oldBitlen(double a) { return EXP / NWORDS + signBit(a); }
Carry unweight(T x, T weight) { return rint(x * fabs(weight)); }
Word2 unweightAndCarry(uint mul, T2 u, Carry *carry, T2 weight) {
Word a = carryStep(mul * unweight(u.x, weight.x), carry, oldBitlen(weight.x));
Word b = carryStep(mul * unweight(u.y, weight.y), carry, oldBitlen(weight.y));
return (Word2) (a, b);
}
T2 weightAux(Word x, Word y, T2 weight) { return U2(x, y) * fabs(weight); }
T2 weight(Word2 a, T2 w) { return weightAux(a.x, a.y, w); }
// No carry out. The final carry is "absorbed" in the last word.
T2 carryAndWeightFinal(Word2 u, Carry carry, T2 w) {
Word x = carryStep(u.x, &carry, oldBitlen(w.x));
Word y = u.y + carry;
return weightAux(x, y, w);
}
// Carry propagation from word and carry.
Word2 carryWord(Word2 a, Carry *carry, uint pos) {
a.x = carryStep(a.x, carry, bitlen(2 * pos + 0));
a.y = carryStep(a.y, carry, bitlen(2 * pos + 1));
return a;
}
T2 addsub(T2 a) { return U2(a.x + a.y, a.x - a.y); }
T2 foo2(T2 a, T2 b) {
a = addsub(a);
b = addsub(b);
return addsub(U2(a.x * b.x, a.y * b.y));
}
// computes 2*[x^2+y^2 + i*(2*x*y)]. Needs a name.
T2 foo(T2 a) { return foo2(a, a); }
#define X2(a, b) { T2 t = a; a = t + b; b = t - b; }
#define SWAP(a, b) { T2 t = a; a = b; b = t; }
void fft4Core(T2 *u) {
X2(u[0], u[2]);
X2(u[1], u[3]);
u[3] = mul_t4(u[3]);
X2(u[0], u[1]);
X2(u[2], u[3]);
}
void fft4(T2 *u) {
fft4Core(u);
// revbin [0, 2, 1, 3] undo
SWAP(u[1], u[2]);
}
void fft8Core(T2 *u) {
for (int i = 0; i < 4; ++i) { X2(u[i], u[i + 4]); }
u[5] = mul_t8(u[5]);
u[6] = mul_t4(u[6]);
u[7] = mul_3t8(u[7]);
fft4Core(u);
fft4Core(u + 4);
}
void fft8(T2 *u) {
fft8Core(u);
// revbin [0, 4, 2, 6, 1, 5, 3, 7] undo
SWAP(u[1], u[4]);
SWAP(u[3], u[6]);
}
void fft3(T2 *u) {
const double SQRT3_2 = 0x1.bb67ae8584caap-1; // sin(tau/3), sqrt(3)/2, 0.86602540378443859659;
X2(u[1], u[2]);
T2 u0 = u[0];
u[0] += u[1];
u[1] = u0 - u[1] / 2;
u[2] = mul_t4(u[2] * SQRT3_2);
X2(u[1], u[2]);
}
void fft6(T2 *u) {
const double SQRT3_2 = 0x1.bb67ae8584caap-1; // sin(tau/3), sqrt(3)/2, 0.86602540378443859659;
for (int i = 0; i < 3; ++i) { X2(u[i], u[i + 3]); }
u[4] = mul(u[4], U2( 0.5, -SQRT3_2));
u[5] = mul(u[5], U2(-0.5, -SQRT3_2));
fft3(u);
fft3(u + 3);
// fix order [0, 2, 4, 1, 3, 5]
T2 tmp = u[1];
u[1] = u[3];
u[3] = u[4];
u[4] = u[2];
u[2] = tmp;
}
// Adapted from: Nussbaumer, "Fast Fourier Transform and Convolution Algorithms", 5.5.4 "5-Point DFT".
void fft5(T2 *u) {
const double SIN1 = 0x1.e6f0e134454ffp-1; // sin(tau/5), 0.95105651629515353118
const double SIN2 = 0x1.89f188bdcd7afp+0; // sin(tau/5) + sin(2*tau/5), 1.53884176858762677931
const double SIN3 = 0x1.73fd61d9df543p-2; // sin(tau/5) - sin(2*tau/5), 0.36327126400268044959
const double COS1 = 0x1.1e3779b97f4a8p-1; // (cos(tau/5) - cos(2*tau/5))/2, 0.55901699437494745126
X2(u[2], u[3]);
X2(u[1], u[4]);
X2(u[1], u[2]);
T2 tmp = u[0];
u[0] += u[1];
u[1] = u[1] * (-0.25) + tmp;
u[2] *= COS1;
tmp = (u[4] - u[3]) * SIN1;
tmp = U2(tmp.y, -tmp.x);
u[3] = U2(u[3].y, -u[3].x) * SIN2 + tmp;
u[4] = U2(-u[4].y, u[4].x) * SIN3 + tmp;
SWAP(u[3], u[4]);
X2(u[1], u[2]);
X2(u[1], u[4]);
X2(u[2], u[3]);
}
void fft10(T2 *u) {
const double COS1 = 0x1.9e3779b97f4a8p-1; // cos(tau/10), 0.80901699437494745126
const double SIN1 = -0x1.2cf2304755a5ep-1; // sin(tau/10), 0.58778525229247313710
const double COS2 = 0x1.3c6ef372fe95p-2; // cos(tau/5), 0.30901699437494745126
const double SIN2 = -0x1.e6f0e134454ffp-1; // sin(tau/5), 0.95105651629515353118
for (int i = 0; i < 5; ++i) { X2(u[i], u[i + 5]); }
u[6] = mul(u[6], U2( COS1, SIN1));
u[7] = mul(u[7], U2( COS2, SIN2));
u[8] = mul(u[8], U2(-COS2, SIN2));
u[9] = mul(u[9], U2(-COS1, SIN1));
fft5(u);
fft5(u + 5);
// fix order [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
SWAP(u[3], u[6]);
T2 tmp = u[1];
u[1] = u[5];
u[5] = u[7];
u[7] = u[8];
u[8] = u[4];
u[4] = u[2];
u[2] = tmp;
}
// Adapted from: Nussbaumer, "Fast Fourier Transform and Convolution Algorithms", 5.5.7 "9-Point DFT".
void fft9(T2 *u) {
const double C0 = 0x1.8836fa2cf5039p-1; // 0.766044443118978013 (2*c(u) - c(2*u) - c(4*u))/3
const double C1 = 0x1.e11f642522d1cp-1; // 0.939692620785908428 (c(u) + c(2*u) - 2*c(4*u))/3
const double C2 = 0x1.63a1a7e0b738ap-3; // 0.173648177666930359 -(c(u) - 2*c(2*u) + c(4*u))/3
const double C3 = 0x1.bb67ae8584caap-1; // 0.866025403784438597 s(3*u)
const double C4 = 0x1.491b7523c161dp-1; // 0.642787609686539363 s(u)
const double C5 = 0x1.5e3a8748a0bf5p-2; // 0.342020143325668713 s(4*u)
const double C6 = 0x1.f838b8c811c17p-1; // 0.984807753012208020 s(2*u)
X2(u[1], u[8]);
X2(u[2], u[7]);
X2(u[3], u[6]);
X2(u[4], u[5]);
T2 m4 = (u[2] - u[4]) * C1;
T2 s0 = (u[2] - u[1]) * C0 - m4;
X2(u[1], u[4]);
T2 t5 = u[1] + u[2];
T2 m8 = mul_t4(u[7] + u[8]) * C4;
T2 m10 = mul_t4(u[5] - u[8]) * C6;
X2(u[5], u[7]);
T2 m9 = mul_t4(u[5]) * C5;
T2 t10 = u[8] + u[7];
T2 s2 = m8 + m9;
u[5] = m9 - m10;
u[2] = u[0] - u[3] / 2;
u[0] += u[3];
u[3] = u[0] - t5 / 2;
u[0] += t5;
u[7] = mul_t4(u[6]) * C3;
u[8] = u[7] + s2;
u[6] = mul_t4(t10) * C3;
u[1] = u[2] - s0;
u[4] = u[4] * C2 - m4;
X2(u[2], u[4]);
u[4] += s0;
X2(u[5], u[7]);
u[5] -= s2;
X2(u[4], u[5]);
X2(u[3], u[6]);
X2(u[2], u[7]);
X2(u[1], u[8]);
}
void shufl(uint WG, local T *lds, T2 *u, uint n, uint f) {
uint me = get_local_id(0);
uint m = me / f;
for (int b = 0; b < 2; ++b) {
bar();
for (uint i = 0; i < n; ++i) { lds[(m + i * WG / f) / n * f + m % n * WG + me % f] = ((T *) (u + i))[b]; }
bar();
for (uint i = 0; i < n; ++i) { ((T *) (u + i))[b] = lds[i * WG + me]; }
}
}
void tabMul(uint WG, const global T2 *trig, T2 *u, uint n, uint f) {
uint me = get_local_id(0);
for (int i = 1; i < n; ++i) { u[i] = mul(u[i], trig[me / f + i * (WG / f)]); }
}
void shuflAndMul(uint WG, local T *lds, const global T2 *trig, T2 *u, uint n, uint f) {
#if 0
uint me = get_local_id(0);
uint m = me / f;
for (int b = 0; b < 2; ++b) {
bar();
for (uint i = 0; i < n; ++i) { lds[(m + i * WG / f) / n * f + m % n * WG + me % f] = ((T *) (u + i))[b]; }
bar();
for (uint i = 0; i < n; ++i) { ((T *) (u + i))[b] = lds[i * WG + me]; }
}
for (int i = 1; i < n; ++i) { u[i] = mul(u[i], trig[me / f + i * (WG / f)]); }
#else
shufl(WG, lds, u, n, f);
tabMul(WG, trig, u, n, f);
#endif
}
// 8x8
void fft64(local T *lds, T2 *u, const global T2 *trig) {
fft8(u);
shuflAndMul(8, lds, trig, u, 8, 1);
fft8(u);
}
// 64x4
void fft256(local T *lds, T2 *u, const global T2 *trig) {
for (int s = 4; s >= 0; s -= 2) {
fft4(u);
shuflAndMul(64, lds, trig, u, 4, 1 << s);
}
fft4(u);
}
// 64x8
void fft512(local T *lds, T2 *u, const global T2 *trig) {
for (int s = 3; s >= 0; s -= 3) {
fft8(u);
shuflAndMul(64, lds, trig, u, 8, 1 << s);
}
fft8(u);
}
// 256x4
void fft1K(local T *lds, T2 *u, const global T2 *trig) {
for (int s = 6; s >= 0; s -= 2) {
fft4(u);
shuflAndMul(256, lds, trig, u, 4, 1 << s);
}
fft4(u);
}
// 512x8
void fft4K(local T *lds, T2 *u, const global T2 *trig) {
for (int s = 6; s >= 0; s -= 3) {
fft8(u);
shuflAndMul(512, lds, trig, u, 8, 1 << s);
}
fft8(u);
}
// 256x8
void fft2K(local T *lds, T2 *u, const global T2 *trig) {
for (int s = 5; s >= 2; s -= 3) {
fft8(u);
shuflAndMul(256, lds, trig, u, 8, 1 << s);
}
fft8(u);
uint me = get_local_id(0);
for (int b = 0; b < 2; ++b) {
bar();
for (int i = 0; i < 8; ++i) { lds[(me + i * 256) / 4 + me % 4 * 512] = ((T *) (u + i))[b]; }
bar();
for (int i = 0; i < 4; ++i) {
((T *) (u + i))[b] = lds[i * 512 + me];
((T *) (u + i + 4))[b] = lds[i * 512 + 256 + me];
}
}
for (int i = 1; i < 4; ++i) {
u[i] = mul(u[i], trig[i * 512 + me]);
u[i + 4] = mul(u[i + 4], trig[i * 512 + 256 + me]);
}
fft4(u);
fft4(u + 4);
// fix order: interleave u[0:3] and u[4:7], like (u.even, u.odd) = (u.lo, u.hi).
SWAP(u[1], u[2]);
SWAP(u[1], u[4]);
SWAP(u[5], u[6]);
SWAP(u[3], u[6]);
}
void read(uint WG, uint N, T2 *u, const global T2 *in, uint base) {
for (int i = 0; i < N; ++i) { u[i] = in[base + i * WG + (uint) get_local_id(0)]; }
}
void write(uint WG, uint N, T2 *u, global T2 *out, uint base) {
for (int i = 0; i < N; ++i) { out[base + i * WG + (uint) get_local_id(0)] = u[i]; }
}
void readDelta(uint WG, uint N, T2 *u, const global T2 *a, const global T2 *b, uint base) {
for (uint i = 0; i < N; ++i) {
uint pos = base + i * WG + (uint) get_local_id(0);
u[i] = a[pos] - b[pos];
}
}
// Returns e^(-i * pi * k/n);
double2 slowTrig(int k, int n) {
double c;
double s = sincos(M_PI / n * k, &c);
return U2(c, -s);
}
// transpose LDS 64 x 64.
void transposeLDS(local T *lds, T2 *u) {
uint me = get_local_id(0);
for (int b = 0; b < 2; ++b) {
if (b) { bar(); }
for (int i = 0; i < 16; ++i) {
uint l = i * 4 + me / 64;
lds[l * 64 + (me + l) % 64 ] = ((T *)(u + i))[b];
}
bar();
for (int i = 0; i < 16; ++i) {
uint c = i * 4 + me / 64;
uint l = me % 64;
((T *)(u + i))[b] = lds[l * 64 + (c + l) % 64];
}
}
}
// Transpose the matrix of WxH, and MUL with FFT twiddles; by blocks of 64x64.
void transpose(uint W, uint H, local T *lds, const T2 *in, T2 *out) {
uint GPW = W / 64, GPH = H / 64;
uint g = get_group_id(0);
uint gy = g % GPH;
uint gx = g / GPH;
gx = (gy + gx) % GPW;
in += gy * 64 * W + gx * 64;
out += gy * 64 + gx * 64 * H;
uint me = get_local_id(0), mx = me % 64, my = me / 64;
T2 u[16];
for (int i = 0; i < 16; ++i) { u[i] = in[(4 * i + my) * W + mx]; }
transposeLDS(lds, u);
uint col = 64 * gy + mx;
T2 base = slowTrig(col * (64 * gx + my), W * H / 2);
T2 step = slowTrig(col, W * H / 8);
for (int i = 0; i < 16; ++i) {
out[(4 * i + my) * H + mx] = mul(u[i], base);
base = mul(base, step);
}
}
void transposeWords(uint W, uint H, local Word2 *lds, const Word2 *in, Word2 *out) {
uint GPW = W / 64, GPH = H / 64;
uint g = get_group_id(0);
uint gy = g % GPH;
uint gx = g / GPH;
gx = (gy + gx) % GPW;
in += gy * 64 * W + gx * 64;
out += gy * 64 + gx * 64 * H;
uint me = get_local_id(0);
uint mx = me % 64;
uint my = me / 64;
Word2 u[16];
for (int i = 0; i < 16; ++i) { u[i] = in[(4 * i + my) * W + mx]; }
for (int i = 0; i < 16; ++i) {
uint l = i * 4 + me / 64;
lds[l * 64 + (me + l) % 64 ] = u[i];
}
bar();
for (int i = 0; i < 16; ++i) {
uint c = i * 4 + me / 64;
uint l = me % 64;
u[i] = lds[l * 64 + (c + l) % 64];
}
for (int i = 0; i < 16; ++i) {
out[(4 * i + my) * H + mx] = u[i];
}
}
#ifndef ALT_RESTRICT
#define P(x) global x * restrict
#define CP(x) const P(x)
typedef CP(T2) Trig;
#else
#define P(x) global x *
#define CP(x) const P(x)
typedef CP(T2) restrict Trig;
#endif
#define KERNEL(x) kernel __attribute__((reqd_work_group_size(x, 1, 1))) void
// Read 64 Word2 starting at position 'startDword'.
KERNEL(64) readResidue(CP(Word2) in, P(Word2) out, uint startDword) {
uint me = get_local_id(0);
uint k = (startDword + me) % ND;
uint y = k % BIG_HEIGHT;
uint x = k / BIG_HEIGHT;
out[me] = in[WIDTH * y + x];
}
uint dwordToBitpos(uint dword) { return wordToBitpos(EXP, ND, dword); }
uint bitposToDword(uint bitpos) { return bitposToWord(EXP, ND, bitpos); }
uint transPos(uint k, uint width, uint height) { return k / height + k % height * width; }
uint kAt(uint gx, uint gy, uint i) {
return CARRY_LEN * gy + BIG_HEIGHT * G_W * gx + BIG_HEIGHT * ((uint) get_local_id(0)) + i;
}
// outEqual must be "true" on entry.
KERNEL(256) isEqual(uint sizeBytes, global long *in1, global long *in2, P(bool) outEqual) {
for (int p = get_global_id(0); p < sizeBytes / sizeof(long); p += get_global_size(0)) {
if (in1[p] != in2[p]) {
*outEqual = false;
return;
}
}
}
// outNotZero must be "false" on entry.
KERNEL(256) isNotZero(uint sizeBytes, global long *in, P(bool) outNotZero) {
for (int p = get_global_id(0); p < sizeBytes / sizeof(long); p += get_global_size(0)) {
if (in[p] != 0) {
*outNotZero = true;
return;
}
}
}
void fft_WIDTH(local T *lds, T2 *u, Trig trig) {
#if WIDTH == 64
fft64(lds, u, trig);
#elif WIDTH == 256
fft256(lds, u, trig);
#elif WIDTH == 512
fft512(lds, u, trig);
#elif WIDTH == 1024
fft1K(lds, u, trig);
#elif WIDTH == 2048
fft2K(lds, u, trig);
#elif WIDTH == 4096
fft4K(lds, u, trig);
#else
#error unexpected WIDTH.
#endif
}
void fft_HEIGHT(local T *lds, T2 *u, Trig trig) {
#if SMALL_HEIGHT == 64
fft64(lds, u, trig);
#elif SMALL_HEIGHT == 256
fft256(lds, u, trig);
#elif SMALL_HEIGHT == 512
fft512(lds, u, trig);
#elif SMALL_HEIGHT == 1024
fft1K(lds, u, trig);
#elif SMALL_HEIGHT == 2048
fft2K(lds, u, trig);
#else
#error unexpected SMALL_HEIGHT.
#endif
}
KERNEL(G_W) fftW(P(T2) io, Trig smallTrig) {
local T lds[WIDTH];
T2 u[NW];
uint g = get_group_id(0);
io += WIDTH * g;
read(G_W, NW, u, io, 0);
fft_WIDTH(lds, u, smallTrig);
write(G_W, NW, u, io, 0);
}
KERNEL(G_H) fftH(P(T2) io, Trig smallTrig) {
local T lds[SMALL_HEIGHT];
T2 u[NH];
uint g = get_group_id(0);
io += SMALL_HEIGHT * transPos(g, MIDDLE, WIDTH);
read(G_H, NH, u, io, 0);
fft_HEIGHT(lds, u, smallTrig);
write(G_H, NH, u, io, 0);
}
// fftPremul: weight words with "A" (for IBDWT) followed by FFT.
KERNEL(G_W) fftP(CP(Word2) in, P(T2) out, CP(T2) A, Trig smallTrig) {
local T lds[WIDTH];
T2 u[NW];
uint g = get_group_id(0);
uint step = WIDTH * g;
A += step;
in += step;
out += step;
uint me = get_local_id(0);
for (int i = 0; i < NW; ++i) {
uint p = G_W * i + me;
u[i] = weight(in[p], A[p]);
}
fft_WIDTH(lds, u, smallTrig);
write(G_W, NW, u, out, 0);
}
void middleMul(T2 *u, uint gx, uint me) {
T2 step = slowTrig(256 * gx + me, BIG_HEIGHT / 2);
T2 t = step;
for (int i = 1; i < MIDDLE; ++i, t = mul(t, step)) { u[i] = mul(u[i], t); }
}
void fft_MIDDLE(T2 *u) {
#if MIDDLE == 3
fft3(u);
#elif MIDDLE == 5
fft5(u);
#elif MIDDLE == 6
fft6(u);
#elif MIDDLE == 9
fft9(u);
#elif MIDDLE == 10
fft10(u);
#elif MIDDLE != 1
#error
#endif
}
KERNEL(256) fftMiddleIn(P(T2) io) {
T2 u[MIDDLE];
uint N = SMALL_HEIGHT / 256;
uint g = get_group_id(0);
uint gx = g % N;
uint gy = g / N;
uint me = get_local_id(0);
io += BIG_HEIGHT * gy + 256 * gx;
read(SMALL_HEIGHT, MIDDLE, u, io, 0);
fft_MIDDLE(u);
middleMul(u, gx, me);
write(SMALL_HEIGHT, MIDDLE, u, io, 0);
}
KERNEL(256) fftMiddleOut(P(T2) io) {
T2 u[MIDDLE];
uint N = SMALL_HEIGHT / 256;
uint g = get_group_id(0);
uint gx = g % N;
uint gy = g / N;
uint me = get_local_id(0);
io += BIG_HEIGHT * gy + 256 * gx;
read(SMALL_HEIGHT, MIDDLE, u, io, 0);
middleMul(u, gx, me);
fft_MIDDLE(u);
write(SMALL_HEIGHT, MIDDLE, u, io, 0);
}
// Carry propagation with optional MUL-3, over CARRY_LEN words.
// Input is conjugated and inverse-weighted.
void carryACore(uint mul, const global T2 *in, const global T2 *A, global Word2 *out, global Carry *carryOut) {
uint g = get_group_id(0);
uint me = get_local_id(0);
uint gx = g % NW;
uint gy = g / NW;
Carry carry = 0;
for (int i = 0; i < CARRY_LEN; ++i) {
uint p = G_W * gx + WIDTH * (CARRY_LEN * gy + i) + me;
out[p] = unweightAndCarry(mul, conjugate(in[p]), &carry, A[p]);
}
carryOut[G_W * g + me] = carry;
}
KERNEL(G_W) carryA(CP(T2) in, P(Word2) out, P(Carry) carryOut, CP(T2) A) {
carryACore(1, in, A, out, carryOut);
}
KERNEL(G_W) carryM(CP(T2) in, P(Word2) out, P(Carry) carryOut, CP(T2) A) {
carryACore(3, in, A, out, carryOut);
}
KERNEL(G_W) carryB(P(Word2) io, CP(Carry) carryIn) {
uint g = get_group_id(0);
uint me = get_local_id(0);
uint gx = g % NW;
uint gy = g / NW;
uint step = G_W * gx + WIDTH * CARRY_LEN * gy;
io += step;
uint HB = BIG_HEIGHT / CARRY_LEN;
uint prev = (gy + HB * G_W * gx + HB * me + (HB * WIDTH - 1)) % (HB * WIDTH);
uint prevLine = prev % HB;
uint prevCol = prev / HB;
Carry carry = carryIn[WIDTH * prevLine + prevCol];
for (int i = 0; i < CARRY_LEN; ++i) {
uint k = kAt(gx, gy, i);
uint p = i * WIDTH + me;
io[p] = carryWord(io[p], &carry, k);
if (!carry) { return; }
}
}
void release() {
#if 0
atomic_work_item_fence(CLK_GLOBAL_MEM_FENCE, memory_order_release, memory_scope_device);
work_group_barrier(0);
#else
work_group_barrier(CLK_GLOBAL_MEM_FENCE, memory_scope_device);
#endif
}
void acquire() {
#if 0
atomic_work_item_fence(CLK_GLOBAL_MEM_FENCE, memory_order_acquire, memory_scope_device);
work_group_barrier(0);
#else
work_group_barrier(CLK_GLOBAL_MEM_FENCE, memory_scope_device);
#endif
}
// The "carryFused" is equivalent to the sequence: fftW, carryA, carryB, fftPremul.
// It uses "stairway" carry data forwarding from one group to the next.
KERNEL(G_W) carryFused(P(T2) io, P(Carry) carryShuttle, P(uint) ready,
CP(T2) A, CP(T2) iA, Trig smallTrig) {
local T lds[WIDTH];
uint gr = get_group_id(0);
uint me = get_local_id(0);
uint H = BIG_HEIGHT;
uint line = gr % H;
uint step = WIDTH * line;
io += step;
A += step;
iA += step;
T2 u[NW];
Word2 wu[NW];
read(G_W, NW, u, io, 0);
fft_WIDTH(lds, u, smallTrig);
for (int i = 0; i < NW; ++i) {
uint p = i * G_W + me;
Carry carry = 0;
wu[i] = unweightAndCarry(1, conjugate(u[i]), &carry, iA[p]);
if (gr < H) { carryShuttle[gr * WIDTH + p] = carry; }
}
release();
// Signal that this group is done writing the carry.
if (gr < H && me == 0) {
atomic_store_explicit((atomic_uint *) &ready[gr], 1, memory_order_release, memory_scope_device);
}
if (gr == 0) { return; }
// Wait until the previous group is ready with the carry.
if (me == 0) {
while(!atomic_load_explicit((atomic_uint *) &ready[gr - 1], memory_order_acquire, memory_scope_device));
ready[gr - 1] = 0;
}
acquire();
for (int i = 0; i < NW; ++i) {
uint p = i * G_W + me;
u[i] = carryAndWeightFinal(wu[i], carryShuttle[(gr - 1) * WIDTH + ((p + WIDTH - gr / H) % WIDTH)], A[p]);
}
fft_WIDTH(lds, u, smallTrig);
write(G_W, NW, u, io, 0);
}
// copy of carryFused() above, with the only difference the mul-by-3 in unweightAndCarry().
KERNEL(G_W) carryFusedMul(P(T2) io, P(Carry) carryShuttle, P(uint) ready,
CP(T2) A, CP(T2) iA, Trig smallTrig) {
local T lds[WIDTH];
uint gr = get_group_id(0);
uint me = get_local_id(0);
uint H = BIG_HEIGHT;
uint line = gr % H;
uint step = WIDTH * line;
io += step;
A += step;
iA += step;
T2 u[NW];
Word2 wu[NW];
read(G_W, NW, u, io, 0);
fft_WIDTH(lds, u, smallTrig);
for (int i = 0; i < NW; ++i) {
uint p = i * G_W + me;
Carry carry = 0;
wu[i] = unweightAndCarry(3, conjugate(u[i]), &carry, iA[p]);
if (gr < H) { carryShuttle[gr * WIDTH + p] = carry; }
}
release();
// Signal that this group is done writing the carry.
if (gr < H && me == 0) {
atomic_store_explicit((atomic_uint *) &ready[gr], 1, memory_order_release, memory_scope_device);
}
if (gr == 0) { return; }
// Wait until the previous group is ready with the carry.
if (me == 0) {
while(!atomic_load_explicit((atomic_uint *) &ready[gr - 1], memory_order_acquire, memory_scope_device));
ready[gr - 1] = 0;
}
acquire();
for (int i = 0; i < NW; ++i) {
uint p = i * G_W + me;
Carry carry = carryShuttle[(gr - 1) * WIDTH + ((p + WIDTH - gr / H) % WIDTH)];
u[i] = carryAndWeightFinal(wu[i], carry, A[p]);
}
fft_WIDTH(lds, u, smallTrig);
write(G_W, NW, u, io, 0);
}
KERNEL(256) transposeW(CP(T2) in, P(T2) out) {
local T lds[4096];
transpose(WIDTH, BIG_HEIGHT, lds, in, out);
}
KERNEL(256) transposeH(CP(T2) in, P(T2) out) {
local T lds[4096];
transpose(BIG_HEIGHT, WIDTH, lds, in, out);
}
// from transposed to sequential.
KERNEL(256) transposeOut(CP(Word2) in, P(Word2) out) {
local Word2 lds[4096];
transposeWords(WIDTH, BIG_HEIGHT, lds, in, out);
}
// from sequential to transposed.
KERNEL(256) transposeIn(CP(Word2) in, P(Word2) out) {
local Word2 lds[4096];
transposeWords(BIG_HEIGHT, WIDTH, lds, in, out);
}
KERNEL(SMALL_HEIGHT / 2 / 4) square(P(T2) io) {
uint W = SMALL_HEIGHT;
uint H = ND / W;
uint me = get_local_id(0);
uint line1 = get_group_id(0);
uint line2 = (H - line1) % H;
uint g1 = transPos(line1, MIDDLE, WIDTH);
uint g2 = transPos(line2, MIDDLE, WIDTH);
T2 base = slowTrig(me * H + line1, W * H);
T2 step = slowTrig(1, 8);
for (uint i = 0; i < 4; ++i, base = mul(base, step)) {
if (i == 0 && line1 == 0 && me == 0) {
io[0] = shl(foo(conjugate(io[0])), 2);
io[W / 2] = shl(sq(conjugate(io[W / 2])), 3);
} else {
uint k = g1 * W + i * (W / 8) + me;
uint v = g2 * W + (W - 1) + (line1 == 0) - i * (W / 8) - me;
T2 a = io[k];
T2 b = conjugate(io[v]);
T2 t = swap(base);
X2(a, b);
b = mul(b, conjugate(t));
X2(a, b);
a = sq(a);
b = sq(b);
X2(a, b);
b = mul(b, t);
X2(a, b);
io[k] = conjugate(a);
io[v] = b;
}
}
}
KERNEL(SMALL_HEIGHT / 2) multiply(P(T2) io, CP(T2) in) {
uint W = SMALL_HEIGHT;
uint H = ND / W;
uint line1 = get_group_id(0);
uint me = get_local_id(0);
if (line1 == 0 && me == 0) {
io[0] = shl(conjugate(foo2(io[0], in[0])), 2);
io[W / 2] = shl(conjugate(mul(io[W / 2], in[W / 2])), 3);
return;
}
uint line2 = (H - line1) % H;
uint g1 = transPos(line1, MIDDLE, WIDTH);
uint g2 = transPos(line2, MIDDLE, WIDTH);
uint k = g1 * W + me;
uint v = g2 * W + (W - 1) - me + (line1 == 0);
T2 a = io[k];
T2 b = conjugate(io[v]);
T2 t = swap(slowTrig(me * H + line1, W * H));
X2(a, b);
b = mul(b, conjugate(t));
X2(a, b);
T2 c = in[k];
T2 d = conjugate(in[v]);
X2(c, d);
d = mul(d, conjugate(t));
X2(c, d);
a = mul(a, c);
b = mul(b, d);
X2(a, b);
b = mul(b, t);
X2(a, b);
io[k] = conjugate(a);
io[v] = b;