Skip to content

Look up kernel metadata by dispatch on the kernel - #236

Merged
omlins merged 3 commits into
omlins:mainfrom
albert-de-montserrat:adm/fix234
Oct 30, 2026
Merged

omlins merged 3 commits into
omlins:mainfrom
albert-de-montserrat:adm/fix234

Conversation

@albert-de-montserrat

Copy link
Copy Markdown
Contributor

Since v0.16.1, @parallel <kernelcall> reads the kernel's metadata (nb_parallel_indices, memopt) at launch. The name of the metadata function was derived from the callee expression, so the lookup only works when the kernel is called by its own name. Calling a kernel through a struct field or a function argument fails:

struct Foo{F}; f::F; end
bar(s::Foo, A) = @parallel s.f(A)  # FieldError: type Foo has no field `##META, ParallelStencil#…, f`
g(f, A) = @parallel f(A)           # UndefVarError: `##META, ParallelStencil#…, f` not defined

Change

The metadata is now stored as an additional method of the kernel function itself,

@inline kernel(::ParallelStencil.KernelMetadata, args...) = <metadata module>

and the call site emits callee(ParallelStencil.KernelMetadata(), args...). Any callee expression that evaluates to the kernel (f, Mod.f, s.f, a function argument) thus finds the metadata by dispatch, in whatever scope the kernel is defined (including let blocks and function bodies). This also covers module-qualified calls (#224) and replaces the special case added for them in #225. The lookup still returns a constant module, so with const metadata memopt and nb_parallel_indices remain constant-folded.

The metadata method is defined after the kernel, as KernelAbstractions' @kernel defines the kernel constructors only if the kernel name is not yet bound.

test/runtests.jl: the stderr filter for metadata method-overwrite warnings is updated to the new method signature.

Tests

  • New testset in test/test_parallel.jl ("6. metadata"): @metadata and @parallel called through a NamedTuple field and through a function argument.
  • The MWE from Issue with functions inside structures #234 (with the backend swapped to Threads, and to Metal with Float32) errors on main and gives the correct result with this change.
  • Pkg.test() passes locally (Julia 1.13, macOS aarch64) with Threads, Polyester, KernelAbstractions (CPU) and Metal. CUDA and AMDGPU were not tested locally.

Fixes #234

🤖 Generated with Claude Code

`@parallel <kernelcall>` reads the kernel's metadata (nb_parallel_indices,
memopt) at launch. The metadata function had a mangled name derived from
the callee expression, so it was only found when the kernel was called by
its own name: calls through a struct field (`@parallel s.f(A)`) or a
function argument (`g(f, A) = @parallel f(A)`) failed with a FieldError or
UndefVarError.

The metadata is now an additional method of the kernel function itself,
`kernel(::ParallelStencil.KernelMetadata, args...)`, so any callee
expression evaluating to the kernel finds it by dispatch, in whatever scope
the kernel is defined. The method is defined after the kernel, as
KernelAbstractions defines its kernel constructors only if the kernel name
is not yet bound.

Fixes omlins#234

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Oct 22, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.00%. Comparing base (6cf80ee) to head (532d2e9).
⚠️ Report is 8 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

☔ 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.

@albert-de-montserrat
albert-de-montserrat marked this pull request as ready for review October 22, 2026 14:27
@omlins

omlins commented Oct 26, 2026

Copy link
Copy Markdown
Owner

@luraess Why does it not trigger now the GPU CI?

@luraess

luraess commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

@luraess Why does it not trigger now the GPU CI?

Ah, I forgot, CSCS CI cannot run from forks. So to have it running one would need to rebase on a branch from PS's main.

@omlins

omlins commented Oct 26, 2026

Copy link
Copy Markdown
Owner

@albert-de-montserrat : thanks for the PR! There are unit tests that are looking for the old metadata functions (e.g. with ###META.) - this needs to be adapted in order to verify that the new metadata methods were created instead

@omlins
omlins self-requested a review October 27, 2026 06:47

@omlins omlins left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are unit tests that are looking for the old metadata functions (e.g. with ###META.) - this needs to be adapted in order to verify that the new metadata methods were created instead

@albert-de-montserrat

Copy link
Copy Markdown
Contributor Author

Unit tests are passing now :)

@omlins
omlins merged commit 7cd56cb into omlins:main Oct 30, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue with functions inside structures

4 participants