-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathcsr_kernels.dp.cpp
More file actions
3114 lines (2802 loc) · 128 KB
/
Copy pathcsr_kernels.dp.cpp
File metadata and controls
3114 lines (2802 loc) · 128 KB
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
// SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause
#include "core/matrix/csr_kernels.hpp"
#include <algorithm>
#include <optional>
#include <oneapi/mkl.hpp>
#include <sycl/sycl.hpp>
#include <ginkgo/core/base/array.hpp>
#include <ginkgo/core/base/exception_helpers.hpp>
#include <ginkgo/core/base/math.hpp>
#include <ginkgo/core/base/std_extensions.hpp>
#include <ginkgo/core/matrix/sellp.hpp>
#include "accessor/sycl_helper.hpp"
#include "core/base/array_access.hpp"
#include "core/base/index_range.hpp"
#include "core/base/mixed_precision_types.hpp"
#include "core/base/utils.hpp"
#include "core/components/fill_array_kernels.hpp"
#include "core/components/prefix_sum_kernels.hpp"
#include "core/matrix/csr_accessor_helper.hpp"
#include "core/matrix/csr_builder.hpp"
#include "core/matrix/dense_kernels.hpp"
#include "core/synthesizer/implementation_selection.hpp"
#include "dpcpp/base/config.hpp"
#include "dpcpp/base/dim3.dp.hpp"
#include "dpcpp/base/dpct.hpp"
#include "dpcpp/base/helper.hpp"
#include "dpcpp/base/math.hpp"
#include "dpcpp/base/onemkl_bindings.hpp"
#include "dpcpp/base/types.hpp"
#include "dpcpp/components/atomic.dp.hpp"
#include "dpcpp/components/cooperative_groups.dp.hpp"
#include "dpcpp/components/reduction.dp.hpp"
#include "dpcpp/components/segment_scan.dp.hpp"
#include "dpcpp/components/thread_ids.dp.hpp"
#include "dpcpp/components/uninitialized_array.hpp"
namespace gko {
namespace kernels {
namespace dpcpp {
/**
* @brief The Compressed sparse row matrix format namespace.
*
* @ingroup csr
*/
namespace csr {
constexpr int default_block_size = 256;
constexpr int warps_in_block = 4;
constexpr int spmv_block_size = warps_in_block * config::warp_size;
constexpr int classical_oversubscription = 32;
/**
* A compile-time list of the number items per threads for which spmv kernel
* should be compiled.
*/
using compiled_kernels = syn::value_list<int, 6>;
using classical_kernels = syn::value_list<int, config::warp_size, 16, 1>;
namespace kernel {
template <typename T>
__dpct_inline__ T ceildivT(T nom, T denom)
{
return (nom + denom - 1ll) / denom;
}
template <typename ValueType, typename IndexType>
__dpct_inline__ bool block_segment_scan_reverse(
const IndexType* __restrict__ ind, ValueType* __restrict__ val,
sycl::nd_item<3> item_ct1)
{
bool last = true;
const auto reg_ind = ind[item_ct1.get_local_id(2)];
#pragma unroll
for (int i = 1; i < spmv_block_size; i <<= 1) {
if (i == 1 && item_ct1.get_local_id(2) < spmv_block_size - 1 &&
reg_ind == ind[item_ct1.get_local_id(2) + 1]) {
last = false;
}
auto temp = zero<ValueType>();
if (item_ct1.get_local_id(2) >= i &&
reg_ind == ind[item_ct1.get_local_id(2) - i]) {
temp = val[item_ct1.get_local_id(2) - i];
}
group::this_thread_block(item_ct1).sync();
val[item_ct1.get_local_id(2)] += temp;
group::this_thread_block(item_ct1).sync();
}
return last;
}
template <bool overflow, typename IndexType>
__dpct_inline__ void find_next_row(
const IndexType num_rows, const IndexType data_size, const IndexType ind,
IndexType& row, IndexType& row_end, const IndexType row_predict,
const IndexType row_predict_end, const IndexType* __restrict__ row_ptr)
{
if (!overflow || ind < data_size) {
if (ind >= row_end) {
row = row_predict;
row_end = row_predict_end;
while (ind >= row_end) {
row_end = row_ptr[++row + 1];
}
}
} else {
row = num_rows - 1;
row_end = data_size;
}
}
template <unsigned subgroup_size, typename ValueType, typename IndexType,
typename output_accessor, typename Closure>
__dpct_inline__ void warp_atomic_add(
const group::thread_block_tile<subgroup_size>& group, bool force_write,
ValueType& val, const IndexType row, acc::range<output_accessor>& c,
const IndexType column_id, Closure scale)
{
// do a local scan to avoid atomic collisions
const bool need_write = segment_scan(group, row, &val);
if (need_write && force_write) {
atomic_add(c->get_storage_address(row, column_id), scale(val));
}
if (!need_write || force_write) {
val = zero<ValueType>();
}
}
template <bool last, unsigned subgroup_size, typename arithmetic_type,
typename matrix_accessor, typename IndexType, typename input_accessor,
typename output_accessor, typename Closure>
__dpct_inline__ void process_window(
const group::thread_block_tile<subgroup_size>& group,
const IndexType num_rows, const IndexType data_size, const IndexType ind,
IndexType& row, IndexType& row_end, IndexType& nrow, IndexType& nrow_end,
arithmetic_type& temp_val, acc::range<matrix_accessor> val,
const IndexType* __restrict__ col_idxs,
const IndexType* __restrict__ row_ptrs, acc::range<input_accessor> b,
acc::range<output_accessor> c, const IndexType column_id, Closure scale)
{
const IndexType curr_row = row;
find_next_row<last>(num_rows, data_size, ind, row, row_end, nrow, nrow_end,
row_ptrs);
// segmented scan
if (group.any(curr_row != row)) {
warp_atomic_add(group, curr_row != row, temp_val, curr_row, c,
column_id, scale);
nrow = group.shfl(row, subgroup_size - 1);
nrow_end = group.shfl(row_end, subgroup_size - 1);
}
if (!last || ind < data_size) {
const auto col = col_idxs[ind];
temp_val += val(ind) * b(col, column_id);
}
}
template <typename IndexType>
__dpct_inline__ IndexType get_warp_start_idx(const IndexType nwarps,
const IndexType nnz,
const IndexType warp_idx)
{
const long long cache_lines = ceildivT<IndexType>(nnz, config::warp_size);
return (warp_idx * cache_lines / nwarps) * config::warp_size;
}
template <typename matrix_accessor, typename input_accessor,
typename output_accessor, typename IndexType, typename Closure>
__dpct_inline__ void spmv_kernel(
const IndexType nwarps, const IndexType num_rows,
acc::range<matrix_accessor> val, const IndexType* __restrict__ col_idxs,
const IndexType* __restrict__ row_ptrs, const IndexType* __restrict__ srow,
acc::range<input_accessor> b, acc::range<output_accessor> c, Closure scale,
sycl::nd_item<3> item_ct1)
{
using arithmetic_type = typename output_accessor::arithmetic_type;
const IndexType warp_idx =
item_ct1.get_group(2) * warps_in_block + item_ct1.get_local_id(1);
const IndexType column_id = item_ct1.get_group(1);
if (warp_idx >= nwarps) {
return;
}
const IndexType data_size = row_ptrs[num_rows];
const IndexType start = get_warp_start_idx(nwarps, data_size, warp_idx);
constexpr IndexType wsize = config::warp_size;
const IndexType end =
min(get_warp_start_idx(nwarps, data_size, warp_idx + 1),
ceildivT<IndexType>(data_size, wsize) * wsize);
auto row = srow[warp_idx];
auto row_end = row_ptrs[row + 1];
auto nrow = row;
auto nrow_end = row_end;
auto temp_val = zero<arithmetic_type>();
IndexType ind = start + item_ct1.get_local_id(2);
find_next_row<true>(num_rows, data_size, ind, row, row_end, nrow, nrow_end,
row_ptrs);
const IndexType ind_end = end - wsize;
const auto tile_block =
group::tiled_partition<wsize>(group::this_thread_block(item_ct1));
for (; ind < ind_end; ind += wsize) {
process_window<false>(tile_block, num_rows, data_size, ind, row,
row_end, nrow, nrow_end, temp_val, val, col_idxs,
row_ptrs, b, c, column_id, scale);
}
process_window<true>(tile_block, num_rows, data_size, ind, row, row_end,
nrow, nrow_end, temp_val, val, col_idxs, row_ptrs, b,
c, column_id, scale);
warp_atomic_add(tile_block, true, temp_val, row, c, column_id, scale);
}
template <typename matrix_accessor, typename input_accessor,
typename output_accessor, typename IndexType>
void abstract_spmv(const IndexType nwarps, const IndexType num_rows,
acc::range<matrix_accessor> val,
const IndexType* __restrict__ col_idxs,
const IndexType* __restrict__ row_ptrs,
const IndexType* __restrict__ srow,
acc::range<input_accessor> b, acc::range<output_accessor> c,
sycl::nd_item<3> item_ct1)
{
using arithmetic_type = typename output_accessor::arithmetic_type;
using output_type = typename output_accessor::storage_type;
spmv_kernel(
nwarps, num_rows, val, col_idxs, row_ptrs, srow, b, c,
[](const arithmetic_type& x) {
// using atomic add to accumluate data, so it needs to be
// the output storage type
// TODO: Does it make sense to use atomicCAS when the
// arithmetic_type and output_type are different? It may
// allow the non floating point storage or more precise
// result.
return static_cast<output_type>(x);
},
item_ct1);
}
template <typename matrix_accessor, typename input_accessor,
typename output_accessor, typename IndexType>
void abstract_spmv(
const IndexType nwarps, const IndexType num_rows,
const typename matrix_accessor::storage_type* __restrict__ alpha,
acc::range<matrix_accessor> val, const IndexType* __restrict__ col_idxs,
const IndexType* __restrict__ row_ptrs, const IndexType* __restrict__ srow,
acc::range<input_accessor> b, acc::range<output_accessor> c,
sycl::nd_item<3> item_ct1)
{
using arithmetic_type = typename output_accessor::arithmetic_type;
using output_type = typename output_accessor::storage_type;
const auto scale_factor = static_cast<arithmetic_type>(alpha[0]);
spmv_kernel(
nwarps, num_rows, val, col_idxs, row_ptrs, srow, b, c,
[&scale_factor](const arithmetic_type& x) {
return static_cast<output_type>(scale_factor * x);
},
item_ct1);
}
GKO_ENABLE_DEFAULT_HOST(abstract_spmv, abstract_spmv);
template <typename IndexType>
__dpct_inline__ void merge_path_search(
const IndexType diagonal, const IndexType a_len, const IndexType b_len,
const IndexType* __restrict__ a, const IndexType offset_b,
IndexType* __restrict__ x, IndexType* __restrict__ y)
{
auto x_min = max(diagonal - b_len, zero<IndexType>());
auto x_max = min(diagonal, a_len);
while (x_min < x_max) {
auto pivot = x_min + (x_max - x_min) / 2;
if (a[pivot] <= offset_b + diagonal - pivot - 1) {
x_min = pivot + 1;
} else {
x_max = pivot;
}
}
*x = min(x_min, a_len);
*y = diagonal - x_min;
}
template <typename arithmetic_type, typename IndexType,
typename output_accessor, typename Alpha_op>
void merge_path_reduce(
const IndexType nwarps, const arithmetic_type* __restrict__ last_val,
const IndexType* __restrict__ last_row, acc::range<output_accessor> c,
Alpha_op alpha_op, sycl::nd_item<3> item_ct1,
uninitialized_array<IndexType, spmv_block_size>& tmp_ind,
uninitialized_array<arithmetic_type, spmv_block_size>& tmp_val)
{
const IndexType cache_lines = ceildivT<IndexType>(nwarps, spmv_block_size);
const IndexType tid = item_ct1.get_local_id(2);
const IndexType start = min(tid * cache_lines, nwarps);
const IndexType end = min((tid + 1) * cache_lines, nwarps);
auto value = zero<arithmetic_type>();
IndexType row = last_row[nwarps - 1];
if (start < nwarps) {
value = last_val[start];
row = last_row[start];
for (IndexType i = start + 1; i < end; i++) {
if (last_row[i] != row) {
c(row, 0) += alpha_op(value);
row = last_row[i];
value = last_val[i];
} else {
value += last_val[i];
}
}
}
tmp_val[item_ct1.get_local_id(2)] = value;
tmp_ind[item_ct1.get_local_id(2)] = row;
group::this_thread_block(item_ct1).sync();
bool last = block_segment_scan_reverse(
static_cast<IndexType*>(tmp_ind),
static_cast<arithmetic_type*>(tmp_val), item_ct1);
group::this_thread_block(item_ct1).sync();
if (last) {
c(row, 0) += alpha_op(tmp_val[item_ct1.get_local_id(2)]);
}
}
template <int items_per_thread, typename matrix_accessor,
typename input_accessor, typename output_accessor, typename IndexType,
typename Alpha_op, typename Beta_op>
void merge_path_spmv(
const IndexType num_rows, acc::range<matrix_accessor> val,
const IndexType* __restrict__ col_idxs,
const IndexType* __restrict__ row_ptrs, const IndexType* __restrict__ srow,
acc::range<input_accessor> b, acc::range<output_accessor> c,
IndexType* __restrict__ row_out,
typename output_accessor::arithmetic_type* __restrict__ val_out,
Alpha_op alpha_op, Beta_op beta_op, sycl::nd_item<3> item_ct1,
IndexType* shared_row_ptrs)
{
using arithmetic_type = typename output_accessor::arithmetic_type;
const auto* row_end_ptrs = row_ptrs + 1;
const auto nnz = row_ptrs[num_rows];
const IndexType num_merge_items = num_rows + nnz;
const auto block_items = spmv_block_size * items_per_thread;
const IndexType diagonal =
min(IndexType(block_items * item_ct1.get_group(2)), num_merge_items);
const IndexType diagonal_end = min(diagonal + block_items, num_merge_items);
IndexType block_start_x;
IndexType block_start_y;
IndexType end_x;
IndexType end_y;
merge_path_search(diagonal, num_rows, nnz, row_end_ptrs, zero<IndexType>(),
&block_start_x, &block_start_y);
merge_path_search(diagonal_end, num_rows, nnz, row_end_ptrs,
zero<IndexType>(), &end_x, &end_y);
const IndexType block_num_rows = end_x - block_start_x;
const IndexType block_num_nonzeros = end_y - block_start_y;
for (int i = item_ct1.get_local_id(2);
i < block_num_rows && block_start_x + i < num_rows;
i += spmv_block_size) {
shared_row_ptrs[i] = row_end_ptrs[block_start_x + i];
}
group::this_thread_block(item_ct1).sync();
IndexType start_x;
IndexType start_y;
merge_path_search(IndexType(items_per_thread * item_ct1.get_local_id(2)),
block_num_rows, block_num_nonzeros, shared_row_ptrs,
block_start_y, &start_x, &start_y);
IndexType ind = block_start_y + start_y;
IndexType row_i = block_start_x + start_x;
auto value = zero<arithmetic_type>();
#pragma unroll
for (IndexType i = 0; i < items_per_thread; i++) {
if (row_i < num_rows) {
if (start_x == block_num_rows || ind < shared_row_ptrs[start_x]) {
value += val(ind) * b(col_idxs[ind], 0);
ind++;
} else {
c(row_i, 0) = alpha_op(value) + beta_op(c(row_i, 0));
start_x++;
row_i++;
value = zero<arithmetic_type>();
}
}
}
group::this_thread_block(item_ct1).sync();
IndexType* tmp_ind = shared_row_ptrs;
arithmetic_type* tmp_val =
reinterpret_cast<arithmetic_type*>(shared_row_ptrs + spmv_block_size);
tmp_val[item_ct1.get_local_id(2)] = value;
tmp_ind[item_ct1.get_local_id(2)] = row_i;
group::this_thread_block(item_ct1).sync();
bool last = block_segment_scan_reverse(tmp_ind, tmp_val, item_ct1);
if (item_ct1.get_local_id(2) == spmv_block_size - 1) {
row_out[item_ct1.get_group(2)] = min(end_x, num_rows - 1);
val_out[item_ct1.get_group(2)] = tmp_val[item_ct1.get_local_id(2)];
} else if (last) {
c(row_i, 0) += alpha_op(tmp_val[item_ct1.get_local_id(2)]);
}
}
template <int items_per_thread, typename matrix_accessor,
typename input_accessor, typename output_accessor, typename IndexType>
void abstract_merge_path_spmv(
const IndexType num_rows, acc::range<matrix_accessor> val,
const IndexType* __restrict__ col_idxs,
const IndexType* __restrict__ row_ptrs, const IndexType* __restrict__ srow,
acc::range<input_accessor> b, acc::range<output_accessor> c,
IndexType* __restrict__ row_out,
typename output_accessor::arithmetic_type* __restrict__ val_out,
sycl::nd_item<3> item_ct1, IndexType* shared_row_ptrs)
{
using type = typename output_accessor::arithmetic_type;
merge_path_spmv<items_per_thread>(
num_rows, val, col_idxs, row_ptrs, srow, b, c, row_out, val_out,
[](const type& x) { return x; },
[](const type& x) { return zero<type>(); }, item_ct1, shared_row_ptrs);
}
template <int items_per_thread, typename matrix_accessor,
typename input_accessor, typename output_accessor, typename IndexType>
void abstract_merge_path_spmv(
dim3 grid, dim3 block, size_type dynamic_shared_memory, sycl::queue* queue,
const IndexType num_rows, acc::range<matrix_accessor> val,
const IndexType* col_idxs, const IndexType* row_ptrs, const IndexType* srow,
acc::range<input_accessor> b, acc::range<output_accessor> c,
IndexType* row_out, typename output_accessor::arithmetic_type* val_out)
{
queue->submit([&](sycl::handler& cgh) {
sycl::local_accessor<IndexType, 1> shared_row_ptrs_acc_ct1(
sycl::range<1>(spmv_block_size * items_per_thread), cgh);
cgh.parallel_for(sycl_nd_range(grid, block),
[=](sycl::nd_item<3> item_ct1) {
abstract_merge_path_spmv<items_per_thread>(
num_rows, val, col_idxs, row_ptrs, srow, b, c,
row_out, val_out, item_ct1,
static_cast<IndexType*>(
shared_row_ptrs_acc_ct1.get_pointer()));
});
});
}
template <int items_per_thread, typename matrix_accessor,
typename input_accessor, typename output_accessor, typename IndexType>
void abstract_merge_path_spmv(
const IndexType num_rows,
const typename matrix_accessor::storage_type* __restrict__ alpha,
acc::range<matrix_accessor> val, const IndexType* __restrict__ col_idxs,
const IndexType* __restrict__ row_ptrs, const IndexType* __restrict__ srow,
acc::range<input_accessor> b,
const typename output_accessor::storage_type* __restrict__ beta,
acc::range<output_accessor> c, IndexType* __restrict__ row_out,
typename output_accessor::arithmetic_type* __restrict__ val_out,
sycl::nd_item<3> item_ct1, IndexType* shared_row_ptrs)
{
using type = typename output_accessor::arithmetic_type;
const type alpha_val = static_cast<type>(alpha[0]);
const type beta_val = static_cast<type>(beta[0]);
if (is_zero(beta_val)) {
merge_path_spmv<items_per_thread>(
num_rows, val, col_idxs, row_ptrs, srow, b, c, row_out, val_out,
[&alpha_val](const type& x) { return alpha_val * x; },
[](const type& x) { return zero<type>(); }, item_ct1,
shared_row_ptrs);
} else {
merge_path_spmv<items_per_thread>(
num_rows, val, col_idxs, row_ptrs, srow, b, c, row_out, val_out,
[&alpha_val](const type& x) { return alpha_val * x; },
[&beta_val](const type& x) { return beta_val * x; }, item_ct1,
shared_row_ptrs);
}
}
template <int items_per_thread, typename matrix_accessor,
typename input_accessor, typename output_accessor, typename IndexType>
void abstract_merge_path_spmv(
dim3 grid, dim3 block, size_type dynamic_shared_memory, sycl::queue* queue,
const IndexType num_rows,
const typename matrix_accessor::storage_type* alpha,
acc::range<matrix_accessor> val, const IndexType* col_idxs,
const IndexType* row_ptrs, const IndexType* srow,
acc::range<input_accessor> b,
const typename output_accessor::storage_type* beta,
acc::range<output_accessor> c, IndexType* row_out,
typename output_accessor::arithmetic_type* val_out)
{
queue->submit([&](sycl::handler& cgh) {
sycl::local_accessor<IndexType, 1> shared_row_ptrs_acc_ct1(
sycl::range<1>(spmv_block_size * items_per_thread), cgh);
cgh.parallel_for(sycl_nd_range(grid, block),
[=](sycl::nd_item<3> item_ct1) {
abstract_merge_path_spmv<items_per_thread>(
num_rows, alpha, val, col_idxs, row_ptrs, srow,
b, beta, c, row_out, val_out, item_ct1,
static_cast<IndexType*>(
shared_row_ptrs_acc_ct1.get_pointer()));
});
});
}
template <typename arithmetic_type, typename IndexType,
typename output_accessor>
void abstract_reduce(
const IndexType nwarps, const arithmetic_type* __restrict__ last_val,
const IndexType* __restrict__ last_row, acc::range<output_accessor> c,
sycl::nd_item<3> item_ct1,
uninitialized_array<IndexType, spmv_block_size>& tmp_ind,
uninitialized_array<arithmetic_type, spmv_block_size>& tmp_val)
{
merge_path_reduce(
nwarps, last_val, last_row, c,
[](const arithmetic_type& x) { return x; }, item_ct1, tmp_ind, tmp_val);
}
template <typename arithmetic_type, typename IndexType,
typename output_accessor>
void abstract_reduce(dim3 grid, dim3 block, size_type dynamic_shared_memory,
sycl::queue* queue, const IndexType nwarps,
const arithmetic_type* __restrict__ last_val,
const IndexType* __restrict__ last_row,
acc::range<output_accessor> c)
{
queue->submit([&](sycl::handler& cgh) {
sycl::local_accessor<uninitialized_array<IndexType, spmv_block_size>, 0>
tmp_ind_acc_ct1(cgh);
sycl::local_accessor<
uninitialized_array<arithmetic_type, spmv_block_size>, 0>
tmp_val_acc_ct1(cgh);
cgh.parallel_for(
sycl_nd_range(grid, block), [=](sycl::nd_item<3> item_ct1) {
abstract_reduce(nwarps, last_val, last_row, c, item_ct1,
*tmp_ind_acc_ct1.get_pointer(),
*tmp_val_acc_ct1.get_pointer());
});
});
}
template <typename arithmetic_type, typename MatrixValueType,
typename IndexType, typename output_accessor>
void abstract_reduce(
const IndexType nwarps, const arithmetic_type* __restrict__ last_val,
const IndexType* __restrict__ last_row,
const MatrixValueType* __restrict__ alpha, acc::range<output_accessor> c,
sycl::nd_item<3> item_ct1,
uninitialized_array<IndexType, spmv_block_size>& tmp_ind,
uninitialized_array<arithmetic_type, spmv_block_size>& tmp_val)
{
const auto alpha_val = static_cast<arithmetic_type>(alpha[0]);
merge_path_reduce(
nwarps, last_val, last_row, c,
[&alpha_val](const arithmetic_type& x) { return alpha_val * x; },
item_ct1, tmp_ind, tmp_val);
}
template <typename arithmetic_type, typename MatrixValueType,
typename IndexType, typename output_accessor>
void abstract_reduce(dim3 grid, dim3 block, size_type dynamic_shared_memory,
sycl::queue* queue, const IndexType nwarps,
const arithmetic_type* last_val, const IndexType* last_row,
const MatrixValueType* alpha,
acc::range<output_accessor> c)
{
queue->submit([&](sycl::handler& cgh) {
sycl::local_accessor<uninitialized_array<IndexType, spmv_block_size>, 0>
tmp_ind_acc_ct1(cgh);
sycl::local_accessor<
uninitialized_array<arithmetic_type, spmv_block_size>, 0>
tmp_val_acc_ct1(cgh);
cgh.parallel_for(
sycl_nd_range(grid, block), [=](sycl::nd_item<3> item_ct1) {
abstract_reduce(nwarps, last_val, last_row, alpha, c, item_ct1,
*tmp_ind_acc_ct1.get_pointer(),
*tmp_val_acc_ct1.get_pointer());
});
});
}
template <size_type subgroup_size, typename matrix_accessor,
typename input_accessor, typename output_accessor, typename IndexType,
typename Closure>
void device_classical_spmv(const size_type num_rows,
acc::range<matrix_accessor> val,
const IndexType* __restrict__ col_idxs,
const IndexType* __restrict__ row_ptrs,
acc::range<input_accessor> b,
acc::range<output_accessor> c, Closure scale,
sycl::nd_item<3> item_ct1)
{
using arithmetic_type = typename output_accessor::arithmetic_type;
auto subgroup_tile = group::tiled_partition<subgroup_size>(
group::this_thread_block(item_ct1));
const auto subrow = thread::get_subwarp_num_flat<subgroup_size>(item_ct1);
const auto subid = subgroup_tile.thread_rank();
const auto column_id = item_ct1.get_group(1);
auto row = thread::get_subwarp_id_flat<subgroup_size>(item_ct1);
for (; row < num_rows; row += subrow) {
const auto ind_end = row_ptrs[row + 1];
auto temp_val = zero<arithmetic_type>();
for (auto ind = row_ptrs[row] + subid; ind < ind_end;
ind += subgroup_size) {
temp_val += val(ind) * b(col_idxs[ind], column_id);
}
auto subgroup_result = ::gko::kernels::dpcpp::reduce(
subgroup_tile, temp_val,
[](const arithmetic_type& a, const arithmetic_type& b) {
return a + b;
});
// TODO: check the barrier
subgroup_tile.sync();
if (subid == 0) {
c(row, column_id) = scale(subgroup_result, c(row, column_id));
}
}
}
template <size_type subgroup_size, typename matrix_accessor,
typename input_accessor, typename output_accessor, typename IndexType>
void abstract_classical_spmv(const size_type num_rows,
acc::range<matrix_accessor> val,
const IndexType* __restrict__ col_idxs,
const IndexType* __restrict__ row_ptrs,
acc::range<input_accessor> b,
acc::range<output_accessor> c,
sycl::nd_item<3> item_ct1)
{
using type = typename output_accessor::arithmetic_type;
device_classical_spmv<subgroup_size>(
num_rows, val, col_idxs, row_ptrs, b, c,
[](const type& x, const type& y) { return x; }, item_ct1);
}
template <size_type subgroup_size, typename matrix_accessor,
typename input_accessor, typename output_accessor, typename IndexType>
void abstract_classical_spmv(
dim3 grid, dim3 block, size_type dynamic_shared_memory, sycl::queue* queue,
const size_type num_rows, acc::range<matrix_accessor> val,
const IndexType* col_idxs, const IndexType* row_ptrs,
acc::range<input_accessor> b, acc::range<output_accessor> c)
{
if (subgroup_size > 1) {
queue->submit([&](sycl::handler& cgh) {
cgh.parallel_for(sycl_nd_range(grid, block),
[=](sycl::nd_item<3> item_ct1)
[[sycl::reqd_sub_group_size(subgroup_size)]] {
abstract_classical_spmv<subgroup_size>(
num_rows, val, col_idxs, row_ptrs, b,
c, item_ct1);
});
});
} else {
queue->submit([&](sycl::handler& cgh) {
cgh.parallel_for(
sycl_nd_range(grid, block), [=](sycl::nd_item<3> item_ct1) {
abstract_classical_spmv<subgroup_size>(
num_rows, val, col_idxs, row_ptrs, b, c, item_ct1);
});
});
}
}
template <size_type subgroup_size, typename matrix_accessor,
typename input_accessor, typename output_accessor, typename IndexType>
void abstract_classical_spmv(
const size_type num_rows,
const typename matrix_accessor::storage_type* __restrict__ alpha,
acc::range<matrix_accessor> val, const IndexType* __restrict__ col_idxs,
const IndexType* __restrict__ row_ptrs, acc::range<input_accessor> b,
const typename output_accessor::storage_type* __restrict__ beta,
acc::range<output_accessor> c, sycl::nd_item<3> item_ct1)
{
using type = typename output_accessor::arithmetic_type;
const type alpha_val = static_cast<type>(alpha[0]);
const type beta_val = static_cast<type>(beta[0]);
if (is_zero(beta_val)) {
device_classical_spmv<subgroup_size>(
num_rows, val, col_idxs, row_ptrs, b, c,
[&alpha_val](const type& x, const type& y) {
return alpha_val * x;
},
item_ct1);
} else {
device_classical_spmv<subgroup_size>(
num_rows, val, col_idxs, row_ptrs, b, c,
[&alpha_val, &beta_val](const type& x, const type& y) {
return alpha_val * x + beta_val * y;
},
item_ct1);
}
}
template <size_type subgroup_size, typename matrix_accessor,
typename input_accessor, typename output_accessor, typename IndexType>
void abstract_classical_spmv(
dim3 grid, dim3 block, size_type dynamic_shared_memory, sycl::queue* queue,
const size_type num_rows,
const typename matrix_accessor::storage_type* alpha,
acc::range<matrix_accessor> val, const IndexType* col_idxs,
const IndexType* row_ptrs, acc::range<input_accessor> b,
const typename output_accessor::storage_type* beta,
acc::range<output_accessor> c)
{
if (subgroup_size > 1) {
queue->submit([&](sycl::handler& cgh) {
cgh.parallel_for(sycl_nd_range(grid, block),
[=](sycl::nd_item<3> item_ct1)
[[sycl::reqd_sub_group_size(subgroup_size)]] {
abstract_classical_spmv<subgroup_size>(
num_rows, alpha, val, col_idxs,
row_ptrs, b, beta, c, item_ct1);
});
});
} else {
queue->submit([&](sycl::handler& cgh) {
cgh.parallel_for(sycl_nd_range(grid, block),
[=](sycl::nd_item<3> item_ct1) {
abstract_classical_spmv<subgroup_size>(
num_rows, alpha, val, col_idxs, row_ptrs,
b, beta, c, item_ct1);
});
});
}
}
template <typename ValueType, typename IndexType>
void fill_in_dense(size_type num_rows, const IndexType* __restrict__ row_ptrs,
const IndexType* __restrict__ col_idxs,
const ValueType* __restrict__ values, size_type stride,
ValueType* __restrict__ result, sycl::nd_item<3> item_ct1)
{
const auto tidx = thread::get_thread_id_flat(item_ct1);
if (tidx < num_rows) {
for (auto i = row_ptrs[tidx]; i < row_ptrs[tidx + 1]; i++) {
result[stride * tidx + col_idxs[i]] = values[i];
}
}
}
GKO_ENABLE_DEFAULT_HOST(fill_in_dense, fill_in_dense);
template <typename IndexType>
void check_unsorted(const IndexType* __restrict__ row_ptrs,
const IndexType* __restrict__ col_idxs, IndexType num_rows,
bool* flag, sycl::nd_item<3> item_ct1, bool* sh_flag)
{
auto block = group::this_thread_block(item_ct1);
if (block.thread_rank() == 0) {
*sh_flag = *flag;
}
block.sync();
auto row = thread::get_thread_id_flat<IndexType>(item_ct1);
if (row >= num_rows) {
return;
}
// fail early
if ((*sh_flag)) {
for (auto nz = row_ptrs[row]; nz < row_ptrs[row + 1] - 1; ++nz) {
if (col_idxs[nz] > col_idxs[nz + 1]) {
*flag = false;
*sh_flag = false;
return;
}
}
}
}
template <typename IndexType>
void check_unsorted(dim3 grid, dim3 block, size_type dynamic_shared_memory,
sycl::queue* queue, const IndexType* row_ptrs,
const IndexType* col_idxs, IndexType num_rows, bool* flag)
{
queue->submit([&](sycl::handler& cgh) {
sycl::local_accessor<bool, 0> sh_flag_acc_ct1(cgh);
cgh.parallel_for(
sycl_nd_range(grid, block), [=](sycl::nd_item<3> item_ct1) {
check_unsorted(row_ptrs, col_idxs, num_rows, flag, item_ct1,
sh_flag_acc_ct1.get_pointer());
});
});
}
template <typename ValueType, typename IndexType>
void extract_diagonal(size_type diag_size, size_type nnz,
const ValueType* __restrict__ orig_values,
const IndexType* __restrict__ orig_row_ptrs,
const IndexType* __restrict__ orig_col_idxs,
ValueType* __restrict__ diag, sycl::nd_item<3> item_ct1)
{
constexpr auto warp_size = config::warp_size;
const auto row = thread::get_subwarp_id_flat<warp_size>(item_ct1);
const auto local_tidx = item_ct1.get_local_id(2) % warp_size;
if (row < diag_size) {
for (size_type i = local_tidx;
i < orig_row_ptrs[row + 1] - orig_row_ptrs[row]; i += warp_size) {
const auto orig_idx = i + orig_row_ptrs[row];
if (orig_idx < nnz) {
if (orig_col_idxs[orig_idx] == row) {
diag[row] = orig_values[orig_idx];
return;
}
}
}
}
}
GKO_ENABLE_DEFAULT_HOST(extract_diagonal, extract_diagonal);
template <typename IndexType>
void check_diagonal_entries(const IndexType num_min_rows_cols,
const IndexType* const __restrict__ row_ptrs,
const IndexType* const __restrict__ col_idxs,
bool* const __restrict__ has_all_diags,
sycl::nd_item<3> item_ct1)
{
constexpr int subgroup_size = config::warp_size;
auto tile_grp = group::tiled_partition<subgroup_size>(
group::this_thread_block(item_ct1));
const auto row =
thread::get_subwarp_id_flat<subgroup_size, IndexType>(item_ct1);
if (row < num_min_rows_cols) {
const auto tid_in_warp = tile_grp.thread_rank();
const auto row_start = row_ptrs[row];
const auto num_nz = row_ptrs[row + 1] - row_start;
bool row_has_diag_local{false};
for (IndexType iz = tid_in_warp; iz < num_nz; iz += subgroup_size) {
if (col_idxs[iz + row_start] == row) {
row_has_diag_local = true;
break;
}
}
auto row_has_diag = static_cast<bool>(tile_grp.any(row_has_diag_local));
if (!row_has_diag) {
if (tile_grp.thread_rank() == 0) {
*has_all_diags = false;
}
}
}
}
GKO_ENABLE_DEFAULT_HOST(check_diagonal_entries, check_diagonal_entries);
template <typename ValueType, typename IndexType>
void add_scaled_identity(const ValueType* const __restrict__ alpha,
const ValueType* const __restrict__ beta,
const IndexType num_rows,
const IndexType* const __restrict__ row_ptrs,
const IndexType* const __restrict__ col_idxs,
ValueType* const __restrict__ values,
sycl::nd_item<3> item_ct1)
{
constexpr int subgroup_size = config::warp_size;
auto tile_grp = group::tiled_partition<subgroup_size>(
group::this_thread_block(item_ct1));
const auto row =
thread::get_subwarp_id_flat<subgroup_size, IndexType>(item_ct1);
if (row < num_rows) {
const auto tid_in_warp = tile_grp.thread_rank();
const auto row_start = row_ptrs[row];
const auto num_nz = row_ptrs[row + 1] - row_start;
const auto beta_val = beta[0];
const auto alpha_val = alpha[0];
for (IndexType iz = tid_in_warp; iz < num_nz; iz += subgroup_size) {
if (beta_val != one<ValueType>()) {
values[iz + row_start] *= beta_val;
}
if (col_idxs[iz + row_start] == row &&
alpha_val != zero<ValueType>()) {
values[iz + row_start] += alpha_val;
}
}
}
}
GKO_ENABLE_DEFAULT_HOST(add_scaled_identity, add_scaled_identity);
} // namespace kernel
template <typename IndexType>
void row_ptr_permute_kernel(size_type num_rows,
const IndexType* __restrict__ permutation,
const IndexType* __restrict__ in_row_ptrs,
IndexType* __restrict__ out_nnz,
sycl::nd_item<3> item_ct1)
{
auto tid = thread::get_thread_id_flat(item_ct1);
if (tid >= num_rows) {
return;
}
auto in_row = permutation[tid];
auto out_row = tid;
out_nnz[out_row] = in_row_ptrs[in_row + 1] - in_row_ptrs[in_row];
}
GKO_ENABLE_DEFAULT_HOST(row_ptr_permute_kernel, row_ptr_permute_kernel);
template <typename IndexType>
void inv_row_ptr_permute_kernel(size_type num_rows,
const IndexType* __restrict__ permutation,
const IndexType* __restrict__ in_row_ptrs,
IndexType* __restrict__ out_nnz,
sycl::nd_item<3> item_ct1)
{
auto tid = thread::get_thread_id_flat(item_ct1);
if (tid >= num_rows) {
return;
}
auto in_row = tid;
auto out_row = permutation[tid];
out_nnz[out_row] = in_row_ptrs[in_row + 1] - in_row_ptrs[in_row];
}
GKO_ENABLE_DEFAULT_HOST(inv_row_ptr_permute_kernel, inv_row_ptr_permute_kernel);
template <int subgroup_size = config::warp_size, typename ValueType,
typename IndexType>
void row_permute_kernel(size_type num_rows,
const IndexType* __restrict__ permutation,
const IndexType* __restrict__ in_row_ptrs,
const IndexType* __restrict__ in_cols,
const ValueType* __restrict__ in_vals,
const IndexType* __restrict__ out_row_ptrs,
IndexType* __restrict__ out_cols,
ValueType* __restrict__ out_vals,
sycl::nd_item<3> item_ct1)
{
auto tid = thread::get_subwarp_id_flat<subgroup_size>(item_ct1);
if (tid >= num_rows) {
return;
}
const auto lane = item_ct1.get_local_id(2) % subgroup_size;
const auto in_row = permutation[tid];
const auto out_row = tid;
const auto in_begin = in_row_ptrs[in_row];
const auto in_size = in_row_ptrs[in_row + 1] - in_begin;
const auto out_begin = out_row_ptrs[out_row];
for (IndexType i = lane; i < in_size; i += subgroup_size) {
out_cols[out_begin + i] = in_cols[in_begin + i];
out_vals[out_begin + i] = in_vals[in_begin + i];
}
}
GKO_ENABLE_DEFAULT_HOST(row_permute_kernel, row_permute_kernel);
template <int subgroup_size = config::warp_size, typename ValueType,
typename IndexType>
void inv_row_permute_kernel(size_type num_rows,
const IndexType* __restrict__ permutation,
const IndexType* __restrict__ in_row_ptrs,
const IndexType* __restrict__ in_cols,
const ValueType* __restrict__ in_vals,