-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathoneAPIKernels.jl
More file actions
176 lines (127 loc) · 4.85 KB
/
oneAPIKernels.jl
File metadata and controls
176 lines (127 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
module oneAPIKernels
using ..oneAPI
using ..oneAPI: @device_override, SPIRVIntrinsics, method_table
import KernelAbstractions as KA
import StaticArrays
import Adapt
## Back-end Definition
export oneAPIBackend
struct oneAPIBackend <: KA.GPU
end
KA.allocate(::oneAPIBackend, ::Type{T}, dims::Tuple) where T = oneArray{T}(undef, dims)
KA.zeros(::oneAPIBackend, ::Type{T}, dims::Tuple) where T = oneAPI.zeros(T, dims)
KA.ones(::oneAPIBackend, ::Type{T}, dims::Tuple) where T = oneAPI.ones(T, dims)
KA.get_backend(::oneArray) = oneAPIBackend()
# TODO should be non-blocking
KA.synchronize(::oneAPIBackend) = oneL0.synchronize()
KA.supports_float64(::oneAPIBackend) = false # TODO: Check if this is device dependent
Adapt.adapt_storage(::oneAPIBackend, a::Array) = Adapt.adapt(oneArray, a)
Adapt.adapt_storage(::oneAPIBackend, a::oneArray) = a
Adapt.adapt_storage(::KA.CPU, a::oneArray) = convert(Array, a)
## Memory Operations
function KA.copyto!(::oneAPIBackend, A, B)
copyto!(A, B)
# TODO: Address device to host copies in jl being synchronizing
end
## Kernel Launch
function KA.mkcontext(kernel::KA.Kernel{oneAPIBackend}, _ndrange, iterspace)
KA.CompilerMetadata{KA.ndrange(kernel), KA.DynamicCheck}(_ndrange, iterspace)
end
function KA.mkcontext(kernel::KA.Kernel{oneAPIBackend}, I, _ndrange, iterspace,
::Dynamic) where Dynamic
KA.CompilerMetadata{KA.ndrange(kernel), Dynamic}(I, _ndrange, iterspace)
end
function KA.launch_config(kernel::KA.Kernel{oneAPIBackend}, ndrange, workgroupsize)
if ndrange isa Integer
ndrange = (ndrange,)
end
if workgroupsize isa Integer
workgroupsize = (workgroupsize, )
end
# partition checked that the ndrange's agreed
if KA.ndrange(kernel) <: KA.StaticSize
ndrange = nothing
end
iterspace, dynamic = if KA.workgroupsize(kernel) <: KA.DynamicSize &&
workgroupsize === nothing
# use ndrange as preliminary workgroupsize for autotuning
KA.partition(kernel, ndrange, ndrange)
else
KA.partition(kernel, ndrange, workgroupsize)
end
return ndrange, workgroupsize, iterspace, dynamic
end
function threads_to_workgroupsize(threads, ndrange)
total = 1
return map(ndrange) do n
x = min(div(threads, total), n)
total *= x
return x
end
end
function (obj::KA.Kernel{oneAPIBackend})(args...; ndrange=nothing, workgroupsize=nothing)
ndrange, workgroupsize, iterspace, dynamic = KA.launch_config(obj, ndrange, workgroupsize)
# this might not be the final context, since we may tune the workgroupsize
ctx = KA.mkcontext(obj, ndrange, iterspace)
kernel = @oneapi launch=false obj.f(ctx, args...)
# figure out the optimal workgroupsize automatically
if KA.workgroupsize(obj) <: KA.DynamicSize && workgroupsize === nothing
items = oneAPI.launch_configuration(kernel)
workgroupsize = threads_to_workgroupsize(items, ndrange)
iterspace, dynamic = KA.partition(obj, ndrange, workgroupsize)
ctx = KA.mkcontext(obj, ndrange, iterspace)
end
groups = length(KA.blocks(iterspace))
items = length(KA.workitems(iterspace))
if groups == 0
return nothing
end
# Launch kernel
kernel(ctx, args...; items, groups)
return nothing
end
## Indexing Functions
@device_override @inline function KA.__index_Local_Linear(ctx)
return get_local_id()
end
@device_override @inline function KA.__index_Group_Linear(ctx)
return get_group_id()
end
@device_override @inline function KA.__index_Global_Linear(ctx)
return get_global_id()
end
@device_override @inline function KA.__index_Local_Cartesian(ctx)
@inbounds KA.workitems(KA.__iterspace(ctx))[get_local_id()]
end
@device_override @inline function KA.__index_Group_Cartesian(ctx)
@inbounds KA.blocks(KA.__iterspace(ctx))[get_group_id()]
end
@device_override @inline function KA.__index_Global_Cartesian(ctx)
return @inbounds KA.expand(KA.__iterspace(ctx), get_group_id(), get_local_id())
end
@device_override @inline function KA.__validindex(ctx)
if KA.__dynamic_checkbounds(ctx)
I = @inbounds KA.expand(KA.__iterspace(ctx), get_group_id(), get_local_id())
return I in KA.__ndrange(ctx)
else
return true
end
end
## Shared and Scratch Memory
@device_override @inline function KA.SharedMemory(::Type{T}, ::Val{Dims}, ::Val{Id}) where {T, Dims, Id}
ptr = oneAPI.emit_localmemory(T, Val(prod(Dims)))
oneDeviceArray(Dims, ptr)
end
@device_override @inline function KA.Scratchpad(ctx, ::Type{T}, ::Val{Dims}) where {T, Dims}
StaticArrays.MArray{KA.__size(Dims), T}(undef)
end
## Synchronization and Printing
@device_override @inline function KA.__synchronize()
barrier(0)
end
@device_override @inline function KA.__print(args...)
oneAPI._print(args...)
end
## Other
KA.argconvert(::KA.Kernel{oneAPIBackend}, arg) = kernel_convert(arg)
end