This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
/
columnwise_lu_solver.jl
908 lines (767 loc) · 25.9 KB
/
columnwise_lu_solver.jl
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
#### Columnwise LU Solver
export ManyColumnLU, SingleColumnLU
abstract type AbstractColumnLUSolver <: AbstractSystemSolver end
"""
ManyColumnLU()
This solver is used for systems that are block diagonal where each block is
associated with a column of the mesh. The systems are solved using a
non-pivoted LU factorization.
"""
struct ManyColumnLU <: AbstractColumnLUSolver end
"""
SingleColumnLU()
This solver is used for systems that are block diagonal where each block is
associated with a column of the mesh. Moreover, each block is assumed to be
the same. The systems are solved using a non-pivoted LU factorization.
"""
struct SingleColumnLU <: AbstractColumnLUSolver end
struct ColumnwiseLU{AT}
A::AT
end
struct DGColumnBandedMatrix{D, P, NS, EH, EV, EB, SC, AT}
data::AT
end
DGColumnBandedMatrix(
A::DGColumnBandedMatrix{D, P, NS, EH, EV, EB, SC},
data,
) where {D, P, NS, EH, EV, EB, SC} =
DGColumnBandedMatrix{D, P, NS, EH, EV, EB, SC, typeof(data)}(data)
Base.eltype(A::DGColumnBandedMatrix) = eltype(A.data)
Base.size(A::DGColumnBandedMatrix) = size(A.data)
dimensionality(::DGColumnBandedMatrix{D}) where {D} = D
polynomialorder(::DGColumnBandedMatrix{D, P}) where {D, P} = P
num_state(::DGColumnBandedMatrix{D, P, NS}) where {D, P, NS} = NS
num_horz_elem(::DGColumnBandedMatrix{D, P, NS, EH}) where {D, P, NS, EH} = EH
num_vert_elem(
::DGColumnBandedMatrix{D, P, NS, EH, EV},
) where {D, P, NS, EH, EV} = EV
elem_band(
::DGColumnBandedMatrix{D, P, NS, EH, EV, EB},
) where {D, P, NS, EH, EV, EB} = EB
single_column(
::DGColumnBandedMatrix{D, P, NS, EH, EV, EB, SC},
) where {D, P, NS, EH, EV, EB, SC} = SC
lower_bandwidth(A::DGColumnBandedMatrix) =
(polynomialorder(A) + 1) * num_state(A) * elem_band(A) - 1
upper_bandwidth(A::DGColumnBandedMatrix) = lower_bandwidth(A)
Base.reshape(A::DGColumnBandedMatrix, args...) =
DGColumnBandedMatrix(A, reshape(A.data, args...))
Adapt.adapt_structure(to, A::DGColumnBandedMatrix) =
DGColumnBandedMatrix(A, adapt(to, A.data))
Base.@propagate_inbounds function Base.getindex(A::DGColumnBandedMatrix, I...)
return A.data[I...]
end
Base.@propagate_inbounds function Base.setindex!(
A::DGColumnBandedMatrix,
val,
I...,
)
A.data[I...] = val
end
function prefactorize(op, solver::AbstractColumnLUSolver, Q, args...)
dg = op.f!
# TODO: can we get away with just passing the grid?
A = banded_matrix(
op,
dg,
similar(Q),
similar(Q),
args...;
single_column = typeof(solver) <: SingleColumnLU,
)
band_lu!(A)
ColumnwiseLU(A)
end
function linearsolve!(
linop,
clu::ColumnwiseLU,
::AbstractColumnLUSolver,
Q,
Qrhs,
args...,
)
A = clu.A
Q .= Qrhs
band_forward!(Q, A)
band_back!(Q, A)
end
"""
band_lu!(A)
"""
function band_lu!(A)
device = array_device(A.data)
nstate = num_state(A)
Nq = polynomialorder(A) + 1
Nqj = dimensionality(A) == 2 ? 1 : Nq
nhorzelem = num_horz_elem(A)
groupsize = (Nq, Nqj)
ndrange = (nhorzelem * Nq, Nqj)
if single_column(A)
# single column case
#
# TODO Would it be faster to copy the matrix to the host and factorize it
# there?
groupsize = (1, 1)
ndrange = groupsize
A = reshape(A, 1, 1, size(A)..., 1)
end
event = Event(device)
event = band_lu_kernel!(device, groupsize)(
A,
ndrange = ndrange,
dependencies = (event,),
)
wait(device, event)
end
function band_forward!(Q, A)
device = array_device(Q)
Nq = polynomialorder(A) + 1
Nqj = dimensionality(A) == 2 ? 1 : Nq
nhorzelem = num_horz_elem(A)
event = Event(device)
event = band_forward_kernel!(device, (Nq, Nqj))(
Q.data,
A,
ndrange = (nhorzelem * Nq, Nqj),
dependencies = (event,),
)
wait(device, event)
end
function band_back!(Q, A)
device = array_device(Q)
Nq = polynomialorder(A) + 1
Nqj = dimensionality(A) == 2 ? 1 : Nq
nhorzelem = num_horz_elem(A)
event = Event(device)
event = band_back_kernel!(device, (Nq, Nqj))(
Q.data,
A,
ndrange = (nhorzelem * Nq, Nqj),
dependencies = (event,),
)
wait(device, event)
end
"""
banded_matrix(
dg::DGModel,
Q::MPIStateArray = MPIStateArray(dg),
dQ::MPIStateArray = MPIStateArray(dg);
single_column = false,
)
Forms the banded matrices for each the column operator defined by the `DGModel`
dg. If `single_column=false` then a banded matrix is stored for each column and
if `single_column=true` only the banded matrix associated with the first column
of the first element is stored. The bandwidth of the DG column banded matrix is
`p = q = (polynomialorder + 1) * nstate * eband - 1` with `p` and `q` being
the upper and lower bandwidths.
The banded matrices are stored in the LAPACK band storage format
<https://www.netlib.org/lapack/lug/node124.html>.
The banded matrices are returned as an arrays where the array type matches that
of `Q`. If `single_column=false` then the returned array has 5 dimensions, which
are:
- first horizontal column index
- second horizontal column index
- band index (-q:p)
- vertical DOF index with state `s`, vertical DOF index `k`, and vertical
element `ev` mapping to `s + nstate * (k - 1) + nstate * nvertelem * (ev - 1)`
- horizontal element index
If the `single_column=true` then the returned array has 2 dimensions which are
the band index and the vertical DOF index.
"""
function banded_matrix(
dg::DGModel,
Q::MPIStateArray = MPIStateArray(dg),
dQ::MPIStateArray = MPIStateArray(dg);
single_column = false,
)
banded_matrix(
(dQ, Q) -> dg(dQ, Q, nothing, 0; increment = false),
dg,
Q,
dQ;
single_column = single_column,
)
end
"""
banded_matrix(
f!,
dg::DGModel,
Q::MPIStateArray = MPIStateArray(dg),
dQ::MPIStateArray = MPIStateArray(dg),
args...;
single_column = false,
)
Forms the banded matrices for each the column operator defined by the linear
operator `f!` which is assumed to have the same banded structure as the
`DGModel` dg. If `single_column=false` then a banded matrix is stored for each
column and if `single_column=true` only the banded matrix associated with the
first column of the first element is stored. The bandwidth of the DG column
banded matrix is `p = q = (polynomialorder + 1) * nstate * eband - 1` with
`p` and `q` being the upper and lower bandwidths.
The banded matrices are stored in the LAPACK band storage format
<https://www.netlib.org/lapack/lug/node124.html>.
The banded matrices are returned as an arrays where the array type matches that
of `Q`. If `single_column=false` then the returned array has 5 dimensions, which
are:
- first horizontal column index
- second horizontal column index
- band index (-q:p)
- vertical DOF index with state `s`, vertical DOF index `k`, and vertical
element `ev` mapping to `s + nstate * (k - 1) + nstate * nvertelem * (ev - 1)`
- horizontal element index
If the `single_column=true` then the returned array has 2 dimensions which are
the band index and the vertical DOF index.
Here `args` are passed to `f!`.
"""
function banded_matrix(
f!,
dg::DGModel,
Q::MPIStateArray = MPIStateArray(dg),
dQ::MPIStateArray = MPIStateArray(dg),
args...;
single_column = false,
)
# Initialize banded matrix data structure
A = empty_banded_matrix(dg, Q; single_column = single_column)
# Populate matrix with data
update_banded_matrix!(
A,
f!,
dg,
Q,
dQ,
args...;
single_column = single_column,
)
A
end
"""
empty_banded_matrix(
dg::DGModel,
Q::MPIStateArray;
single_column = false,
)
Initializes an empty banded matrix stored in the LAPACK band storage format
<https://www.netlib.org/lapack/lug/node124.html>.
"""
function empty_banded_matrix(
dg::DGModel,
Q::MPIStateArray;
single_column = false,
)
bl = dg.balance_law
grid = dg.grid
topology = grid.topology
@assert isstacked(topology)
@assert typeof(dg.direction) <: VerticalDirection
FT = eltype(Q.data)
device = array_device(Q)
nstate = number_states(bl, Prognostic())
N = polynomialorder(grid)
Nq = N + 1
# p is lower bandwidth
# q is upper bandwidth
eband = number_states(bl, GradientFlux()) == 0 ? 1 : 2
p = q = nstate * Nq * eband - 1
nrealelem = length(topology.realelems)
nvertelem = topology.stacksize
nhorzelem = div(nrealelem, nvertelem)
dim = dimensionality(grid)
Nqj = dim == 2 ? 1 : Nq
# first horizontal DOF index
# second horizontal DOF index
# band index -q:p
# vertical DOF index
# horizontal element index
A = if single_column
similar(Q.data, p + q + 1, Nq * nstate * nvertelem)
else
similar(Q.data, Nq, Nqj, p + q + 1, Nq * nstate * nvertelem, nhorzelem)
end
fill!(A, zero(FT))
A = DGColumnBandedMatrix{
dim,
N,
nstate,
nhorzelem,
nvertelem,
eband,
single_column,
typeof(A),
}(
A,
)
A
end
"""
update_banded_matrix!(
A::DGColumnBandedMatrix,
f!,
dg::DGModel,
Q::MPIStateArray = MPIStateArray(dg),
dQ::MPIStateArray = MPIStateArray(dg),
args...;
single_column = false,
)
Updates the banded matrices for each the column operator defined by the linear
operator `f!` which is assumed to have the same banded structure as the
`DGModel` dg. If `single_column=false` then a banded matrix is stored for each
column and if `single_column=true` only the banded matrix associated with the
first column of the first element is stored. The bandwidth of the DG column
banded matrix is `p = q = (polynomialorder + 1) * nstate * eband - 1` with
`p` and `q` being the upper and lower bandwidths.
Here `args` are passed to `f!`.
"""
function update_banded_matrix!(
A::DGColumnBandedMatrix,
f!,
dg::DGModel,
Q::MPIStateArray = MPIStateArray(dg),
dQ::MPIStateArray = MPIStateArray(dg),
args...;
single_column = false,
)
bl = dg.balance_law
grid = dg.grid
topology = grid.topology
@assert isstacked(topology)
@assert typeof(dg.direction) <: VerticalDirection
FT = eltype(Q.data)
device = array_device(Q)
nstate = number_states(bl, Prognostic())
N = polynomialorder(grid)
Nq = N + 1
# p is lower bandwidth
# q is upper bandwidth
eband = number_states(bl, GradientFlux()) == 0 ? 1 : 2
p = q = nstate * Nq * eband - 1
nrealelem = length(topology.realelems)
nvertelem = topology.stacksize
nhorzelem = div(nrealelem, nvertelem)
dim = dimensionality(grid)
Nqj = dim == 2 ? 1 : Nq
# loop through all DOFs in a column and compute the matrix column
# loop only the first min(nvertelem, 2eband+1) elements
# in each element loop, updating these columns correspond
# to elements (ev :2eband+1 : nvertelem)
for ev in 1:min(nvertelem, 2eband + 1)
for s in 1:nstate
for k in 1:Nq
# Set a single 1 per column and rest 0
event = Event(device)
event = kernel_set_banded_data!(device, (Nq, Nqj, Nq))(
bl,
Val(dim),
Val(nstate),
Val(N),
Val(nvertelem),
Q.data,
k,
s,
ev,
1:nhorzelem,
1:nvertelem;
ndrange = (nvertelem * Nq, nhorzelem * Nqj, Nq),
dependencies = (event,),
)
wait(device, event)
# Get the matrix column
f!(dQ, Q, args...)
# Store the banded matrix
event = Event(device)
event = kernel_set_banded_matrix!(device, (Nq, Nqj, Nq))(
A,
dQ.data,
k,
s,
ev,
1:nhorzelem,
(-eband):eband;
ndrange = ((2eband + 1) * Nq, nhorzelem * Nqj, Nq),
dependencies = (event,),
)
wait(device, event)
end
end
end
end
"""
banded_matrix_vector_product!(
A,
dQ::MPIStateArray,
Q::MPIStateArray
)
Compute a matrix vector product `dQ = A * Q` where `A` is assumed to be a matrix
created using the `banded_matrix` function.
This function is primarily for testing purposes.
"""
function banded_matrix_vector_product!(A, dQ::MPIStateArray, Q::MPIStateArray)
device = array_device(Q)
Nq = polynomialorder(A) + 1
Nqj = dimensionality(A) == 2 ? 1 : Nq
nvertelem = num_vert_elem(A)
nhorzelem = num_horz_elem(A)
event = Event(device)
event = kernel_banded_matrix_vector_product!(device, (Nq, Nqj, Nq))(
dQ.data,
A,
Q.data,
1:nhorzelem,
1:nvertelem;
ndrange = (nvertelem * Nq, nhorzelem * Nqj, Nq),
dependencies = (event,),
)
wait(device, event)
end
using StaticArrays
using KernelAbstractions.Extras: @unroll
@doc """
band_lu_kernel!(A)
This performs Band Gaussian Elimination (Algorithm 4.3.1 of Golub and Van
Loan). The array `A` contains a band matrix for each vertical column. For
example, `A[i, j, :, :, h]`, is the band matrix associated with the `(i, j)`th
degree of freedom in the horizontal element `h`.
Each `n` by `n` band matrix is assumed to have upper bandwidth `q` and lower
bandwidth `p` where `n = nstate * Nq * nvertelem` and `p = q = nstate * Nq *
eband - 1`.
Each band matrix is stored in the [LAPACK band storage](https://www.netlib.org/lapack/lug/node124.html).
For example the band matrix
B = [b₁₁ b₁₂ 0 0 0
b₂₁ b₂₂ b₂₃ 0 0
b₃₁ b₃₂ b₃₃ b₃₄ 0
0 b₄₂ b₄₃ b₄₄ b₄₅
0 0 b₅₃ b₅₄ b₅₅]
is stored as
B = [0 b₁₂ b₂₃ b₃₄ b₄₅
b₁₁ b₂₂ b₃₃ b₄₄ b₅₅
b₂₁ b₃₂ b₄₃ b₅₄ 0
b₃₁ b₄₂ b₅₃ 0 0]
### Reference
@book{GolubVanLoan,
title = {Matrix Computations},
author = {Gene H. Golub and Charles F. Van Loan},
edition = {4th},
isbn = {9781421407944},
publisher = {Johns Hopkins University Press},
address = {Baltimore, MD, USA},
url = {http://www.cs.cornell.edu/cv/GVL4/golubandvanloan.htm},
year = 2013
}
""" band_lu_kernel!
@kernel function band_lu_kernel!(A)
@uniform begin
Nq = polynomialorder(A) + 1
nstate = num_state(A)
nvertelem = num_vert_elem(A)
n = nstate * Nq * nvertelem
p, q = lower_bandwidth(A), upper_bandwidth(A)
end
h = @index(Group, Linear)
i, j = @index(Local, NTuple)
@inbounds begin
for v in 1:nvertelem
for k in 1:Nq
for s in 1:nstate
kk = s + (k - 1) * nstate + (v - 1) * nstate * Nq
Aq = A[i, j, q + 1, kk, h]
for ii in 1:p
A[i, j, q + ii + 1, kk, h] /= Aq
end
for jj in 1:q
if jj + kk ≤ n
Ajj = A[i, j, q - jj + 1, jj + kk, h]
for ii in 1:p
A[i, j, q + ii - jj + 1, jj + kk, h] -=
A[i, j, q + ii + 1, kk, h] * Ajj
end
end
end
end
end
end
end
end
@doc """
band_forward_kernel!(b, LU)
This performs Band Forward Substitution (Algorithm 4.3.2 of Golub and Van
Loan), i.e., the right-hand side `b` is replaced with the solution of `L*x=b`.
The array `b` is of the size `(Nq * Nqj * Nq, nstate, nvertelem * nhorzelem)`.
The LU-factorization array `LU` contains a single band matrix or one
for each vertical column, see [`band_lu!`](@ref).
Each `n` by `n` band matrix is assumed to have upper bandwidth `q` and lower
bandwidth `p` where `n = nstate * Nq * nvertelem` and `p = q = nstate * Nq *
eband - 1`.
### Reference
@book{GolubVanLoan,
title = {Matrix Computations},
author = {Gene H. Golub and Charles F. Van Loan},
edition = {4th},
isbn = {9781421407944},
publisher = {Johns Hopkins University Press},
address = {Baltimore, MD, USA},
url = {http://www.cs.cornell.edu/cv/GVL4/golubandvanloan.htm},
year = 2013
}
""" band_forward_kernel!
@kernel function band_forward_kernel!(b, LU)
@uniform begin
FT = eltype(b)
nstate = num_state(LU)
Nq = polynomialorder(LU) + 1
Nqj = dimensionality(LU) == 2 ? 1 : Nq
nvertelem = num_vert_elem(LU)
n = nstate * Nq * nvertelem
eband = elem_band(LU)
p, q = lower_bandwidth(LU), upper_bandwidth(LU)
l_b = MArray{Tuple{p + 1}, FT}(undef)
end
h = @index(Group, Linear)
i, j = @index(Local, NTuple)
@inbounds begin
@unroll for v in 1:eband
@unroll for k in 1:Nq
@unroll for s in 1:nstate
ijk = i + Nqj * (j - 1) + Nq * Nqj * (k - 1)
ee = v + nvertelem * (h - 1)
ii = s + (k - 1) * nstate + (v - 1) * nstate * Nq
l_b[ii] = nvertelem ≥ v ? b[ijk, s, ee] : zero(FT)
end
end
end
for v in 1:nvertelem
@unroll for k in 1:Nq
@unroll for s in 1:nstate
jj = s + (k - 1) * nstate + (v - 1) * nstate * Nq
@unroll for ii in 2:(p + 1)
Lii = single_column(LU) ? LU[ii + q, jj] :
LU[i, j, ii + q, jj, h]
l_b[ii] -= Lii * l_b[1]
end
ijk = i + Nqj * (j - 1) + Nq * Nqj * (k - 1)
ee = v + nvertelem * (h - 1)
b[ijk, s, ee] = l_b[1]
@unroll for ii in 1:p
l_b[ii] = l_b[ii + 1]
end
if jj + p < n
(idx, si) = fldmod1(jj + p + 1, nstate)
(vi, ki) = fldmod1(idx, Nq)
ijk = i + Nqj * (j - 1) + Nq * Nqj * (ki - 1)
ee = vi + nvertelem * (h - 1)
l_b[p + 1] = b[ijk, si, ee]
end
end
end
end
end
end
@doc """
band_back_kernel!(b, LU)
This performs Band Back Substitution (Algorithm 4.3.3 of Golub and Van
Loan), i.e., the right-hand side `b` is replaced with the solution of `U*x=b`.
The array `b` is of the size `(Nq * Nqj * Nq, nstate, nvertelem * nhorzelem)`.
The LU-factorization array `LU` contains a single band matrix or one
for each vertical column, see [`band_lu!`](@ref).
Each `n` by `n` band matrix is assumed to have upper bandwidth `q` and lower
bandwidth `p` where `n = nstate * Nq * nvertelem` and `p = q = nstate * Nq *
eband - 1`.
### Reference
@book{GolubVanLoan,
title = {Matrix Computations},
author = {Gene H. Golub and Charles F. Van Loan},
edition = {4th},
isbn = {9781421407944},
publisher = {Johns Hopkins University Press},
address = {Baltimore, MD, USA},
url = {http://www.cs.cornell.edu/cv/GVL4/golubandvanloan.htm},
year = 2013
}
""" band_back_kernel!
@kernel function band_back_kernel!(b, LU)
@uniform begin
FT = eltype(b)
nstate = num_state(LU)
Nq = polynomialorder(LU) + 1
Nqj = dimensionality(LU) == 2 ? 1 : Nq
nvertelem = num_vert_elem(LU)
n = nstate * Nq * nvertelem
q = upper_bandwidth(LU)
eband = elem_band(LU)
l_b = MArray{Tuple{q + 1}, FT}(undef)
end
h = @index(Group, Linear)
i, j = @index(Local, NTuple)
@inbounds begin
@unroll for v in nvertelem:-1:(nvertelem - eband + 1)
@unroll for k in Nq:-1:1
@unroll for s in nstate:-1:1
vi = eband - nvertelem + v
ii = s + (k - 1) * nstate + (vi - 1) * nstate * Nq
ijk = i + Nqj * (j - 1) + Nq * Nqj * (k - 1)
ee = v + nvertelem * (h - 1)
l_b[ii] = b[ijk, s, ee]
end
end
end
for v in nvertelem:-1:1
@unroll for k in Nq:-1:1
@unroll for s in nstate:-1:1
jj = s + (k - 1) * nstate + (v - 1) * nstate * Nq
l_b[q + 1] /= single_column(LU) ? LU[q + 1, jj] :
LU[i, j, q + 1, jj, h]
@unroll for ii in 1:q
Uii =
single_column(LU) ? LU[ii, jj] : LU[i, j, ii, jj, h]
l_b[ii] -= Uii * l_b[q + 1]
end
ijk = i + Nqj * (j - 1) + Nq * Nqj * (k - 1)
ee = v + nvertelem * (h - 1)
b[ijk, s, ee] = l_b[q + 1]
@unroll for ii in q:-1:1
l_b[ii + 1] = l_b[ii]
end
if jj - q > 1
(idx, si) = fldmod1(jj - q - 1, nstate)
(vi, ki) = fldmod1(idx, Nq)
ijk = i + Nqj * (j - 1) + Nq * Nqj * (ki - 1)
ee = vi + nvertelem * (h - 1)
l_b[1] = b[ijk, si, ee]
end
end
end
end
end
end
@kernel function kernel_set_banded_data!(
bl::BalanceLaw,
::Val{dim},
::Val{nstate},
::Val{N},
::Val{nvertelem},
Q,
kin,
sin,
evin0,
helems,
velems,
) where {dim, N, nstate, nvertelem}
@uniform begin
FT = eltype(Q)
Nq = N + 1
Nqj = dim == 2 ? 1 : Nq
eband = number_states(bl, GradientFlux()) == 0 ? 1 : 2
end
ev, eh = @index(Group, NTuple)
i, j, k = @index(Local, NTuple)
@inbounds begin
e = ev + (eh - 1) * nvertelem
ijk = i + Nqj * (j - 1) + Nq * Nqj * (k - 1)
@unroll for s in 1:nstate
if k == kin && s == sin && ((ev - evin0) % (2eband + 1) == 0)
Q[ijk, s, e] = 1
else
Q[ijk, s, e] = 0
end
end
end
end
@kernel function kernel_set_banded_matrix!(
A,
dQ,
kin,
sin,
evin0,
helems,
vpelems,
)
@uniform begin
FT = eltype(A)
nstate = num_state(A)
Nq = polynomialorder(A) + 1
Nqj = dimensionality(A) == 2 ? 1 : Nq
nvertelem = num_vert_elem(A)
p = lower_bandwidth(A)
q = upper_bandwidth(A)
eband = elem_band(A)
eshift = elem_band(A) + 1
end
ep, eh = @index(Group, NTuple)
ep = ep - eshift
i, j, k = @index(Local, NTuple)
for evin in evin0:(2eband + 1):nvertelem
# sin, kin, evin are the state, vertical fod, and vert element we are
# handling
# column index of matrix
jj = sin + (kin - 1) * nstate + (evin - 1) * nstate * Nq
# one thread is launch for dof that might contribute to column jj's band
@inbounds begin
# ep is the shift we need to add to evin to get the element we need to
# consider
ev = ep + evin
if 1 ≤ ev ≤ nvertelem
e = ev + (eh - 1) * nvertelem
ijk = i + Nqj * (j - 1) + Nq * Nqj * (k - 1)
@unroll for s in 1:nstate
# row index of matrix
ii = s + (k - 1) * nstate + (ev - 1) * nstate * Nq
# row band index
bb = ii - jj
# make sure we're in the bandwidth
if -q ≤ bb ≤ p
if !single_column(A)
A[i, j, bb + q + 1, jj, eh] = dQ[ijk, s, e]
else
if (i, j, eh) == (1, 1, 1)
A[bb + q + 1, jj] = dQ[ijk, s, e]
end
end
end
end
end
end
end
end
@kernel function kernel_banded_matrix_vector_product!(dQ, A, Q, helems, velems)
@uniform begin
FT = eltype(A)
nstate = num_state(A)
Nq = polynomialorder(A) + 1
Nqj = dimensionality(A) == 2 ? 1 : Nq
nvertelem = num_vert_elem(A)
p = lower_bandwidth(A)
q = upper_bandwidth(A)
elo = div(q, Nq * nstate - 1)
eup = div(p, Nq * nstate - 1)
end
ev, eh = @index(Group, NTuple)
i, j, k = @index(Local, NTuple)
# matrix row loops
@inbounds begin
e = ev + nvertelem * (eh - 1)
@unroll for s in 1:nstate
Ax = -zero(FT)
ii = s + (k - 1) * nstate + (ev - 1) * nstate * Nq
# banded matrix column loops
@unroll for evv in max(1, ev - elo):min(nvertelem, ev + eup)
ee = evv + nvertelem * (eh - 1)
@unroll for kk in 1:Nq
ijk = i + Nqj * (j - 1) + Nq * Nqj * (kk - 1)
@unroll for ss in 1:nstate
jj = ss + (kk - 1) * nstate + (evv - 1) * nstate * Nq
bb = ii - jj
if -q ≤ bb ≤ p
if !single_column(A)
Ax +=
A[i, j, bb + q + 1, jj, eh] * Q[ijk, ss, ee]
else
Ax += A[bb + q + 1, jj] * Q[ijk, ss, ee]
end
end
end
end
end
ijk = i + Nqj * (j - 1) + Nq * Nqj * (k - 1)
dQ[ijk, s, e] = Ax
end
end
end