Skip to content

Commit

Permalink
Merge pull request #317 from pxl-th/pxl-th/gpuarrays
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnoack authored Nov 16, 2024
2 parents 83fb3d7 + dcc7780 commit 4400fee
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6'
- 'min'
- 'lts'
- '1'
- 'nightly'
- 'pre'
os:
- ubuntu-latest
arch:
Expand Down
18 changes: 10 additions & 8 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
name = "StructArrays"
uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
version = "0.6.19"
version = "0.7.0"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
Expand All @@ -16,26 +15,28 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[extensions]
StructArraysAdaptExt = "Adapt"
StructArraysGPUArraysCoreExt = "GPUArraysCore"
StructArraysGPUArraysCoreExt = ["GPUArraysCore", "KernelAbstractions"]
StructArraysLinearAlgebraExt = "LinearAlgebra"
StructArraysSparseArraysExt = "SparseArrays"
StructArraysStaticArraysExt = "StaticArrays"

[compat]
Adapt = "3.4, 4"
Adapt = "4"
Aqua = "0.8"
ConstructionBase = "1"
DataAPI = "1"
Documenter = "1"
GPUArraysCore = "0.1.2, 0.2"
GPUArraysCore = "0.2"
InfiniteArrays = "0.13"
JLArrays = "0.1"
JLArrays = "0.2"
LinearAlgebra = "1"
KernelAbstractions = "0.9"
OffsetArrays = "1"
PooledArrays = "1"
SparseArrays = "1"
Expand All @@ -44,7 +45,7 @@ Tables = "1"
Test = "1"
TypedTables = "1"
WeakRefStrings = "1"
julia = "1.6"
julia = "1.10"

[extras]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand All @@ -54,6 +55,7 @@ GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
InfiniteArrays = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
JLArrays = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
PooledArrays = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Expand All @@ -63,4 +65,4 @@ TypedTables = "9d95f2ec-7b3d-5a63-8d20-e2491e220bb9"
WeakRefStrings = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"

[targets]
test = ["Adapt", "Aqua", "Documenter", "GPUArraysCore", "InfiniteArrays", "JLArrays", "LinearAlgebra", "OffsetArrays", "PooledArrays", "SparseArrays", "StaticArrays", "Test", "TypedTables", "WeakRefStrings"]
test = ["Adapt", "Aqua", "Documenter", "GPUArraysCore", "InfiniteArrays", "JLArrays", "LinearAlgebra", "KernelAbstractions", "OffsetArrays", "PooledArrays", "SparseArrays", "StaticArrays", "Test", "TypedTables", "WeakRefStrings"]
5 changes: 4 additions & 1 deletion ext/StructArraysAdaptExt.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module StructArraysAdaptExt
# Use Adapt allows for automatic conversion of CPU to GPU StructArrays
using Adapt, StructArrays
Adapt.adapt_structure(to, s::StructArray) = replace_storage(adapt(to), s)

function Adapt.adapt_structure(to, s::StructArray)
replace_storage(adapt(to), s)
end
end
15 changes: 11 additions & 4 deletions ext/StructArraysGPUArraysCoreExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@ using StructArrays: map_params, array_types
using Base: tail

import GPUArraysCore
import KernelAbstractions as KA

function KA.get_backend(x::T) where {T<:StructArray}
components = StructArrays.components(x)
array_components = filter(
fn -> getfield(components, fn) isa AbstractArray,
fieldnames(typeof(components)))
backends = map(
fn -> KA.get_backend(getfield(components, fn)),
array_components)

# for GPU broadcast
import GPUArraysCore
function GPUArraysCore.backend(::Type{T}) where {T<:StructArray}
backends = map_params(GPUArraysCore.backend, array_types(T))
backend, others = backends[1], tail(backends)
isconsistent = mapfoldl(isequal(backend), &, others; init=true)
isconsistent || throw(ArgumentError("all component arrays must have the same GPU backend"))
return backend
end

StructArrays.always_struct_broadcast(::GPUArraysCore.AbstractGPUArrayStyle) = true

end # module
12 changes: 7 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ using StructArrays
using StructArrays: staticschema, iscompatible, _promote_typejoin, append!!
using OffsetArrays: OffsetArray, OffsetVector, OffsetMatrix
using StaticArrays
import Tables, PooledArrays, WeakRefStrings
using TypedTables: Table
using DataAPI: refarray, refvalue
using Adapt: adapt, Adapt
using JLArrays
using GPUArraysCore: backend
using LinearAlgebra
using Test
using SparseArrays
using InfiniteArrays

import Aqua
import KernelAbstractions as KA
import Tables, PooledArrays, WeakRefStrings

using Documenter: doctest
if Base.VERSION == v"1.6" && Int === Int64
Expand Down Expand Up @@ -1192,6 +1193,7 @@ end
struct ArrayConverter end

Adapt.adapt_storage(::ArrayConverter, xs::AbstractArray) = convert(Array, xs)
Adapt.adapt_structure(::ArrayConverter, xs::UnitRange) = convert(Array, xs)

@testset "adapt" begin
s = StructArray(a = 1:10, b = StructArray(c = 1:10, d = 1:10))
Expand Down Expand Up @@ -1372,11 +1374,11 @@ Base.BroadcastStyle(::Broadcast.ArrayStyle{MyArray2}, S::Broadcast.DefaultArrayS
a = StructArray(randn(ComplexF32, 10, 10))
sa = jl(a)
@test sa isa StructArray
@test @inferred(backend(sa)) === backend(sa.re) === backend(sa.im) === backend(jl(a.re))
@test @inferred(KA.get_backend(sa)) === KA.get_backend(sa.re) === KA.get_backend(sa.im) === KA.get_backend(jl(a.re))
@test collect(@inferred(bcabs(sa))) == bcabs(a)
@test backend(bcabs(sa)) === backend(sa)
@test KA.get_backend(bcabs(sa)) === KA.get_backend(sa)
@test @inferred(bcmul2(sa)) isa StructArray
@test backend(bcmul2(sa)) === backend(sa)
@test KA.get_backend(bcmul2(sa)) === KA.get_backend(sa)
@test (sa .+= 1) === sa
end

Expand Down

2 comments on commit 4400fee

@andreasnoack
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/119574

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.7.0 -m "<description of version>" 4400feeef57414b27477f8e347baba24f6644b64
git push origin v0.7.0

Please sign in to comment.