Skip to content

Document the contract of custom NDRange mappings - #781

Open
giordano wants to merge 4 commits into
mainfrom
mg/document-mapping-contract
Open

giordano wants to merge 4 commits into
mainfrom
mg/document-mapping-contract

Conversation

@giordano

Copy link
Copy Markdown
Collaborator

No description provided.

@giordano giordano added the documentation Improvements or additions to documentation label Sep 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Benchmark Results

Show table
main 7f06431... main / 7f06431...
launch/3D static workgroup, dynamic ndrange 23.5 ± 17 μs 23.5 ± 20 μs 1 ± 1.1
launch/3D static workgroup, static ndrange 23.1 ± 16 μs 23.6 ± 20 μs 0.979 ± 1.1
launch/dynamic workgroup, dynamic ndrange 22.6 ± 8.4 μs 22.8 ± 4.9 μs 0.99 ± 0.43
launch/dynamic workgroup, dynamic ndrange, workgroupsize given 23.7 ± 17 μs 23.8 ± 20 μs 0.997 ± 1.1
launch/static workgroup, dynamic ndrange 23.7 ± 18 μs 23.4 ± 21 μs 1.02 ± 1.2
launch/static workgroup, static ndrange 22.9 ± 15 μs 23.4 ± 19 μs 0.983 ± 1
partition/dynamic workgroup, dynamic ndrange 0.0524 ± 0.0073 μs 0.0535 ± 0.0053 μs 0.979 ± 0.17
partition/static workgroup, dynamic ndrange 0.0612 ± 0.011 μs 0.0605 ± 0.011 μs 1.01 ± 0.25
partition/static workgroup, static ndrange 2.01 ± 0.005 ns 1.35 ± 0.009 ns 1.48 ± 0.011
saxpy/default/Float16/1024 27.2 ± 25 μs 25.2 ± 22 μs 1.08 ± 1.4
saxpy/default/Float16/1048576 0.365 ± 0.021 ms 0.365 ± 0.019 ms 0.999 ± 0.078
saxpy/default/Float16/16384 0.0503 ± 0.025 ms 0.0468 ± 0.025 ms 1.07 ± 0.79
saxpy/default/Float16/2048 0.0372 ± 0.023 ms 0.0362 ± 0.023 ms 1.03 ± 0.92
saxpy/default/Float16/256 23.5 ± 8.3 μs 23.5 ± 5 μs 1 ± 0.41
saxpy/default/Float16/262144 0.126 ± 0.023 ms 0.127 ± 0.023 ms 0.994 ± 0.26
saxpy/default/Float16/32768 0.0472 ± 0.023 ms 0.0505 ± 0.025 ms 0.933 ± 0.64
saxpy/default/Float16/4096 0.0504 ± 0.023 ms 0.0499 ± 0.023 ms 1.01 ± 0.66
saxpy/default/Float16/512 24.1 ± 14 μs 23.7 ± 8.2 μs 1.02 ± 0.68
saxpy/default/Float16/64 23.9 ± 16 μs 23.4 ± 3 μs 1.02 ± 0.69
saxpy/default/Float16/65536 0.0567 ± 0.022 ms 0.0575 ± 0.023 ms 0.986 ± 0.55
saxpy/default/Float32/1024 27.9 ± 27 μs 26.3 ± 26 μs 1.06 ± 1.5
saxpy/default/Float32/1048576 0.367 ± 0.046 ms 0.359 ± 0.045 ms 1.02 ± 0.18
saxpy/default/Float32/16384 0.049 ± 0.026 ms 0.0432 ± 0.024 ms 1.13 ± 0.87
saxpy/default/Float32/2048 0.0489 ± 0.023 ms 0.0477 ± 0.024 ms 1.02 ± 0.7
saxpy/default/Float32/256 23.4 ± 14 μs 23.7 ± 5.4 μs 0.988 ± 0.63
saxpy/default/Float32/262144 0.111 ± 0.019 ms 0.111 ± 0.018 ms 1 ± 0.24
saxpy/default/Float32/32768 0.0477 ± 0.025 ms 0.0476 ± 0.025 ms 1 ± 0.73
saxpy/default/Float32/4096 0.0509 ± 0.022 ms 0.0512 ± 0.022 ms 0.995 ± 0.61
saxpy/default/Float32/512 25.8 ± 27 μs 28 ± 26 μs 0.923 ± 1.3
saxpy/default/Float32/64 23.2 ± 6.1 μs 23.4 ± 3.3 μs 0.991 ± 0.29
saxpy/default/Float32/65536 0.0591 ± 0.026 ms 0.0568 ± 0.025 ms 1.04 ± 0.65
saxpy/default/Float64/1024 0.0347 ± 0.029 ms 27.8 ± 27 μs 1.24 ± 1.6
saxpy/default/Float64/1048576 0.684 ± 0.083 ms 0.663 ± 0.083 ms 1.03 ± 0.18
saxpy/default/Float64/16384 0.049 ± 0.026 ms 0.0487 ± 0.027 ms 1.01 ± 0.77
saxpy/default/Float64/2048 0.0401 ± 0.027 ms 0.0377 ± 0.029 ms 1.06 ± 1.1
saxpy/default/Float64/256 23.5 ± 12 μs 24.5 ± 13 μs 0.958 ± 0.69
saxpy/default/Float64/262144 0.194 ± 0.025 ms 0.189 ± 0.027 ms 1.03 ± 0.2
saxpy/default/Float64/32768 0.0563 ± 0.028 ms 0.0567 ± 0.028 ms 0.994 ± 0.69
saxpy/default/Float64/4096 0.045 ± 0.028 ms 0.0528 ± 0.023 ms 0.852 ± 0.64
saxpy/default/Float64/512 23.7 ± 13 μs 24.3 ± 8.8 μs 0.974 ± 0.64
saxpy/default/Float64/64 23.5 ± 7.4 μs 23.6 ± 3.2 μs 0.996 ± 0.34
saxpy/default/Float64/65536 0.071 ± 0.026 ms 0.0732 ± 0.025 ms 0.969 ± 0.49
saxpy/static workgroup=(1024,)/Float16/1024 29.6 ± 25 μs 27.4 ± 25 μs 1.08 ± 1.4
saxpy/static workgroup=(1024,)/Float16/1048576 0.363 ± 0.019 ms 0.363 ± 0.02 ms 1 ± 0.076
saxpy/static workgroup=(1024,)/Float16/16384 0.0404 ± 0.022 ms 0.0409 ± 0.022 ms 0.986 ± 0.76
saxpy/static workgroup=(1024,)/Float16/2048 0.0378 ± 0.023 ms 0.0357 ± 0.022 ms 1.06 ± 0.93
saxpy/static workgroup=(1024,)/Float16/256 27.4 ± 25 μs 26.2 ± 23 μs 1.04 ± 1.3
saxpy/static workgroup=(1024,)/Float16/262144 0.124 ± 0.023 ms 0.124 ± 0.023 ms 1 ± 0.26
saxpy/static workgroup=(1024,)/Float16/32768 0.0471 ± 0.023 ms 0.0483 ± 0.024 ms 0.975 ± 0.68
saxpy/static workgroup=(1024,)/Float16/4096 0.0491 ± 0.02 ms 0.0483 ± 0.021 ms 1.02 ± 0.61
saxpy/static workgroup=(1024,)/Float16/512 27.1 ± 25 μs 26.2 ± 22 μs 1.04 ± 1.3
saxpy/static workgroup=(1024,)/Float16/64 26.8 ± 25 μs 26.3 ± 22 μs 1.02 ± 1.3
saxpy/static workgroup=(1024,)/Float16/65536 0.0549 ± 0.023 ms 0.0549 ± 0.022 ms 1 ± 0.58
saxpy/static workgroup=(1024,)/Float32/1024 27 ± 27 μs 27.6 ± 26 μs 0.976 ± 1.3
saxpy/static workgroup=(1024,)/Float32/1048576 0.372 ± 0.046 ms 0.362 ± 0.042 ms 1.03 ± 0.17
saxpy/static workgroup=(1024,)/Float32/16384 0.0478 ± 0.025 ms 0.0447 ± 0.024 ms 1.07 ± 0.8
saxpy/static workgroup=(1024,)/Float32/2048 0.048 ± 0.021 ms 0.0467 ± 0.023 ms 1.03 ± 0.68
saxpy/static workgroup=(1024,)/Float32/256 25.9 ± 24 μs 23.1 ± 13 μs 1.12 ± 1.2
saxpy/static workgroup=(1024,)/Float32/262144 0.109 ± 0.018 ms 0.109 ± 0.018 ms 1 ± 0.23
saxpy/static workgroup=(1024,)/Float32/32768 0.0456 ± 0.024 ms 0.0459 ± 0.025 ms 0.993 ± 0.75
saxpy/static workgroup=(1024,)/Float32/4096 0.0487 ± 0.021 ms 0.0487 ± 0.021 ms 1 ± 0.61
saxpy/static workgroup=(1024,)/Float32/512 30.2 ± 27 μs 26.7 ± 26 μs 1.13 ± 1.5
saxpy/static workgroup=(1024,)/Float32/64 26 ± 24 μs 23.7 ± 17 μs 1.1 ± 1.3
saxpy/static workgroup=(1024,)/Float32/65536 0.0569 ± 0.025 ms 0.0573 ± 0.024 ms 0.993 ± 0.6
saxpy/static workgroup=(1024,)/Float64/1024 0.0341 ± 0.028 ms 27.5 ± 27 μs 1.24 ± 1.6
saxpy/static workgroup=(1024,)/Float64/1048576 0.685 ± 0.077 ms 0.683 ± 0.071 ms 1 ± 0.15
saxpy/static workgroup=(1024,)/Float64/16384 0.047 ± 0.024 ms 0.0435 ± 0.024 ms 1.08 ± 0.82
saxpy/static workgroup=(1024,)/Float64/2048 0.0413 ± 0.027 ms 0.0435 ± 0.026 ms 0.951 ± 0.85
saxpy/static workgroup=(1024,)/Float64/256 0.0318 ± 0.024 ms 27.7 ± 25 μs 1.15 ± 1.4
saxpy/static workgroup=(1024,)/Float64/262144 0.185 ± 0.026 ms 0.185 ± 0.025 ms 1 ± 0.2
saxpy/static workgroup=(1024,)/Float64/32768 0.052 ± 0.024 ms 0.0547 ± 0.026 ms 0.95 ± 0.63
saxpy/static workgroup=(1024,)/Float64/4096 0.0491 ± 0.022 ms 0.0505 ± 0.022 ms 0.971 ± 0.61
saxpy/static workgroup=(1024,)/Float64/512 27.9 ± 26 μs 24.1 ± 19 μs 1.16 ± 1.4
saxpy/static workgroup=(1024,)/Float64/64 25.9 ± 24 μs 24 ± 17 μs 1.08 ± 1.3
saxpy/static workgroup=(1024,)/Float64/65536 0.0684 ± 0.024 ms 0.0694 ± 0.024 ms 0.985 ± 0.49
time_to_load 0.774 ± 0.016 s 0.759 ± 0.016 s 1.02 ± 0.029
main 7f06431... main / 7f06431...
launch/3D static workgroup, dynamic ndrange 7 allocs: 0.297 kB 7 allocs: 0.297 kB 1
launch/3D static workgroup, static ndrange 8 allocs: 0.328 kB 8 allocs: 0.328 kB 1
launch/dynamic workgroup, dynamic ndrange 17 allocs: 0.469 kB 17 allocs: 0.469 kB 1
launch/dynamic workgroup, dynamic ndrange, workgroupsize given 3 allocs: 0.172 kB 3 allocs: 0.172 kB 1
launch/static workgroup, dynamic ndrange 3 allocs: 0.172 kB 3 allocs: 0.172 kB 1
launch/static workgroup, static ndrange 4 allocs: 0.203 kB 4 allocs: 0.203 kB 1
partition/dynamic workgroup, dynamic ndrange 2 allocs: 0.0625 kB 2 allocs: 0.0625 kB 1
partition/static workgroup, dynamic ndrange 2 allocs: 32 B 2 allocs: 32 B 1
partition/static workgroup, static ndrange 0 allocs: 0 B 0 allocs: 0 B
saxpy/default/Float16/1024 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float16/1048576 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float16/16384 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float16/2048 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float16/256 18 allocs: 0.484 kB 18 allocs: 0.484 kB 1
saxpy/default/Float16/262144 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float16/32768 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float16/4096 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float16/512 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float16/64 18 allocs: 0.484 kB 18 allocs: 0.484 kB 1
saxpy/default/Float16/65536 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float32/1024 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float32/1048576 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float32/16384 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float32/2048 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float32/256 18 allocs: 0.484 kB 18 allocs: 0.484 kB 1
saxpy/default/Float32/262144 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float32/32768 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float32/4096 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float32/512 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float32/64 18 allocs: 0.484 kB 18 allocs: 0.484 kB 1
saxpy/default/Float32/65536 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float64/1024 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float64/1048576 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float64/16384 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float64/2048 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float64/256 18 allocs: 0.484 kB 18 allocs: 0.484 kB 1
saxpy/default/Float64/262144 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float64/32768 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float64/4096 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float64/512 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/default/Float64/64 18 allocs: 0.484 kB 18 allocs: 0.484 kB 1
saxpy/default/Float64/65536 26 allocs: 0.609 kB 26 allocs: 0.609 kB 1
saxpy/static workgroup=(1024,)/Float16/1024 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float16/1048576 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float16/16384 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float16/2048 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float16/256 3 allocs: 0.172 kB 3 allocs: 0.172 kB 1
saxpy/static workgroup=(1024,)/Float16/262144 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float16/32768 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float16/4096 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float16/512 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float16/64 3 allocs: 0.172 kB 3 allocs: 0.172 kB 1
saxpy/static workgroup=(1024,)/Float16/65536 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float32/1024 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float32/1048576 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float32/16384 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float32/2048 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float32/256 3 allocs: 0.172 kB 3 allocs: 0.172 kB 1
saxpy/static workgroup=(1024,)/Float32/262144 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float32/32768 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float32/4096 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float32/512 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float32/64 3 allocs: 0.172 kB 3 allocs: 0.172 kB 1
saxpy/static workgroup=(1024,)/Float32/65536 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float64/1024 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float64/1048576 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float64/16384 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float64/2048 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float64/256 3 allocs: 0.172 kB 3 allocs: 0.172 kB 1
saxpy/static workgroup=(1024,)/Float64/262144 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float64/32768 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float64/4096 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float64/512 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
saxpy/static workgroup=(1024,)/Float64/64 3 allocs: 0.172 kB 3 allocs: 0.172 kB 1
saxpy/static workgroup=(1024,)/Float64/65536 6 allocs: 0.219 kB 6 allocs: 0.219 kB 1
time_to_load 0.201 k allocs: 11.8 kB 0.201 k allocs: 11.8 kB 1

