npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gpujsutils

v1.0.15

Published

Wrapper for gpujs (with a stable slightly edited gpu.js source file) with a bunch of premade functions for use or demonstration. The main show here is bulk GPU FFTs which run REALLY fast.

Downloads

30

Readme

gpujsutils

gpujsutils-status gpujsutils-downloads gpujsutils-l

gpu.js is an amazing library for writing gpu kernels in plain js. This wrapper makes life easier to use it with a stable gpu.js bundle and adds some flexible macros. This revolve around persistent kernels with resizable i/o on-the-fly so we can make the best use of the performance benefits of parallelization. This is even better with web workers (see our MagicWorker library)

See GPU.js docs for information on how to write kernels

npm i gpujsutils

Create a GPU util instance, + general utility functions

import {gpuUtils, makeKrnl, makeCanvasKrnl} from 'gpuUtils'

const gpuutils = new gpuUtils();

Add a GPU utilty function which can be called in kernels (see gpujs docs for more)


function rms(arr, mean, len) { //root mean square error
    var est = 0;
    var vari = 0;
    for (var i = 0; i < len; i++) {
        vari = arr[i]-mean;
        est += vari*vari;
    }
    return Math.sqrt(est/len);
}

gpuutils.addFunction(rms);

Create a GPU kernel with default settings


function transpose2DKern(mat2) { //Transpose a 2D matrix, meant to be combined
    return mat2[this.thread.y][this.thread.x];
}

let kernel = makeKrnl(
    gpuutils.gpu, //the actual GPUjs instance, used normally  
    transpose2DKern, //the kernel
{ //kernel settings, meant to use all of the flexibility features by default (e.g. dynamic sizing)
  setDynamicOutput: true,
  setDynamicArguments: true,
  setPipeline: true,
  setImmutable: true,
  setGraphical: false
}); //makeKrnl(gpuutils.gpu);

let mat2 = [[1,2,3,4],[5,6,7,8],[8,9,10,11],[12,13,14,15]];

let result = kernel(mat2)

//OR to add to the gpu utilities

kernel = gpuutils.addKernel('transpose2D', transpose2DKern);
result = gpuutils.callKernel('transpose2D',mat2);

Create a kernel that renders to a canvas


//From a gpujs observable: https://observablehq.com/@robertleeplummerjr/video-convolution-using-gpu-js
function ImgConv2DKern(img, width, height, kernel, kernelLength) {
    let kernelRadius = (Math.sqrt(kernelLength) - 1) / 2;
    const kSize = 2 * kernelRadius + 1;
    let r = 0, g = 0, b = 0;

    let i = -kernelRadius;
    let kernelOffset = 0;
    while (i <= kernelRadius) {
        if (this.thread.x + i < 0 || this.thread.x + i >= width) {
            i++;
            continue;
        }

        let j = -kernelRadius;
        while (j <= kernelRadius) {
            if (this.thread.y + j < 0 || this.thread.y + j >= height) {
                j++;
                continue;
            }

            kernelOffset = (j + kernelRadius) * kSize + i + kernelRadius;
            const weights = kernel[kernelOffset];
            const pixel = img[this.thread.y + i][this.thread.x + j];
            r += pixel.r * weights;
            g += pixel.g * weights;
            b += pixel.b * weights;
            j++;
        }
        i++;
    }

    this.color(r, g, b);
}


let kernel = makeCanvasKernel( 
    gpu,
    ImgConv2DKern,
    {
        output: [300,300],
        setDynamicArguments: true,
        setDynamicOutput: true,
        setPipeline: false,
        setImmutable: true,
        setGraphical: true
    },
    divId //id of the div to append a new canvas to. Leave blank to append to body
);

kernel(video); //input an image or video from a source

//OR to add to the gpu utilities

kernel = gpuutils.addCanvasKernel('imgConv', ImgConv2DKern);
gpuutils.callCanvasKernel('imgConv',video, [video.width,video.height]);


Combine Kernels


//adapted from gpujs tutorial
const add = gpuutils.addKernel('add',function(a, b) {
  return a[this.thread.x] + b[this.thread.x];
}).setOutput([20]);

const multiply = gpuutils.addKernel('multiply',function(a, b) {
  return a[this.thread.x] * b[this.thread.x];
}).setOutput([20]);

//multi-step operations
const superKernel = gpuutils.combineKernels(
    'superKernel', //name the combined kernel
    ['add','multiply'], 
    function(a, b, c) {
        return multiply(add(a, b), c);
    }
);

let result = gpuutils.callKernel('superKernel',[3,4,5]);

Default Macros


//pass signal buffer to receive an ordered amplitude spectrum based 0.5x the size of sample rate (nyquist frequency)
gpuutils.gpuDFT(
    signalBuffer, //the signal buffer
    nSeconds, //number of seconds of data. sample rate = length/nSeconds 
    scalar, //can apply a scalar multiplier to the amplitudes
    texOut //receive a texture map out instead of arrays?
);

//or
gpuutils.gpuFFT(...); //faster


// pass a 1D array of evenly-sized signal buffers to receive a list of ordered amplitude spectrums 0.5x the size of sample rate (nyquist frequency)
gpuutils.MultiChannelDFT(
    signalBuffer,  //1D list of evenly sized signal buffers
    nSeconds, //number of seconds of data. sample rate = length/nSeconds 
    scalar, //can apply a scalar multiplier to the amplitudes
    false //receive a texture map out instead of arrays?
);

//or 
gpuutils.MultiChannelFFT(...); //faster

// pass a 1D array of evenly-sized signal buffers to receive a list of ordered amplitude spectrums 0.5x the size of sample rate (nyquist frequency), between the two frequencies. Better with more seconds or higher samplerate
gpuutils.MultiChannelDFT_Bandpass(
    signalBuffer,  //1D list of evenly sized signal buffers
    nSeconds, //number of seconds of data. sample rate = length/nSeconds 
    freqStart, //start frequency e.g. 3Hz
    freqEnd, //end Frequency e.g. 100Hz
     scalar, //can apply a scalar multiplier to the amplitudes
    false //receive a texture map out instead of arrays?
);

//or 
gpuutils.MultiChannelFFT_BandPass(...);

Default Kernels


    gpuutils.dft //discrete fourier transform
    gpuutils.idft //inverse DFT (untested :P)
    gpuutils.dft_windowed //DFT between two frequencies, need sufficient sample rate or number of seconds of data
    gpuutils.idft_windowed //inverse dft to reverse the bandpassed dft (untested)
    gpuutils.fft //fast fourier transform, simply decimates the number of samples used to compute lower frequency amplitudes
    gpuutils.ifft //inverse fft (untested)
    gpuutils.fft_windowed //bandpassed fft
    gpuutils.ifft_windowed  //inverse bandpassed fft (untested)
    gpuutils.listdft2D  //pass an array of arrays in of the same length to return a list of DFTs (broken) 
    gpuutils.listdft1D   //pass a single array of evenly sized sample sets of data to return the DFTs. 
    gpuutils.listdft1D_windowed  //etc
    gpuutils.listfft1D             //etc
    gpuutils.listfft1D_windowed     //tc
    gpuutils.listidft1D_windowed  //untested
    gpuutils.listifft1D_windowed  //untested
    gpuutils.bulkArrayMul 
    gpuutils.correlograms; //cross correlations, untested
    gpuutils.correlogramsPC; //precomputed means and estimators

There's a few other things used internally but they're not that useful.

Joshua Brewster & Dovydas Stirpeika License: AGPL v3.0