This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ntt.cpp
1001 lines (828 loc) · 33.2 KB
/
ntt.cpp
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
#include "ntt.hpp"
uint64_t
get_root_of_unity(uint64_t n)
{
uint64_t power = 1ul << (TWO_ADICITY - n);
return ff_p_pow(TWO_ADIC_ROOT_OF_UNITY, power);
}
sycl::event
compute_omega(sycl::queue& q, buf_1d_u64_t& omega, const uint64_t n)
{
sycl::event evt = q.submit([&](sycl::handler& h) {
buf_1d_u64_wr_t acc_omega{ omega, h, sycl::no_init };
h.single_task([=]() { acc_omega[0] = get_root_of_unity(n); });
});
return evt;
}
sycl::event
compute_omega_inv(sycl::queue& q, buf_1d_u64_t& omega_inv, const uint64_t n)
{
sycl::event evt = q.submit([&](sycl::handler& h) {
buf_1d_u64_wr_t acc_omega_inv{ omega_inv, h, sycl::no_init };
h.single_task([=]() { acc_omega_inv[0] = ff_p_inv(get_root_of_unity(n)); });
});
return evt;
}
sycl::event
compute_dft_matrix(sycl::queue& q,
buf_2d_u64_t& mat,
buf_1d_u64_t& omega,
const uint64_t dim,
const uint64_t wg_size)
{
q.submit([&](sycl::handler& h) {
buf_2d_u64_wr_t acc_mat{ mat, h, sycl::no_init };
buf_1d_u64_rd_t acc_omega{ omega, h };
sycl::
accessor<uint64_t, 1, sycl::access_mode::read_write, sycl::target::local>
loc_acc_omega{ sycl::range<1>{ 1 }, h };
h.parallel_for<class kernelPowerSeriesOfOmega>(
sycl::nd_range<1>{ sycl::range<1>{ dim }, sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) {
sycl::sub_group sg = it.get_sub_group();
const uint64_t c = it.get_global_id(0);
if (sycl::ext::oneapi::leader(sg)) {
loc_acc_omega[0] = acc_omega[0];
}
it.barrier(sycl::access::fence_space::local_space);
acc_mat[0][c] = 1ul;
acc_mat[1][c] = ff_p_pow(loc_acc_omega[0], c);
});
});
sycl::event evt = q.submit([&](sycl::handler& h) {
buf_2d_u64_rw_t acc_mat{ mat, h };
h.parallel_for<class kernelComputeDFTMatrix>(
sycl::nd_range<2>{ sycl::range<2>{ dim, dim },
sycl::range<2>{ 1, wg_size } },
[=](sycl::nd_item<2> it) {
const uint64_t r = it.get_global_id(0);
const uint64_t c = it.get_global_id(1);
if (r < 2) {
return;
}
acc_mat[r][c] = ff_p_pow(acc_mat[1][c], r);
});
});
return evt;
}
sycl::event
compute_matrix_vector_multiplication(sycl::queue& q,
buf_2d_u64_t& mat,
buf_1d_u64_t& vec,
buf_1d_u64_t& res,
const uint64_t dim,
const uint64_t wg_size)
{
sycl::event evt = q.submit([&](sycl::handler& h) {
buf_2d_u64_rd_t acc_mat{ mat, h };
buf_1d_u64_rd_t acc_vec{ vec, h };
buf_1d_u64_rw_t acc_res{ res, h, sycl::no_init };
h.parallel_for<class kernelComputeDFTMatrixVectorMultipication>(
sycl::nd_range<1>{ sycl::range<1>{ dim }, sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) {
sycl::sub_group sg = it.get_sub_group();
const uint64_t r = it.get_global_id(0);
uint64_t sum = 0ul;
for (uint64_t c = 0; c < dim; c++) {
sum = ff_p_add(
sum,
ff_p_mult(acc_mat[r][c], sycl::group_broadcast(sg, acc_vec[c])));
}
acc_res[r] = sum;
});
});
return evt;
}
sycl::event
compute_vector_scalar_multilication(sycl::queue& q,
buf_1d_u64_t& vec,
const uint64_t factor,
const uint64_t dim,
const uint64_t wg_size)
{
sycl::event evt = q.submit([&](sycl::handler& h) {
buf_1d_u64_rw_t acc_vec{ vec, h };
h.parallel_for<class kernelInvDFTScalarMultiplication>(
sycl::nd_range<1>{ sycl::range<1>{ dim }, sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) {
const size_t r = it.get_global_id(0);
acc_vec[r] = ff_p_mult(acc_vec[r], factor);
});
});
return evt;
}
void
forward_transform(sycl::queue& q,
buf_1d_u64_t& vec,
buf_1d_u64_t& res,
const uint64_t dim,
const uint64_t wg_size)
{
// size of input vector must be power of two !
assert((dim & (dim - 1ul)) == 0);
uint64_t log_2_dim = (uint64_t)sycl::log2((float)dim);
// order can't exceed 2 ** 32 and can't also
// find root of unity for n = 0
assert(log_2_dim > 0 && log_2_dim <= TWO_ADICITY);
uint64_t* omega = static_cast<uint64_t*>(malloc(sizeof(uint64_t)));
uint64_t* mat = static_cast<uint64_t*>(malloc(sizeof(uint64_t) * dim * dim));
// putting actual computation in block so that
// allocated memory can be safely freed before
// returning control back from function
{
buf_1d_u64_t buf_omega{ omega, sycl::range<1>{ 1 } };
buf_2d_u64_t buf_mat{ mat, sycl::range<2>{ dim, dim } };
compute_omega(q, buf_omega, log_2_dim);
compute_dft_matrix(q, buf_mat, buf_omega, dim, wg_size);
compute_matrix_vector_multiplication(q, buf_mat, vec, res, dim, wg_size)
.wait();
}
std::free(omega);
std::free(mat);
}
void
inverse_transform(sycl::queue& q,
buf_1d_u64_t& vec,
buf_1d_u64_t& res,
const uint64_t dim,
const uint64_t wg_size)
{
// size of input vector must be power of two !
assert((dim & (dim - 1ul)) == 0);
uint64_t log_2_dim = (uint64_t)sycl::log2((float)dim);
// order can't exceed 2 ** 32 and can't also
// find root of unity for n = 0
assert(log_2_dim > 0 && log_2_dim <= TWO_ADICITY);
uint64_t* omega_inv = static_cast<uint64_t*>(malloc(sizeof(uint64_t)));
uint64_t* mat = static_cast<uint64_t*>(malloc(sizeof(uint64_t) * dim * dim));
// putting actual computation in block so that
// allocated memory can be safely freed before
// returning control back from function
{
buf_1d_u64_t buf_omega_inv{ omega_inv, sycl::range<1>{ 1 } };
buf_2d_u64_t buf_mat{ mat, sycl::range<2>{ dim, dim } };
compute_omega_inv(q, buf_omega_inv, log_2_dim);
compute_dft_matrix(q, buf_mat, buf_omega_inv, dim, wg_size);
compute_matrix_vector_multiplication(q, buf_mat, vec, res, dim, wg_size);
uint64_t inv_dim = 0ul;
{
buf_1d_u64_t buf_inv_dim{ &inv_dim, sycl::range<1>{ 1 } };
q.submit([&](sycl::handler& h) {
buf_1d_u64_wr_t acc_inv_dim{ buf_inv_dim, h };
h.single_task([=]() { acc_inv_dim[0] = ff_p_inv(dim); });
})
.wait();
}
compute_vector_scalar_multilication(q, res, inv_dim, dim, wg_size).wait();
}
std::free(omega_inv);
std::free(mat);
}
uint64_t
bit_rev(uint64_t v, uint64_t max_bit_width)
{
uint64_t v_rev = 0ul;
for (uint64_t i = 0; i < max_bit_width; i++) {
v_rev += ((v >> i) & 0b1) * (1ul << (max_bit_width - 1ul - i));
}
return v_rev;
}
uint64_t
rev_all_bits(uint64_t n)
{
uint64_t rev = 0;
for (uint8_t i = 0; i < 64; i++) {
if ((1ul << i) & n) {
rev |= (1ul << (63 - i));
}
}
return rev;
}
uint64_t
permute_index(uint64_t idx, uint64_t size)
{
if (size == 1ul) {
return 0ul;
}
uint64_t bits = sycl::ext::intel::ctz(size);
return rev_all_bits(idx) >> (64ul - bits);
}
void
cooley_tukey_fft(sycl::queue& q,
buf_1d_u64_t& vec,
buf_1d_u64_t& res,
const uint64_t dim,
const uint64_t wg_size)
{
assert((dim & (dim - 1ul)) == 0);
uint64_t log_2_dim = (uint64_t)sycl::log2((float)dim);
assert(log_2_dim > 0 && log_2_dim <= TWO_ADICITY);
uint64_t* omega = static_cast<uint64_t*>(malloc(sizeof(uint64_t)));
{
// if you change this number, make sure
// you also change `[[intel::reqd_sub_group_size(Z)]]`
// below, such that SUBGROUP_SIZE == Z
constexpr uint64_t SUBGROUP_SIZE = 1ul << 5;
// don't change following assertions !
assert((SUBGROUP_SIZE & (SUBGROUP_SIZE - 1ul)) == 0ul &&
(SUBGROUP_SIZE <= (1ul << 6)));
assert((wg_size % SUBGROUP_SIZE) == 0);
buf_1d_u64_t buf_omega{ omega, sycl::range<1>{ 1 } };
buf_omega.set_write_back(false);
compute_omega(q, buf_omega, log_2_dim);
q.submit([&](sycl::handler& h) {
buf_1d_u64_rd_t acc_vec{ vec, h };
buf_1d_u64_wr_t acc_res{ res, h, sycl::no_init };
h.copy(acc_vec, acc_res);
});
for (int64_t i = log_2_dim - 1ul; i >= 0; i--) {
q.submit([&](sycl::handler& h) {
buf_1d_u64_rd_t acc_omega{ buf_omega, h };
buf_1d_u64_rw_t acc_res{ res, h };
if ((1ul << i) >= SUBGROUP_SIZE) {
h.parallel_for<class kernelCooleyTukeyGMFFT>(
sycl::nd_range<1>{ sycl::range<1>{ dim },
sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) [[intel::reqd_sub_group_size(32)]] {
sycl::sub_group sg = it.get_sub_group();
const uint64_t k = it.get_global_id(0);
const uint64_t p = 1ul << i;
const uint64_t q = dim / p;
uint64_t k_rev = bit_rev(k, log_2_dim) % q;
uint64_t ω =
ff_p_pow(sycl::group_broadcast(sg, acc_omega[0]), p * k_rev);
if (k < (k ^ p)) {
uint64_t tmp_k = acc_res[k];
uint64_t tmp_k_p = acc_res[k ^ p];
uint64_t tmp_k_p_ω = ff_p_mult(tmp_k_p, ω);
acc_res[k] = ff_p_add(tmp_k, tmp_k_p_ω);
acc_res[k ^ p] = ff_p_sub(tmp_k, tmp_k_p_ω);
}
});
} else {
// note, subgroup based shuffling implementation
// takes inspiration from https://arxiv.org/pdf/2109.14704.pdf
h.parallel_for<class kernelCooleyTukeySGFFT>(
sycl::nd_range<1>{ sycl::range<1>{ dim },
sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) [[intel::reqd_sub_group_size(32)]] {
sycl::sub_group sg = it.get_sub_group();
const uint64_t k = it.get_global_id(0);
const uint64_t p = 1ul << i; // mask for subgroup shuffle_xor
const uint64_t q = dim / p;
uint64_t k_rev = bit_rev(k, log_2_dim) % q;
uint64_t ω =
ff_p_pow(sycl::group_broadcast(sg, acc_omega[0]), p * k_rev);
uint64_t tmp_k = acc_res[k];
// obtain value with work-item index (k ^ p)
uint64_t tmp_k_p = sg.shuffle_xor(tmp_k, p);
// obtain ω of work-item with index (k ^ p)
uint64_t ω_ = sg.shuffle_xor(ω, p);
acc_res[k] = k < (k ^ p)
? ff_p_add(tmp_k, ff_p_mult(tmp_k_p, ω))
: ff_p_sub(tmp_k_p, ff_p_mult(tmp_k, ω_));
});
}
});
}
q.submit([&](sycl::handler& h) {
buf_1d_u64_rw_t acc_res{ res, h };
h.parallel_for<class kernelCooleyTukeyFFTFinalReorder>(
sycl::nd_range<1>{ sycl::range<1>{ dim }, sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) {
const uint64_t k = it.get_global_id(0);
const uint64_t k_perm = permute_index(k, dim);
if (k_perm > k) {
uint64_t a = acc_res[k];
uint64_t b = acc_res[k_perm];
a ^= b;
b ^= a;
a ^= b;
acc_res[k] = a;
acc_res[k_perm] = b;
}
});
})
.wait();
}
std::free(omega);
}
void
cooley_tukey_ifft(sycl::queue& q,
buf_1d_u64_t& vec,
buf_1d_u64_t& res,
const uint64_t dim,
const uint64_t wg_size)
{
assert((dim & (dim - 1ul)) == 0);
uint64_t log_2_dim = (uint64_t)sycl::log2((float)dim);
assert(log_2_dim > 0 && log_2_dim <= TWO_ADICITY);
uint64_t* omega_inv = static_cast<uint64_t*>(malloc(sizeof(uint64_t)));
uint64_t* dim_inv = static_cast<uint64_t*>(malloc(sizeof(uint64_t)));
{
// if you change this number, make sure
// you also change `[[intel::reqd_sub_group_size(Z)]]`
// below, such that SUBGROUP_SIZE == Z
constexpr uint64_t SUBGROUP_SIZE = 1ul << 5;
// don't change following assertions !
assert((SUBGROUP_SIZE & (SUBGROUP_SIZE - 1ul)) == 0ul &&
(SUBGROUP_SIZE <= (1ul << 6)));
assert((wg_size % SUBGROUP_SIZE) == 0);
buf_1d_u64_t buf_omega_inv{ omega_inv, sycl::range<1>{ 1 } };
buf_1d_u64_t buf_dim_inv{ dim_inv, sycl::range<1>{ 1 } };
buf_omega_inv.set_write_back(false);
buf_dim_inv.set_write_back(false);
compute_omega_inv(q, buf_omega_inv, log_2_dim);
q.submit([&](sycl::handler& h) {
buf_1d_u64_wr_t acc_inv_dim{ buf_dim_inv, h };
h.single_task([=]() { acc_inv_dim[0] = ff_p_inv(dim); });
});
q.submit([&](sycl::handler& h) {
buf_1d_u64_rd_t acc_vec{ vec, h };
buf_1d_u64_wr_t acc_res{ res, h, sycl::no_init };
h.copy(acc_vec, acc_res);
});
for (int64_t i = log_2_dim - 1ul; i >= 0; i--) {
q.submit([&](sycl::handler& h) {
buf_1d_u64_rd_t acc_omega_inv{ buf_omega_inv, h };
buf_1d_u64_rw_t acc_res{ res, h };
if ((1ul << i) >= SUBGROUP_SIZE) {
h.parallel_for<class kernelCooleyTukeyGMIFFT>(
sycl::nd_range<1>{ sycl::range<1>{ dim },
sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) [[intel::reqd_sub_group_size(32)]] {
sycl::sub_group sg = it.get_sub_group();
const uint64_t k = it.get_global_id(0);
const uint64_t p = 1ul << i;
const uint64_t q = dim / p;
uint64_t k_rev = bit_rev(k, log_2_dim) % q;
uint64_t ω = ff_p_pow(sycl::group_broadcast(sg, acc_omega_inv[0]),
p * k_rev);
if (k < (k ^ p)) {
uint64_t tmp_k = acc_res[k];
uint64_t tmp_k_p = acc_res[k ^ p];
uint64_t tmp_k_p_ω = ff_p_mult(tmp_k_p, ω);
acc_res[k] = ff_p_add(tmp_k, tmp_k_p_ω);
acc_res[k ^ p] = ff_p_sub(tmp_k, tmp_k_p_ω);
}
});
} else {
// note, subgroup based shuffling implementation
// takes inspiration from https://arxiv.org/pdf/2109.14704.pdf
h.parallel_for<class kernelCooleyTukeySGIFFT>(
sycl::nd_range<1>{ sycl::range<1>{ dim },
sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) [[intel::reqd_sub_group_size(32)]] {
sycl::sub_group sg = it.get_sub_group();
const uint64_t k = it.get_global_id(0);
const uint64_t p = 1ul << i; // mask for subgroup shuffle_xor
const uint64_t q = dim / p;
uint64_t k_rev = bit_rev(k, log_2_dim) % q;
uint64_t ω = ff_p_pow(sycl::group_broadcast(sg, acc_omega_inv[0]),
p * k_rev);
uint64_t tmp_k = acc_res[k];
// obtain value with work-item index (k ^ p)
uint64_t tmp_k_p = sg.shuffle_xor(tmp_k, p);
// obtain ω of work-item with index (k ^ p)
uint64_t ω_ = sg.shuffle_xor(ω, p);
acc_res[k] = k < (k ^ p)
? ff_p_add(tmp_k, ff_p_mult(tmp_k_p, ω))
: ff_p_sub(tmp_k_p, ff_p_mult(tmp_k, ω_));
});
}
});
}
q.submit([&](sycl::handler& h) {
buf_1d_u64_rw_t acc_res{ res, h };
buf_1d_u64_rd_t acc_inv_dim{ buf_dim_inv, h };
h.parallel_for<class kernelCooleyTukeyIFFTFinalReorder>(
sycl::nd_range<1>{ sycl::range<1>{ dim }, sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) {
sycl::sub_group sg = it.get_sub_group();
uint64_t inv_dim = sycl::group_broadcast(sg, acc_inv_dim[0]);
const uint64_t k = it.get_global_id(0);
const uint64_t k_perm = permute_index(k, dim);
if (k_perm == k) {
acc_res[k] = ff_p_mult(acc_res[k], inv_dim);
} else if (k_perm > k) {
uint64_t a = acc_res[k];
uint64_t b = acc_res[k_perm];
a ^= b;
b ^= a;
a ^= b;
acc_res[k] = ff_p_mult(a, inv_dim);
acc_res[k_perm] = ff_p_mult(b, inv_dim);
}
});
})
.wait();
}
std::free(omega_inv);
std::free(dim_inv);
}
sycl::event
matrix_transpose(sycl::queue& q,
uint64_t* data,
const uint64_t dim,
std::vector<sycl::event> evts)
{
constexpr size_t TILE_DIM = 1 << 5;
constexpr size_t BLOCK_ROWS = 1 << 3;
assert(TILE_DIM >= BLOCK_ROWS);
return q.submit([&](sycl::handler& h) {
sycl::
accessor<uint64_t, 2, sycl::access_mode::read_write, sycl::target::local>
tile_s{ sycl::range<2>{ TILE_DIM, TILE_DIM + 1 }, h };
sycl::
accessor<uint64_t, 2, sycl::access_mode::read_write, sycl::target::local>
tile_d{ sycl::range<2>{ TILE_DIM, TILE_DIM + 1 }, h };
h.depends_on(evts);
h.parallel_for<class kernelMatrixTransposition>(
sycl::nd_range<2>{ sycl::range<2>{ dim / (TILE_DIM / BLOCK_ROWS), dim },
sycl::range<2>{ BLOCK_ROWS, TILE_DIM } },
[=](sycl::nd_item<2> it) {
const size_t grp_id_x = it.get_group().get_id(1);
const size_t grp_id_y = it.get_group().get_id(0);
const size_t loc_id_x = it.get_local_id(1);
const size_t loc_id_y = it.get_local_id(0);
const size_t grp_width_x = it.get_group().get_group_range(1);
// @note x denotes index along x-axis
// while y denotes index along y-axis
//
// so in usual (row, col) indexing of 2D array
// row = y, col = x
const size_t x = grp_id_x * TILE_DIM + loc_id_x;
const size_t y = grp_id_y * TILE_DIM + loc_id_y;
const size_t width = grp_width_x * TILE_DIM;
if (grp_id_y > grp_id_x) {
size_t dx = grp_id_y * TILE_DIM + loc_id_x;
size_t dy = grp_id_x * TILE_DIM + loc_id_y;
for (size_t j = 0; j < TILE_DIM; j += BLOCK_ROWS) {
tile_s[loc_id_y + j][loc_id_x] = *(data + (y + j) * width + x);
}
for (size_t j = 0; j < TILE_DIM; j += BLOCK_ROWS) {
tile_d[loc_id_y + j][loc_id_x] = *(data + (dy + j) * width + dx);
}
it.barrier(sycl::access::fence_space::local_space);
for (size_t j = 0; j < TILE_DIM; j += BLOCK_ROWS) {
*(data + (dy + j) * width + dx) = tile_s[loc_id_x][loc_id_y + j];
}
for (size_t j = 0; j < TILE_DIM; j += BLOCK_ROWS) {
*(data + (y + j) * width + x) = tile_d[loc_id_x][loc_id_y + j];
}
return;
}
if (grp_id_y == grp_id_x) {
for (size_t j = 0; j < TILE_DIM; j += BLOCK_ROWS) {
tile_s[loc_id_y + j][loc_id_x] = *(data + (y + j) * width + x);
}
it.barrier(sycl::access::fence_space::local_space);
for (size_t j = 0; j < TILE_DIM; j += BLOCK_ROWS) {
*(data + (y + j) * width + x) = tile_s[loc_id_x][loc_id_y + j];
}
}
});
});
}
sycl::event
matrix_transposed_initialise(sycl::queue& q,
uint64_t* vec_src,
uint64_t* vec_dst,
const uint64_t rows,
const uint64_t cols,
const uint64_t width,
const uint64_t wg_size,
std::vector<sycl::event> evts)
{
return q.submit([&](sycl::handler& h) {
h.depends_on(evts);
h.parallel_for<class kernelMatrixTransposedInitialise>(
sycl::nd_range<2>{ sycl::range<2>{ rows, cols },
sycl::range<2>{ 1, wg_size } },
[=](sycl::nd_item<2> it) {
sycl::sub_group sg = it.get_sub_group();
const size_t r = it.get_global_id(0);
const size_t c = it.get_global_id(1);
// read into work-item's private memory, using
// subgroup collective function
const uint64_t width_ = sycl::group_broadcast(sg, width);
*(vec_dst + r * width_ + c) = *(vec_src + c * width_ + r);
});
});
}
sycl::event
row_wise_transform(sycl::queue& q,
uint64_t* vec,
uint64_t* omega,
const uint64_t rows,
const uint64_t cols,
const uint64_t width,
const uint64_t wg_size,
std::vector<sycl::event> evts)
{
uint64_t log_2_dim = (uint64_t)sycl::log2((float)cols);
std::vector<sycl::event> _evts;
_evts.reserve(log_2_dim);
// if you change this number, make sure
// you also change `[[intel::reqd_sub_group_size(Z)]]`
// below, such that SUBGROUP_SIZE == Z
constexpr uint64_t SUBGROUP_SIZE = 1ul << 5;
// don't change following assertions !
assert((SUBGROUP_SIZE & (SUBGROUP_SIZE - 1ul)) == 0ul &&
(SUBGROUP_SIZE <= (1ul << 6)));
assert((wg_size % SUBGROUP_SIZE) == 0);
for (int64_t i = log_2_dim - 1ul; i >= 0; i--) {
sycl::event evt = q.submit([&](sycl::handler& h) {
if (i == log_2_dim - 1ul) {
// only first submission depends on
// previous kernel executions, whose events
// are passed as argument to this function
h.depends_on(evts);
} else {
// all next kernel submissions
// depend on just previous kernel submission
h.depends_on(_evts.at(log_2_dim - (i + 2)));
}
if ((1ul << i) >= SUBGROUP_SIZE) {
// cooley tukey based NTT implementation, with global memory
// based communication ( relatively slow ! )
h.parallel_for<class kernelCooleyTukeyRowWiseGMFFT>(
sycl::nd_range<2>{ sycl::range<2>{ rows, cols },
sycl::range<2>{ 1, wg_size } },
[=](sycl::nd_item<2> it) [[intel::reqd_sub_group_size(32)]] {
sycl::sub_group sg = it.get_sub_group();
const uint64_t r = it.get_global_id(0);
const uint64_t k = it.get_global_id(1);
const uint64_t p = 1ul << i;
const uint64_t q = cols / p;
uint64_t k_rev = bit_rev(k, log_2_dim) % q;
uint64_t ω = ff_p_pow(sycl::group_broadcast(sg, *omega), p * k_rev);
if (k < (k ^ p)) {
uint64_t tmp_k = *(vec + r * width + k);
uint64_t tmp_k_p = *(vec + r * width + (k ^ p));
uint64_t tmp_k_p_ω = ff_p_mult(tmp_k_p, ω);
*(vec + r * width + k) = ff_p_add(tmp_k, tmp_k_p_ω);
*(vec + r * width + (k ^ p)) = ff_p_sub(tmp_k, tmp_k_p_ω);
}
});
} else {
// cooley tukey based NTT implementation, with subgroup
// based shuffling ( much faster than global memory based ! )
//
// but I can't use this variant for all iterations, as
// subgroup size is not always larger than (1ul << i)
//
// note, (1ul << i) is NTT butterfly gap for iteration `i`
//
// only when control flow reaches this conditional block
// NTT butterfly shuffling will be in a subgroup itself
//
// so in that case, I get to enjoy faster subgroup collective
// functions, which make use of SIMD in better way
//
// note, subgroup based shuffling implementation
// takes inspiration from https://arxiv.org/pdf/2109.14704.pdf
h.parallel_for<class kernelCooleyTukeyRowWiseSGFFT>(
sycl::nd_range<2>{ sycl::range<2>{ rows, cols },
sycl::range<2>{ 1, wg_size } },
[=](sycl::nd_item<2> it) [[intel::reqd_sub_group_size(32)]] {
sycl::sub_group sg = it.get_sub_group();
const uint64_t r = it.get_global_id(0);
const uint64_t k = it.get_global_id(1);
const uint64_t p = 1ul << i; // mask for subgroup shuffling
const uint64_t q = cols / p;
uint64_t k_rev = bit_rev(k, log_2_dim) % q;
uint64_t ω = ff_p_pow(sycl::group_broadcast(sg, *omega), p * k_rev);
uint64_t tmp_k = *(vec + r * width + k);
uint64_t tmp_k_p = sg.shuffle_xor(tmp_k, p);
uint64_t ω_ = sg.shuffle_xor(ω, p);
*(vec + r * width + k) =
k < (k ^ p) ? ff_p_add(tmp_k, ff_p_mult(tmp_k_p, ω))
: ff_p_sub(tmp_k_p, ff_p_mult(tmp_k, ω_));
// if (k % p == k % (2 * p)) {
// uint64_t tmp_k = *(vec + r * width + k);
// uint64_t tmp_k_p = *(vec + r * width + k + p);
// uint64_t tmp_k_p_ω = ff_p_mult(tmp_k_p, ω);
// *(vec + r * width + k) = ff_p_add(tmp_k, tmp_k_p_ω);
// *(vec + r * width + k + p) = ff_p_sub(tmp_k, tmp_k_p_ω);
// }
});
}
});
_evts.push_back(evt);
}
return q.submit([&](sycl::handler& h) {
// final reordering kernel depends on very
// last kernel submission performed in above loop
h.depends_on(_evts.at(log_2_dim - 1));
h.parallel_for<class kernelCooleyTukeyRowWiseFFTFinalReorder>(
sycl::nd_range<2>{ sycl::range<2>{ rows, cols },
sycl::range<2>{ 1, wg_size } },
[=](sycl::nd_item<2> it) {
const uint64_t r = it.get_global_id(0);
const uint64_t k = it.get_global_id(1);
const uint64_t k_perm = permute_index(k, cols);
if (k_perm > k) {
uint64_t a = *(vec + r * width + k);
uint64_t b = *(vec + r * width + k_perm);
a ^= b;
b ^= a;
a ^= b;
*(vec + r * width + k) = a;
*(vec + r * width + k_perm) = b;
}
});
});
}
sycl::event
compute_twiddles(sycl::queue& q,
uint64_t* twiddles,
uint64_t* omega,
const uint64_t dim,
const uint64_t wg_size,
std::vector<sycl::event> evts)
{
return q.submit([&](sycl::handler& h) {
h.depends_on(evts);
h.parallel_for(
sycl::nd_range<1>{ sycl::range<1>{ dim }, sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) {
sycl::sub_group sg = it.get_sub_group();
const uint64_t c = it.get_global_id(0);
*(twiddles + c) = ff_p_pow(sycl::group_broadcast(sg, *omega), c);
});
});
}
sycl::event
twiddle_multiplication(sycl::queue& q,
uint64_t* vec,
uint64_t* twiddles,
const uint64_t rows,
const uint64_t cols,
const uint64_t width,
const uint64_t wg_size,
std::vector<sycl::event> evts)
{
assert(cols == width || 2 * cols == width);
return q.submit([&](sycl::handler& h) {
h.depends_on(evts);
h.parallel_for<class kernelTwiddleFactorMultiplication>(
sycl::nd_range<2>{ sycl::range<2>{ rows, cols },
sycl::range<2>{ 1, wg_size } },
[=](sycl::nd_item<2> it) {
sycl::sub_group sg = it.get_sub_group();
const uint64_t r = it.get_global_id(0);
const uint64_t c = it.get_global_id(1);
*(vec + r * width + c) =
ff_p_mult(*(vec + r * width + c),
ff_p_pow(sycl::group_broadcast(sg, *(twiddles + r)), c));
});
});
}
void
six_step_fft(sycl::queue& q,
uint64_t* vec,
const uint64_t dim,
const uint64_t wg_size)
{
assert((dim & (dim - 1ul)) == 0);
uint64_t log_2_dim = (uint64_t)sycl::log2((float)dim);
uint64_t n1 = 1 << (log_2_dim / 2);
uint64_t n2 = dim / n1;
uint64_t n = sycl::max(n1, n2);
uint64_t log_2_n1 = (uint64_t)sycl::log2((float)n1);
uint64_t log_2_n2 = (uint64_t)sycl::log2((float)n2);
assert(n1 == n2 || n2 == 2 * n1);
assert(log_2_dim > 0 && log_2_dim <= TWO_ADICITY);
uint64_t* vec_ =
static_cast<uint64_t*>(sycl::malloc_device(sizeof(uint64_t) * n * n, q));
uint64_t* twiddles =
static_cast<uint64_t*>(sycl::malloc_device(sizeof(uint64_t) * n2, q));
uint64_t* omega_dim =
static_cast<uint64_t*>(sycl::malloc_device(sizeof(uint64_t), q));
uint64_t* omega_n1 =
static_cast<uint64_t*>(sycl::malloc_device(sizeof(uint64_t), q));
uint64_t* omega_n2 =
static_cast<uint64_t*>(sycl::malloc_device(sizeof(uint64_t), q));
// compute i-th root of unity, where n = {dim, n1, n2}
sycl::event evt_0 =
q.single_task([=]() { *omega_dim = get_root_of_unity(log_2_dim); });
sycl::event evt_1 =
q.single_task([=]() { *omega_n1 = get_root_of_unity(log_2_n1); });
sycl::event evt_2 =
q.single_task([=]() { *omega_n2 = get_root_of_unity(log_2_n2); });
// Step 1: Transpose Matrix
sycl::event evt_3 =
matrix_transposed_initialise(q, vec, vec_, n2, n1, n, wg_size, {});
// Step 2: n2-many parallel n1-point Cooley-Tukey style FFT
sycl::event evt_4 =
row_wise_transform(q, vec_, omega_n1, n2, n1, n, wg_size, { evt_1, evt_3 });
// Step 3: Multiply by twiddle factors
sycl::event evt_5 =
compute_twiddles(q, twiddles, omega_dim, n2, wg_size, { evt_0 });
sycl::event evt_6 = twiddle_multiplication(
q, vec_, twiddles, n2, n1, n, wg_size, { evt_4, evt_5 });
// Step 4: Transpose Matrix
sycl::event evt_7 = matrix_transpose(q, vec_, n, { evt_6 });
// Step 5: n1-many parallel n2-point Cooley-Tukey FFT
sycl::event evt_8 =
row_wise_transform(q, vec_, omega_n2, n1, n2, n, wg_size, { evt_2, evt_7 });
// Step 6: Transpose Matrix
sycl::event evt_9 = matrix_transpose(q, vec_, n, { evt_8 });
// copy result back to source matrix
sycl::event evt_10 = q.submit([&](sycl::handler& h) {
h.depends_on(evt_9);
h.parallel_for<class kernelFFTCopyBack>(
sycl::nd_range<2>{ sycl::range<2>{ n2, n1 },
sycl::range<2>{ 1, wg_size } },
[=](sycl::nd_item<2> it) {
const size_t r = it.get_global_id(0);
const size_t c = it.get_global_id(1);
*(vec + it.get_global_linear_id()) = *(vec_ + r * n + c);
});
});
evt_10.wait();
sycl::free(vec_, q);
sycl::free(twiddles, q);
sycl::free(omega_dim, q);
sycl::free(omega_n1, q);
sycl::free(omega_n2, q);
}
void
six_step_ifft(sycl::queue& q,
uint64_t* vec,
const uint64_t dim,
const uint64_t wg_size)
{
assert((dim & (dim - 1ul)) == 0);
uint64_t log_2_dim = (uint64_t)sycl::log2((float)dim);
uint64_t n1 = 1 << (log_2_dim / 2);
uint64_t n2 = dim / n1;
uint64_t n = sycl::max(n1, n2);
uint64_t log_2_n1 = (uint64_t)sycl::log2((float)n1);
uint64_t log_2_n2 = (uint64_t)sycl::log2((float)n2);
assert(n1 == n2 || n2 == 2 * n1);
assert(log_2_dim > 0 && log_2_dim <= TWO_ADICITY);
uint64_t* vec_ =
static_cast<uint64_t*>(sycl::malloc_device(sizeof(uint64_t) * n * n, q));
uint64_t* twiddles =
static_cast<uint64_t*>(sycl::malloc_device(sizeof(uint64_t) * n2, q));
uint64_t* omega_dim_inv =
static_cast<uint64_t*>(sycl::malloc_device(sizeof(uint64_t), q));
uint64_t* omega_n1_inv =
static_cast<uint64_t*>(sycl::malloc_device(sizeof(uint64_t), q));
uint64_t* omega_n2_inv =
static_cast<uint64_t*>(sycl::malloc_device(sizeof(uint64_t), q));
uint64_t* omega_domain_size_inv =
static_cast<uint64_t*>(sycl::malloc_device(sizeof(uint64_t), q));
// compute inverse of i-th root of unity, where n = {dim, n1, n2}
sycl::event evt_0 = q.single_task(
[=]() { *omega_dim_inv = ff_p_inv(get_root_of_unity(log_2_dim)); });
sycl::event evt_1 = q.single_task(
[=]() { *omega_n1_inv = ff_p_inv(get_root_of_unity(log_2_n1)); });
sycl::event evt_2 = q.single_task(
[=]() { *omega_n2_inv = ff_p_inv(get_root_of_unity(log_2_n2)); });
sycl::event evt_3 =
q.single_task([=]() { *omega_domain_size_inv = ff_p_inv(dim); });
// Step 1: Transpose Matrix
sycl::event evt_4 =
matrix_transposed_initialise(q, vec, vec_, n2, n1, n, wg_size, {});
// Step 2: n2-many parallel n1-point Cooley-Tukey style IFFT
sycl::event evt_5 = row_wise_transform(
q, vec_, omega_n1_inv, n2, n1, n, wg_size, { evt_1, evt_4 });
// Step 3: Multiply by twiddle factors
sycl::event evt_6 =
compute_twiddles(q, twiddles, omega_dim_inv, n2, wg_size, { evt_0 });
sycl::event evt_7 = twiddle_multiplication(
q, vec_, twiddles, n2, n1, n, wg_size, { evt_5, evt_6 });
// Step 4: Transpose Matrix
sycl::event evt_8 = matrix_transpose(q, vec_, n, { evt_7 });
// Step 5: n1-many parallel n2-point Cooley-Tukey IFFT
sycl::event evt_9 = row_wise_transform(
q, vec_, omega_n2_inv, n1, n2, n, wg_size, { evt_2, evt_8 });
// Step 6: Transpose Matrix
sycl::event evt_10 = matrix_transpose(q, vec_, n, { evt_9 });
// copy result back to source matrix, while
// also multiplying by inverse of domain size
sycl::event evt_11 = q.submit([&](sycl::handler& h) {
h.depends_on({ evt_3, evt_10 });
h.parallel_for<class kernelIFFTCopyBack>(
sycl::nd_range<2>{ sycl::range<2>{ n2, n1 },
sycl::range<2>{ 1, wg_size } },
[=](sycl::nd_item<2> it) {
sycl::sub_group sg = it.get_sub_group();
const size_t r = it.get_global_id(0);
const size_t c = it.get_global_id(1);
*(vec + it.get_global_linear_id()) =
ff_p_mult(sycl::group_broadcast(sg, *omega_domain_size_inv),
*(vec_ + r * n + c));
});
});
evt_11.wait();
sycl::free(vec_, q);
sycl::free(twiddles, q);
sycl::free(omega_dim_inv, q);
sycl::free(omega_n1_inv, q);
sycl::free(omega_n2_inv, q);
sycl::free(omega_domain_size_inv, q);