Benchmark Plots

A plot of the benchmark results have been uploaded as an artifact to the workflow run for this PR.
Go to "Actions"->"Benchmark a pull request"->[the most recent run]->"Artifacts" (at the bottom).

@codecov

codecov Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.21%. Comparing base (a585449) to head (3a24a82).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #781      +/-   ##
==========================================
- Coverage   90.79%   89.21%   -1.59%     
==========================================
  Files          33       33              
  Lines        3293     3004     -289     
==========================================
- Hits         2990     2680     -310     
- Misses        303      324      +21     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread docs/src/implementations.md Outdated

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

Copy link
Copy Markdown
Member

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

abstract type Mapping end

And then Oceananigans could do its subtype of that, and so ndrange=Mapping()

Comment thread src/nditeration.jl Resolved
giordano and others added 4 commits November 28, 2026 08:22
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VHciC8x39gm97sABrSvBkt
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WMgptbAJCWVM9f2oykYof8
- 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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WMgptbAJCWVM9f2oykYof8
- `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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WMgptbAJCWVM9f2oykYof8
@giordano
giordano force-pushed the mg/document-mapping-contract branch from 7f06431 to 3a24a82 Compare November 28, 2026 14:54
@giordano

giordano commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

I updated the PR following previous review comments, but the bot really insisted for me to ask you one question:

I've updated the PR: the contract is now partition, cartesian, expand and the linear_index(iterspace, ndrange, groupidx, idx) hook from #848, all dispatching on types the package owns. The backend-side section is gone, since KA now computes the indices itself. These functions are now in a "Custom iteration spaces" section of the API docs instead of "Internal".

Oceananigans' port (CliMA/Oceananigans.jl#5799) fits this contract and drops its __validindex overrides and CompilerMetadata hacks. But it still depends on KA API that isn't public, in two groups.

1. Implementing a custom iteration space. The methods themselves can't be written with public API alone:

  • partition uses NDIteration.get and NDIteration.partition to compute the blocks and the bounds-check flag. It builds the space with the NDRange{N, B, W}(blocks, workitems, mapping) constructor and returns DynamicCheck()/NoDynamicCheck(). It restricts itself to kernels with a static workgroup size through the type parameters of Kernel (Kernel{<:Any, <:StaticSize, <:DynamicSize}). Kernel and workgroupsize(kernel) are documented, but the order of the parameters isn't, and StaticSize/DynamicSize are internal.
  • expand and linear_index dispatch on the mapping as the sixth type parameter of NDRange, read the .mapping field, and compute the position of a work-item with length(workitems(iterspace)). workitems isn't documented at all.

I see three ways forward:

  1. Make all of these public as well (documented, and marked public on Julia ≥ 1.11).
  2. Add a small public helper layer. For example, KernelAbstractions.partition_mapping(kernel, n::Integer, mapping, workgroupsize) -> (iterspace, check) would build the 1-D space of n work-items for mapping. mapping(iterspace) and position(iterspace, groupidx, idx) would give expand/linear_index the mapping and the linear position of a work-item. Then get, DynamicSize, DynamicCheck, workitems and the layout of NDRange can stay internal.
  3. Go with your earlier suggestion of a public abstract type Mapping end that a package subtypes and passes as ndrange. KA would do the partitioning itself and call something like expand(m::MyMapping, p::Int) with the linear position. The package would only define that (and optionally in/linear_index). This is the smallest surface, but it needs KA to own more of the logic.

I lean towards 2 or 3, rather than exposing the internals. What do you think?

2. Constructing kernels with StaticSize/DynamicSize. This is separate from custom mappings. Oceananigans calls kernel!(dev, StaticSize(wgs), StaticSize(range)) and kernel!(dev, DynamicSize(), StaticSize(range)), which go through the undocumented name(dev, ::_Size, ::_Size) method that @kernel generates. It also builds and dispatches on StaticSize values in its own code. The first form has a public equivalent, kernel!(dev, wgs, range). The second has none: there's no documented way to get a kernel with a dynamic workgroup size and a static ndrange. Should we document the _Size constructor (and make StaticSize/DynamicSize public), or add a public way to ask for that combination, e.g. kernel!(dev, nothing, range)?

(Test-only, for completeness: Oceananigans' tests also call KernelAbstractions.argconvert, which is listed as internal.)

@github-actions

Copy link
Copy Markdown
Contributor

@github-actions

Copy link
Copy Markdown
Contributor

Benchmark results

Compared with a585449d8, the merge base.
The environment differs from the baseline's: OS Linux 6.8.0-124-generic → Linux 6.8.0-151-generic, CompilerCaching 0.5.4 → 0.6.0, GPUCompiler 2.24.0 → 2.25.0, GPUToolbox 3.6.0 → 3.7.0, LLVM 10.7.0 → 10.8.1, LLVMExtra_jll 0.0.60+0 → 0.0.61+0, SPIRV_LLVM_Backend_jll 23.1.4+5 → 23.1.4+6.

Times are the minimum time of every benchmark. A change is unusual when it exceeds both three times the benchmark's typical change between consecutive runs on the default branch and a minimum per category (performance 3%, latency 5%, applications 3%, other 3%). A benchmark that starts or stops allocating is listed however few the allocations. A single run can still be a fluke, so rerun before drawing conclusions.

Geometric mean Unusual: slower Unusual: faster Compared
Performance −0.2% 0 0 32
Latency +0.4% 0 0 13
Applications +2.0% 0 0 5

No unusual changes.

All benchmarks
Benchmark Baseline This PR Change
Performance
julia/serial/arithmetic/muladd-reference 655 µs 655 µs −0.0%
julia/serial/math/exp-f64-reference 284 µs 291 µs +2.5%
julia/serial/narrow/masked-reference 715 µs 709 µs −0.8%
pocl/parallel/atomics/histogram 5.21 ms 5.22 ms +0.2%
pocl/parallel/groupreduce/add-f64 1.09 ms 1.10 ms +0.3%
pocl/parallel/groupreduce/fallback 4.38 ms 4.40 ms +0.4%
pocl/parallel/groupscan/add-i32 393 µs 392 µs −0.2%
pocl/parallel/indexing/cartesian3d 39.5 µs 40.3 µs +1.9%
pocl/parallel/indexing/linear3d 39.9 µs 40.5 µs +1.4%
pocl/parallel/indexing/ntuple3d 40.5 µs 39.9 µs −1.5%
pocl/parallel/indexing/ntuple4d 127 µs 128 µs +0.4%
pocl/parallel/localmem/transpose-naive 433 µs 427 µs −1.2%
pocl/parallel/localmem/transpose-tiled 65.9 µs 66.1 µs +0.4%
pocl/parallel/private/across-barrier 42.5 µs 42.3 µs −0.4%
pocl/parallel/scheduling/default 117 µs 117 µs +0.0%
pocl/parallel/scheduling/explicit 117 µs 117 µs −0.0%
pocl/parallel/stream/copy/cache 663 ns 679 ns +2.4%
pocl/parallel/stream/copy/dram 6.50 ms 6.59 ms +1.4%
pocl/parallel/stream/triad/cache 1.29 µs 1.15 µs −11.2%
pocl/parallel/stream/triad/dram 9.54 ms 9.66 ms +1.3%
pocl/parallel/util/copyto-small 25.0 ns 25.0 ns +0.0%
pocl/parallel/util/zeros-large 941 µs 933 µs −0.8%
pocl/parallel/util/zeros-small 627 ns 633 ns +1.0%
pocl/serial/arithmetic/contract 1.85 ms 1.85 ms +0.0%
pocl/serial/arithmetic/muladd 705 µs 705 µs −0.0%
pocl/serial/const/marked 668 µs 668 µs −0.0%
pocl/serial/const/unmarked 860 µs 860 µs −0.1%
pocl/serial/math/exp-f64 123 µs 123 µs −0.1%
pocl/serial/math/sin-f64 167 µs 167 µs −0.1%
pocl/serial/narrow/masked 635 µs 632 µs −0.4%
pocl/serial/narrow/unmasked 635 µs 629 µs −1.1%
pocl/serial/stream/triad/dram 8.62 ms 8.53 ms −1.1%
Latency
host/import/ka 764 ms 764 ms −0.0%
host/precompile/ka 27.1 s 27.4 s +0.9%
host/startup 130 ms 128 ms −1.1%
pocl/parallel/launch/dynamic-default 165 ns 166 ns +0.6%
pocl/parallel/launch/groupreduce-f64 770 ns 771 ns +0.1%
pocl/parallel/launch/many-args 662 ns 683 ns +3.2%
pocl/parallel/launch/partial-workgroup 234 ns 238 ns +1.7%
pocl/serial/compile/groupreduce 1.51 s 1.48 s −1.9%
pocl/serial/compile/localmem-barrier 674 ms 672 ms −0.2%
pocl/serial/compile/new-kernel 127 ms 127 ms −0.2%
pocl/serial/compile/new-workgroupsize 8.73 ms 8.87 ms +1.6%
pocl/serial/first-kernel/cold-cache 395 ms 399 ms +1.1%
pocl/serial/first-kernel/warm-cache 321 ms 319 ms −0.6%
Applications
pocl/parallel/dgsem/fluxdiff-node 246 µs 249 µs +1.6%
pocl/parallel/dgsem/weak-element 90.7 µs 89.9 µs −0.8%
pocl/parallel/dgsem/weak-node-localmem 93.1 µs 93.5 µs +0.4%
pocl/parallel/stencil/timestep 7.29 ms 7.60 ms +4.3%
pocl/serial/stencil/timestep 10.1 ms 10.5 ms +4.8%

History: https://juliagpu.org/perf/?pkg=kernelabstractions

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants