Swift · Runtime Dispatch

Runtime Dispatch

import NumKong 8.0.0-rc.1 · b1f866ab

Capability detection is exposed directly for diagnostics and tests:

import NumKong

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

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

// Set up each thread that runs kernels:
try Device.cpu.configureThread(enabled)

// GPUs are devices too, by their runtime's own ordinal:
for ordinal in 0..<((try? Device.count(.metal)) ?? 0) {
    print(try Device(kind: .metal, ordinal: ordinal).capabilitiesEnabled) // like "metal,apple9"
}
  • Device.cpu is the host CPU, and Device(kind:ordinal:) any device Device.count(_:) counts, throwing an Error past the last one.
  • 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 runs within, both axes at once, which the library settles for the CPU as it loads, and on the CPU always contains .serial. Every CPU kernel call passes Capabilities.cpus, which the library clamps to it, so the module keeps no mask of its own.
  • configureThread(_:) sets per-thread state such as Intel AMX tiles for the given CPU capabilities, usually the enabled ones.

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.

You usually do not need to branch on this in application code. The native layer still selects the best enabled kernel automatically.

API

StructCapabilitiesA set of capabilities of a CPU or a GPU, as a Device reports them.55
NumKong.swift:234 ↗
struct Capabilities

Conforms to

OptionSet, Sendable, CustomStringConvertible, SetAlgebra, RawRepresentable, Equatable, ExpressibleByArrayLiteral
rawValue
NumKong.swift:235 ↗
let rawValue: UInt64
init
NumKong.swift:236 ↗
init(rawValue: UInt64)
serial
NumKong.swift:241 ↗
static let serial: Capabilities
neon
NumKong.swift:242 ↗
static let neon: Capabilities
neonHalf
NumKong.swift:243 ↗
static let neonHalf: Capabilities
neonBFDot
NumKong.swift:244 ↗
static let neonBFDot: Capabilities
neonFHM
NumKong.swift:245 ↗
static let neonFHM: Capabilities
neonSDot
NumKong.swift:246 ↗
static let neonSDot: Capabilities
neonFP8
NumKong.swift:247 ↗
static let neonFP8: Capabilities
sve
NumKong.swift:248 ↗
static let sve: Capabilities
sveHalf
NumKong.swift:249 ↗
static let sveHalf: Capabilities
sveSDot
NumKong.swift:250 ↗
static let sveSDot: Capabilities
sveBFDot
NumKong.swift:251 ↗
static let sveBFDot: Capabilities
sve2
NumKong.swift:252 ↗
static let sve2: Capabilities
sme
NumKong.swift:253 ↗
static let sme: Capabilities
smeF64
NumKong.swift:254 ↗
static let smeF64: Capabilities
smeBI32
NumKong.swift:255 ↗
static let smeBI32: Capabilities
haswell
NumKong.swift:256 ↗
static let haswell: Capabilities
alder
NumKong.swift:257 ↗
static let alder: Capabilities
sierra
NumKong.swift:258 ↗
static let sierra: Capabilities
skylake
NumKong.swift:259 ↗
static let skylake: Capabilities
iceLake
NumKong.swift:260 ↗
static let iceLake: Capabilities
genoa
NumKong.swift:261 ↗
static let genoa: Capabilities
turin
NumKong.swift:262 ↗
static let turin: Capabilities
sapphire
NumKong.swift:263 ↗
static let sapphire: Capabilities
diamond
NumKong.swift:264 ↗
static let diamond: Capabilities
sapphireAMX
NumKong.swift:265 ↗
static let sapphireAMX: Capabilities
graniteAMX
NumKong.swift:266 ↗
static let graniteAMX: Capabilities
diamondAMX
NumKong.swift:267 ↗
static let diamondAMX: Capabilities
rvv
NumKong.swift:268 ↗
static let rvv: Capabilities
rvvBF16
NumKong.swift:269 ↗
static let rvvBF16: Capabilities
rvvHalf
NumKong.swift:270 ↗
static let rvvHalf: Capabilities
rvvBB
NumKong.swift:271 ↗
static let rvvBB: Capabilities
v128
NumKong.swift:272 ↗
static let v128: Capabilities
v128Relaxed
NumKong.swift:273 ↗
static let v128Relaxed: Capabilities
powerVSX
NumKong.swift:274 ↗
static let powerVSX: Capabilities
loongsonASX
NumKong.swift:275 ↗
static let loongsonASX: Capabilities
cuda
NumKong.swift:277 ↗
static let cuda: Capabilities
ampere
NumKong.swift:278 ↗
static let ampere: Capabilities
ada
NumKong.swift:279 ↗
static let ada: Capabilities
hopper
NumKong.swift:280 ↗
static let hopper: Capabilities
blackwell
NumKong.swift:281 ↗
static let blackwell: Capabilities
blackwellRTX
NumKong.swift:282 ↗
static let blackwellRTX: Capabilities
blackwellUltra
NumKong.swift:283 ↗
static let blackwellUltra: Capabilities
rocm
NumKong.swift:284 ↗
static let rocm: Capabilities
cdna3
NumKong.swift:285 ↗
static let cdna3: Capabilities
cdna4
NumKong.swift:286 ↗
static let cdna4: Capabilities
cdna5
NumKong.swift:287 ↗
static let cdna5: Capabilities
metal
NumKong.swift:288 ↗
static let metal: Capabilities
apple9
NumKong.swift:289 ↗
static let apple9: Capabilities
apple10
NumKong.swift:290 ↗
static let apple10: Capabilities
cpusEvery CPU capability, the bits below the first GPU vendor's, which every CPU kernel call passes, as the library clamps it to capabilitiesEnabled.
NumKong.swift:294 ↗
static let cpus: Capabilities
gpusEvery GPU capability.
NumKong.swift:296 ↗
static let gpus: Capabilities
anyEvery capability.
NumKong.swift:301 ↗
static let any: Capabilities
descriptionThe capability names, comma-separated, like "serial,haswell".
NumKong.swift:304 ↗
var description: String { get }
EnumDeviceKindWhich runtime a device belongs to, as the nk_<kind>_* C functions name it.
NumKong.swift:314 ↗
enum DeviceKind

Values

cpu
cuda
rocm
metal

Conforms to

Equatable, Hashable, Sendable
StructDeviceOne device NumKong can run kernels on: 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
NumKong.swift:357 ↗
struct Device

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, so selecting on it alone claims support for code that may not exist.

Conforms to

Sendable, Equatable
kind
NumKong.swift:358 ↗
let kind: DeviceKind
ordinal
NumKong.swift:359 ↗
let ordinal: Int
cpuThe host CPU, which every build has.
NumKong.swift:362 ↗
static let cpu: Device
initDevice ordinal of kind.
NumKong.swift:371 ↗
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.
NumKong.swift:380 ↗
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.
NumKong.swift:392 ↗
var capabilitiesDetected: Capabilities { get throws }
capabilitiesCompiledWhat this binary holds kernels for on devices of this kind, whether or not this one runs.
NumKong.swift:407 ↗
var capabilitiesCompiled: Capabilities { get }
capabilitiesEnabledWhat this device's kernel calls run within: capabilitiesDetected and capabilitiesCompiled at once.
NumKong.swift:421 ↗
var capabilitiesEnabled: Capabilities { get throws }

On the CPU the library settles it as it loads, and it always contains serial.

configureThreadConfigures the current thread for capabilities, usually capabilitiesEnabled, e.g.
NumKong.swift:438 ↗
func configureThread(_ capabilities: Capabilities) throws

AMX tile state on x86. Must be called once per thread before using AMX operations.

Throws

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