Skip to content

Move SparseArrays dependency to extension #365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[weakdeps]
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[extensions]
LazyArraysBandedMatricesExt = "BandedMatrices"
LazyArraysBlockArraysExt = "BlockArrays"
LazyArraysBlockBandedMatricesExt = "BlockBandedMatrices"
LazyArraysSparseArraysExt = "SparseArrays"
LazyArraysStaticArraysExt = "StaticArrays"

[compat]
Expand Down Expand Up @@ -48,8 +49,9 @@ BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0"
Infinities = "e1ba4f0e-776d-440f-acd9-e1d2e9742647"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"

[targets]
test = ["Aqua", "BandedMatrices", "Base64", "BlockArrays", "BlockBandedMatrices", "StaticArrays", "Tracker", "Test", "Infinities", "Random"]
test = ["Aqua", "BandedMatrices", "Base64", "BlockArrays", "BlockBandedMatrices", "StaticArrays", "SparseArrays", "Tracker", "Test", "Infinities", "Random"]
13 changes: 13 additions & 0 deletions ext/LazyArraysSparseArraysExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module LazyArraysSparseArraysExt

using SparseArrays: issparse, nnz, AbstractSparseArray
import LazyArrays: my_issparse, my_nnz

my_nnz(A::AbstractSparseArray) = nnz(A)

my_issparse(A::AbstractArray) = issparse(A)
my_issparse(A::DenseArray) = issparse(A)
my_issparse(S::AbstractSparseArray) = issparse(S)


end
2 changes: 1 addition & 1 deletion src/LazyArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module LazyArrays
# Use README as the docstring of the module:
@doc read(joinpath(dirname(@__DIR__), "README.md"), String) LazyArrays

using Base.Broadcast, LinearAlgebra, FillArrays, ArrayLayouts, SparseArrays
using Base.Broadcast, LinearAlgebra, FillArrays, ArrayLayouts #, SparseArrays
import LinearAlgebra.BLAS

import Base: *, +, -, /, <, ==, >, \, ≤, ≥, (:), @_gc_preserve_begin, @_gc_preserve_end, @propagate_inbounds,
Expand Down
8 changes: 5 additions & 3 deletions src/lazyoperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@
return shuffle_algorithm(algo_type, M.A, M.B, eltype(M))
end

my_issparse(A) = false
my_nnz(A) = prod(size(A))

Check warning on line 165 in src/lazyoperations.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyoperations.jl#L164-L165

Added lines #L164 - L165 were not covered by tests

function shuffle_algorithm(
::ModifiedShuffle, K::Kron{T,2} where T, p::AbstractVecOrMat, OT::Type{<:Number}
Expand All @@ -184,11 +186,11 @@
R_H::Vector{Vector{Int}} = []
C_H::Vector{Vector{Int}} = []

is_dense = !any(issparse, K.args)
is_dense = !any(my_issparse, K.args)

# note: the following computation costs are for multiplication against
# a single vector.
nnz_ = [issparse(X_h) ? nnz(X_h) : prod(size(X_h)) for X_h in K.args]
nnz_ = [my_issparse(X_h) ? my_nnz(X_h) : prod(size(X_h)) for X_h in K.args]
trad_cost = 2*sum([
prod(size.(K.args[1:h-1], 2)) * nnz_[h] * prod(size.(K.args[h+1:end], 1))
for (h, X_h) in enumerate(K.args)
Expand Down Expand Up @@ -226,7 +228,7 @@
return shuffle_algorithm(Shuffle(), K, p, OT)
end

nnz_m = [issparse(X_h) ? nnz(X_h) : prod(size(X_h)) for X_h in K_shrunk_factors]
nnz_m = [my_issparse(X_h) ? my_nnz(X_h) : prod(size(X_h)) for X_h in K_shrunk_factors]
modified_cost = 2*sum([
prod(length.(C_H[1:h-1])) * nnz_m[h] * prod(length.(R_H[h+1:end]))
for (h, X_h) in enumerate(K.args)
Expand Down
Loading