Skip to content

Commit

Permalink
Improve inlining, faster Lazy mul getindex (#268)
Browse files Browse the repository at this point in the history
* Improve inlining, faster Lazy mul getindex

* Fix _permutedims

* fix instantiate for lazy arrays

* Update lazyconcat.jl

* Require Julia v1.9
  • Loading branch information
dlfivefifty authored Aug 22, 2023
1 parent ca0ebf5 commit 4a1c403
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6'
- '1' # Latest stable release
- '1.9'
os:
- ubuntu-latest
- macOS-latest
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "LazyArrays"
uuid = "5078a376-72f3-5289-bfd5-ec5146d43c02"
version = "1.5.2"
version = "1.6"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand All @@ -24,7 +24,7 @@ FillArrays = "1.0"
MacroTools = "0.5"
MatrixFactorizations = "1.0, 2.0"
StaticArrays = "1.0"
julia = "1.6"
julia = "1.9"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Expand Down
21 changes: 12 additions & 9 deletions src/lazyconcat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ Vcat(A...) = ApplyArray(vcat, A...)
Vcat{T}(A...) where T = ApplyArray{T}(vcat, A...)
Vcat() = Vcat{Any}()

Vcat(A::AbstractVector...) = ApplyVector(vcat, A...)
Vcat{T}(A::AbstractVector...) where T = ApplyVector{T}(vcat, A...)

function instantiate(A::Applied{DefaultApplyStyle,typeof(vcat)})
isempty(A.args) && return A
m = size(A.args[1],2)
for k=2:length(A.args)
size(A.args[k],2) == m || throw(ArgumentError("number of columns of each array must match (got $(map(x->size(x,2), A)))"))
@inline Vcat(A::AbstractVector...) = ApplyVector(vcat, A...)
@inline Vcat(A::AbstractVector{T}...) where T = ApplyVector{T}(vcat, A...)
@inline Vcat{T}(A::AbstractVector...) where T = ApplyVector{T}(vcat, A...)

@inline function applied_instantiate(::typeof(vcat), args...)
iargs = map(instantiate, args)
if !isempty(iargs)
m = size(iargs[1],2)
for k=2:length(iargs)
size(iargs[k],2) == m || throw(ArgumentError("number of columns of each array must match (got $(map(x->size(x,2), args)))"))
end
end
Applied{DefaultApplyStyle}(A.f,map(instantiate,A.args))
vcat, iargs
end

@inline applied_eltype(::typeof(vcat)) = Any
Expand Down
10 changes: 9 additions & 1 deletion src/linalg/mul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ __mul_args_cols(jr, z, y...) =
(__mul_args_cols(_mul_args_colsupport(z,jr), y...)..., jr)
_mul_args_cols(jr, z, y...) = __mul_args_cols(_mul_args_colsupport(z,jr), y...)

_transposefirst(a, b...) = (transpose(a), b...)

function _mul_getindex(args::Tuple, k::Int, j::Int)
kjr = intersect.(_mul_args_rows(k, args...), _mul_args_cols(j, reverse(args)...))
any(isempty, kjr) && return zero(mapreduce(eltype, promote_type, args))
*(_transposefirst(map(getindex, args, (k, kjr...), (kjr..., j))...)...)
end

sublayout(::ApplyLayout{typeof(*)}, _...) = ApplyLayout{typeof(*)}()
# matrix-indexing loses the multiplication structure as we don't support tensor multiplication
sublayout(::ApplyLayout{typeof(*)}, ::Type{<:Tuple{AbstractMatrix}}) = UnknownLayout()
Expand Down Expand Up @@ -235,7 +243,7 @@ _vec_mul_arguments(args, (kr,jr)::Tuple{AbstractVector,Number}) =

# this is a row-vector view
_vec_mul_arguments(args, (kr,jr)::Tuple{Number,AbstractVector}) =
_vec_mul_arguments(reverse(map(permutedims, args)), (jr,kr))
_vec_mul_arguments(reverse(map(transpose, args)), (jr,kr))

_mat_mul_arguments(V) = _mat_mul_arguments(arguments(parent(V)), parentindices(V))
_vec_mul_arguments(V) = _vec_mul_arguments(arguments(parent(V)), parentindices(V))
Expand Down
2 changes: 1 addition & 1 deletion test/lazymultests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Base.convert(::Type{AbstractMatrix{T}}, A::MyMatrix) where T = MyMatrix(convert(
Base.convert(::Type{MyMatrix{T}}, A::AbstractArray{T}) where T = MyMatrix{T}(A)
Base.convert(::Type{MyMatrix{T}}, A::AbstractArray) where T = MyMatrix{T}(convert(AbstractArray{T}, A))
Base.convert(::Type{MyMatrix}, A::AbstractArray{T}) where T = MyMatrix{T}(A)
Base.getindex(A::MyMatrix, kj...) = A.A[kj...]
Base.getindex(A::MyMatrix, k::Integer, j::Integer) = A.A[k, j]
Base.getindex(A::MyMatrix, k::Integer, ::Colon) = A.A[k,:]
Base.getindex(A::MyMatrix, ::Colon, j::Integer) = A.A[:,j]
Base.getindex(A::MyMatrix, ::Colon, j::AbstractVector) = MyMatrix(A.A[:,j])
Expand Down

4 comments on commit 4a1c403

@dlfivefifty
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/90109

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.6.0 -m "<description of version>" 4a1c403d17268e7562af9ecb2d055e23c9586288
git push origin v1.6.0

@jishnub
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit seems to have broken downstream packages (InfiniteLinearAlgebra, as seen from the CI, and ContinuumArrays appears to be broken as well)

@dlfivefifty
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InfiniteLinearAlgebra was only broken because this commit exposed a bug. The latest version fixes that bug

ContinuumArrays.jl is fixed in the next sectiontag

Please sign in to comment.