Skip to content

KernelInterface: dynamic local memory - #628

Draft
vchuravy wants to merge 2 commits into
mainfrom
vc/ki-dynamic-localmem
Draft

vchuravy wants to merge 2 commits into
mainfrom
vc/ki-dynamic-localmem

Conversation

@vchuravy

Copy link
Copy Markdown
Member

Implements KernelInterface's dynamic local memory for OpenCLBackend. The KernelInterface side is the ccr-c54f0813-yayzye branch of JuliaGPU/KernelAbstractions.jl; the CUDA counterpart is JuliaGPU/CUDA.jl#3620.

Changes

  • Device: OpenCL.dynamic_localmemory() is an intrinsic that becomes a local memory kernel argument through a second add_input_arguments! call, so it is always the last argument, after the RNG's. Only kernels that use the block get it; a kernel that only reads the size gets none.
  • Requested size: a new KernelState.dynamic_localmemory_size::UInt32 field in the existing padding after the seed. localmem is therefore limited to typemax(UInt32), checked with an ArgumentError.
  • Launch: HostKernel calls take localmem::Int, which KI.launch forwards. The allocation is localmem + 16 bytes, and 16 bytes without localmem: clSetKernelArg rejects an empty local argument, and OpenCL doesn't specify the alignment of a local pointer argument, so the kernel rounds the pointer up to 16 bytes itself. The kernel sees exactly the requested size.
  • Limit: KI.max_dynamic_localmemory is CL_DEVICE_LOCAL_MEM_SIZE, less CL_KERNEL_LOCAL_MEM_SIZE, the 16 bytes of padding and, for kernels using the RNG, its worst-case local memory. It is computed at link time, before any local argument is set, because drivers count those in CL_KERNEL_LOCAL_MEM_SIZE afterwards.
  • @opencl does not accept localmem; only calling the kernel object does.

One change beyond the feature

The two RNG local memory arguments were allocated with LocalMem(UInt32, sizeof(UInt32) * num_slots), i.e. 16 bytes per slot, while the device code uses one UInt32 per slot. They are now 4 bytes per slot. With the old size, the worst-case RNG deduction on Intel's CPU runtime (32 × 8192 bytes) equalled the device's whole local memory, so every kernel using rand got a limit of 0. It is a separate hunk in lib/cl/kernel.jl and can be split out.

Testing

Julia 1.13, with KernelAbstractions and KernelInterface from the branch:

Run Result
Full suite on PoCL, SPIR-V and OpenCL C paths 53682 pass, 9 broken, 0 fail
kernelinterface on Intel's CPU runtime 8869 pass
kernelinterface, execution, device/random on NVIDIA (OpenCL C path) 531 pass
  • The 9 broken were not compared against main.
  • On NVIDIA the KernelInterface suite runs far fewer tests (242 in "Interface" against 8753 on PoCL); whether its "Dynamic local memory" set is among them was not checked. The backend-specific test set added here did run there.
  • Not run: other Julia versions, the pocl_next driver, the khronos translator, and the buffer/svm memory back-ends.

Before merging

  • The last commit is temporary: it points [sources] at the KernelAbstractions branch, and makes CI add both packages from it on Julia < 1.11, which ignores [sources]. The CI edits have not been executed.
  • KernelInterface has no version bump for this feature yet, so the compat bound here needs raising once it is released.

🤖 Generated with Claude Code

Support the local memory whose size is chosen at launch: the `localmem`
launch keyword, `KI.dynamic_localmemory` and
`KI.dynamic_localmemory_size` on the device, and
`KI.max_dynamic_localmemory`.

A kernel that uses the block gets a local memory argument for it, added
after those of the RNG so that it is always the last one. Every launch
of such a kernel sizes the argument, also launches that ask for none,
and the requested size travels in the kernel state for the bounds
checks. OpenCL neither allows empty local memory arguments nor specifies
their alignment, so launches allocate 16 bytes more than they were asked
for, and the kernel starts the block at the first address that is
aligned to 16 bytes. Kernels still see the size they asked for.

The limit is the local memory of the device, less those 16 bytes, the
static local memory that the driver reports for the kernel, and what
the RNG of the kernel takes for the largest work-group. It is determined
when the kernel is linked, because the driver counts the local memory
arguments once they are set.

The two local memory arguments of the RNG were four times as large as
the `UInt32` per slot that the kernel uses, which would have left
kernels that use `rand` without any dynamic local memory on Intel's CPU
runtime. Allocate what is used.

Assisted-by: Claude Code (Fable 5.2)
…cal memory branch

The dynamic local memory of KernelInterface isn't released yet, so get
both packages from the branch that has it. Julia 1.10 doesn't read
`[sources]`, so CI adds them there explicitly.

Assisted-by: Claude Code (Fable 5.2)

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant