From 989706be7e59846ca53361f5a8a623f219c9b813 Mon Sep 17 00:00:00 2001 From: Mateusz Baran Date: Tue, 6 Sep 2022 12:56:56 +0200 Subject: [PATCH 1/2] Improve Tullio compatibility --- Project.toml | 3 +- src/abstractarray.jl | 21 +++++++++++++ src/array_interface_compat.jl | 2 ++ test/abstractarray.jl | 55 ++++++++++++++++++++-------------- test/array_interface_compat.jl | 2 ++ 5 files changed, 59 insertions(+), 24 deletions(-) diff --git a/Project.toml b/Project.toml index 4941b41..4f9a853 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "HybridArrays" uuid = "1baab800-613f-4b0a-84e4-9cd3431bfbb9" authors = ["Mateusz Baran "] -version = "0.4.10" +version = "0.4.11" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" @@ -18,6 +18,7 @@ julia = "1.5" ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" EllipsisNotation = "da5c29d0-fa7d-589e-88eb-ea29b0a81949" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] diff --git a/src/abstractarray.jl b/src/abstractarray.jl index 91e69cb..a566b7f 100644 --- a/src/abstractarray.jl +++ b/src/abstractarray.jl @@ -38,7 +38,28 @@ end @inline copy(a::HybridArray) = typeof(a)(copy(parent(a))) +homogenized_last(::StaticArrays.HeterogeneousBaseShape) = StaticArrays.Dynamic() +homogenized_last(a::SOneTo) = last(a) + +hybrid_homogenize_shape(::Tuple{}) = () +hybrid_homogenize_shape(shape::Tuple{Vararg{StaticArrays.HeterogeneousShape}}) = Size(map(homogenized_last, shape)) +hybrid_homogenize_shape(shape::Tuple{Vararg{StaticArrays.HeterogeneousBaseShape}}) = map(last, shape) + similar(a::HA, ::Type{T2}) where {S, HA<:HybridArray{S}, T2} = HybridArray{S, T2}(similar(parent(a), T2)) +function similar(a::HybridArray, ::Type{T2}, shape::StaticArrays.HeterogeneousShapeTuple) where {T2} + s = hybrid_homogenize_shape(shape) + HT = hybridarray_similar_type(T2, s, StaticArrays.length_val(s)) + return HT(similar(a.data, T2, shape)) +end +function similar(a::HybridArray, shape::StaticArrays.HeterogeneousShapeTuple) + return similar(a, eltype(a), shape) +end +function similar(a::HybridArray, ::Type{T2}, shape::Tuple{SOneTo, Vararg{SOneTo}}) where {T2} + return similar(a.data, T2, StaticArrays.homogenize_shape(shape)) +end +function similar(a::HybridArray, shape::Tuple{SOneTo, Vararg{SOneTo}}) + return similar(a.data, StaticArrays.homogenize_shape(shape)) +end similar(::Type{<:HybridArray{S,T,N,M}},::Type{T2}) where {S,T,N,M,T2} = HybridArray{S,T2,N,M}(undef) similar(::Type{SA},::Type{T},s::Size{S}) where {SA<:HybridArray,T,S} = hybridarray_similar_type(T,s,StaticArrays.length_val(s))(undef) diff --git a/src/array_interface_compat.jl b/src/array_interface_compat.jl index d3ebbaa..f87c3e2 100644 --- a/src/array_interface_compat.jl +++ b/src/array_interface_compat.jl @@ -58,3 +58,5 @@ end ArrayInterface.contiguous_axis(::Type{HybridArray{S,T,N,N,TData}}) where {S,T,N,TData} = ArrayInterface.contiguous_axis(TData) ArrayInterface.contiguous_batch_size(::Type{HybridArray{S,T,N,N,TData}}) where {S,T,N,TData} = ArrayInterface.contiguous_batch_size(TData) ArrayInterface.stride_rank(::Type{HybridArray{S,T,N,N,TData}}) where {S,T,N,TData} = ArrayInterface.stride_rank(TData) + +ArrayInterface.dense_dims(x::HybridArray) = ArrayInterface.dense_dims(x.data) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 4ad7813..933be6b 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -1,14 +1,15 @@ using StaticArrays, HybridArrays, Test, LinearAlgebra +using StaticArrays: Dynamic @testset "AbstractArray interface" begin @testset "size and length" begin - M = HybridMatrix{2, StaticArrays.Dynamic()}([1 2 3; 4 5 6]) + M = HybridMatrix{2, Dynamic()}([1 2 3; 4 5 6]) @test length(M) == 6 @test size(M) == (2, 3) @test Base.isassigned(M, 2, 2) == true - @test (@inferred Size(M)) == Size(Tuple{2, StaticArrays.Dynamic()}) - @test (@inferred Size(typeof(M))) == Size(Tuple{2, StaticArrays.Dynamic()}) + @test (@inferred Size(M)) == Size(Tuple{2, Dynamic()}) + @test (@inferred Size(typeof(M))) == Size(Tuple{2, Dynamic()}) end @testset "reshape" begin @@ -17,16 +18,16 @@ using StaticArrays, HybridArrays, Test, LinearAlgebra @testset "convert" begin MM = [1 2 3; 4 5 6] - M = HybridMatrix{2, StaticArrays.Dynamic()}(MM) - @test parent(@inferred convert(HybridArray{Tuple{2,StaticArrays.Dynamic()}}, MM)) == M - @test parent(@inferred convert(HybridArray{Tuple{2,StaticArrays.Dynamic()},Int}, MM)) == M - @test parent(@inferred convert(HybridArray{Tuple{2,StaticArrays.Dynamic()},Float64}, MM)) == M - @test parent(@inferred convert(HybridArray{Tuple{2,StaticArrays.Dynamic()},Float64,2}, MM)) == M - @test parent(@inferred convert(HybridArray{Tuple{2,StaticArrays.Dynamic()},Float64,2,2}, MM)) == M - @test parent(@inferred convert(HybridArray{Tuple{2,StaticArrays.Dynamic()},Float64,2,2,Matrix{Float64}}, MM)) == M + M = HybridMatrix{2, Dynamic()}(MM) + @test parent(@inferred convert(HybridArray{Tuple{2,Dynamic()}}, MM)) == M + @test parent(@inferred convert(HybridArray{Tuple{2,Dynamic()},Int}, MM)) == M + @test parent(@inferred convert(HybridArray{Tuple{2,Dynamic()},Float64}, MM)) == M + @test parent(@inferred convert(HybridArray{Tuple{2,Dynamic()},Float64,2}, MM)) == M + @test parent(@inferred convert(HybridArray{Tuple{2,Dynamic()},Float64,2,2}, MM)) == M + @test parent(@inferred convert(HybridArray{Tuple{2,Dynamic()},Float64,2,2,Matrix{Float64}}, MM)) == M @test convert(typeof(M), M) === M if VERSION >= v"1.1" - @test convert(HybridArray{Tuple{2,StaticArrays.Dynamic()},Float64}, M) == M + @test convert(HybridArray{Tuple{2,Dynamic()},Float64}, M) == M end @test convert(Array, M) === parent(M) @test convert(Array{Int}, M) === parent(M) @@ -43,22 +44,30 @@ using StaticArrays, HybridArrays, Test, LinearAlgebra end @testset "copy" begin - M = HybridMatrix{2, StaticArrays.Dynamic()}([1 2; 3 4]) + M = HybridMatrix{2, Dynamic()}([1 2; 3 4]) @test @inferred(copy(M))::HybridMatrix == M @test parent(copy(M)) !== parent(M) end @testset "similar" begin - M = HybridMatrix{2, StaticArrays.Dynamic(), Int}([1 2; 3 4]) + M = HybridMatrix{2, Dynamic(), Int}([1 2; 3 4]) - @test isa(@inferred(similar(M)), HybridMatrix{2, StaticArrays.Dynamic(), Int}) - @test isa(@inferred(similar(M, Float64)), HybridMatrix{2, StaticArrays.Dynamic(), Float64}) + @test isa(@inferred(similar(M)), HybridMatrix{2, Dynamic(), Int}) + @test isa(@inferred(similar(M, Float64)), HybridMatrix{2, Dynamic(), Float64}) @test isa(@inferred(similar(M, Float64, Size(Tuple{3, 3}))), HybridMatrix{3, 3, Float64}) + + @test isa(@inferred(similar(M, SOneTo(3))), SizedVector{3, Int}) + @test isa(@inferred(similar(M, Float64, SOneTo(3))), SizedVector{3, Float64}) + @test isa(@inferred(similar(M, (SOneTo(3), 10, Base.OneTo(12)))), + HybridArray{Tuple{3,Dynamic(),Dynamic()}, Int}) + @test isa(@inferred(similar(M, Float64, (SOneTo(3), 10, Base.OneTo(12)))), + HybridArray{Tuple{3,Dynamic(),Dynamic()}, Float64}) + @test size(similar(M, Float64, (SOneTo(3), 10, Base.OneTo(12)))) === (3, 10, 12) end @testset "IndexStyle" begin - M = HybridMatrix{2, StaticArrays.Dynamic(), Int}([1 2; 3 4]) - MT = HybridMatrix{2, StaticArrays.Dynamic(), Int}([1 2; 3 4]') + M = HybridMatrix{2, Dynamic(), Int}([1 2; 3 4]) + MT = HybridMatrix{2, Dynamic(), Int}([1 2; 3 4]') @test (@inferred IndexStyle(M)) === IndexLinear() @test (@inferred IndexStyle(MT)) === IndexCartesian() @test (@inferred IndexStyle(typeof(M))) === IndexLinear() @@ -66,7 +75,7 @@ using StaticArrays, HybridArrays, Test, LinearAlgebra end @testset "vec" begin - M = HybridMatrix{2, StaticArrays.Dynamic(), Int}([1 2; 3 4]) + M = HybridMatrix{2, Dynamic(), Int}([1 2; 3 4]) Mv = vec(M) @test Mv == [1, 3, 2, 4] @test Mv isa Vector{Int} @@ -81,21 +90,21 @@ using StaticArrays, HybridArrays, Test, LinearAlgebra @testset "elsize, eltype" begin for T in [Int8, Int16, Int32, Int64, Int128, Float16, Float32, Float64, BigFloat] M_array = rand(T, 2, 2) - M_hybrid = HybridMatrix{2, StaticArrays.Dynamic()}(M_array) + M_hybrid = HybridMatrix{2, Dynamic()}(M_array) @test Base.eltype(M_hybrid) === Base.eltype(M_array) @test Base.elsize(M_hybrid) === Base.elsize(M_array) end end @testset "strides" begin - M = HybridMatrix{2, StaticArrays.Dynamic(), Int}([1 2; 3 4]) + M = HybridMatrix{2, Dynamic(), Int}([1 2; 3 4]) @test strides(M) == strides(parent(M)) end @testset "pointer" begin - M = HybridMatrix{2, StaticArrays.Dynamic(), Int}([1 2; 3 4]) - MT = HybridMatrix{2, StaticArrays.Dynamic(), Int}([1 2; 3 4]') + M = HybridMatrix{2, Dynamic(), Int}([1 2; 3 4]) + MT = HybridMatrix{2, Dynamic(), Int}([1 2; 3 4]') @test pointer(M) == pointer(parent(M)) if VERSION >= v"1.5" @@ -105,7 +114,7 @@ using StaticArrays, HybridArrays, Test, LinearAlgebra end @testset "unsafe_convert" begin - M = HybridMatrix{2, StaticArrays.Dynamic(), Int}([1 2; 3 4]) + M = HybridMatrix{2, Dynamic(), Int}([1 2; 3 4]) @test Base.unsafe_convert(Ptr{Int}, M) === pointer(parent(M)) end diff --git a/test/array_interface_compat.jl b/test/array_interface_compat.jl index c8d1733..71cfc7a 100644 --- a/test/array_interface_compat.jl +++ b/test/array_interface_compat.jl @@ -1,6 +1,7 @@ using HybridArrays, ArrayInterface, Test, StaticArrays using ArrayInterface: StaticInt +using Static @testset "ArrayInterface compatibility" begin M = HybridMatrix{2, StaticArrays.Dynamic()}([1 2; 4 5]) @@ -22,4 +23,5 @@ using ArrayInterface: StaticInt @test ArrayInterface.contiguous_axis(typeof(M')) === ArrayInterface.contiguous_axis(typeof(parent(M)')) @test ArrayInterface.contiguous_batch_size(typeof(M')) === ArrayInterface.contiguous_batch_size(typeof(parent(M)')) @test ArrayInterface.stride_rank(typeof(M')) === ArrayInterface.stride_rank(typeof(parent(M)')) + @test ArrayInterface.dense_dims(M) === (static(true), static(true)) end From 6f4fb2ac1d936eaab9ee3a11b67ef81b4b8bf270 Mon Sep 17 00:00:00 2001 From: Mateusz Baran Date: Tue, 6 Sep 2022 13:04:05 +0200 Subject: [PATCH 2/2] fix test --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 4f9a853..7ed3943 100644 --- a/Project.toml +++ b/Project.toml @@ -22,4 +22,4 @@ Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Test", "Random", "ArrayInterface", "EllipsisNotation"] +test = ["Test", "Random", "ArrayInterface", "EllipsisNotation", "Static"]