Options
All
  • Public
  • Public/Protected
  • All
Menu

gpcpu

Asynchronous Parallel Programming for TypedArray

About

gpcpu enables user-friendly parallel programming in JavaScript by defining a forEach() function which executes a user-defined callback on multiple concurrent threads and returns a Promise that resolves when all the results have been copied to the TypedArray provided as input.

This significantly speeds up execution of sequential for loops over large typed arrays by delegating each block of execution to a concurrently running thread.

Support

gpcpu provides out-of-the-box support for both browsers and Node.js by selectively using Web Workers or Worker Threads depending on the runtime environment. It also ships with typings for use in TypeScript projects.

Install

npm (Recommended)

npm i gpcpu

unpkg

<script src="https://unpkg.com/gpcpu"></script>

Usage

import { Manager } from 'gpcpu';

// uses the number of threads your hardware can run concurrently by default
const parallel = new Manager();
const fltArr = new Float64Array(100000000);

console.time('parallel');

parallel.forEach(fltArr, (subArr, localIndex, globalIndex) => {
  subArr[localIndex] = Math.sqrt(globalIndex);
}).then(() => {
  // ~557ms on Intel Core i7-7700K @ 4.20GHz using 8 threads
  console.timeEnd('parallel');
  // use fltArr here
});

Benchmark Comparison

const fltArr = new Float64Array(100000000);

console.time('sequential');

for (let i = 0; i < fltArr.length; ++i) {
  fltArr[i] = Math.sqrt(i);
}

// ~2564ms on Intel Core i7-7700K @ 4.20GHz
console.timeEnd('sequential');
// use fltArr here

Documentation

Available on Github Pages.

Index

Type aliases

MessageRequestListener

MessageRequestListener: MessageEventListener<MessageRequest>

MessageResponseListener

NodeMessagePort

NodeMessagePort: MessagePort

NodeWorker

NodeWorker: Worker

NodeWorkerOptions

NodeWorkerOptions: WorkerOptions

TaskOptions

TaskOptions: WorkerOptions & NodeWorkerOptions

TypedArray

TypedArray: Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array

Variables

Const Task

Task: TaskConstructor = typeof Worker === 'function'? Worker: getWorker()

Functions

getCpus

  • getCpus(): number

getObjectURL

  • getObjectURL(): string

getSource

  • getSource(): string

getWorker

registerListener

  • registerListener(): void

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc