Skip to main content

Class: PrefixScan<T>

A cascade of shaders to do a prefix scan operation, based on a shader that does a prefix scan of a workgroup sized chunk of data (e.g. 64 or 256 elements).

The scan operation is parameterized by the module mechanism. The user can instantiate a PrefixScan with sum to get prefix-sum, or use another module for other parallel scan applications.

For small data sets that fit in workgroup, only a single shader pass is needed. For larger data sets, a sequence of shaders is orchestrated as follows:

  1. One shader does a prefix scan on each workgroup sized chunk of data. It emits a partial prefix sum for each workgroup and single block level sum from each workgroup
  2. Another instance of the same shader does a prefix scan on the block sums from the previous shader. The end result is a set of block level prefix sums
  3. A final shader sums the block prefix sums back with the partial prefix sums

For for very large data sets, steps 2 and 3 repeat heirarchically. Each level of summing reduces the data set by a factor of the workgroup size. So three levels handles e.g. 16M elements (256 ** 3) if the workgroup size is 256.

Type parameters

NameTypeDescription
TnumberType of elements returned from the scan

Hierarchy

  • HasReactive

    PrefixScan

Implements

Constructors

constructor

new PrefixScan<T>(args): PrefixScan<T>

Create a new scanner

Type parameters

NameType
Tnumber

Parameters

NameType
argsPrefixScanArgs

Returns

PrefixScan<T>

Overrides

HasReactive.constructor

Defined in

packages/stoneberry/src/scan/PrefixScan.ts:130

Properties

binOps

binOps: BinOpModule

customize the type of scan (e.g. prefix sum on 32 bit floats)

Defined in

packages/stoneberry/src/scan/PrefixScan.ts:85


device

device: GPUDevice

end index (exclusive) in src buffer (src.length if undefined)

Defined in

packages/stoneberry/src/scan/PrefixScan.ts:121


exclusive

exclusive: boolean

Inclusive scan accumulates a binary operation across all source elements. Exclusive scan accumulates a binary operation across source elements, using initialValue as the first element and stopping before the final source element.

Default Value

false (inclusive scan).

Defined in

packages/stoneberry/src/scan/PrefixScan.ts:109


forceWorkgroupLength

Optional forceWorkgroupLength: number

Override to set compute workgroup size e.g. for testing.

Default Value

max workgroup size of the GPUDevice

Defined in

packages/stoneberry/src/scan/PrefixScan.ts:96


initialValue

Optional initialValue: number

Initial value for exclusive scan

Default Value

0

Defined in

packages/stoneberry/src/scan/PrefixScan.ts:114


label

Optional label: string

Debug label attached to gpu objects for error reporting

Defined in

packages/stoneberry/src/scan/PrefixScan.ts:91


maxWorkgroups

Optional maxWorkgroups: number

Override to set max number of workgroups for dispatch e.g. for testing.

Default Value

maxComputeWorkgroupsPerDimension from the GPUDevice

Defined in

packages/stoneberry/src/scan/PrefixScan.ts:101


source

source: GPUBuffer

Source data to be scanned

Defined in

packages/stoneberry/src/scan/PrefixScan.ts:88

Accessors

result

get result(): GPUBuffer

Buffer containing results of the scan after the shader has run.

Returns

GPUBuffer

Defined in

packages/stoneberry/src/scan/PrefixScan.ts:154

Methods

commands

commands(commandEncoder): void

Add compute or render passes for this shader to the provided GPUCommandEncoder

Parameters

NameType
commandEncoderGPUCommandEncoder

Returns

void

Implementation of

ComposableShader.commands

Defined in

packages/stoneberry/src/scan/PrefixScan.ts:135


destroy

destroy(): void

Release the scanResult buffer for destruction.

Returns

void

Implementation of

ComposableShader.destroy

Defined in

packages/stoneberry/src/scan/PrefixScan.ts:140


scan

scan(): Promise<number[]>

Execute the prefix scan immediately and copy the results back to the CPU. (results are copied from the PrefixScan.result GPUBuffer)

Returns

Promise<number[]>

the scanned result in an array

Defined in

packages/stoneberry/src/scan/PrefixScan.ts:148