wgblas
    Preparing search index...

    Class GpuVector

    Represents a Float32Array stored in GPU memory.

    Index
    _buf: GPUBuffer
    dtype:
        | Float32ArrayConstructor
        | Float64ArrayConstructor
        | typeof Complex32Array
        | typeof Complex64Array

    Typed array (or complex array) constructor used when reading data back from the GPU.

    length: number

    Number of elements in the vector.

    • Destroys the underlying GPU buffer. Call when the vector is no longer needed to free GPU memory — especially important in long-running programs.

      import { init, cleanup, GpuVector } from "wgblas";

      await init();
      const vec = GpuVector.from(new Float32Array([1, 2, 3, 4]));
      vec.destroy();
      console.log("GPU buffer released");
      if (typeof process !== "undefined") cleanup();

      Returns void

    • Reads the vector data back from GPU memory.

      Returns Promise<
          | Float32Array<ArrayBufferLike>
          | Complex32Array
          | Complex64Array
          | Float64Array<ArrayBufferLike>,
      >

      vector data in the same shape it was created from — a Float32Array, Float64Array, Complex32Array, or Complex64Array

      import { init, cleanup, GpuVector } from "wgblas";

      await init();
      const vec = GpuVector.from(new Float32Array([1, 2, 3, 4]));
      const data = await vec.read();
      console.log(data);

      vec.destroy();
      if (typeof process !== "undefined") cleanup();
    • Uploads a Float32Array, Float64Array, Complex32Array, or Complex64Array to GPU memory. A Float64Array is split into a double-double (hi, lo) f32 pair per element (WGSL has no f64 type) and stored across two GPU buffers internally; read() reassembles doubles from these pairs. This gives ~48 bits of mantissa (vs. 24 for a single f32) but less than true f64 precision (52 bits), so round-tripped values are not always bit-exact with the original input. A Complex32Array is stored interleaved ([re0, im0, re1, im1, ...]) in one buffer; a Complex64Array gets the same double-double split applied independently to its real and imaginary components, interleaved per (hi, lo) channel.

      Omitting the device falls back to the one from the last init call — the historical form, and fine for a single-GPU program. Pass a device explicitly (matching every routine's own (device, ...) convention) when driving more than one GPU at once, since a GpuVector is bound for life to whichever device created it.

      Parameters

      • data:
            | Float32Array<ArrayBufferLike>
            | Complex32Array
            | Complex64Array
            | Float64Array<ArrayBufferLike>

        input vector data

      Returns GpuVector

      GpuVector backed by a GPU buffer

      import { init, cleanup, GpuVector } from "wgblas";

      await init();
      const vec = GpuVector.from(new Float32Array([1, 2, 3, 4]));
      console.log("length:", vec.length, "dtype:", vec.dtype.name);

      const dvec = GpuVector.from(new Float64Array([1.1, 2.2, 3.3]));
      console.log("dtype:", dvec.dtype.name); // Float64Array

      vec.destroy();
      dvec.destroy();
      if (typeof process !== "undefined") cleanup();

      Explicit device (multi-GPU):

      import { init, cleanup, gpuName, sscal } from "wgblas";
      import { GpuVector } from "wgblas/classes/GpuVector";

      const dGpu = await init({ powerPreference: "high-performance" });
      const iGpu = await init({ powerPreference: "low-power" });
      console.log(gpuName(dGpu).description, "and", gpuName(iGpu).description);

      // A GpuVector is bound to whichever device created it — pass one explicitly
      // to keep each vector resident on its own GPU.
      const dVec = GpuVector.from(dGpu, new Float32Array([1, 2, 3, 4]));
      const iVec = GpuVector.from(iGpu, new Float32Array([1, 2, 3, 4]));

      await Promise.all([sscal(dGpu, 4, 2, dVec, 1), sscal(iGpu, 4, 5, iVec, 1)]);

      const [a, b] = await Promise.all([dVec.read(), iVec.read()]);
      console.log("dGpu result:", a);
      console.log("iGpu result:", b);

      dVec.destroy();
      iVec.destroy();
      if (typeof process !== "undefined") cleanup(); // releases both
    • Parameters

      • device: GPUDevice

        GPUDevice from init() — the vector is bound to this device for life

      • data:
            | Float32Array<ArrayBufferLike>
            | Complex32Array
            | Complex64Array
            | Float64Array<ArrayBufferLike>

        input vector data

      Returns GpuVector

      GpuVector backed by a GPU buffer