Skip to content

Commit

Permalink
GPU arrays (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Feb 10, 2025
1 parent ca6b2c0 commit 24b3add
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 59 deletions.
27 changes: 21 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
name = "TypeParameterAccessors"
uuid = "7e5a90cf-f82e-492e-a09b-e3e26432c138"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.3.0"
version = "0.3.1"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"

[weakdeps]
StridedViews = "4db3bf67-4bd7-4b4e-b153-31dc3fb37143"
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
JLArrays = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb"
Metal = "dde4c033-4e86-420c-a63e-0dd931031962"
StridedViews = "4db3bf67-4bd7-4b4e-b153-31dc3fb37143"
oneAPI = "8f75cd03-7ff8-4ecb-9b8f-daf728133b1b"

[extensions]
TypeParameterAccessorsAMDGPUExt = "AMDGPU"
TypeParameterAccessorsCUDAExt = "CUDA"
TypeParameterAccessorsFillArraysExt = "FillArrays"
TypeParameterAccessorsJLArraysExt = "JLArrays"
TypeParameterAccessorsMetalExt = "Metal"
TypeParameterAccessorsStridedViewsExt = "StridedViews"
TypeParameterAccessorsoneAPIExt = "oneAPI"

[compat]
AMDGPU = "0, 1"
CUDA = "3, 4, 5"
FillArrays = "1.13"
JLArrays = "0.1, 0.2"
LinearAlgebra = "1.10"
Metal = "0, 1"
SimpleTraits = "0.9.4"
StridedViews = "0.3.2"
julia = "1.10"

[extensions]
TypeParameterAccessorsStridedViewsExt = "StridedViews"
TypeParameterAccessorsFillArraysExt = "FillArrays"
oneAPI = "0, 1, 2"
9 changes: 9 additions & 0 deletions ext/TypeParameterAccessorsAMDGPUExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module TypeParameterAccessorsAMDGPUExt

using AMDGPU: ROCArray
using TypeParameterAccessors: TypeParameterAccessors, Position

TypeParameterAccessors.position(::Type{<:ROCArray}, ::typeof(eltype)) = Position(1)
TypeParameterAccessors.position(::Type{<:ROCArray}, ::typeof(ndims)) = Position(2)

end
9 changes: 9 additions & 0 deletions ext/TypeParameterAccessorsCUDAExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module TypeParameterAccessorsCUDAExt

using CUDA: CuArray
using TypeParameterAccessors: TypeParameterAccessors, Position

TypeParameterAccessors.position(::Type{<:CuArray}, ::typeof(eltype)) = Position(1)
TypeParameterAccessors.position(::Type{<:CuArray}, ::typeof(ndims)) = Position(2)

end
9 changes: 9 additions & 0 deletions ext/TypeParameterAccessorsJLArraysExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module TypeParameterAccessorsJLArraysExt

using JLArrays: JLArray
using TypeParameterAccessors: TypeParameterAccessors, Position

TypeParameterAccessors.position(::Type{<:JLArray}, ::typeof(eltype)) = Position(1)
TypeParameterAccessors.position(::Type{<:JLArray}, ::typeof(ndims)) = Position(2)

end
9 changes: 9 additions & 0 deletions ext/TypeParameterAccessorsMetalExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module TypeParameterAccessorsMetalExt

using Metal: MtlArray
using TypeParameterAccessors: TypeParameterAccessors, Position

TypeParameterAccessors.position(::Type{<:MtlArray}, ::typeof(eltype)) = Position(1)
TypeParameterAccessors.position(::Type{<:MtlArray}, ::typeof(ndims)) = Position(2)

end
9 changes: 9 additions & 0 deletions ext/TypeParameterAccessorsoneAPIExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module TypeParameterAccessorsoneAPIExt

using oneAPI: oneArray
using TypeParameterAccessors: TypeParameterAccessors, Position

TypeParameterAccessors.position(::Type{<:oneArray}, ::typeof(eltype)) = Position(1)
TypeParameterAccessors.position(::Type{<:oneArray}, ::typeof(ndims)) = Position(2)

end
3 changes: 2 additions & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
JLArrays = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
StridedViews = "4db3bf67-4bd7-4b4e-b153-31dc3fb37143"
Expand All @@ -12,8 +13,8 @@ TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138"
[compat]
Aqua = "0.8.9"
FillArrays = "1.13"
StridedViews = "0.3"
SafeTestsets = "0.1"
StridedViews = "0.3"
Suppressor = "0.2"
Test = "1.10"
TestExtras = "0.3"
120 changes: 68 additions & 52 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
@@ -1,64 +1,80 @@
using JLArrays: JLArray, JLMatrix, JLVector
using Test: @test, @test_throws, @test_broken, @testset
using TestExtras: @constinferred
using TypeParameterAccessors:
set_type_parameters, specify_type_parameters, type_parameters, unspecify_type_parameters

@testset "Get parameters" begin
@test @constinferred(type_parameters($(AbstractArray{Float64}), 1)) == Float64
@test @constinferred(type_parameters($(AbstractArray{Float64}), eltype)) == Float64
@test @constinferred(type_parameters($(AbstractMatrix{Float64}), ndims)) == 2
const anyarrayts = (
(arrayt=Array, matrixt=Matrix, vectort=Vector),
(arrayt=JLArray, matrixt=JLMatrix, vectort=JLVector),
)
@testset "basics (arrayts=$anyarrayt)" for anyarrayt in anyarrayts
(; arrayt, matrixt, vectort) = anyarrayt

@test @constinferred(type_parameters($(Array{Float64}), 1)) == Float64
@test @constinferred(type_parameters($(Val{3}))) == (3,)
@testset "Get parameters" begin
@test @constinferred(type_parameters($(AbstractArray{Float64}), 1)) == Float64
@test @constinferred(type_parameters($(AbstractArray{Float64}), eltype)) == Float64
@test @constinferred(type_parameters($(AbstractMatrix{Float64}), ndims)) == 2

# @test_throws ErrorException type_parameter(Array, 1)
@test @constinferred(type_parameters($(Array{Float64}), eltype)) == Float64
@test @constinferred(type_parameters($(Matrix{Float64}), ndims)) == 2
@test @constinferred(type_parameters($(Matrix{Float64}), (ndims, eltype))) == (2, Float64)
# TODO: Not inferrable without interpolating positions:
# https://github.com/ITensor/TypeParameterAccessors.jl/issues/21.
@test @constinferred(type_parameters($(Matrix{Float64}), $((2, eltype)))) == (2, Float64)
@test @constinferred(type_parameters($(Matrix{Float64}), (ndims, eltype))) == (2, Float64)
# @test_throws ErrorException type_parameters(Array{Float64}, ndims) == 2
@test @constinferred(broadcast($type_parameters, $(Matrix{Float64}), $((2, eltype)))) ==
(2, Float64)
end
@test @constinferred(type_parameters($(arrayt{Float64}), 1)) == Float64
@test @constinferred(type_parameters($(Val{3}))) == (3,)

@testset "Set parameters" begin
@test @constinferred(set_type_parameters($Array, 1, $Float64)) == Array{Float64}
@test @constinferred(set_type_parameters($Array, 2, 2)) == Matrix
@test @constinferred(set_type_parameters($Array, $eltype, $Float32)) == Array{Float32}
@test @constinferred(set_type_parameters($Array, $((eltype, 2)), $((Float32, 3)))) ==
Array{Float32,3}
end
# @test_throws ErrorException type_parameter(arrayt, 1)
@test @constinferred(type_parameters($(arrayt{Float64}), eltype)) == Float64
@test @constinferred(type_parameters($(matrixt{Float64}), ndims)) == 2
@test @constinferred(type_parameters($(matrixt{Float64}), (ndims, eltype))) ==
(2, Float64)
# TODO: Not inferrable without interpolating positions:
# https://github.com/ITensor/TypeParameterAccessors.jl/issues/21.
@test @constinferred(type_parameters($(matrixt{Float64}), $((2, eltype)))) ==
(2, Float64)
@test @constinferred(type_parameters($(matrixt{Float64}), (ndims, eltype))) ==
(2, Float64)
# @test_throws ErrorException type_parameters(arrayt{Float64}, ndims) == 2
@test @constinferred(
broadcast($type_parameters, $(matrixt{Float64}), $((2, eltype)))
) == (2, Float64)
end

@testset "Specify parameters" begin
@test @constinferred(specify_type_parameters($Array, 1, $Float64)) == Array{Float64}
@test @constinferred(specify_type_parameters($Matrix, $((2, 1)), $((4, Float32)))) ==
Matrix{Float32}
@test @constinferred(specify_type_parameters($Array, $((Float64, 2)))) == Matrix{Float64}
@test @constinferred(specify_type_parameters($Array, $eltype, $Float32)) == Array{Float32}
@test @constinferred(specify_type_parameters($Array, $((eltype, 2)), $((Float32, 3)))) ==
Array{Float32,3}
end
@testset "Set parameters" begin
@test @constinferred(set_type_parameters($arrayt, 1, $Float64)) == arrayt{Float64}
@test @constinferred(set_type_parameters($arrayt, 2, 2)) == matrixt
@test @constinferred(set_type_parameters($arrayt, $eltype, $Float32)) == arrayt{Float32}
@test @constinferred(set_type_parameters($arrayt, $((eltype, 2)), $((Float32, 3)))) ==
arrayt{Float32,3}
end

@testset "Unspecify parameters" begin
@test @constinferred(unspecify_type_parameters($Vector, 2)) == Array
@test @constinferred(unspecify_type_parameters($(Vector{Float64}), eltype)) == Vector
@test @constinferred(unspecify_type_parameters($(Vector{Float64}))) == Array
@test @constinferred(unspecify_type_parameters($(Vector{Float64}), $((eltype, 2)))) ==
Array
end
@testset "Specify parameters" begin
@test @constinferred(specify_type_parameters($arrayt, 1, $Float64)) == arrayt{Float64}
@test @constinferred(specify_type_parameters($matrixt, $((2, 1)), $((4, Float32)))) ==
matrixt{Float32}
@test @constinferred(specify_type_parameters($arrayt, $((Float64, 2)))) ==
matrixt{Float64}
@test @constinferred(specify_type_parameters($arrayt, $eltype, $Float32)) ==
arrayt{Float32}
@test @constinferred(
specify_type_parameters($arrayt, $((eltype, 2)), $((Float32, 3)))
) == arrayt{Float32,3}
end

@testset "Unspecify parameters" begin
@test @constinferred(unspecify_type_parameters($vectort, 2)) == arrayt
@test @constinferred(unspecify_type_parameters($(vectort{Float64}), eltype)) == vectort
@test @constinferred(unspecify_type_parameters($(vectort{Float64}))) == arrayt
@test @constinferred(unspecify_type_parameters($(vectort{Float64}), $((eltype, 2)))) ==
arrayt
end

@testset "On objects" begin
@test @constinferred(type_parameters($(Val{3}()))) == (3,)
@test @constinferred(type_parameters($(Val{Float32}()))) == (Float32,)
a = randn(Float32, (2, 2, 2))
@test @constinferred(type_parameters(a, 1)) == Float32
@test @constinferred(type_parameters(a, eltype)) == Float32
@test @constinferred(type_parameters(a, 2)) == 3
@test @constinferred(type_parameters(a, ndims)) == 3
@test @constinferred(type_parameters(a)) == (Float32, 3)
@test @constinferred(broadcast($type_parameters, $(Ref(a)), $((2, eltype)))) ==
(3, Float32)
@testset "On objects" begin
@test @constinferred(type_parameters($(Val{3}()))) == (3,)
@test @constinferred(type_parameters($(Val{Float32}()))) == (Float32,)
a = arrayt(randn(Float32, (2, 2, 2)))
@test @constinferred(type_parameters(a, 1)) == Float32
@test @constinferred(type_parameters(a, eltype)) == Float32
@test @constinferred(type_parameters(a, 2)) == 3
@test @constinferred(type_parameters(a, ndims)) == 3
@test @constinferred(type_parameters(a)) == (Float32, 3)
@test @constinferred(broadcast($type_parameters, $(Ref(a)), $((2, eltype)))) ==
(3, Float32)
end
end

2 comments on commit 24b3add

@mtfishman
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/124740

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 v0.3.1 -m "<description of version>" 24b3add4cf81f594ea34d4e19de34777f68e66a7
git push origin v0.3.1

Please sign in to comment.