Device
On this page

Device

Interface in Runtime

The owner of a host's kernel calls: the tier they run on, the Buffers they take and the pool those arrays return to, the device's limits and lost signal, the plans of fuse(), and dispose().

import type { Device } from 'typeshade'
import type { Device } from 'typeshade/runtime'

The signature, the description and the examples come from the compiler's own source at commit af54ddad5.

Syntax

interface Device {
readonly info: DeviceInfo;
readonly tier: Tier;
readonly fp64Flavor: Fp64Flavor;
readonly skipped: readonly { readonly tier: Tier; readonly reason: string }[];
readonly limits: Readonly<Record<string, number>>;
readonly lost: Promise<{ readonly reason: string; readonly message: string }>;
array<T extends ScalarArray>(value: T): Buffer<ElementOfTyped<T>>;
array(value: readonly number[]): Buffer<number>;
array<E extends ZeroedElement>(length: number, element: E): Buffer<ZeroedBufferElement<E>>;
texture(options: { readonly size: readonly [number, number]; readonly format: string; readonly storage?: boolean; }): Texture;
release(array: AnyBuffer): void;
readonly statistics: DeviceStatistics;
fuse( body: (scope: FuseScope) => void, options?: { readonly chunk?: { readonly key: string; readonly units: number } }, ): Promise<FuseReport>;
readonly timeBudgetMs: number;
chunkSize(key: string, max: number, min?: number): number;
readonly chunkRates: Readonly<Record<string, ChunkRate>>;
wait(): Promise<void>;
dispose(): Promise<void>;
use(): DeviceUse;
}

Description

Made by createDevice. Two devices in one context are independent: each has its tier, its arrays, its pool and its plans. Ordinary kernel, compute-entry and draw calls infer this owner from its arrays and textures. Mixing owners is refused before execution. A kernel or entry call without owned resources uses the device in use (use(), change 0138, R3), else the default owner; a draw, the default owner.

Instance properties

inforead only DeviceInfo

Frozen historical metadata of the opened device (change 0083), readable after loss or disposal. It does not identify the effective tier of each call.

tierread only Tier

The tier that opened: 'webgpu', 'webgl2' or 'cpu'.

fp64Flavorread only Fp64Flavor

The resolved flavor of the emulated double. Automatic selection is resolved when this device opens; this property is always 'float' or 'integer' (change 0085).

skippedread only readonly { readonly tier: Tier; readonly reason: string }[]

Each tier of prefer before the one that opened, with the reason it did not open.

limitsread only Readonly<Record<string, number>>

The device’s WebGPU limits, by their names in GPUSupportedLimits, frozen (change 0051). Empty on a tier that has none.

lostread only Promise<{ readonly reason: string; readonly message: string }>

Resolves when the browser invalidates the WebGPU device. On another tier it never resolves.

statisticsread only DeviceStatistics

What the device counts of its arrays.

timeBudgetMsread only number

The time one chunk of a long kernel may take, in milliseconds: createDevice({ timeBudgetMs })’s value, 250 when none was given (change 0074).

chunkRatesread only Readonly<Record<string, ChunkRate>>

What the device measured of each chunk key a chunked fuse() named, by key (change 0074). A frozen copy, empty before the first chunk.

Instance methods

array <T extends ScalarArray>(value: T): Buffer<ElementOfTyped<T>>

A Buffer of scalars that holds a copy of the typed array value, as array(value) makes one (Rule 11.8; change 0138, Amendment A10, Q-I), owned by this device: taken from the pool when a released array of the same element and length is there, and made new otherwise.

array (value: readonly number[]): Buffer<number>

A Buffer<f32> of the numbers of value, as array(value) makes one: a number[] is held as a Float32Array (change 0138; the owner’s decision of 2026-11-11).

array <E extends ZeroedElement>(length: number, element: E): Buffer<ZeroedBufferElement<E>>

A zeroed Buffer of length numbers of element, as array(length, element) makes one (change 0065), owned by this device: taken from the pool and zeroed on the device when a released array of the same element and length is there, and made new otherwise.

texture (options: { readonly size: readonly [number, number]; readonly format: string; readonly storage?: boolean; }): Texture

A 2D texture of size and format, owned by this device (change 0077): a sampled texture, a copy source and destination and a render target, as a program runtime’s texture is, and a storage texture a kernel function writes when storage is true. A call takes it as a texture parameter, and a call of another owner refuses it. dispose() destroys it. On a device with no WebGPU device it is a texture of the CPU tier’s memory, of a format that tier holds.

release (array: AnyBuffer): void

Give an array back. The device keeps it for a later array() of the same element and length while the pool has room, and frees it otherwise. The handle is not used after.

fuse (body: (scope: FuseScope) => void, options?: { readonly chunk?: { readonly key: string; readonly units: number } }): Promise<FuseReport>

Record the kernel calls and entry calls body makes and run them as one plan on this device (Rule 11.13). A call inside the body that takes a Buffer of another owner is refused with a TypeError naming both owners. No registration import is needed (change 0085). The body runs synchronously; the queued plan automatically loads its local executor. Deploy every emitted chunk. A loading failure reaches each recorded call and written Buffer. A failed content dependency rejects the whole plan with the same error (0129). An earlier write-only call in the plan can recover a handle before a later call reads it.

With { chunk: { key, units } } the plan is one chunk of a long kernel (change 0074): key names the kernel loop and units is the work the chunk holds. The plan runs alone, at the place fuse was called. The device first waits for the work submitted before, as wait() does, and starts a wall clock (performance.now()). It runs the plan, fences it as wait() does and stops the clock. It learns the rate of key (chunkRates), and the report carries the chunk row. The promise resolves after the fence. A plan with no chunk runs as before and does not wait for the fence.

chunkSize (key: string, max: number, min?: number): number

The units of the next chunk of key (change 0074). With no chunk of key measured yet, it is min, capped at max: the first chunk teaches the rate. After that, it is the units that fit eight tenths of timeBudgetMs at the learned rate, at most four times the last chunk’s units, at least min and at least 1. It is never more than max. On the CPU tier, which has no watchdog, it is max, so the whole work runs as one chunk.

wait (): Promise<void>

Resolves when the device has completed every plan submitted so far: on WebGPU queue.onSubmittedWorkDone(), after the kernel queue has settled; on the other tiers when the calls have settled.

dispose (): Promise<void>

Wait for the plans in flight. Land what a call of this device wrote into each exported storage array, and drop this device’s copy of it (change 0138, R3). Then destroy every array the device holds, pooled or not, each once, and destroy the WebGPU device the owner requested. A GPUDevice or a runtime the host handed in is left as it is. A use after dispose() is a TypeError.

use (): DeviceUse

Make this device the device in use until the returned handle is disposed (the default device of change 0138, Amendment A2): using _ = device.use(). Uses nest, and the innermost is the device in use. texture(), zeros(), array() and uniform() make what they make on it, and a kernel call or an entry call with no owned resource runs on it (change 0138, R3). A Uniform takes no part in the choice of a call’s device. The uses are one stack for each instance of the runtime module, not one for each async task: while a task awaits inside a block, a resource or a call with no owned resource that another task makes takes this device. Caution: do not await inside a block while other tasks may make resources or calls. Where the lib declares no Symbol.dispose, call dispose() on the handle.

See also

Source

src/runtime/device.ts, line 147, at commit af54ddad5

Edit this page Report a problem