gpuFold (Function)

Signature: gpuFold(buffer: GpuBuffer<int8> | GpuBuffer<int16> | GpuBuffer<int32> | GpuBuffer<int> | GpuBuffer<uint8> | GpuBuffer<uint16> | GpuBuffer<uint32> | GpuBuffer<float16> | GpuBuffer<float32> | GpuBuffer<float> | GpuBuffer<bool>, initial: t1, combine: (t1, t0) -> t1) -> Result<t1, Error>

Description: Reduces a buffer to one scalar with a pure combine function, applied left to right on every backend. A device runs the fold on one thread, so fold a large buffer by rows and then fold the rows. Allocation, dispatch or synchronization failure returns Error with its reason.

Parameters

  • buffer (GpuBuffer<int8> | GpuBuffer<int16> | GpuBuffer<int32> | GpuBuffer<int> | GpuBuffer<uint8> | GpuBuffer<uint16> | GpuBuffer<uint32> | GpuBuffer<float16> | GpuBuffer<float32> | GpuBuffer<float> | GpuBuffer<bool>): The buffer to reduce
  • initial (t1): The initial scalar accumulator
  • combine ((t1, t0) -> t1): The pure (accumulator, element) function

Returns: Result<t1, Error>

Example

match toGpu([1, 2, 3]) {
    Success { value } => { let buffer = value
    match gpuFold(buffer, 0, wrapAdd) {
        Success { value } => print(value)
        Error { message } => print(message)
    }
    }
    Error { message } => print(message)
}