A simple yet powerful Node.js and Express.js middleware to offload CPU-intensive tasks to the GPU, boosting performance for parallel computations. Ideal for machine learning, data analysis, and scientific computing applications.
npm install warp-drive-express
A simple yet powerful Node.js and Express.js middleware to offload CPU-intensive tasks to the GPU, boosting performance for parallel computations. Ideal for machine learning, data analysis, and scientific computing applications.
This middleware acts as a drop-in solution that prefers the GPU over the CPU for heavy computations, with a graceful fallback to the CPU when a GPU context is not available. It uses the powerful gpu.js library to perform GPGPU (General-Purpose computing on Graphics Processing Units).
- Performance Boost: Drastically speed up parallelizable tasks by leveraging the massive parallel processing power of modern GPUs.
- Easy Integration: A simple-to-use Express.js middleware that can be added to any existing application.
- Graceful Fallback: Automatically falls back to CPU execution if a GPU is not available, ensuring your application runs anywhere.
- Extensible: Register your own custom GPU and CPU kernels to tailor the middleware to your specific needs.
1. Install the package:
``bash`
npm install warp-drive-express
2. Add the middleware to your Express.js application:
`javascript
const express = require('express');
const { createWarpDriveExpress } = require('warp-drive-express');
const app = express();
// Add the middleware
app.use(createWarpDriveExpress());
// Your routes can now access the GPU kernels via req.warpDrive
app.get('/my-computation', (req, res) => {
const { matmul } = req.warpDrive.kernels;
const result = matmul([1, 2], [3, 4]); // This will run on the GPU if available
res.json(result);
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
`
Here's an example of a route that calculates the Mandelbrot set, a computationally intensive task, using the warp-drive-express middleware.
`javascript
app.get('/mandelbrot', (req, res) => {
const { mandelbrot } = req.warpDrive.kernels;
const { w = 200, h = 200, iter = 100 } = req.query;
// The 'auto' mode will try to use the GPU first, and fallback to CPU
const result = mandelbrot(parseInt(w), parseInt(h), parseInt(iter), 'auto');
res.send(result); // The result will be an image buffer
});
``
For a more comprehensive set of examples and benchmarks, please see the demo server available on our GitHub repository. The demo server includes examples for:
- Mandelbrot set generation
- Matrix multiplication
- An A/B test to compare CPU vs. GPU performance
Node.js GPU acceleration, Express.js GPU middleware, gpu.js express example, force GPU usage in Node.js, offload CPU to GPU Node.js, parallel computing Node.js, high performance computing Node.js, speed up Node.js server with GPU.