From b1400f91b7cb454dc97aa2d1a46ece69bc6c5f00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Sun, 13 Sep 2026 19:40:28 -0500 Subject: [PATCH 1/4] Document the contract of custom `NDRange` mappings Since #771 the `mapping` of an `NDRange` decides how blocked indices map to `ndrange` indices, and every backend derives the index, validity and linear index of a work item from `expand`, `in` and `linear_index`. That makes it possible for a package to launch kernels over an iteration space of its own, such as a list of indices, by specializing `partition` and `cartesian` for its launch object and `expand` for its mapping, but nothing said so, and a backend overriding `__validindex` for a generic context would silently break such an extension. The `NDRange` docstring now lists the functions a custom mapping defines, `partition` and the new `cartesian` and `expand` docstrings point to it, and the notes for backend implementations state that a backend must go through `expand`, `in` and `linear_index` and must not assume the `ndrange` of a context is a `CartesianIndices`. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01VHciC8x39gm97sABrSvBkt --- docs/src/api.md | 2 ++ docs/src/implementations.md | 23 +++++++++++++++++++++++ src/KernelAbstractions.jl | 3 +++ src/compiler.jl | 9 ++++++++- src/nditeration.jl | 25 +++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) diff --git a/docs/src/api.md b/docs/src/api.md index 450e9f074..8b3eecd26 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -111,11 +111,13 @@ They are only documented here for developers and contributors of `KernelAbstract ```@docs KernelAbstractions.partition +KernelAbstractions.cartesian KernelAbstractions.@context KernelAbstractions.argconvert KernelAbstractions.NDIteration.DynamicSize KernelAbstractions.NDIteration.StaticSize KernelAbstractions.NDIteration.NDRange +KernelAbstractions.NDIteration.expand KernelAbstractions.NDIteration.StaticOffset KernelAbstractions.NDIteration.DynamicOffset KernelAbstractions.NDIteration.extents diff --git a/docs/src/implementations.md b/docs/src/implementations.md index f51173a3a..3a693f465 100644 --- a/docs/src/implementations.md +++ b/docs/src/implementations.md @@ -124,3 +124,26 @@ Packages with an `Adapt` rule for `CompilerMetadata` **must** preserve its `laun passing `launch = KernelAbstractions.__launch(ctx)` to the constructor. Otherwise the kernel computes its indices as if it had been launched on a 1-D grid, which gives wrong results for a kernel launched with an `NDLaunch`. + +## Iteration spaces and the validity of work items + +The context a backend passes to a kernel carries the blocked iteration space, +[`__iterspace(ctx)`](@ref KernelAbstractions.NDIteration.NDRange), and the +`ndrange` of the launch, `__ndrange(ctx)`. A backend must derive everything +about a work item from these two objects through three functions: + +- [`expand(iterspace, groupidx, idx)`](@ref KernelAbstractions.NDIteration.expand) + gives the `CartesianIndex` handled by work item `idx` of workgroup `groupidx`; +- `expand(iterspace, groupidx, idx) in __ndrange(ctx)` tells whether that work + item has an index to handle, which is how `__validindex` must be implemented; +- [`linear_index(__ndrange(ctx), I)`](@ref KernelAbstractions.NDIteration.linear_index) + gives the linear index of `I`. + +A backend must not assume that `__ndrange(ctx)` is a `CartesianIndices` or that +`expand` is an affine map: the `mapping` field of the `NDRange` lets a package +define its own iteration space, for example a list of indices to visit, by +extending these functions for its mapping type. Overriding `__validindex` or +`__index_Global_Linear` for a generic `ctx` would bypass such an extension. +See [`NDRange`](@ref KernelAbstractions.NDIteration.NDRange) for the functions +a custom mapping has to define. + diff --git a/src/KernelAbstractions.jl b/src/KernelAbstractions.jl index bda2510d3..d7ae3b509 100644 --- a/src/KernelAbstractions.jl +++ b/src/KernelAbstractions.jl @@ -587,6 +587,9 @@ 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. + +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..75f8cd521 100644 --- a/src/compiler.jl +++ b/src/compiler.jl @@ -27,7 +27,14 @@ 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 supports `Base.in` for a `CartesianIndex` and [`linear_index`](@ref KernelAbstractions.NDIteration.linear_index). +""" 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..7862b53ac 100644 --- a/src/nditeration.jl +++ b/src/nditeration.jl @@ -142,6 +142,24 @@ 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: + +- [`partition(kernel, ndrange, workgroupsize)`](@ref KernelAbstractions.partition) for that + type, returning an `NDRange` whose `mapping` describes the space, and whether the last + workgroup needs bounds-checking; +- [`cartesian(ndrange)`](@ref KernelAbstractions.cartesian) for that type, returning the + object stored as `ndrange` of the kernel context, which supports `Base.in` for a + `CartesianIndex` and [`linear_index`](@ref); +- [`expand`](@ref) for an `NDRange` with that mapping and `groupidx`, `idx` given as + `Integer` or `CartesianIndex`, returning the index handled by a work item, or an index that + is not `in` the `ndrange` object for a work item without one. + +Backends check the validity of a work item as `expand(iterspace, groupidx, idx) in ndrange`, +so nothing else is needed for the kernel to see the mapped index through `@index`. + # Example ``` ndrange = NDRange{2, DynamicSize, DynamicSize}(CartesianIndices((256, 256)), CartesianIndices((32, 32))) @@ -194,6 +212,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 From a56f92fe551d1fe0067c2e5896a55d44b39ac7ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Sat, 28 Nov 2026 08:26:00 -0600 Subject: [PATCH 2/4] Address review: make custom iteration spaces a public contract Drop the section on what backends must do from the implementation notes, as the notes on launching `@kernel` kernels already forbid overriding the index functions, and point them at the custom mapping contract instead. Document `partition`, `cartesian`, `NDRange`, `expand` and `linear_index` in a section of their own in the API docs, rather than with the internals, and state that the methods of a custom iteration space dispatch on the package's own types. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WMgptbAJCWVM9f2oykYof8 --- docs/src/api.md | 21 ++++++++++++++++----- docs/src/implementations.md | 28 +++------------------------- src/nditeration.jl | 4 +++- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/docs/src/api.md b/docs/src/api.md index 8b3eecd26..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,19 +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.cartesian KernelAbstractions.@context KernelAbstractions.argconvert KernelAbstractions.NDIteration.DynamicSize KernelAbstractions.NDIteration.StaticSize -KernelAbstractions.NDIteration.NDRange -KernelAbstractions.NDIteration.expand 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 3a693f465..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. @@ -124,26 +125,3 @@ Packages with an `Adapt` rule for `CompilerMetadata` **must** preserve its `laun passing `launch = KernelAbstractions.__launch(ctx)` to the constructor. Otherwise the kernel computes its indices as if it had been launched on a 1-D grid, which gives wrong results for a kernel launched with an `NDLaunch`. - -## Iteration spaces and the validity of work items - -The context a backend passes to a kernel carries the blocked iteration space, -[`__iterspace(ctx)`](@ref KernelAbstractions.NDIteration.NDRange), and the -`ndrange` of the launch, `__ndrange(ctx)`. A backend must derive everything -about a work item from these two objects through three functions: - -- [`expand(iterspace, groupidx, idx)`](@ref KernelAbstractions.NDIteration.expand) - gives the `CartesianIndex` handled by work item `idx` of workgroup `groupidx`; -- `expand(iterspace, groupidx, idx) in __ndrange(ctx)` tells whether that work - item has an index to handle, which is how `__validindex` must be implemented; -- [`linear_index(__ndrange(ctx), I)`](@ref KernelAbstractions.NDIteration.linear_index) - gives the linear index of `I`. - -A backend must not assume that `__ndrange(ctx)` is a `CartesianIndices` or that -`expand` is an affine map: the `mapping` field of the `NDRange` lets a package -define its own iteration space, for example a list of indices to visit, by -extending these functions for its mapping type. Overriding `__validindex` or -`__index_Global_Linear` for a generic `ctx` would bypass such an extension. -See [`NDRange`](@ref KernelAbstractions.NDIteration.NDRange) for the functions -a custom mapping has to define. - diff --git a/src/nditeration.jl b/src/nditeration.jl index 7862b53ac..f76ee5c09 100644 --- a/src/nditeration.jl +++ b/src/nditeration.jl @@ -145,7 +145,9 @@ 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: +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 that type, returning an `NDRange` whose `mapping` describes the space, and whether the last From 35bd81c3a84d3d47afa6fa73cb1ad43c12080c99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Sat, 28 Nov 2026 08:26:53 -0600 Subject: [PATCH 3/4] Update the custom mapping contract for the current launch code - only `expand` with `CartesianIndex` positions is needed: the methods taking `Integer` positions call it; - mention the `linear_index(iterspace, ndrange, groupidx, idx)` hook (#848), which makes `linear_index` on the `ndrange` object optional; - `in` is only checked when `partition` reports work-items without an index; - `cartesian` is applied again to its own result when the context is adapted, and its result isn't adapted; - `extents` is needed to tune the workgroup size of a launch without one; - KernelAbstractions computes the indices itself, rather than backends. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WMgptbAJCWVM9f2oykYof8 --- docs/src/api.md | 2 +- src/compiler.jl | 3 ++- src/nditeration.jl | 40 +++++++++++++++++++++++++++------------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/docs/src/api.md b/docs/src/api.md index f896d98b7..f66d590ee 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -99,6 +99,7 @@ KernelAbstractions.cartesian KernelAbstractions.NDIteration.NDRange KernelAbstractions.NDIteration.expand KernelAbstractions.NDIteration.linear_index +KernelAbstractions.NDIteration.extents ``` ## Reflection @@ -132,7 +133,6 @@ KernelAbstractions.NDIteration.DynamicSize KernelAbstractions.NDIteration.StaticSize KernelAbstractions.NDIteration.StaticOffset KernelAbstractions.NDIteration.DynamicOffset -KernelAbstractions.NDIteration.extents KernelAbstractions.NDIteration.offsets KernelAbstractions.LinearLaunch KernelAbstractions.NDLaunch diff --git a/src/compiler.jl b/src/compiler.jl index 75f8cd521..e651678fe 100644 --- a/src/compiler.jl +++ b/src/compiler.jl @@ -33,7 +33,8 @@ end 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 supports `Base.in` for a `CartesianIndex` and [`linear_index`](@ref KernelAbstractions.NDIteration.linear_index). +that 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 diff --git a/src/nditeration.jl b/src/nditeration.jl index f76ee5c09..0b4127161 100644 --- a/src/nditeration.jl +++ b/src/nditeration.jl @@ -27,7 +27,8 @@ axis_offset(r::AbstractUnitRange) = first(r) - 1 extents(ndrange) Number of indices along each axis of `ndrange`, given as a tuple of extents and/or ranges, -a `CartesianIndices`, a single range, or an integer. +a `CartesianIndices`, a single range, or an integer. Extend it for an `ndrange` of your own type +launched without a workgroup size, see [`NDRange`](@ref). """ extents(t::Tuple) = map(extent, t) extents(ci::CartesianIndices) = size(ci) @@ -149,18 +150,31 @@ kernel with an `ndrange` object of its own type and defining the methods below. 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 that - type, returning an `NDRange` whose `mapping` describes the space, and whether the last - workgroup needs bounds-checking; -- [`cartesian(ndrange)`](@ref KernelAbstractions.cartesian) for that type, returning the - object stored as `ndrange` of the kernel context, which supports `Base.in` for a - `CartesianIndex` and [`linear_index`](@ref); -- [`expand`](@ref) for an `NDRange` with that mapping and `groupidx`, `idx` given as - `Integer` or `CartesianIndex`, returning the index handled by a work item, or an index that - is not `in` the `ndrange` object for a work item without one. - -Backends check the validity of a work item as `expand(iterspace, groupidx, idx) in ndrange`, -so nothing else is needed for the kernel to see the mapped index through `@index`. +- [`partition(kernel, ndrange, workgroupsize)`](@ref KernelAbstractions.partition) for the + type of `ndrange`, returning an `NDRange` whose `mapping` describes the space, and whether + some work-items have no index to handle (e.g. those past the end of the last workgroup). + `workgroupsize` is a tuple, or `nothing` when the kernel has a static workgroup size; +- [`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`, and is passed to the kernel as is, without being adapted. `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; +- [`extents(ndrange)`](@ref extents) for the type of `ndrange`, returning the number of + work-items along each dimension of the blocked iteration space, if the kernel is launched + without a workgroup size: it is used to tune one. + +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 ``` From 3a24a8216fffdace5e67234e0ed0af1ec79bf769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Sat, 28 Nov 2026 08:34:05 -0600 Subject: [PATCH 4/4] Fix the custom mapping contract and drop `extents` from it - `partition` returns `DynamicCheck()` or `NoDynamicCheck()`, not a `Bool`; - require a static or given workgroup size for a custom iteration space rather than making `extents` part of the contract, as no user needs to tune its workgroup size (Oceananigans uses a static one); - the result of `cartesian` must be usable on the device, as it isn't adapted; - `partition` is no longer only for backends and tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WMgptbAJCWVM9f2oykYof8 --- docs/src/api.md | 2 +- src/KernelAbstractions.jl | 4 ++-- src/compiler.jl | 4 ++-- src/nditeration.jl | 26 ++++++++++++++------------ 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/docs/src/api.md b/docs/src/api.md index f66d590ee..f896d98b7 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -99,7 +99,6 @@ KernelAbstractions.cartesian KernelAbstractions.NDIteration.NDRange KernelAbstractions.NDIteration.expand KernelAbstractions.NDIteration.linear_index -KernelAbstractions.NDIteration.extents ``` ## Reflection @@ -133,6 +132,7 @@ KernelAbstractions.NDIteration.DynamicSize KernelAbstractions.NDIteration.StaticSize KernelAbstractions.NDIteration.StaticOffset KernelAbstractions.NDIteration.DynamicOffset +KernelAbstractions.NDIteration.extents KernelAbstractions.NDIteration.offsets KernelAbstractions.LinearLaunch KernelAbstractions.NDLaunch diff --git a/src/KernelAbstractions.jl b/src/KernelAbstractions.jl index d7ae3b509..101af1697 100644 --- a/src/KernelAbstractions.jl +++ b/src/KernelAbstractions.jl @@ -585,8 +585,8 @@ 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). diff --git a/src/compiler.jl b/src/compiler.jl index e651678fe..713199fb1 100644 --- a/src/compiler.jl +++ b/src/compiler.jl @@ -33,8 +33,8 @@ end 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 supports `Base.in` for a `CartesianIndex`, and define it as the identity on that object, see -[`NDRange`](@ref KernelAbstractions.NDIteration.NDRange). +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 diff --git a/src/nditeration.jl b/src/nditeration.jl index 0b4127161..977c20135 100644 --- a/src/nditeration.jl +++ b/src/nditeration.jl @@ -27,8 +27,7 @@ axis_offset(r::AbstractUnitRange) = first(r) - 1 extents(ndrange) Number of indices along each axis of `ndrange`, given as a tuple of extents and/or ranges, -a `CartesianIndices`, a single range, or an integer. Extend it for an `ndrange` of your own type -launched without a workgroup size, see [`NDRange`](@ref). +a `CartesianIndices`, a single range, or an integer. """ extents(t::Tuple) = map(extent, t) extents(ci::CartesianIndices) = size(ci) @@ -151,24 +150,27 @@ dispatches on a type the package owns (the type of its `ndrange`, or of its `map 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 whether - some work-items have no index to handle (e.g. those past the end of the last workgroup). - `workgroupsize` is a tuple, or `nothing` when the kernel has a static workgroup size; + 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`, and is passed to the kernel as is, without being adapted. `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; + `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; -- [`extents(ndrange)`](@ref extents) for the type of `ndrange`, returning the number of - work-items along each dimension of the blocked iteration space, if the kernel is launched - without a workgroup size: it is used to tune one. + `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`