Skip to content

Commit 46e356a

Browse files
committed
Add conditional multithreaded matvec
1 parent 5fb8281 commit 46e356a

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
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/utils.jl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,19 @@ spmatvec(A::SparseMatrixCSC{TA,S}, x::StridedVector{Tx}) where {TA,S,Tx} =
123123
(T = promote_type(TA, Tx); mul!(one(T), A, x, zero(T), similar(x, T, A.m)))=#
124124

125125
# export spmatvec
126-
using Base.Threads
127-
import Base: *
128-
function *(A::SparseMatrixCSC{T,V}, b::Vector{T}) where {T,V}
129-
m, n = size(A)
130-
ret = zeros(T, m)
131-
@threads for i = 1:n
132-
for j in nzrange(A, i)
133-
row = A.rowval[j]
134-
val = A.nzval[j]
135-
ret[row] += val * b[i]
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
136138
end
139+
ret
137140
end
138-
ret
139141
end

0 commit comments

Comments
 (0)