-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathKokkosSparse_par_ilut_numeric_impl.hpp
866 lines (736 loc) · 38.9 KB
/
KokkosSparse_par_ilut_numeric_impl.hpp
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
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#ifndef KOKKOSSPARSE_IMPL_PAR_ILUT_NUMERIC_HPP_
#define KOKKOSSPARSE_IMPL_PAR_ILUT_NUMERIC_HPP_
/// \file KokkosSparse_par_ilut_numeric_impl.hpp
/// \brief Implementation(s) of the numeric phase of sparse parallel ILUT.
#include <KokkosKernels_config.h>
#include <Kokkos_ArithTraits.hpp>
#include <KokkosSparse_par_ilut_handle.hpp>
#include <KokkosSparse_spgemm.hpp>
#include <KokkosSparse_spadd.hpp>
#include <KokkosSparse_Utils.hpp>
#include <KokkosSparse_SortCrs.hpp>
#include <KokkosKernels_Utils.hpp>
#include <limits>
namespace KokkosSparse {
namespace Impl {
namespace Experimental {
template <class IlutHandle>
struct IlutWrap {
//
// Useful types
//
using execution_space = typename IlutHandle::execution_space;
using index_t = typename IlutHandle::nnz_lno_t;
using size_type = typename IlutHandle::size_type;
using scalar_t = typename IlutHandle::nnz_scalar_t;
using HandleDeviceEntriesType = typename IlutHandle::nnz_lno_view_t;
using HandleDeviceRowMapType = typename IlutHandle::nnz_row_view_t;
using HandleDeviceValueType = typename IlutHandle::nnz_value_view_t;
using karith = typename Kokkos::ArithTraits<scalar_t>;
using policy_type = typename IlutHandle::TeamPolicy;
using member_type = typename policy_type::member_type;
using range_policy = typename IlutHandle::RangePolicy;
/**
* prefix_sum: Take a row_map of counts and transform it to sums, and
* return the total sum.
*/
template <class RowMapType>
static size_type prefix_sum(RowMapType& row_map) {
size_type result = 0;
KokkosKernels::Impl::kk_exclusive_parallel_prefix_sum<typename IlutHandle::HandleExecSpace>(row_map.extent(0),
row_map, result);
return result;
}
/**
* Just a convenience wrapper around spgemm
*/
template <class KHandle, class LRowMapType, class LEntriesType, class LValuesType, class URowMapType,
class UEntriesType, class UValuesType, class LURowMapType, class LUEntriesType, class LUValuesType>
static void multiply_matrices(KHandle& kh, IlutHandle& ih, const LRowMapType& L_row_map,
const LEntriesType& L_entries, const LValuesType& L_values,
const URowMapType& U_row_map, const UEntriesType& U_entries,
const UValuesType& U_values, LURowMapType& LU_row_map, LUEntriesType& LU_entries,
LUValuesType& LU_values) {
std::string myalg("SPGEMM_KK_MEMORY");
KokkosSparse::SPGEMMAlgorithm spgemm_algorithm = KokkosSparse::StringToSPGEMMAlgorithm(myalg);
kh.create_spgemm_handle(spgemm_algorithm);
const size_type nrows = ih.get_nrows();
KokkosSparse::Experimental::spgemm_symbolic(&kh, nrows, nrows, nrows, L_row_map, L_entries, false, U_row_map,
U_entries, false, LU_row_map);
const size_type lu_nnz_size = kh.get_spgemm_handle()->get_c_nnz();
Kokkos::resize(LU_entries, lu_nnz_size);
Kokkos::resize(LU_values, lu_nnz_size);
KokkosSparse::Experimental::spgemm_numeric(&kh, nrows, nrows, nrows, L_row_map, L_entries, L_values, false,
U_row_map, U_entries, U_values, false, LU_row_map, LU_entries,
LU_values);
// Need to sort LU CRS if on CUDA!
sort_crs_matrix<execution_space>(LU_row_map, LU_entries, LU_values);
kh.destroy_spgemm_handle();
}
/**
* Just a convenience wrapper around transpose_matrix
*/
template <class RowMapType, class EntriesType, class ValuesType, class TRowMapType, class TEntriesType,
class TValuesType>
static void transpose_wrap(IlutHandle& ih, const RowMapType& row_map, const EntriesType& entries,
const ValuesType& values, TRowMapType& t_row_map, TEntriesType& t_entries,
TValuesType& t_values) {
const size_type nrows = ih.get_nrows();
// Need to reset t_row_map
Kokkos::deep_copy(t_row_map, 0);
Kokkos::resize(t_entries, entries.extent(0));
Kokkos::resize(t_values, values.extent(0));
KokkosSparse::Impl::transpose_matrix<HandleDeviceRowMapType, HandleDeviceEntriesType, HandleDeviceValueType,
HandleDeviceRowMapType, HandleDeviceEntriesType, HandleDeviceValueType,
HandleDeviceRowMapType, execution_space>(
nrows, nrows, row_map, entries, values, t_row_map, t_entries, t_values);
// Need to ensure output is sorted
sort_crs_matrix<execution_space>(t_row_map, t_entries, t_values);
}
/**
* Adds new entries from the sparsity pattern of A - L * U
* to L and U, where new values are chosen based on the residual
* value divided by the corresponding diagonal entry.
*/
template <class ARowMapType, class AEntriesType, class AValuesType, class LRowMapType, class LEntriesType,
class LValuesType, class URowMapType, class UEntriesType, class UValuesType, class LURowMapType,
class LUEntriesType, class LUValuesType, class LNewRowMapType, class LNewEntriesType, class LNewValuesType,
class UNewRowMapType, class UNewEntriesType, class UNewValuesType>
static void add_candidates(IlutHandle& ih, const ARowMapType& A_row_map, const AEntriesType& A_entries,
const AValuesType& A_values, const LRowMapType& L_row_map, const LEntriesType& L_entries,
const LValuesType& L_values, const URowMapType& U_row_map, const UEntriesType& U_entries,
const UValuesType& U_values, const LURowMapType& LU_row_map,
const LUEntriesType& LU_entries, const LUValuesType& LU_values,
LNewRowMapType& L_new_row_map, LNewEntriesType& L_new_entries,
LNewValuesType& L_new_values, UNewRowMapType& U_new_row_map,
UNewEntriesType& U_new_entries, UNewValuesType& U_new_values) {
const size_type nrows = ih.get_nrows();
const policy_type policy = ih.get_default_team_policy();
// Sizing run for add_candidates. Count nnz's and remove dupes
Kokkos::parallel_for(
"add_candidates sizing", policy, KOKKOS_LAMBDA(const member_type& team) {
const auto row_idx = team.league_rank();
const auto a_row_nnz_begin = A_row_map(row_idx);
const auto a_row_nnz_end = A_row_map(row_idx + 1);
const auto lu_row_nnz_begin = LU_row_map(row_idx);
const auto lu_row_nnz_end = LU_row_map(row_idx + 1);
// Really wish kokkos could do a multi-reduce here
size_type a_l_nnz = 0, a_u_nnz = 0, lu_l_nnz = 0, lu_u_nnz = 0, dup_l_nnz = 0, dup_u_nnz = 0;
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange(team, a_row_nnz_begin, a_row_nnz_end),
[&](const size_type nnz, size_type& nnzL_inner) {
const auto col_idx = A_entries(nnz);
nnzL_inner += col_idx <= row_idx;
},
a_l_nnz);
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange(team, a_row_nnz_begin, a_row_nnz_end),
[&](const size_type nnz, size_type& nnzU_inner) {
const auto col_idx = A_entries(nnz);
nnzU_inner += col_idx >= row_idx;
},
a_u_nnz);
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange(team, lu_row_nnz_begin, lu_row_nnz_end),
[&](const size_type nnz, size_type& nnzL_inner) {
const auto col_idx = LU_entries(nnz);
nnzL_inner += col_idx <= row_idx;
},
lu_l_nnz);
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange(team, lu_row_nnz_begin, lu_row_nnz_end),
[&](const size_type nnz, size_type& nnzU_inner) {
const auto col_idx = LU_entries(nnz);
nnzU_inner += col_idx >= row_idx;
},
lu_u_nnz);
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange(team, a_row_nnz_begin, a_row_nnz_end),
[&](const size_type nnz, size_type& dupL_inner) {
const auto a_col_idx = A_entries(nnz);
if (a_col_idx <= row_idx) {
for (size_type lu_i = lu_row_nnz_begin; lu_i < lu_row_nnz_end; ++lu_i) {
const auto lu_col_idx = LU_entries(lu_i);
if (a_col_idx == lu_col_idx) {
++dupL_inner;
break;
} else if (lu_col_idx > a_col_idx) {
break;
}
}
}
},
dup_l_nnz);
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange(team, a_row_nnz_begin, a_row_nnz_end),
[&](const size_type nnz, size_type& dupU_inner) {
const auto a_col_idx = A_entries(nnz);
if (a_col_idx >= row_idx) {
for (size_type lu_i = lu_row_nnz_begin; lu_i < lu_row_nnz_end; ++lu_i) {
const auto lu_col_idx = LU_entries(lu_i);
if (a_col_idx == lu_col_idx) {
++dupU_inner;
break;
} else if (lu_col_idx > a_col_idx) {
break;
}
}
}
},
dup_u_nnz);
team.team_barrier();
Kokkos::single(Kokkos::PerTeam(team), [&]() {
const auto l_nnz = (a_l_nnz + lu_l_nnz - dup_l_nnz);
const auto u_nnz = (a_u_nnz + lu_u_nnz - dup_u_nnz);
L_new_row_map(row_idx) = l_nnz;
U_new_row_map(row_idx) = u_nnz;
});
});
// prefix sum
const size_type l_new_nnz_tot = prefix_sum(L_new_row_map);
const size_type u_new_nnz_tot = prefix_sum(U_new_row_map);
Kokkos::resize(L_new_entries, l_new_nnz_tot);
Kokkos::resize(U_new_entries, u_new_nnz_tot);
Kokkos::resize(L_new_values, l_new_nnz_tot);
Kokkos::resize(U_new_values, u_new_nnz_tot);
constexpr auto sentinel = std::numeric_limits<size_type>::max();
// Now compute the actual candidate values
Kokkos::parallel_for(
"add_candidates", range_policy(0, nrows), // No team level parallelism in this alg
KOKKOS_LAMBDA(const size_type row_idx) {
auto a_row_nnz_begin = A_row_map(row_idx);
const auto a_row_nnz_end = A_row_map(row_idx + 1);
const auto a_tot = a_row_nnz_end - a_row_nnz_begin;
auto lu_row_nnz_begin = LU_row_map(row_idx);
const auto lu_row_nnz_end = LU_row_map(row_idx + 1);
const auto lu_tot = lu_row_nnz_end - lu_row_nnz_begin;
const auto tot = a_tot + lu_tot;
size_type l_new_nnz = L_new_row_map(row_idx);
size_type u_new_nnz = U_new_row_map(row_idx);
size_type l_old_begin = L_row_map(row_idx);
size_type l_old_end = L_row_map(row_idx + 1) - 1; // skip diagonal
size_type u_old_begin = U_row_map(row_idx);
size_type u_old_end = U_row_map(row_idx + 1);
bool finished_l = l_old_begin == l_old_end;
bool skip = false;
for (size_type i = 0; i < tot; ++i) {
if (skip) {
skip = false;
continue;
}
const auto a_col = a_row_nnz_begin < a_row_nnz_end ? A_entries(a_row_nnz_begin) : sentinel;
auto a_val = a_row_nnz_begin < a_row_nnz_end ? A_values(a_row_nnz_begin) : 0.;
const auto lu_col = lu_row_nnz_begin < lu_row_nnz_end ? LU_entries(lu_row_nnz_begin) : sentinel;
auto lu_val = lu_row_nnz_begin < lu_row_nnz_end ? LU_values(lu_row_nnz_begin) : 0.;
const size_type col_idx = Kokkos::fmin(a_col, lu_col);
const bool a_active = col_idx == a_col;
const bool lu_active = col_idx == lu_col;
a_val = a_active ? a_val : 0.;
lu_val = lu_active ? lu_val : 0.;
skip = a_active && lu_active;
a_row_nnz_begin += a_active;
lu_row_nnz_begin += lu_active;
const auto r_val = a_val - lu_val;
// load matching entry of L + U
const auto lpu_col =
finished_l ? (u_old_begin < u_old_end ? U_entries(u_old_begin) : sentinel) : L_entries(l_old_begin);
const auto lpu_val =
finished_l ? (u_old_begin < u_old_end ? U_values(u_old_begin) : 0.) : L_values(l_old_begin);
// load diagonal entry of U for lower diagonal entries
const auto diag = col_idx < row_idx ? U_values(U_row_map(col_idx)) : 1.;
// if there is already an entry present, use that instead.
const auto out_val = lpu_col == col_idx ? lpu_val : r_val / diag;
// store output entries
if (row_idx >= col_idx) {
KK_KERNEL_ASSERT_MSG(l_new_nnz < L_new_row_map(row_idx + 1),
"add_candidates: Overflowed L_new, is your A matrix sorted?");
L_new_entries(l_new_nnz) = col_idx;
L_new_values(l_new_nnz) = row_idx == col_idx ? 1. : out_val;
++l_new_nnz;
}
if (row_idx <= col_idx) {
KK_KERNEL_ASSERT_MSG(u_new_nnz < U_new_row_map(row_idx + 1),
"add_candidates: Overflowed U_new, is your A matrix sorted?");
U_new_entries(u_new_nnz) = col_idx;
U_new_values(u_new_nnz) = out_val;
++u_new_nnz;
}
// advance entry of L + U if we used it
if (finished_l) {
u_old_begin += (lpu_col == col_idx);
} else {
l_old_begin += (lpu_col == col_idx);
finished_l = (l_old_begin == l_old_end);
}
}
});
}
/**
* A device-safe lower_bound impl
*/
template <class ForwardIterator, class T>
KOKKOS_FUNCTION static ForwardIterator kok_lower_bound(ForwardIterator first, ForwardIterator last, const T& val) {
ForwardIterator it;
size_t count, step;
count = last - first;
while (count > 0) {
it = first;
step = count / 2;
it += step;
if (*it < val) { // or: if (comp(*it,val)), for version (2)
first = ++it;
count -= step + 1;
} else
count = step;
}
return first;
}
/**
* The compute_sum component of compute_l_u_factors
*/
template <class ARowMapType, class AEntriesType, class AValuesType, class LRowMapType, class LEntriesType,
class LValuesType, class UtRowMapType, class UtEntriesType, class UtValuesType>
KOKKOS_FUNCTION static Kokkos::pair<typename AValuesType::non_const_value_type, size_type> compute_sum(
const size_type row_idx, typename IlutHandle::nnz_lno_t col_idx, const ARowMapType& A_row_map,
const AEntriesType& A_entries, const AValuesType& A_values, const LRowMapType& L_row_map,
const LEntriesType& L_entries, const LValuesType& L_values, const UtRowMapType& Ut_row_map,
const UtEntriesType& Ut_entries, const UtValuesType& Ut_values) {
const auto a_row_nnz_begin = A_row_map(row_idx);
const auto a_row_nnz_end = A_row_map(row_idx + 1);
auto a_nnz_it = kok_lower_bound(A_entries.data() + a_row_nnz_begin, A_entries.data() + a_row_nnz_end, col_idx);
const size_type a_nnz = a_nnz_it - A_entries.data();
const bool has_a = a_nnz < a_row_nnz_end && A_entries(a_nnz) == col_idx;
const auto a_val = has_a ? A_values(a_nnz) : 0.0;
scalar_t sum = 0.0;
size_type ut_nnz = 0;
auto l_row_nnz = L_row_map(row_idx);
const auto l_row_nnz_end = L_row_map(row_idx + 1);
auto ut_row_nnz = Ut_row_map(col_idx);
const auto ut_row_nnz_end = Ut_row_map(col_idx + 1);
const auto last_entry = Kokkos::fmin(row_idx, col_idx);
while (l_row_nnz < l_row_nnz_end && ut_row_nnz < ut_row_nnz_end) {
const auto l_col = L_entries(l_row_nnz);
const auto u_row = Ut_entries(ut_row_nnz);
if (l_col == u_row && l_col < last_entry) {
const scalar_t ut_val = Ut_values(ut_row_nnz);
sum += L_values(l_row_nnz) * ut_val;
}
if (static_cast<size_type>(u_row) == row_idx) {
ut_nnz = ut_row_nnz;
}
l_row_nnz += l_col <= u_row ? 1 : 0;
ut_row_nnz += u_row <= l_col ? 1 : 0;
}
return Kokkos::make_pair(a_val - sum, ut_nnz);
}
/**
* Implements a single iteration/sweep of the fixed-point ILU algorithm.
* The results of this function are non-deterministic due to concurrent
* reading and writing of Ut values. async_update can be set to false to
* make this function determistic, but that could cause par_ilut
* to take longer (more iterations) to converge.
*/
template <bool async_update, class ARowMapType, class AEntriesType, class AValuesType, class LRowMapType,
class LEntriesType, class LValuesType, class URowMapType, class UEntriesType, class UValuesType,
class UtRowMapType, class UtEntriesType, class UtValuesType>
static void compute_l_u_factors_impl(IlutHandle& ih, const ARowMapType& A_row_map, const AEntriesType& A_entries,
const AValuesType& A_values, LRowMapType& L_row_map, LEntriesType& L_entries,
LValuesType& L_values, URowMapType& U_row_map, UEntriesType& U_entries,
UValuesType& U_values, UtRowMapType& Ut_row_map, UtEntriesType& Ut_entries,
UtValuesType& Ut_values_arg) {
// UtValues needs to be Atomic if async updates are on. Otherwise,
// non-atomic is fine.
using UtValuesSafeType = std::conditional_t<
async_update,
Kokkos::View<typename UtValuesType::non_const_value_type*, typename UtValuesType::array_layout,
typename UtValuesType::device_type,
Kokkos::MemoryTraits<Kokkos::Unmanaged | Kokkos::RandomAccess | Kokkos::Atomic> >,
UtValuesType>;
UtValuesSafeType Ut_values = Ut_values_arg;
const size_type nrows = ih.get_nrows();
Kokkos::parallel_for(
"compute_l_u_factors", range_policy(0, nrows), KOKKOS_LAMBDA(const size_type row_idx) {
const auto l_row_nnz_begin = L_row_map(row_idx);
const auto l_row_nnz_end = L_row_map(row_idx + 1) - 1; // skip diagonal for L
for (auto l_nnz = l_row_nnz_begin; l_nnz < l_row_nnz_end; ++l_nnz) {
const auto col_idx = L_entries(l_nnz);
const scalar_t u_diag = Ut_values(Ut_row_map(col_idx + 1) - 1);
if (u_diag != 0.0) {
const auto new_val = compute_sum(row_idx, col_idx, A_row_map, A_entries, A_values, L_row_map, L_entries,
L_values, Ut_row_map, Ut_entries, Ut_values)
.first /
u_diag;
L_values(l_nnz) = new_val;
}
}
const auto u_row_nnz_begin = U_row_map(row_idx);
const auto u_row_nnz_end = U_row_map(row_idx + 1);
for (auto u_nnz = u_row_nnz_begin; u_nnz < u_row_nnz_end; ++u_nnz) {
const auto col_idx = U_entries(u_nnz);
const auto sum = compute_sum(row_idx, col_idx, A_row_map, A_entries, A_values, L_row_map, L_entries,
L_values, Ut_row_map, Ut_entries, Ut_values);
const auto new_val = sum.first;
const auto ut_nnz = sum.second;
U_values(u_nnz) = new_val;
// ut_nnz is not guarateed to fail into range used exclusively
// by this thread. Updating it here opens up potential race
// conditions but usually causes faster convergence.
if (async_update) {
Ut_values(ut_nnz) = new_val;
}
}
});
}
template <class ARowMapType, class AEntriesType, class AValuesType, class LRowMapType, class LEntriesType,
class LValuesType, class URowMapType, class UEntriesType, class UValuesType, class UtRowMapType,
class UtEntriesType, class UtValuesType>
static void compute_l_u_factors(IlutHandle& ih, const ARowMapType& A_row_map, const AEntriesType& A_entries,
const AValuesType& A_values, LRowMapType& L_row_map, LEntriesType& L_entries,
LValuesType& L_values, URowMapType& U_row_map, UEntriesType& U_entries,
UValuesType& U_values, UtRowMapType& Ut_row_map, UtEntriesType& Ut_entries,
UtValuesType& Ut_values, const bool async_update) {
if (async_update) {
compute_l_u_factors_impl<true>(ih, A_row_map, A_entries, A_values, L_row_map, L_entries, L_values, U_row_map,
U_entries, U_values, Ut_row_map, Ut_entries, Ut_values);
} else {
compute_l_u_factors_impl<false>(ih, A_row_map, A_entries, A_values, L_row_map, L_entries, L_values, U_row_map,
U_entries, U_values, Ut_row_map, Ut_entries, Ut_values);
}
}
/**
* Select threshold based on filter rank. Do all this on host
*/
template <class ValuesType, class ValuesCopyType>
static typename IlutHandle::float_t threshold_select(ValuesType& values, const typename IlutHandle::nnz_lno_t rank,
ValuesCopyType& values_copy) {
const index_t size = values.extent(0);
Kokkos::resize(values_copy, size);
Kokkos::deep_copy(values_copy, values);
auto begin = values_copy.data();
auto target = begin + rank;
auto end = begin + size;
std::nth_element(begin, target, end, [](scalar_t a, scalar_t b) { return karith::abs(a) < karith::abs(b); });
return karith::abs(values_copy(rank));
}
template <class IRowMapType, class IEntriesType, class IValuesType, class ORowMapType>
struct ThresholdFilterCountFunctor {
using float_t = typename IlutHandle::float_t;
ThresholdFilterCountFunctor(const float_t threshold_, const IRowMapType& I_row_map_, const IEntriesType& I_entries_,
const IValuesType& I_values_, const ORowMapType& O_row_map_)
: threshold(threshold_),
I_row_map(I_row_map_),
I_entries(I_entries_),
I_values(I_values_),
O_row_map(O_row_map_) {}
KOKKOS_INLINE_FUNCTION void operator()(const member_type& team) const {
const auto row_idx = team.league_rank();
const auto row_nnx_begin = I_row_map(row_idx);
const auto row_nnx_end = I_row_map(row_idx + 1);
size_type count = 0;
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange(team, row_nnx_begin, row_nnx_end),
[&](const size_type nnz, size_type& count_inner) {
if (karith::abs(I_values(nnz)) >= threshold || I_entries(nnz) == row_idx) {
count_inner += 1;
}
},
count);
Kokkos::single(Kokkos::PerTeam(team), [&]() { O_row_map(row_idx) = count; });
}
float_t threshold;
IRowMapType I_row_map;
IEntriesType I_entries;
IValuesType I_values;
ORowMapType O_row_map;
};
template <class IRowMapType, class IEntriesType, class IValuesType, class ORowMapType, class OEntriesType,
class OValuesType>
struct ThresholdFilterAssignFunctor {
using float_t = typename IlutHandle::float_t;
ThresholdFilterAssignFunctor(const float_t threshold_, const IRowMapType& I_row_map_,
const IEntriesType& I_entries_, const IValuesType& I_values_,
const ORowMapType& O_row_map_, const OEntriesType& O_entries_,
const OValuesType& O_values_)
: threshold(threshold_),
I_row_map(I_row_map_),
I_entries(I_entries_),
I_values(I_values_),
O_row_map(O_row_map_),
O_entries(O_entries_),
O_values(O_values_) {}
KOKKOS_INLINE_FUNCTION void operator()(const size_type row_idx) const {
const auto i_row_nnx_begin = I_row_map(row_idx);
const auto i_row_nnx_end = I_row_map(row_idx + 1);
auto onnz = O_row_map(row_idx);
for (size_type innz = i_row_nnx_begin; innz < i_row_nnx_end; ++innz) {
if (karith::abs(I_values(innz)) >= threshold || static_cast<size_type>(I_entries(innz)) == row_idx) {
O_entries(onnz) = I_entries(innz);
O_values(onnz) = I_values(innz);
++onnz;
}
}
}
float_t threshold;
IRowMapType I_row_map;
IEntriesType I_entries;
IValuesType I_values;
ORowMapType O_row_map;
OEntriesType O_entries;
OValuesType O_values;
};
/**
* Remove non-diagnal elements that are below the threshold.
*/
template <class IRowMapType, class IEntriesType, class IValuesType, class ORowMapType, class OEntriesType,
class OValuesType>
static void threshold_filter(IlutHandle& ih, const typename IlutHandle::float_t threshold,
const IRowMapType& I_row_map, const IEntriesType& I_entries, const IValuesType& I_values,
ORowMapType& O_row_map, OEntriesType& O_entries, OValuesType& O_values) {
const auto policy = ih.get_default_team_policy();
const size_type nrows = ih.get_nrows();
Kokkos::parallel_for("threshold_filter count", policy,
ThresholdFilterCountFunctor<IRowMapType, IEntriesType, IValuesType, ORowMapType>(
threshold, I_row_map, I_entries, I_values, O_row_map));
const auto new_nnz = prefix_sum(O_row_map);
Kokkos::resize(O_entries, new_nnz);
Kokkos::resize(O_values, new_nnz);
Kokkos::parallel_for(
"threshold_filter assign", range_policy(0, nrows),
ThresholdFilterAssignFunctor<IRowMapType, IEntriesType, IValuesType, ORowMapType, OEntriesType, OValuesType>(
threshold, I_row_map, I_entries, I_values, O_row_map, O_entries, O_values));
}
/**
* Compute residual norm for R = A - LU
*/
template <class KHandle, class ARowMapType, class AEntriesType, class AValuesType, class LRowMapType,
class LEntriesType, class LValuesType, class URowMapType, class UEntriesType, class UValuesType,
class RRowMapType, class REntriesType, class RValuesType, class LURowMapType, class LUEntriesType,
class LUValuesType>
static typename IlutHandle::nnz_scalar_t compute_residual_norm(
KHandle& kh, IlutHandle& ih, const ARowMapType& A_row_map, const AEntriesType& A_entries,
const AValuesType& A_values, const LRowMapType& L_row_map, const LEntriesType& L_entries,
const LValuesType& L_values, const URowMapType& U_row_map, const UEntriesType& U_entries,
const UValuesType& U_values, RRowMapType& R_row_map, REntriesType& R_entries, RValuesType& R_values,
LURowMapType& LU_row_map, LUEntriesType& LU_entries, LUValuesType& LU_values) {
scalar_t result;
multiply_matrices(kh, ih, L_row_map, L_entries, L_values, U_row_map, U_entries, U_values, LU_row_map, LU_entries,
LU_values);
auto addHandle = kh.get_spadd_handle();
typename KHandle::const_nnz_lno_t m = A_row_map.extent(0) - 1,
n = m; // square matrix
// TODO: let compute_residual_norm also take an execution space argument and
// use that for exec!
typename KHandle::HandleExecSpace exec{};
KokkosSparse::spadd_symbolic(exec, &kh, m, n, A_row_map, A_entries, LU_row_map, LU_entries,
R_row_map);
const size_type r_nnz = addHandle->get_c_nnz();
Kokkos::resize(exec, R_entries, r_nnz);
Kokkos::resize(exec, R_values, r_nnz);
KokkosSparse::spadd_numeric(exec, &kh, m, n, A_row_map, A_entries, A_values, 1., LU_row_map,
LU_entries, LU_values, -1., R_row_map, R_entries, R_values);
// TODO: how to make this policy use exec?
auto policy = ih.get_default_team_policy();
Kokkos::parallel_reduce(
"compute_residual_norm", policy,
KOKKOS_LAMBDA(const member_type& team, scalar_t& total_sum) {
const auto row_idx = team.league_rank();
const auto a_row_nnz_begin = A_row_map(row_idx);
const auto a_row_nnz_end = A_row_map(row_idx + 1);
const auto a_row_entries_begin = A_entries.data() + a_row_nnz_begin;
const auto a_row_entries_end = A_entries.data() + a_row_nnz_end;
const auto r_row_nnz_begin = R_row_map(row_idx);
const auto r_row_nnz_end = R_row_map(row_idx + 1);
scalar_t team_sum = 0.;
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange(team, r_row_nnz_begin, r_row_nnz_end),
[&](const size_type nnz, scalar_t& sum_inner) {
const auto r_col_idx = R_entries(nnz);
const index_t* lb = kok_lower_bound(a_row_entries_begin, a_row_entries_end, r_col_idx);
if (lb != a_row_entries_end && *lb == r_col_idx) {
sum_inner += R_values(nnz) * R_values(nnz);
}
},
team_sum);
Kokkos::single(Kokkos::PerTeam(team), [&]() { total_sum += team_sum; });
},
result);
return karith::sqrt(result);
}
/**
* Set the initial L/U values for the initial approximation
*/
template <class ARowMapType, class AEntriesType, class AValuesType, class LRowMapType, class LEntriesType,
class LValuesType, class URowMapType, class UEntriesType, class UValuesType>
static void initialize_LU(IlutHandle& ih, const ARowMapType& A_row_map, const AEntriesType& A_entries,
const AValuesType& A_values, const LRowMapType& L_row_map, const LEntriesType& L_entries,
const LValuesType& L_values, const URowMapType& U_row_map, const UEntriesType& U_entries,
const UValuesType& U_values) {
const size_type nrows = ih.get_nrows();
Kokkos::parallel_for(
"approx LU values", range_policy(0, nrows), // No team level parallelism in this alg
KOKKOS_LAMBDA(const index_t& row_idx) {
const auto row_nnz_begin = A_row_map(row_idx);
const auto row_nnz_end = A_row_map(row_idx + 1);
size_type current_index_l = L_row_map(row_idx);
size_type current_index_u = U_row_map(row_idx) + 1; // we treat the diagonal separately
// if there is no diagonal value, set it to 1 by default
scalar_t diag = 1.;
for (size_type row_nnz = row_nnz_begin; row_nnz < row_nnz_end; ++row_nnz) {
const auto val = A_values(row_nnz);
const auto col_idx = A_entries(row_nnz);
if (col_idx < row_idx) {
L_entries(current_index_l) = col_idx;
L_values(current_index_l) = val;
++current_index_l;
} else if (col_idx == row_idx) {
// save diagonal
diag = val;
} else {
U_entries(current_index_u) = col_idx;
U_values(current_index_u) = val;
++current_index_u;
}
}
// store diagonal values separately
const auto l_diag_idx = L_row_map(row_idx + 1) - 1;
const auto u_diag_idx = U_row_map(row_idx);
L_entries(l_diag_idx) = row_idx;
U_entries(u_diag_idx) = row_idx;
L_values(l_diag_idx) = 1.;
U_values(u_diag_idx) = diag;
});
}
/**
* The main par_ilut numeric function.
*/
template <class KHandle, class ARowMapType, class AEntriesType, class AValuesType, class LRowMapType,
class LEntriesType, class LValuesType, class URowMapType, class UEntriesType, class UValuesType>
static void ilut_numeric(KHandle& kh, IlutHandle& thandle, const ARowMapType& A_row_map,
const AEntriesType& A_entries, const AValuesType& A_values, LRowMapType& L_row_map,
LEntriesType& L_entries, LValuesType& L_values, URowMapType& U_row_map,
UEntriesType& U_entries, UValuesType& U_values) {
// Get config settings from handle
const size_type nrows = thandle.get_nrows();
const auto fill_in_limit = thandle.get_fill_in_limit();
const auto l_nnz_limit = static_cast<index_t>(fill_in_limit * thandle.get_nnzL());
const auto u_nnz_limit = static_cast<index_t>(fill_in_limit * thandle.get_nnzU());
const auto residual_norm_delta_stop = thandle.get_residual_norm_delta_stop();
const size_type max_iter = thandle.get_max_iter();
const auto verbose = thandle.get_verbose();
const auto async_update = false; // thandle.get_async_update();
if (verbose) {
std::cout << "Starting PARILUT with..." << std::endl;
std::cout << " num_rows: " << nrows << std::endl;
std::cout << " fill_in_limit: " << fill_in_limit << std::endl;
std::cout << " max_iter: " << max_iter << std::endl;
std::cout << " res_norm_delta_stop: " << residual_norm_delta_stop << std::endl;
std::cout << " async_update: " << async_update << std::endl;
}
kh.create_spadd_handle(true /*we expect inputs to be sorted*/);
//
// temporary workspaces and scalars
//
HandleDeviceRowMapType LU_row_map(Kokkos::view_alloc(Kokkos::WithoutInitializing, "LU_row_map"), nrows + 1),
L_new_row_map(Kokkos::view_alloc(Kokkos::WithoutInitializing, "L_new_row_map"), nrows + 1),
U_new_row_map(Kokkos::view_alloc(Kokkos::WithoutInitializing, "U_new_row_map"), nrows + 1),
R_row_map(Kokkos::view_alloc(Kokkos::WithoutInitializing, "R_row_map"), nrows + 1),
Ut_new_row_map("Ut_new_row_map", nrows + 1);
HandleDeviceEntriesType LU_entries, L_new_entries, U_new_entries, Ut_new_entries, R_entries;
HandleDeviceValueType LU_values, L_new_values, U_new_values, Ut_new_values, V_copy_d, R_values;
auto V_copy = Kokkos::create_mirror_view(V_copy_d);
size_type itr = 0;
scalar_t curr_residual = std::numeric_limits<scalar_t>::max();
scalar_t prev_residual = std::numeric_limits<scalar_t>::max();
// Set the initial L/U values for the initial approximation
initialize_LU(thandle, A_row_map, A_entries, A_values, L_row_map, L_entries, L_values, U_row_map, U_entries,
U_values);
//
// main loop
//
bool stop = nrows == 0; // Don't iterate at all if nrows=0
while (!stop && itr < max_iter) {
// LU = L*U
if (prev_residual == std::numeric_limits<scalar_t>::max()) {
multiply_matrices(kh, thandle, L_row_map, L_entries, L_values, U_row_map, U_entries, U_values, LU_row_map,
LU_entries, LU_values);
}
// Identify candidate locations and add them
add_candidates(thandle, A_row_map, A_entries, A_values, L_row_map, L_entries, L_values, U_row_map, U_entries,
U_values, LU_row_map, LU_entries, LU_values, L_new_row_map, L_new_entries, L_new_values,
U_new_row_map, U_new_entries, U_new_values);
// Get transpose of U_new, needed for compute_l_u_factors
transpose_wrap(thandle, U_new_row_map, U_new_entries, U_new_values, Ut_new_row_map, Ut_new_entries,
Ut_new_values);
// Do one sweep of the fixed-point ILU algorithm
compute_l_u_factors(thandle, A_row_map, A_entries, A_values, L_new_row_map, L_new_entries, L_new_values,
U_new_row_map, U_new_entries, U_new_values, Ut_new_row_map, Ut_new_entries, Ut_new_values,
async_update);
// Filter smallest elements from L_new and U_new. Store result back
// in L and U.
{
const index_t l_nnz = L_new_values.extent(0);
const index_t u_nnz = U_new_values.extent(0);
const auto l_filter_rank = std::max(static_cast<index_t>(0), l_nnz - l_nnz_limit - 1);
const auto u_filter_rank = std::max(static_cast<index_t>(0), u_nnz - u_nnz_limit - 1);
const auto l_threshold = threshold_select(L_new_values, l_filter_rank, V_copy);
const auto u_threshold = threshold_select(U_new_values, u_filter_rank, V_copy);
threshold_filter(thandle, l_threshold, L_new_row_map, L_new_entries, L_new_values, L_row_map, L_entries,
L_values);
threshold_filter(thandle, u_threshold, U_new_row_map, U_new_entries, U_new_values, U_row_map, U_entries,
U_values);
}
// Get transpose of U, needed for compute_l_u_factors. Store in Ut_new*
// since we aren't using those temporaries anymore
transpose_wrap(thandle, U_row_map, U_entries, U_values, Ut_new_row_map, Ut_new_entries, Ut_new_values);
// Do one sweep of the fixed-point ILU algorithm
compute_l_u_factors(thandle, A_row_map, A_entries, A_values, L_row_map, L_entries, L_values, U_row_map, U_entries,
U_values, Ut_new_row_map, Ut_new_entries, Ut_new_values, async_update);
// Compute residual and check stop conditions
{
curr_residual = compute_residual_norm(kh, thandle, A_row_map, A_entries, A_values, L_row_map, L_entries,
L_values, U_row_map, U_entries, U_values, R_row_map, R_entries, R_values,
LU_row_map, LU_entries, LU_values);
if (verbose) {
std::cout << "Completed itr " << itr << ", residual is: " << curr_residual << std::endl;
}
const auto curr_delta = karith::abs(prev_residual - curr_residual);
if (curr_delta <= residual_norm_delta_stop) {
if (verbose) {
std::cout << " Itr-to-itr residual change has dropped below "
"residual_norm_delta_stop, stop"
<< std::endl;
}
stop = true;
} else {
prev_residual = curr_residual;
}
}
++itr;
}
curr_residual = nrows == 0 ? scalar_t(0.) : curr_residual;
if (verbose) {
std::cout << "PAR_ILUT stopped in " << itr << " iterations with residual " << curr_residual << std::endl;
}
thandle.set_stats(itr, curr_residual);
kh.destroy_spadd_handle();
} // end ilut_numeric
}; // struct IlutWrap
} // namespace Experimental
} // namespace Impl
} // namespace KokkosSparse
#endif