Skip to content

Commit 9c1972f

Browse files
committed
Support both KA 0.9 and KA 0.10
1 parent 597efcc commit 9c1972f

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

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.3.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.10"
15+
KernelAbstractions = "0.9, 0.10"
1616
Random = "1"
1717
julia = "1.8"

lib/JLArrays/src/JLArrays.jl

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ using GPUArrays
1313
using Adapt
1414

1515
import KernelAbstractions
16-
import KernelAbstractions: Adapt, StaticArrays, Backend, Kernel, StaticSize, DynamicSize, partition, blocks, workitems, launch_config, POCL
16+
import KernelAbstractions: Adapt, StaticArrays, Backend, Kernel, StaticSize, DynamicSize, partition, blocks, workitems, launch_config
17+
18+
@static if isdefined(JLArrays.KernelAbstractions, :POCL) # KA v0.10
19+
import KernelAbstractions: POCL
20+
end
1721

1822

1923
#
@@ -40,8 +44,29 @@ 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
53+
54+
Base.elsize(::Type{<:JLDeviceArray{T}}) where {T} = sizeof(T)
4355

56+
Base.size(x::JLDeviceArray) = x.dims
57+
Base.sizeof(x::JLDeviceArray) = Base.elsize(x) * length(x)
4458

59+
Base.unsafe_convert(::Type{Ptr{T}}, x::JLDeviceArray{T}) where {T} =
60+
convert(Ptr{T}, pointer(x.data)) + x.offset*Base.elsize(x)
61+
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
66+
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)
69+
end
4570

4671
#
4772
# Host abstractions
@@ -314,9 +339,14 @@ end
314339

315340
## GPUArrays interfaces
316341

317-
function Adapt.adapt_storage(::Adaptor, x::JLArray{T,N}) where {T,N}
318-
arr = typed_data(x)
319-
Adapt.adapt_storage(POCL.KernelAdaptor([pointer(arr)]), arr)
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
320350
end
321351

322352
function GPUArrays.mapreducedim!(f, op, R::AnyJLArray, A::Union{AbstractArray,Broadcast.Broadcasted};
@@ -358,8 +388,18 @@ KernelAbstractions.allocate(::JLBackend, ::Type{T}, dims::Tuple) where T = JLArr
358388
return ndrange, workgroupsize, iterspace, dynamic
359389
end
360390

361-
function convert_to_cpu(obj::Kernel{JLBackend, W, N, F}) where {W, N, F}
362-
return Kernel{typeof(KernelAbstractions.POCLBackend()), W, N, F}(KernelAbstractions.POCLBackend(), obj.f)
391+
@static if !isdefined(JLArrays.KernelAbstractions, :isgpu) # KA v0.9
392+
KernelAbstractions.isgpu(b::JLBackend) = false
393+
end
394+
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
363403
end
364404

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

371411
Adapt.adapt_storage(::JLBackend, a::Array) = Adapt.adapt(JLArrays.JLArray, a)
372412
Adapt.adapt_storage(::JLBackend, a::JLArrays.JLArray) = a
373-
Adapt.adapt_storage(::KernelAbstractions.POCLBackend, 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
374419

375420
end

0 commit comments

Comments
 (0)