JavaScript · Runtime Dispatch

Runtime Dispatch

import * as stringzilla from 'stringzilla';import { Device } from 'stringzilla';import { DeviceConstructor } from 'stringzilla'; 6.0.0-rc.1 · 91a27bdd

Every call runs on the CPU and dispatches to the best kernel of its enabled capabilities, a BigInt bitmask a Device reports:

const cpu = sz.Device.cpu();
console.log(cpu.capabilitiesEnabled()); // what dispatch uses
console.log((cpu.capabilitiesEnabled() & sz.Capability.haswell) !== 0n);

capabilitiesEnabled() is the one you usually want, and derives from two independent axes:

  • capabilitiesDetected(): what this device can execute.
  • capabilitiesCompiled(): what this build contains for devices of its kind, from the ISA probes at build time.
  • capabilitiesEnabled(): what dispatch uses, both axes at once.

capabilitiesDetected() describes the machine and says nothing about whether a kernel was compiled in, so a build whose ISA probes failed still reports your CPU’s full feature set while containing no SIMD kernels at all. The enabled set always keeps the serial fallback.

A Device is the host CPU or one GPU of a runtime, named by that runtime’s own ordinal:

sz.Device.count("cpu");            // 1
const gpu = new sz.Device("cuda"); // throws without a CUDA device, or past the last one
gpu.capabilitiesEnabled();         // what that GPU runs, like `Capability.cuda`
  • Device.cpu() is the host CPU, which every build has.
  • Device.count(kind) counts the devices of "cpu", "cuda", "rocm" or "metal", and throws without a GPU of that kind.
  • new Device(kind, ordinal) throws a RangeError past the last device.

Capability maps each lowercase capability name, like haswell, neon or cuda, to its bit, and the cpus, devices and any groups to theirs. It is built at load from the C library’s own names.

There is no configure_thread call in the JS binding. Thread configuration is managed internally by the native addon.

API

Types

CapabilityNameA capability name, as Capability keys it: one capability, or the cpus, gpus and any groups.
stringzilla.d.ts:11 ↗
type CapabilityName = | "serial" | "westmere" | "goldmont" | "haswell" | "skylake" | "icelake" | "neon" | "neonaes" | "neonsha" | "sve" | "sve2" | "sve2aes" | "rvv" | "rvvcrypto" | "v128" | "v128relaxed" | "loongsonasx" | "powervsx" | "cuda" | "hopper" | "blackwell" | "rocm" | "metal" | "cpus" | "gpus" | "any"
DeviceKindThe runtime a device belongs to.
stringzilla.d.ts:40 ↗
type DeviceKind = "cpu" | "cuda" | "rocm" | "metal"
InterfaceDeviceOne device StringZilla knows: the host CPU, or a GPU by its runtime's own ordinal, the one cudaSetDevice or hipSetDevice takes, or the position in Metal's device list.5
stringzilla.d.ts:145 ↗
interface Device

Every call of this binding runs on the CPU and dispatches over its Device.capabilitiesEnabled, so a GPU device only reports its masks here.

capabilitiesDetectedThe capabilities this device runs, compiled in or not, as a bitmask.
stringzilla.d.ts:153 ↗
capabilitiesDetected(): bigint
capabilitiesCompiledThe capabilities compiled into this build for devices of this kind, as a bitmask.
stringzilla.d.ts:156 ↗
capabilitiesCompiled(): bigint
capabilitiesEnabledThe capabilities this device's calls run: detected and compiled at once, as a bitmask.
stringzilla.d.ts:160 ↗
capabilitiesEnabled(): bigint

On the CPU it always includes Capability.serial.

kindThe runtime the device belongs to.
stringzilla.d.ts:147 ↗
readonly kind: DeviceKind
ordinalThe runtime's own index of the device.
stringzilla.d.ts:150 ↗
readonly ordinal: number
InterfaceDeviceConstructorConstructs a Device and counts the devices of a kind.3
stringzilla.d.ts:164 ↗
interface DeviceConstructor
DeviceConstructor
stringzilla.d.ts:171 ↗
new DeviceConstructor(kind: DeviceKind, ordinal?: number): Device

Parameters

kindDeviceKindThe runtime the device belongs to
ordinalnumberThe runtime's own index of the device, below DeviceConstructor.count

Throws

Without a device of kind, or past the last one

cpuThe host CPU, which every build has.
stringzilla.d.ts:174 ↗
cpu(): Device
countHow many devices of kind the process sees: one CPU, or the GPUs its runtime counts.
stringzilla.d.ts:180 ↗
count(kind: DeviceKind): number

Throws

Without a GPU of kind