GPU.js is a JavaScript library designed for General Purpose computing on Graphics Processing Units (GPGPU), enabling high-performance numerical computations in both web browsers and Node.js environments. It achieves this by automatically transpiling ordinary JavaScript functions into shader language (e.g., GLSL for WebGL) which then executes directly on the GPU, leveraging parallel processing capabilities for significant speedups, often 1-15x faster than CPU-bound operations. The library includes a robust fallback mechanism, ensuring that if a GPU is unavailable, computations seamlessly revert to standard JavaScript execution on the CPU. The current stable version is 2.16.0, with recent releases primarily focusing on maintenance, bug fixes (including security and memory leak issues in earlier 2.x versions), and performance enhancements. A key differentiator is its abstraction over complex shader programming, allowing developers to write GPU-accelerated code using familiar JavaScript syntax and a `this.thread.x/y/z` model for accessing thread indices within the kernel.
npm install gpu.jsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `gpu.js`, define a GPU kernel for 512x512 matrix multiplication, and execute it, showcasing the basic API for offloading computations.
Review the 'Pipelining' and 'Cleanup pipeline texture memory' sections in the documentation for proper memory management when chaining kernels and using `pipeline: true`.
Use `gpu.addFunction(yourFunction)` to make helper functions available inside kernels, or define helper functions directly inside the kernel if they are simple enough to be transpiled.
Pass external variables as arguments to the `createKernel` function or use `constants` in the kernel configuration. Do not attempt to access `this.someVariable` if `someVariable` is not a kernel property.
Upgrade to GPU.js version 2.8.5 or newer to benefit from memory leak fixes. Ensure `kernel.destroy()` is called when a kernel is no longer needed to explicitly free resources.
Upgrade to GPU.js version 2.8.3 or newer to mitigate potential security vulnerabilities.
Ensure `this.thread` is only accessed within the callback function passed to `gpu.createKernel()` or a function called by it that was added via `gpu.addFunction()`.
Verify that the arguments passed to `.setOutput([width, height, depth])` are positive integers reflecting the desired output size of the computation. Also, check input array dimensions.
Review the list of supported Math methods and language features in the GPU.js documentation. For custom functions, use `gpu.addFunction()` to provide a transpilable implementation or refactor the logic to use supported operations.
Simplify the kernel logic. Avoid complex closures, object manipulations, or unsupported data structures within the kernel. Enable debugging (`gpu.setDebug(true)`) and check browser console logs for detailed GLSL compilation errors.
No dependency data recorded yet.