diff --git a/docs/src/api.md b/docs/src/api.md index 450e9f074..f896d98b7 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -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 @@ -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 diff --git a/docs/src/implementations.md b/docs/src/implementations.md index f51173a3a..5c998a6b9 100644 --- a/docs/src/implementations.md +++ b/docs/src/implementations.md @@ -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. diff --git a/src/KernelAbstractions.jl b/src/KernelAbstractions.jl index bda2510d3..101af1697 100644 --- a/src/KernelAbstractions.jl +++ b/src/KernelAbstractions.jl @@ -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) diff --git a/src/compiler.jl b/src/compiler.jl index cbc22721b..713199fb1 100644 --- a/src/compiler.jl +++ b/src/compiler.jl @@ -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),)) diff --git a/src/nditeration.jl b/src/nditeration.jl index ef7c88b53..977c20135 100644 --- a/src/nditeration.jl +++ b/src/nditeration.jl @@ -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`. + # Example ``` ndrange = NDRange{2, DynamicSize, DynamicSize}(CartesianIndices((256, 256)), CartesianIndices((32, 32))) @@ -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