# Runtime Dispatch

> StringZilla for Swift

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

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

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

`import StringZilla`

## 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
```

## westmere

```swift
static let westmere: Capabilities
```

## goldmont

```swift
static let goldmont: Capabilities
```

## haswell

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

## skylake

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

## iceLake

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

## neon

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

## neonAES

```swift
static let neonAES: Capabilities
```

## neonSHA

```swift
static let neonSHA: Capabilities
```

## sve

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

## sve2

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

## sve2AES

```swift
static let sve2AES: Capabilities
```

## rvv

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

## rvvCrypto

```swift
static let rvvCrypto: Capabilities
```

## v128

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

## v128Relaxed

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

## loongsonASX

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

## powerVSX

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

## cuda

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

## hopper

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

## blackwell

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

## rocm

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

## metal

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

## cpus

Every CPU capability, the bits below the first GPU vendor's.

```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,neon".

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

## DeviceKind

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

```swift
enum DeviceKind
```

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

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

## Device

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

```swift
struct Device
```

Every call of this module runs on the CPU and dispatches over its [`capabilitiesEnabled`](#capabilitiesEnabled), so a
GPU device only reports its capabilities here. 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.

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/stringzilla/swift/types.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/stringzilla/swift/types.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 calls run: [`capabilitiesDetected`](#capabilitiesDetected) and [`capabilitiesCompiled`](#capabilitiesCompiled) at once.

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

On the CPU it always contains [`serial`](#serial).

## configureThread

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

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

Call it
once on every thread that runs kernels.

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

