Swift · Runtime Dispatch

Runtime Dispatch

import StringZilla 6.0.0-rc.1 · 91a27bdd

Every call runs on the CPU and dispatches to the best kernel of Device.cpu.capabilitiesEnabled, a Capabilities OptionSet:

import StringZilla

// `enabled` is what dispatch uses: detected on this CPU AND compiled into the binary.
let cpu = Device.cpu
print(try cpu.capabilitiesEnabled)                 // like "serial,neon,neonaes,neonsha"
print(try cpu.capabilitiesEnabled.contains(.neon)) // `Capabilities` is an `OptionSet`

// The two raw axes, when you specifically mean one of them:
let onThisCpu = try cpu.capabilitiesDetected
let inThisBinary = cpu.capabilitiesCompiled

// Prepare each thread that runs kernels:
try cpu.configureThread(cpu.capabilitiesEnabled)
  • capabilitiesDetected is what the device can execute, from CPUID or HWCAP on the CPU.
  • capabilitiesCompiled is what this binary contains for devices of its kind, from the ISA probes at build time.
  • capabilitiesEnabled is what dispatch uses, both axes at once, and on the CPU always contains .serial.
  • configureThread(_:) prepares the calling thread for the kernels of its argument, once per thread that runs them.

Reach for capabilitiesEnabled unless you specifically mean one of the raw axes. 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.

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

let gpus = try Device.count(.metal)            // throws without a Metal device
let gpu = try Device(kind: .metal, ordinal: 0) // throws past the last one
print(try gpu.capabilitiesEnabled)             // like "metal"

configureThread(_:) throws a StringZilla.Error on a GPU, which keeps no thread state of its own: this package only reports GPU capabilities. .cpus, .gpus and .any group the CPU capabilities, the GPU ones, and all of them.

API

StructCapabilitiesA set of capabilities of a CPU or a GPU, as a Device reports them.29
StringProtocol+StringZilla.swift:867 ↗
struct Capabilities

Conforms to

OptionSet, Sendable, CustomStringConvertible, SetAlgebra, RawRepresentable, Equatable, ExpressibleByArrayLiteral
rawValue
StringProtocol+StringZilla.swift:868 ↗
let rawValue: UInt64
init
StringProtocol+StringZilla.swift:869 ↗
init(rawValue: UInt64)
serial
StringProtocol+StringZilla.swift:874 ↗
static let serial: Capabilities
westmere
StringProtocol+StringZilla.swift:875 ↗
static let westmere: Capabilities
goldmont
StringProtocol+StringZilla.swift:876 ↗
static let goldmont: Capabilities
haswell
StringProtocol+StringZilla.swift:877 ↗
static let haswell: Capabilities
skylake
StringProtocol+StringZilla.swift:878 ↗
static let skylake: Capabilities
iceLake
StringProtocol+StringZilla.swift:879 ↗
static let iceLake: Capabilities
neon
StringProtocol+StringZilla.swift:880 ↗
static let neon: Capabilities
neonAES
StringProtocol+StringZilla.swift:881 ↗
static let neonAES: Capabilities
neonSHA
StringProtocol+StringZilla.swift:882 ↗
static let neonSHA: Capabilities
sve
StringProtocol+StringZilla.swift:883 ↗
static let sve: Capabilities
sve2
StringProtocol+StringZilla.swift:884 ↗
static let sve2: Capabilities
sve2AES
StringProtocol+StringZilla.swift:885 ↗
static let sve2AES: Capabilities
rvv
StringProtocol+StringZilla.swift:886 ↗
static let rvv: Capabilities
rvvCrypto
StringProtocol+StringZilla.swift:887 ↗
static let rvvCrypto: Capabilities
v128
StringProtocol+StringZilla.swift:888 ↗
static let v128: Capabilities
v128Relaxed
StringProtocol+StringZilla.swift:889 ↗
static let v128Relaxed: Capabilities
loongsonASX
StringProtocol+StringZilla.swift:890 ↗
static let loongsonASX: Capabilities
powerVSX
StringProtocol+StringZilla.swift:891 ↗
static let powerVSX: Capabilities
cuda
StringProtocol+StringZilla.swift:893 ↗
static let cuda: Capabilities
hopper
StringProtocol+StringZilla.swift:894 ↗
static let hopper: Capabilities
blackwell
StringProtocol+StringZilla.swift:895 ↗
static let blackwell: Capabilities
rocm
StringProtocol+StringZilla.swift:896 ↗
static let rocm: Capabilities
metal
StringProtocol+StringZilla.swift:897 ↗
static let metal: Capabilities
cpusEvery CPU capability, the bits below the first GPU vendor's.
StringProtocol+StringZilla.swift:900 ↗
static let cpus: Capabilities
gpusEvery GPU capability.
StringProtocol+StringZilla.swift:902 ↗
static let gpus: Capabilities
anyEvery capability.
StringProtocol+StringZilla.swift:904 ↗
static let any: Capabilities
descriptionThe capability names, comma-separated, like "serial,neon".
StringProtocol+StringZilla.swift:907 ↗
var description: String { get }
EnumDeviceKindWhich runtime a device belongs to, as the sz_<kind>_* C functions name it.
StringProtocol+StringZilla.swift:917 ↗
enum DeviceKind

Values

cpu
cuda
rocm
metal

Conforms to

Equatable, Hashable, Sendable
StructDeviceOne 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.9
StringProtocol+StringZilla.swift:969 ↗
struct Device

Every call of this module runs on the CPU and dispatches over its capabilitiesEnabled, so a GPU device only reports its capabilities here. Prefer capabilitiesEnabled unless you specifically mean one of the raw axes: capabilitiesDetected describes the device and says nothing about whether a kernel was compiled into this binary.

Conforms to

Sendable, Equatable
kind
StringProtocol+StringZilla.swift:970 ↗
let kind: DeviceKind
ordinal
StringProtocol+StringZilla.swift:971 ↗
let ordinal: Int
cpuThe host CPU, which every build has.
StringProtocol+StringZilla.swift:974 ↗
static let cpu: Device
initDevice ordinal of kind.
StringProtocol+StringZilla.swift:983 ↗
init(kind: DeviceKind, ordinal: Int) throws

Throws

Error past the last device of kind.

countHow many devices of kind the process sees: one CPU, or the GPUs its runtime counts.
StringProtocol+StringZilla.swift:992 ↗
static func count(_ kind: DeviceKind) throws -> Int

Throws

Error without a GPU of kind.

capabilitiesDetectedWhat this device runs, whether or not this binary holds kernels for it.
StringProtocol+StringZilla.swift:1004 ↗
var capabilitiesDetected: Capabilities { get throws }
capabilitiesCompiledWhat this binary holds kernels for on devices of this kind, whether or not this one runs.
StringProtocol+StringZilla.swift:1019 ↗
var capabilitiesCompiled: Capabilities { get }
capabilitiesEnabledWhat this device's calls run: capabilitiesDetected and capabilitiesCompiled at once.
StringProtocol+StringZilla.swift:1032 ↗
var capabilitiesEnabled: Capabilities { get throws }

On the CPU it always contains serial.

configureThreadConfigures the current thread for capabilities, usually capabilitiesEnabled.
StringProtocol+StringZilla.swift:1049 ↗
func configureThread(_ capabilities: Capabilities) throws

Call it once on every thread that runs kernels.

Throws

Error on a GPU, which has no thread state to configure.