Python · Runtime Dispatch

Runtime Dispatch

import stringzilla 6.0.0-rc.1 · cb8d956a

Ask your coding agent about this page, or add the docs server to it

curl -s 'https://www.unum.cloud/docs/stringzilla/python/runtime-dispatch.md' | claude -p "How would this fit my code?"

StringZilla detects the running CPU’s SIMD features and routes every kernel to the fastest capability both the CPU and this build have, without recompilation. Capabilities are sz.Capability flags, one member per CPU capability, like sz.Capability.HASWELL or sz.Capability.NEON; GPU bits sit above every member, so a GPU mask prints as a number.

The producers report capabilities for the CPU, or for one GPU by its runtime’s ordinal, or make a stream on that GPU, and are the only calls taking an ordinal.

  • sz.__version__ — the package version string.
  • sz.capabilities_detected_cpu(), sz.capabilities_detected_cuda(ordinal) — what the device can execute, whether or not its kernels were compiled in.
  • sz.capabilities_compiled_cpu(), sz.capabilities_compiled_cuda(ordinal) — what this build holds kernels for that the device runs, whether or not it offers them.
  • sz.capabilities_enabled_cpu(), sz.capabilities_enabled_cuda(ordinal) — what calls dispatch with, both of the above; on the CPU it always has SERIAL.
  • sz.count_devices_cuda() — the number of CUDA devices; it raises RuntimeError when none answers, as when this build lacks its kernels, and so does an ordinal past the last device.
  • sz.configure_thread_cpu(capabilities) — prepare the calling thread for the kernels of capabilities; call it on every thread that runs kernels.
  • sz.stream_init_cuda(ordinal), sz.stream_free_cuda(stream) — make a stream on one device, as the integer every stream= takes, and free it once nothing queued on it remains.
  • sz.synchronize(capabilities, stream=None) — wait for one stream of the vendor capabilities names, or its default one.

ROCm and Metal have the same _rocm and _metal producers. Every function that runs kernels also takes a keyword-only capabilities=, narrowing that one call and leaving every other call alone. The keyword keeps no serial fallback, so a mask with no kernel for the call raises LookupError.

import stringzilla as sz

sz.capabilities_enabled_cpu()                                 # e.g. <Capability.SERIAL|NEON|NEONAES|NEONSHA: 449>
sz.find("haystack", "st", capabilities=sz.Capability.SERIAL)  # one call on the scalar kernel
sz.configure_thread_cpu(sz.capabilities_enabled_cpu())        # on a thread that runs kernels
[sz.capabilities_enabled_cuda(ordinal) for ordinal in range(sz.count_devices_cuda())]  # raises without CUDA

Engines and the stateful objects, like Hasher, Sha256 and the AES keys, dispatch every call with the capabilities they were constructed with.

API

Types

Capability
__init__.pyi:19 ↗
class Capability(enum.IntFlag)

Values

SERIAL
WESTMERE
GOLDMONT
HASWELL
SKYLAKE
ICELAKE
NEON
NEONAES
NEONSHA
SVE
SVE2
SVE2AES
RVV
RVVCRYPTO
V128
V128RELAXED
LOONGSONASX
POWERVSX

Functions

capabilities_detected_cpuGet the capabilities this CPU can execute.
__init__.pyi:1317 ↗
def capabilities_detected_cpu() -> Capability

Detected from CPUID or HWCAP. Says nothing about whether the kernels were compiled in, which capabilities_compiled_cpu reports.

capabilities_compiled_cpuGet the CPU capabilities whose kernels were compiled into this binary.
__init__.pyi:1327 ↗
def capabilities_compiled_cpu() -> Capability

Decided at build time. Independent of the hardware: a binary built with a broken probe toolchain still detects this machine's full set while containing no SIMD kernels at all, which is what makes a scalar build hard to spot.

capabilities_enabled_cpuGet the CPU capabilities kernels run with, unless a call passes capabilities=.
__init__.pyi:1338 ↗
def capabilities_enabled_cpu() -> Capability

It is capabilities_detected_cpu() & capabilities_compiled_cpu(), and always has SERIAL.

configure_thread_cpuPrepare the calling thread for kernels of capabilities.
__init__.pyi:1347 ↗
def configure_thread_cpu(capabilities: Capability, /) -> None

Call it on every thread that runs kernels, before the first of them.

Parameters

capabilitiesCapabilityThe mask kernels on this thread run with.
count_devices_*× 3Count the devices of one GPU vendor this process sees: count_devices_cuda, and the _rocm and _metal ones alike.
__init__.pyi:1359 ↗
def count_devices_cuda() -> int
Symbol
count_devices_cuda
count_devices_rocm
count_devices_metal

Raises

  • RuntimeError: No device of that vendor answers, or this build lacks its kernels.
capabilities_detected_*× 3Get the capabilities a GPU can execute, by its runtime's ordinal, whether or not the kernels were compiled in: capabilities_detected_cuda, and the _rocm and _metal ones alike.
__init__.pyi:1392 ↗
def capabilities_detected_cuda(ordinal: int, /) -> Capability
Symbol
capabilities_detected_cuda
capabilities_detected_rocm
capabilities_detected_metal

Returns

GPU bits sit above every member, so they print as a number.

Raises

  • RuntimeError: No device with that ordinal answers.
capabilities_compiled_*× 3Get the capabilities whose kernels this binary holds code for that a GPU runs, by its runtime's ordinal, whether or not it offers them: capabilities_compiled_cuda, and the _rocm and _metal ones alike.
__init__.pyi:1434 ↗
def capabilities_compiled_cuda(ordinal: int, /) -> Capability
Symbol
capabilities_compiled_cuda
capabilities_compiled_rocm
capabilities_compiled_metal

Raises

  • RuntimeError: No device with that ordinal answers.
capabilities_enabled_*× 3Get the capabilities kernels run with on a GPU, by its runtime's ordinal, both detected and compiled: capabilities_enabled_cuda, and the _rocm and _metal ones alike.
__init__.pyi:1470 ↗
def capabilities_enabled_cuda(ordinal: int, /) -> Capability
Symbol
capabilities_enabled_cuda
capabilities_enabled_rocm
capabilities_enabled_metal

Engines, Strings.copy and synchronize take this mask, which names the vendor, and a stream, which names the device.

Raises

  • RuntimeError: No device with that ordinal answers.
stream_init_*× 3Create a stream on a GPU, by its runtime's ordinal, which names that device to the engines, Strings.copy and synchronize it is passed to: stream_init_cuda, and the _rocm and _metal ones alike.
__init__.pyi:1509 ↗
def stream_init_cuda(ordinal: int, /) -> int
Symbol
stream_init_cuda
stream_init_rocm
stream_init_metal

Free it with the matching *_stream_free once its work is synchronized.

Returns

The cudaStream_t, hipStream_t or id<MTLCommandQueue> handle.

Raises

  • RuntimeError: No device with that ordinal answers, or this build lacks its vendor.
stream_free_*× 3Free a stream stream_init_cuda made, and the _rocm and _metal ones alike, once synchronize joined whatever was queued on it; nothing may use the stream afterwards.
__init__.pyi:1554 ↗
def stream_free_cuda(stream: int, /) -> None
Symbol
stream_free_cuda
stream_free_rocm
stream_free_metal
synchronizeWait for everything enqueued on one stream, releasing the GIL meanwhile.
__init__.pyi:1578 ↗
def synchronize(
    capabilities: Capability,
    stream: int | None = None,
) -> None

A GPU engine's verbs enqueue and return, so their outputs are read, and their inputs freed, only after this returns. Returns at once for CPU capabilities.

Parameters

capabilitiesCapabilityA producer's mask, which names the vendor.
streamint | NoneA stream of that vendor as an integer, or None for the default.

Raises

  • BufferError: The stream belongs to another device.
  • RuntimeError: A launch on the stream failed, or the device does not answer.