Repository navigation
Document the contract of custom NDRange mappings
#781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,6 +140,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`. | ||
|
|
||
|
Comment on lines
+143
to
+160
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it ok to document this as part of the public contract? At this point, possibility to extend these three functions and then #848 is what we need in Oceananigans. I don't need to get this PR merged immediately (I still need to remove the extra stuff in |
||
| # Example | ||
| ``` | ||
| ndrange = NDRange{2, DynamicSize, DynamicSize}(CartesianIndices((256, 256)), CartesianIndices((32, 32))) | ||
|
|
@@ -188,6 +206,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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a new contract, I am not supper happy about. These are private implementation details and a packing extending them is doing fishy work, and potentially type piracy.
I would be much happier if we had a public interface that folks could extend
And then Oceananigans could do its subtype of that, and so
ndrange=Mapping()