Skip to content

Commit 129fb5b

Browse files
KernelAbstractions 0.10 support (#623)
1 parent 97bdfdb commit 129fb5b

File tree

5 files changed

+87
-27
lines changed

5 files changed

+87
-27
lines changed

.github/workflows/Test.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,36 @@ jobs:
3838
token: ${{ secrets.CODECOV_TOKEN }}
3939
fail_ci_if_error: false
4040
files: lcov.info
41+
ka:
42+
name: KA 0.10 Julia latest - ${{ matrix.os }}
43+
runs-on: ${{ matrix.os }}
44+
strategy:
45+
fail-fast: false
46+
matrix:
47+
os: [ubuntu-latest, macOS-latest, windows-latest]
48+
steps:
49+
- uses: actions/checkout@v5
50+
- uses: julia-actions/setup-julia@v2
51+
with:
52+
version: '1'
53+
- uses: julia-actions/cache@v2
54+
- name: Develop subpackages
55+
run: |
56+
julia --project -e '
57+
using Pkg
58+
Pkg.develop([PackageSpec(; name=basename(path), path) for path in ARGS])
59+
Pkg.add(url="https://github.com/JuliaGPU/KernelAbstractions.jl", rev="main")
60+
' lib/GPUArraysCore lib/JLArrays
61+
- uses: julia-actions/julia-runtest@v1
62+
continue-on-error: true
63+
- uses: julia-actions/julia-processcoverage@v1
64+
with:
65+
directories: src,lib
66+
- uses: codecov/codecov-action@v5
67+
with:
68+
token: ${{ secrets.CODECOV_TOKEN }}
69+
fail_ci_if_error: false
70+
files: lcov.info
4171
opencl:
4272
name: OpenCL.jl
4373
runs-on: ubuntu-latest

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ JLD2Ext = "JLD2"
2525
Adapt = "4.0"
2626
GPUArraysCore = "= 0.2.0"
2727
JLD2 = "0.4, 0.5, 0.6"
28-
KernelAbstractions = "0.9.28"
28+
KernelAbstractions = "0.9.28, 0.10"
2929
LLVM = "3.9, 4, 5, 6, 7, 8, 9"
3030
LinearAlgebra = "1"
3131
Printf = "1"

lib/JLArrays/Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "JLArrays"
22
uuid = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb"
33
authors = ["Tim Besard <[email protected]>"]
4-
version = "0.2.0"
4+
version = "0.2.1"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
@@ -12,6 +12,6 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1212
[compat]
1313
Adapt = "2.0, 3.0, 4.0"
1414
GPUArrays = "11.1"
15-
KernelAbstractions = "0.9"
15+
KernelAbstractions = "0.9, 0.10"
1616
Random = "1"
1717
julia = "1.8"

lib/JLArrays/src/JLArrays.jl

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ using Adapt
1515
import KernelAbstractions
1616
import KernelAbstractions: Adapt, StaticArrays, Backend, Kernel, StaticSize, DynamicSize, partition, blocks, workitems, launch_config
1717

18+
@static if isdefined(JLArrays.KernelAbstractions, :POCL) # KA v0.10
19+
import KernelAbstractions: POCL
20+
end
21+
1822

1923
#
2024
# Device functionality
@@ -40,30 +44,30 @@ Adapt.adapt_structure(to::Adaptor, r::Base.RefValue) = JlRefValue(adapt(to, r[])
4044
## executed on-device
4145

4246
# array type
47+
@static if !isdefined(JLArrays.KernelAbstractions, :POCL) # KA v0.9
48+
struct JLDeviceArray{T, N} <: AbstractDeviceArray{T, N}
49+
data::Vector{UInt8}
50+
offset::Int
51+
dims::Dims{N}
52+
end
4353

44-
struct JLDeviceArray{T, N} <: AbstractDeviceArray{T, N}
45-
data::Vector{UInt8}
46-
offset::Int
47-
dims::Dims{N}
48-
end
54+
Base.elsize(::Type{<:JLDeviceArray{T}}) where {T} = sizeof(T)
4955

50-
Base.elsize(::Type{<:JLDeviceArray{T}}) where {T} = sizeof(T)
56+
Base.size(x::JLDeviceArray) = x.dims
57+
Base.sizeof(x::JLDeviceArray) = Base.elsize(x) * length(x)
5158

52-
Base.size(x::JLDeviceArray) = x.dims
53-
Base.sizeof(x::JLDeviceArray) = Base.elsize(x) * length(x)
59+
Base.unsafe_convert(::Type{Ptr{T}}, x::JLDeviceArray{T}) where {T} =
60+
convert(Ptr{T}, pointer(x.data)) + x.offset*Base.elsize(x)
5461

55-
Base.unsafe_convert(::Type{Ptr{T}}, x::JLDeviceArray{T}) where {T} =
56-
convert(Ptr{T}, pointer(x.data)) + x.offset*Base.elsize(x)
62+
# conversion of untyped data to a typed Array
63+
function typed_data(x::JLDeviceArray{T}) where {T}
64+
unsafe_wrap(Array, pointer(x), x.dims)
65+
end
5766

58-
# conversion of untyped data to a typed Array
59-
function typed_data(x::JLDeviceArray{T}) where {T}
60-
unsafe_wrap(Array, pointer(x), x.dims)
67+
@inline Base.getindex(A::JLDeviceArray, index::Integer) = getindex(typed_data(A), index)
68+
@inline Base.setindex!(A::JLDeviceArray, x, index::Integer) = setindex!(typed_data(A), x, index)
6169
end
6270

63-
@inline Base.getindex(A::JLDeviceArray, index::Integer) = getindex(typed_data(A), index)
64-
@inline Base.setindex!(A::JLDeviceArray, x, index::Integer) = setindex!(typed_data(A), x, index)
65-
66-
6771
#
6872
# Host abstractions
6973
#
@@ -236,7 +240,7 @@ Base.convert(::Type{T}, x::T) where T <: JLArray = x
236240

237241
## broadcast
238242

239-
using Base.Broadcast: BroadcastStyle, Broadcasted
243+
import Base.Broadcast: BroadcastStyle, Broadcasted
240244

241245
struct JLArrayStyle{N} <: AbstractGPUArrayStyle{N} end
242246
JLArrayStyle{M}(::Val{N}) where {N,M} = JLArrayStyle{N}()
@@ -335,8 +339,15 @@ end
335339

336340
## GPUArrays interfaces
337341

338-
Adapt.adapt_storage(::Adaptor, x::JLArray{T,N}) where {T,N} =
339-
JLDeviceArray{T,N}(x.data[], x.offset, x.dims)
342+
@static if !isdefined(JLArrays.KernelAbstractions, :POCL) # KA v0.9
343+
Adapt.adapt_storage(::Adaptor, x::JLArray{T,N}) where {T,N} =
344+
JLDeviceArray{T,N}(x.data[], x.offset, x.dims)
345+
else
346+
function Adapt.adapt_storage(::Adaptor, x::JLArray{T,N}) where {T,N}
347+
arr = typed_data(x)
348+
Adapt.adapt_storage(POCL.KernelAdaptor([pointer(arr)]), arr)
349+
end
350+
end
340351

341352
function GPUArrays.mapreducedim!(f, op, R::AnyJLArray, A::Union{AbstractArray,Broadcast.Broadcasted};
342353
init=nothing)
@@ -377,10 +388,18 @@ KernelAbstractions.allocate(::JLBackend, ::Type{T}, dims::Tuple) where T = JLArr
377388
return ndrange, workgroupsize, iterspace, dynamic
378389
end
379390

380-
KernelAbstractions.isgpu(b::JLBackend) = false
391+
@static if isdefined(JLArrays.KernelAbstractions, :isgpu) # KA v0.9
392+
KernelAbstractions.isgpu(b::JLBackend) = false
393+
end
381394

382-
function convert_to_cpu(obj::Kernel{JLBackend, W, N, F}) where {W, N, F}
383-
return Kernel{typeof(KernelAbstractions.CPU(; static = obj.backend.static)), W, N, F}(KernelAbstractions.CPU(; static = obj.backend.static), obj.f)
395+
@static if !isdefined(JLArrays.KernelAbstractions, :POCL) # KA v0.9
396+
function convert_to_cpu(obj::Kernel{JLBackend, W, N, F}) where {W, N, F}
397+
return Kernel{typeof(KernelAbstractions.CPU(; static = obj.backend.static)), W, N, F}(KernelAbstractions.CPU(; static = obj.backend.static), obj.f)
398+
end
399+
else
400+
function convert_to_cpu(obj::Kernel{JLBackend, W, N, F}) where {W, N, F}
401+
return Kernel{typeof(KernelAbstractions.POCLBackend()), W, N, F}(KernelAbstractions.POCLBackend(), obj.f)
402+
end
384403
end
385404

386405
function (obj::Kernel{JLBackend})(args...; ndrange=nothing, workgroupsize=nothing)
@@ -391,6 +410,11 @@ end
391410

392411
Adapt.adapt_storage(::JLBackend, a::Array) = Adapt.adapt(JLArrays.JLArray, a)
393412
Adapt.adapt_storage(::JLBackend, a::JLArrays.JLArray) = a
394-
Adapt.adapt_storage(::KernelAbstractions.CPU, a::JLArrays.JLArray) = convert(Array, a)
413+
414+
@static if !isdefined(JLArrays.KernelAbstractions, :POCL) # KA v0.9
415+
Adapt.adapt_storage(::KernelAbstractions.CPU, a::JLArrays.JLArray) = convert(Array, a)
416+
else
417+
Adapt.adapt_storage(::KernelAbstractions.POCLBackend, a::JLArrays.JLArray) = convert(Array, a)
418+
end
395419

396420
end

test/setup.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ using Distributed, Test, JLArrays
22

33
include("testsuite.jl")
44

5+
# Disable Float16-related tests until JuliaGPU/KernelAbstractions#600 is resolved
6+
@static if isdefined(JLArrays.KernelAbstractions, :POCL)
7+
TestSuite.supported_eltypes(::Type{<:JLArray}) =
8+
setdiff(TestSuite.supported_eltypes(), [Float16, ComplexF16])
9+
end
10+
511
using Random
612

713
if VERSION >= v"1.13.0-DEV.1044"

0 commit comments

Comments
 (0)