Last active
April 1, 2018 10:44
-
-
Save haampie/e40272caaa71a64eb778f40fd03d9a26 to your computer and use it in GitHub Desktop.
sparse_mat_vec.jl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| > a, b = bench(100_000, Float64, Int64) | |
| (Trial(704.422 μs), Trial(1.505 ms)) # Julia 0.6 | |
| (Trial(1.354 ms), Trial(735.759 μs)) # Julia 0.7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Ranges, BenchmarkTools | |
| function my_mul!(y::AbstractVector{Tv}, A::SparseMatrixCSC{Tv,Ti}, x::AbstractVector{Tv}) where {Tv,Ti} | |
| fill!(y, zero(Tv)) | |
| @inbounds for i = RightOpen(1, A.n + 1) | |
| xval = x[i] | |
| for j = RightOpen(A.colptr[i], A.colptr[i+1]) | |
| y[A.rowval[j]] += A.nzval[j] * xval | |
| end | |
| end | |
| y | |
| end | |
| function old_mul!(y::AbstractVector{Tv}, A::SparseMatrixCSC{Tv,Ti}, x::AbstractVector{Tv}) where {Tv,Ti} | |
| fill!(y, zero(Tv)) | |
| @inbounds for i = 1 : A.n | |
| xval = x[i] | |
| for j = A.colptr[i] : A.colptr[i+1] - 1 | |
| y[A.rowval[j]] += A.nzval[j] * xval | |
| end | |
| end | |
| y | |
| end | |
| function bench(n = 100_000, ::Type{Tv} = Float64, ::Type{Ti} = Int64) where {Tv,Ti} | |
| mat = spdiagm((fill(-1.0, n - 1), fill(2.0, n), fill(-1.2, n - 1),), (-1, 0, 1)) | |
| A = convert(SparseMatrixCSC{Tv,Ti}, mat) | |
| x = rand(Tv, n) | |
| y = similar(x) | |
| fst = @benchmark my_mul!($y, $A, $x) | |
| snd = @benchmark old_mul!($y, $A, $x) | |
| fst, snd | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment