wgblas
    Preparing search index...

    Function init

    • Initializes the WebGPU device.

      Devices are cached per option set: the same options return the same device, different options a separate one — so one process can drive several GPUs ("high-performance" and "low-power" typically resolve to the discrete and integrated adapters). Each routine dispatches to the device you pass it; GPU buffers cannot cross devices, so a GpuVector/GpuMatrix from one is rejected by another. cleanup releases them all.

      Parameters

      • Optionaloptions: {
            benchmark?: boolean;
            dumpShaders?: boolean;
            powerPreference?: GPUPowerPreference;
        }
        • Optionalbenchmark?: boolean

          enable GPU timestamp queries; BLAS functions then also return gpuTimeMs alongside their normal result (e.g. sscal returns { x, gpuTimeMs }, saxpy returns { y, gpuTimeMs }) — see each routine's own docs for its exact return shape (default: false)

        • OptionaldumpShaders?: boolean

          Node-only. Forwards Dawn's dump_shaders debug toggle, printing each pipeline's WGSL and compiled backend IR (SPIR-V/Vulkan, MSL/Metal, or HLSL/D3D12, whichever Dawn picked) to stderr as it compiles. A Dawn passthrough, not a wgblas format — no effect in the browser, which gives pages no API to request compiled shader IR (default: false)

          Default (high-performance GPU):

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

          await init();
          const { description, device } = gpuName();
          console.log("description:", description, "device:", device);
          if (typeof process !== "undefined") cleanup();

          Low-power (integrated GPU):

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

          await init({ powerPreference: "low-power" });
          const { description, device } = gpuName();
          console.log("description:", description, "device:", device);
          if (typeof process !== "undefined") cleanup();

          Benchmark mode:

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

          const device = await init({ benchmark: true });
          const n = 5;
          const alpha = 2.0;
          const x = new Float32Array([1, 2, 3, 4, 5]);
          const { x: result, gpuTimeMs } = await sscal(device, n, alpha, x, 1);
          console.log(`Result: [${Array.from(result).join(", ")}]`);
          console.log(`GPU time: ${gpuTimeMs.toFixed(3)} ms`);
          if (typeof process !== "undefined") cleanup();

          Two GPUs at once:

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

          const dGpu = await init({ powerPreference: "high-performance" });
          const iGpu = await init({ powerPreference: "low-power" });
          console.log(gpuName(dGpu).description, "and", gpuName(iGpu).description);
          const [a, b] = await Promise.all([
          sscal(dGpu, 4, 2, new Float32Array([1, 2, 3, 4]), 1),
          sscal(iGpu, 4, 5, new Float32Array([1, 2, 3, 4]), 1),
          ]);
          console.log("dGpu result:", a.x);
          console.log("iGpu result:", b.x);
          if (typeof process !== "undefined") cleanup(); // releases both
        • OptionalpowerPreference?: GPUPowerPreference

          GPU power preference (default: "high-performance"). This is a hint to the browser: on dual-GPU systems, "high-performance" typically favors the discrete GPU and "low-power" favors the integrated one. See MDN: GPU.requestAdapter().

      Returns Promise<GPUDevice>