Skip to content
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
8 changes: 5 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
name = "LazyArrays"
uuid = "5078a376-72f3-5289-bfd5-ec5146d43c02"
version = "2.6.2"
version = "2.7.0"

[deps]
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: local_issparse, local_nnz

#
# Add methods to local_nnz and local_issparse calling back to the corresponding
# methods defined in SparseArrays in case SparseArrays is loaded.
#
local_nnz(A::AbstractSparseArray) = nnz(A)
local_issparse(A::AbstractArray) = issparse(A)

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
import LinearAlgebra.BLAS

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

#
# The following functions introduce a local version of `issparse` and `nnz` which allows to use
# them without 'using SparseArrays'. If SparseArrays is loaded, the
# LazyArraysSparseArraysExt extension adds methods to these functions which
# call back to `issparse` and `nnz` from SparseArrays for the appropriate types.
# This design allows to use e.g. LinearSolve.jl together with LazyArrays.jl without
# triggering the SparseArrays extension of LinearSolve.
#
local_issparse(A) = false
local_nnz(A) = length(A)

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

is_dense = !any(issparse, K.args)
is_dense = !any(local_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_ = [local_issparse(X_h) ? local_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 +236,7 @@ function shuffle_algorithm(
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 = [local_issparse(X_h) ? local_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