Skip to content

Commit 05de88c

Browse files
authored
Merge pull request #29 from ranjanan/mem
Efficiency improvements, add multi-threaded matvec
2 parents db32186 + 46e356a commit 05de88c

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

src/AMG.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module AMG
33
import IterativeSolvers: gauss_seidel!
44
using Base.Threads
55

6+
const MT = false
7+
68
include("utils.jl")
79
export approximate_spectral_radius
810

src/smoother.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,26 @@ function weight(::DiagonalWeighting, S, ω)
124124
end
125125

126126
function weight(::LocalWeighting, S, ω)
127-
D = abs.(S) * ones(size(S, 1))
127+
#=D = abs.(S) * ones(eltype(S), size(S, 1))
128128
D_inv = 1 ./ D[find(D)]
129129
D_inv_S = scale_rows(S, D_inv)
130-
eltype(S)(ω) * D_inv_S
130+
eltype(S)(ω) * D_inv_S=#
131+
D = zeros(eltype(S), size(S,1))
132+
for i = 1:size(S, 1)
133+
for j in nzrange(S, i)
134+
row = S.rowval[j]
135+
val = S.nzval[j]
136+
D[row] += abs(val)
137+
end
138+
end
139+
for i = 1:size(D, 1)
140+
if D[i] != 0
141+
D[i] = 1/D[i]
142+
end
143+
end
144+
D_inv_S = scale_rows(S, D)
145+
# eltype(S)(ω) * D_inv_S
146+
scale!(D_inv_S, eltype(S)(ω))
131147
end
132148

133149
#approximate_spectral_radius(A) =

src/utils.jl

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,45 @@ function approximate_eigenvalues(A, tol, maxiter, symmetric, v0)
9797
end
9898

9999
find_breakdown(::Type{Float64}) = eps(Float64) * 10^6
100-
find_breakdown(::Type{Float32}) = eps(Float64) * 10^3
100+
find_breakdown(::Type{Float32}) = eps(Float64) * 10^3
101+
102+
using Base.Threads
103+
#=function mul!(α::Number, A::SparseMatrixCSC, B::StridedVecOrMat, β::Number, C::StridedVecOrMat)
104+
A.n == size(B, 1) || throw(DimensionMismatch())
105+
A.m == size(C, 1) || throw(DimensionMismatch())
106+
size(B, 2) == size(C, 2) || throw(DimensionMismatch())
107+
nzv = A.nzval
108+
rv = A.rowval
109+
if β != 1
110+
β != 0 ? scale!(C, β) : fill!(C, zero(eltype(C)))
111+
end
112+
for k = 1:size(C, 2)
113+
for col = 1:A.n
114+
αxj = α*B[col,k]
115+
for j = A.colptr[col]:(A.colptr[col + 1] - 1)
116+
C[rv[j], k] += nzv[j]*αxj
117+
end
118+
end
119+
end
120+
C
121+
end
122+
spmatvec(A::SparseMatrixCSC{TA,S}, x::StridedVector{Tx}) where {TA,S,Tx} =
123+
(T = promote_type(TA, Tx); mul!(one(T), A, x, zero(T), similar(x, T, A.m)))=#
124+
125+
# export spmatvec
126+
127+
@static if haskey(ENV, "JULIA_NUM_THREADS")
128+
import Base: *
129+
function *(A::SparseMatrixCSC{T,V}, b::Vector{T}) where {T,V}
130+
m, n = size(A)
131+
ret = zeros(T, m)
132+
@threads for i = 1:n
133+
for j in nzrange(A, i)
134+
row = A.rowval[j]
135+
val = A.nzval[j]
136+
ret[row] += val * b[i]
137+
end
138+
end
139+
ret
140+
end
141+
end

0 commit comments

Comments
 (0)