Skip to content

Commit cc51ff0

Browse files
Explicit OffsetNDRange vs NDRange (#3359)
* fix problem with `OffsetNDRange` * some comments
1 parent 80ce6ed commit cc51ff0

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/Utils/kernel_launching.jl

+19-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ using Oceananigans.Grids
88

99
using Oceananigans.Grids: AbstractGrid
1010

11+
import Base
12+
1113
struct KernelParameters{S, O} end
1214

1315
"""
@@ -152,8 +154,15 @@ end
152154

153155
#####
154156
##### Extension to KA for offset indices: to remove when implemented in KA
155-
##### Allows to call a kernel with kernel(dev, workgroup, worksize, offsets)
157+
##### Allows to use `launch!` with offsets, e.g.:
158+
##### `launch!(arch, grid, KernelParameters(size, offsets), kernel!; kernel_args...)`
156159
##### where offsets is a tuple containing the offset to pass to @index
160+
##### Note that this syntax is only usable in conjunction with the `launch!` function and
161+
##### will have no effect if the kernel is launched with `kernel!` directly.
162+
##### To achieve the same result with kernel launching, the correct syntax is:
163+
##### `kernel!(arch, StaticSize(size), OffsetStaticSize(contiguousrange(size, offset)))`
164+
##### Using offsets is (at the moment) incompatible with dynamic workgroup sizes: in case of offset dynamic kernels
165+
##### offsets will have to be passed manually.
157166
#####
158167

159168
# TODO: when offsets are implemented in KA so that we can call `kernel(dev, group, size, offsets)`, remove all of this
@@ -193,7 +202,14 @@ import KernelAbstractions: get, expand
193202
@inline worksize(i::Int) = i
194203
@inline worksize(i::UnitRange) = length(i)
195204

196-
const OffsetNDRange{N} = NDRange{N, <:StaticSize, <:StaticSize, <:Any, <:Tuple} where N
205+
"""a type used to store offsets in `NDRange` types"""
206+
struct KernelOffsets{O}
207+
offsets :: O
208+
end
209+
210+
Base.getindex(o::KernelOffsets, args...) = getindex(o.offsets, args...)
211+
212+
const OffsetNDRange{N} = NDRange{N, <:StaticSize, <:StaticSize, <:Any, <:KernelOffsets} where N
197213

198214
# NDRange has been modified to have offsets in place of workitems: Remember, dynamic offset kernels are not possible with this extension!!
199215
@inline function expand(ndrange::OffsetNDRange{N}, groupidx::CartesianIndex{N}, idx::CartesianIndex{N}) where {N}
@@ -243,7 +259,7 @@ function partition(kernel::OffsetKernel, inrange, ingroupsize)
243259
static_blocks = StaticSize{blocks}
244260
static_workgroupsize = StaticSize{groupsize} # we might have padded workgroupsize
245261

246-
iterspace = NDRange{length(range), static_blocks, static_workgroupsize}(blocks, offsets)
262+
iterspace = NDRange{length(range), static_blocks, static_workgroupsize}(blocks, KernelOffsets(offsets))
247263

248264
return iterspace, dynamic
249265
end

0 commit comments

Comments
 (0)