Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ KernelAbstractions.backend
foreach_index
```

## [Custom iteration spaces](@id custom_iteration_spaces)

A package can launch kernels over an iteration space of its own, for example a list of
indices, by passing an `ndrange` of its own type and extending the functions below for
that type and for the mapping of its [`NDRange`](@ref KernelAbstractions.NDIteration.NDRange).
They are public, but not exported. See [`NDRange`](@ref KernelAbstractions.NDIteration.NDRange)
for the methods a custom iteration space has to define.

```@docs
KernelAbstractions.partition
KernelAbstractions.cartesian
KernelAbstractions.NDIteration.NDRange
KernelAbstractions.NDIteration.expand
KernelAbstractions.NDIteration.linear_index
```

## Reflection

To look at the code a backend actually generates, wrap a kernel launch in one of the
Expand All @@ -110,17 +126,14 @@ The functionalities in this section are considered internal and not part of the
They are only documented here for developers and contributors of `KernelAbstractions.jl`, but should not be used by end users (and if they do, they should expect breakage without notice).

```@docs
KernelAbstractions.partition
KernelAbstractions.@context
KernelAbstractions.argconvert
KernelAbstractions.NDIteration.DynamicSize
KernelAbstractions.NDIteration.StaticSize
KernelAbstractions.NDIteration.NDRange
KernelAbstractions.NDIteration.StaticOffset
KernelAbstractions.NDIteration.DynamicOffset
KernelAbstractions.NDIteration.extents
KernelAbstractions.NDIteration.offsets
KernelAbstractions.NDIteration.linear_index
KernelAbstractions.LinearLaunch
KernelAbstractions.NDLaunch
KernelAbstractions.select_launch
Expand Down
5 changes: 3 additions & 2 deletions docs/src/implementations.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ and tune the workgroup size with
[`launch_workgroupsize`](@ref KernelAbstractions.launch_workgroupsize), as the generic
launch in `src/backend_launch.jl` does.

Packages that customize the iteration space (with a custom `partition` and `expand`)
don't need to do anything for these launches: the index functions only compute the global
Packages that customize the iteration space (with a custom `partition` and `expand`, see
[Custom iteration spaces](@ref custom_iteration_spaces)) don't need to do anything for these
launches: the index functions only compute the global
index directly for the iteration spaces KernelAbstractions creates itself, and call
`expand`, `in` and `linear_index` otherwise.

Expand Down
7 changes: 5 additions & 2 deletions src/KernelAbstractions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,11 @@ end

Partition the iteration space of `kernel` into workgroups.

Returns the blocked iteration space and whether dynamic bounds-checking is required for the
last (possibly partial) workgroup. Primarily used by backend implementations and tests.
Returns the blocked iteration space, and `DynamicCheck()` if dynamic bounds-checking is
required for the last (possibly partial) workgroup, or `NoDynamicCheck()` otherwise.

Specialize on the type of `ndrange` to launch kernels over an iteration space of your own,
see [`NDRange`](@ref KernelAbstractions.NDIteration.NDRange).
"""
@inline function partition(kernel, ndrange, workgroupsize)
static_ndrange = KernelAbstractions.ndrange(kernel)
Expand Down
10 changes: 9 additions & 1 deletion src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ struct CompilerMetadata{StaticNDRange, CheckBounds, I, NDRange, Iterspace, Launc
end
end

# `CartesianIndices` covering a launch `ndrange` (any form accepted by `partition`).
"""
cartesian(ndrange)

The object stored as `ndrange` of a kernel context for a launch `ndrange` in any form accepted
by [`partition`](@ref): `CartesianIndices` covering the range, or `nothing` for a static one.
Specialize it together with `partition` for an iteration space of your own, returning an object
that is usable on the device and supports `Base.in` for a `CartesianIndex`, and define it as the
identity on that object, see [`NDRange`](@ref KernelAbstractions.NDIteration.NDRange).
"""
cartesian(::Nothing) = nothing
cartesian(ci::CartesianIndices) = ci
cartesian(n::Integer) = CartesianIndices((Int(n),))
Expand Down
43 changes: 43 additions & 0 deletions src/nditeration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,42 @@ Encodes a blocked iteration space. The `mapping` field relates blocked indices t
`ndrange` indices: `nothing` for the identity, or a [`StaticOffset`](@ref)/[`DynamicOffset`](@ref)
for an `ndrange` whose indices do not start at 1.

# Custom mappings

A package can iterate over a space of its own, for example a list of indices, by launching a
kernel with an `ndrange` object of its own type and defining the methods below. Each of them
dispatches on a type the package owns (the type of its `ndrange`, or of its `mapping`), so no
method of KernelAbstractions is overwritten:

- [`partition(kernel, ndrange, workgroupsize)`](@ref KernelAbstractions.partition) for the
type of `ndrange`, returning an `NDRange` whose `mapping` describes the space, and
`DynamicCheck()` if some work-items have no index to handle (e.g. those
past the end of the last workgroup), or `NoDynamicCheck()` otherwise.
`workgroupsize` is the workgroup size given at launch as a tuple, or `nothing`;
- [`cartesian(ndrange)`](@ref KernelAbstractions.cartesian) for the type of `ndrange`, returning
the object stored as `ndrange` of the kernel context. It must support `Base.in` for a
`CartesianIndex`. It is passed to the kernel as is, without being adapted, so it must be
usable on the device: e.g. store the length of a list of indices rather than the list itself.
`cartesian` is applied again to its own result when the context is adapted, so it must also
be defined for that type, as the identity;
- [`expand(iterspace, groupidx::CartesianIndex, idx::CartesianIndex)`](@ref expand) for an
`NDRange` with that mapping, returning the index handled by a work item, or an index that is
not `in` the `ndrange` object for a work item without one. The methods taking `Integer`
positions call this one;
- [`linear_index(iterspace, ndrange, groupidx, idx)`](@ref linear_index) for an `NDRange` with
that mapping, returning the index given by `@index(Global, Linear)`. Without it, the
`ndrange` object must support `linear_index(ndrange, I)` for an index `I` it contains.

A kernel launched over such an iteration space must have a static workgroup size, or be
launched with a `workgroupsize`: KernelAbstractions can't tune the workgroup size of a space it
doesn't know the extents of.

KernelAbstractions computes `@index(Global, Cartesian)` as `expand(iterspace, groupidx, idx)`
for such an iteration space, and `@index(Global, Linear)` with `linear_index`. If `partition`
reports that some work-items have no index to handle, the kernel skips those whose expanded
index is not `in` the `ndrange` object. Nothing else is needed for a kernel to see the mapped
index through `@index`.

Comment thread
giordano marked this conversation as resolved.
Resolved
# Example
```
ndrange = NDRange{2, DynamicSize, DynamicSize}(CartesianIndices((256, 256)), CartesianIndices((32, 32)))
Expand Down Expand Up @@ -194,6 +230,13 @@ import Base.iterate

Base.length(range::NDRange) = length(blocks(range))

"""
expand(ndrange::NDRange, groupidx, idx)

Index of the `ndrange` handled by work item `idx` of workgroup `groupidx`, both given as a
`CartesianIndex` or as a linear position in the blocked iteration space. The result follows
the `mapping` of the `ndrange`.
"""
@inline function expand(ndrange::NDRange{N}, groupidx::CartesianIndex{N}, idx::CartesianIndex{N}) where {N}
offset = offsets(ndrange)
nI = ntuple(Val(N)) do I
Expand Down
Loading