Skip to content
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

Improve Tullio compatibility #54

Merged
merged 2 commits into from
Sep 8, 2022
Merged
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
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "HybridArrays"
uuid = "1baab800-613f-4b0a-84e4-9cd3431bfbb9"
authors = ["Mateusz Baran <[email protected]>"]
version = "0.4.10"
version = "0.4.11"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand All @@ -18,7 +18,8 @@ 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]
test = ["Test", "Random", "ArrayInterface", "EllipsisNotation"]
test = ["Test", "Random", "ArrayInterface", "EllipsisNotation", "Static"]
21 changes: 21 additions & 0 deletions src/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/array_interface_compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
55 changes: 32 additions & 23 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -43,30 +44,38 @@ 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()
@test (@inferred IndexStyle(typeof(MT))) === IndexCartesian()
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}
Expand All @@ -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"
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions test/array_interface_compat.jl
Original file line number Diff line number Diff line change
@@ -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])
Expand All @@ -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