Skip to content

Commit e15ef16

Browse files
committed
pre-allocate in Jacobi smoother
1 parent ca7ba87 commit e15ef16

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/smoother.jl

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,40 @@ function gs!(A, b, x, start, step, stop)
4848
end
4949
end
5050

51-
struct Jacobi{T} <: Smoother
51+
struct Jacobi{T,TX} <: Smoother
5252
ω::T
53+
temp::TX
5354
end
55+
Jacobi=0.5, x) = Jacobi{T,TX}(ω, similar(x))
5456

55-
function jacobi!(A, x, b, ω, start, step, stop)
57+
function (jacobi::Jacobi)(A, x, b, ω, start, step, stop)
5658

5759
one = one(eltype(A))
58-
temp = similar(x)
60+
temp = jacobi.temp
5961

60-
for i = start:step:stop
61-
temp[i] = x[i]
62-
end
62+
@inbounds for col in 1:size(x, 2)
63+
for i = start:step:stop
64+
temp[i, col] = x[i, col]
65+
end
6366

64-
for i = start:step:stop
65-
rsum = zero(eltype(A))
66-
diag = zero(eltype(A))
67+
for i = start:step:stop
68+
rsum = zero(eltype(A))
69+
diag = zero(eltype(A))
6770

68-
for j in nzrange(A, i)
69-
row = A.nzval[j]
70-
val = A.nzval[j]
71+
for j in nzrange(A, i)
72+
row = A.rowval[j]
73+
val = A.nzval[j]
7174

72-
if row == i
73-
diag = val
74-
else
75-
rsum += val * temp[row]
75+
if row == i
76+
diag = val
77+
else
78+
rsum += val * temp[row, col]
79+
end
7680
end
77-
end
7881

79-
if diag != 0
80-
x[i] = (one - ω) * temp[i] + ω * ((b[i] - rsum) / diag)
82+
if diag != 0
83+
x[i, col] = (one - ω) * temp[i, col] + ω * ((b[i, col] - rsum) / diag)
84+
end
8185
end
8286
end
8387
end

0 commit comments

Comments
 (0)