# Runtime Dispatch

> NumKong for Swift

Capability detection is exposed directly for diagnostics and tests:

```swift
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.

`import NumKong`

## Capabilities

A set of capabilities of a CPU or a GPU, as a [`Device`](#Device) reports them.

```swift
struct Capabilities
```

Conforms to: `OptionSet`, `Sendable`, `CustomStringConvertible`, `SetAlgebra`, `RawRepresentable`, `Equatable`, `ExpressibleByArrayLiteral`

## rawValue

```swift
let rawValue: UInt64
```

## init

```swift
init(rawValue: UInt64)
```

## serial

```swift
static let serial: Capabilities
```

## neon

```swift
static let neon: Capabilities
```

## neonHalf

```swift
static let neonHalf: Capabilities
```

## neonBFDot

```swift
static let neonBFDot: Capabilities
```

## neonFHM

```swift
static let neonFHM: Capabilities
```

## neonSDot

```swift
static let neonSDot: Capabilities
```

## neonFP8

```swift
static let neonFP8: Capabilities
```

## sve

```swift
static let sve: Capabilities
```

## sveHalf

```swift
static let sveHalf: Capabilities
```

## sveSDot

```swift
static let sveSDot: Capabilities
```

## sveBFDot

```swift
static let sveBFDot: Capabilities
```

## sve2

```swift
static let sve2: Capabilities
```

## sme

```swift
static let sme: Capabilities
```

## smeF64

```swift
static let smeF64: Capabilities
```

## smeBI32

```swift
static let smeBI32: Capabilities
```

## haswell

```swift
static let haswell: Capabilities
```

## alder

```swift
static let alder: Capabilities
```

## sierra

```swift
static let sierra: Capabilities
```

## skylake

```swift
static let skylake: Capabilities
```

## iceLake

```swift
static let iceLake: Capabilities
```

## genoa

```swift
static let genoa: Capabilities
```

## turin

```swift
static let turin: Capabilities
```

## sapphire

```swift
static let sapphire: Capabilities
```

## diamond

```swift
static let diamond: Capabilities
```

## sapphireAMX

```swift
static let sapphireAMX: Capabilities
```

## graniteAMX

```swift
static let graniteAMX: Capabilities
```

## diamondAMX

```swift
static let diamondAMX: Capabilities
```

## rvv

```swift
static let rvv: Capabilities
```

## rvvBF16

```swift
static let rvvBF16: Capabilities
```

## rvvHalf

```swift
static let rvvHalf: Capabilities
```

## rvvBB

```swift
static let rvvBB: Capabilities
```

## v128

```swift
static let v128: Capabilities
```

## v128Relaxed

```swift
static let v128Relaxed: Capabilities
```

## powerVSX

```swift
static let powerVSX: Capabilities
```

## loongsonASX

```swift
static let loongsonASX: Capabilities
```

## cuda

```swift
static let cuda: Capabilities
```

## ampere

```swift
static let ampere: Capabilities
```

## ada

```swift
static let ada: Capabilities
```

## hopper

```swift
static let hopper: Capabilities
```

## blackwell

```swift
static let blackwell: Capabilities
```

## blackwellRTX

```swift
static let blackwellRTX: Capabilities
```

## blackwellUltra

```swift
static let blackwellUltra: Capabilities
```

## rocm

```swift
static let rocm: Capabilities
```

## cdna3

```swift
static let cdna3: Capabilities
```

## cdna4

```swift
static let cdna4: Capabilities
```

## cdna5

```swift
static let cdna5: Capabilities
```

## metal

```swift
static let metal: Capabilities
```

## apple9

```swift
static let apple9: Capabilities
```

## apple10

```swift
static let apple10: Capabilities
```

## cpus

Every CPU capability, the bits below the first GPU vendor's, which every CPU kernel call passes, as the library clamps it to [`capabilitiesEnabled`](#capabilitiesEnabled).

```swift
static let cpus: Capabilities
```

## gpus

Every GPU capability.

```swift
static let gpus: Capabilities
```

## any

Every capability.

```swift
static let any: Capabilities
```

## description

The capability names, comma-separated, like "serial,haswell".

```swift
var description: String { get }
```

## DeviceKind

Which runtime a device belongs to, as the `nk_<kind>_*` C functions name it.

```swift
enum DeviceKind
```

- `cpu`
- `cuda`
- `rocm`
- `metal`

Conforms to: `Equatable`, `Hashable`, `Sendable`

## Device

One 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.

```swift
struct Device
```

Prefer [`capabilitiesEnabled`](#capabilitiesEnabled) unless you specifically mean one of the raw axes:
[`capabilitiesDetected`](#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

```swift
let kind: DeviceKind
```

## ordinal

```swift
let ordinal: Int
```

## cpu

The host CPU, which every build has.

```swift
static let cpu: Device
```

## init

Device [`ordinal`](#ordinal) of [`kind`](#kind).

```swift
init(kind: DeviceKind, ordinal: Int) throws
```

Throws: [`Error`](https://www.unum.cloud/docs/numkong/swift/quickstart.md#Error) past the last device of [`kind`](#kind).

## count

How many devices of [`kind`](#kind) the process sees: one CPU, or the GPUs its runtime counts.

```swift
static func count(_ kind: DeviceKind) throws -> Int
```

Throws: [`Error`](https://www.unum.cloud/docs/numkong/swift/quickstart.md#Error) without a GPU of [`kind`](#kind).

## capabilitiesDetected

What this device runs, whether or not this binary holds kernels for it.

```swift
var capabilitiesDetected: Capabilities { get throws }
```

## capabilitiesCompiled

What this binary holds kernels for on devices of this kind, whether or not this one runs.

```swift
var capabilitiesCompiled: Capabilities { get }
```

## capabilitiesEnabled

What this device's kernel calls run within: [`capabilitiesDetected`](#capabilitiesDetected) and [`capabilitiesCompiled`](#capabilitiesCompiled) at once.

```swift
var capabilitiesEnabled: Capabilities { get throws }
```

On the CPU the library settles it as it loads, and it always
contains [`serial`](#serial).

## configureThread

Configures the current thread for `capabilities`, usually [`capabilitiesEnabled`](#capabilitiesEnabled), e.g.

```swift
func configureThread(_ capabilities: Capabilities) throws
```

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

Throws: [`Error`](https://www.unum.cloud/docs/numkong/swift/quickstart.md#Error) on a GPU, which has no thread state to configure.

