parallel1d

    0.2.1 • Public • Published

    Parallel1d

    npm npm GitHub issues

    A light helper for parallel calculations on one-dimensional arrays. Web workers are alternative here for Array.map method.

    Demo and comparsion

    You can see a live demo at https://bouvens.github.io/parallel1d/ The source code of this demo is available in the repository.

    Example of usage: Griffeath's machine.

    Usage

    Run in a console:

    npm i parallel1d

    Web worker needs an external file as a browsers limitation. Web worker always gets data property in onmessage function. Parallel1d will also add from and to properties to divide work.

    /**
     * sample.worker.js
     */
    
    // just one calculation function for example
    function double (n) {
      return n * 2
    }
    
    // it gets passed options for worker, `from` and `to`
    onmessage = function ({ data: { input, from, to } }) {
      const result = []
    
      for (let j = from; j < to; j++) {
        const n = input[j]
    
        result.push(double(n))
      }
    
      postMessage(result)
    }

    A worker returns the resulting array through the postMessage function.

    The worker may be imported just by const myWorker = new Worker('sample.worker.js'), or with worker-loader in case of using Webpack.

    /**
     * index.js
     */
    
    import Parallel from 'parallel1d'
    import SampleWorker from 'worker-loader!./sample.worker.js'
    
    const someNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    // pass worker constructor and callback to process resulting array
    const workers = new Parallel(SampleWorker, console.log)
    // pass any options for worker and length of array to divide it to workers
    workers.start({ input: someNumbers }, someNumbers.length)
    // console: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

    That's it.

    Options

    Besides worker constructor and callback to process resulting array parallel1d constructor accepts options:

    const workers = new Parallel(SampleWorker, console.log, {
      // handler for errors, console by default
      handleError: console.error,
      // how much workers will be spawned, number of logical processors by default
      numberOfWorkers: navigator.hardwareConcurrency,
      // type of array to be returned from parallel1d and workers
      // may be typed array like Int32Array and Uint8ClampedArray, usual array by default
      ArrayConstructor: Array,
    })
    
    // get numberOfWorkers
    console.log(workers.threads)

    Get the numberOfWorkers set in options or by default from a threads property.

    Terminating

    If you need to stop all workers immediately, call:

    workers.terminate()

    Just 419 bytes

    Size and times are defined with size-limit:

      Package size: 419 B with all dependencies, minified and gzipped
      Loading time: 10 ms on slow 3G
      Running time: 45 ms on Snapdragon 410
      Total time:   55 ms
    

    How to run demo locally

    Run in a console:

    git clone git@github.com:bouvens/parallel1d.git
    cd parallel1d
    yarn
    yarn run start

    For sure, you can use npm instead of yarn.

    Install

    npm i parallel1d

    DownloadsWeekly Downloads

    0

    Version

    0.2.1

    License

    MIT

    Unpacked Size

    17.3 kB

    Total Files

    15

    Last publish

    Collaborators

    • avatar