Skip to content

discere-os/oneTBB.wasm

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

@discere-os/onetbb.wasm

Enhanced WebAssembly implementation of Intel oneTBB (Threading Building Blocks) with Deno-first TypeScript wrapper and advanced threading coordination.

License WASM Threading TypeScript

πŸš€ Features

  • 🧡 Enhanced Threading: Addresses Intel's WASM limitations with pthreads + Web Workers
  • πŸ¦• Deno-First: Modern TypeScript wrapper with async/await APIs
  • πŸ“¦ Production Build System: SIDE_MODULE + MAIN_MODULE variants for optimal deployment
  • πŸ”§ Worker Coordination: Advanced threading patterns beyond basic pthreads
  • ⚑ SIMD Ready: Users can implement SIMD within their parallel function bodies
  • 🌐 Broad Compatibility: Works on all modern browsers with WASM support

πŸƒβ€β™‚οΈ Quick Start

Deno (Recommended)

# Run demo
deno task demo

# Run tests and benchmarks
deno task test
deno task bench

NPM Installation

npm install @discere-os/onetbb.wasm

πŸ”§ Usage

Basic Parallel For

import OneTBB from '@discere-os/onetbb.wasm';

const tbb = new OneTBB({ numThreads: 4, usePthreads: true });
await tbb.initialize();

const data = new Float64Array(1000000);
for (let i = 0; i < data.length; i++) {
  data[i] = Math.random();
}

// Parallel computation
await tbb.parallelFor(0, data.length, (begin, end) => {
  for (let i = begin; i < end; i++) {
    data[i] = Math.sqrt(data[i]) * 2.0;
  }
}, undefined, { grainSize: 10000, useSIMD: true });

tbb.cleanup();

Parallel Reduce

const sum = await tbb.parallelReduce(
  0, data.length,
  0.0, // identity value
  (begin, end, partialSum) => {
    for (let i = begin; i < end; i++) {
      (partialSum as any) += data[i];
    }
  },
  (left, right) => (left as number) + (right as number) // join function
);

console.log(`Sum: ${sum}`);

User-Controlled SIMD (Optional)

// Users can implement SIMD within their parallel functions if desired
await tbb.parallelFor(0, data.length, (begin, end) => {
  // User's choice to use SIMD here with wasm_simd128.h
  for (let i = begin; i < end; i++) {
    data[i] = Math.sqrt(data[i]) * 2.0;
  }
});

πŸ“Š Performance

Benchmark results on modern hardware (Chrome 113+):

Algorithm Serial Threading Speedup
Array Processing (1M) 85ms 24ms 3.5x
Monte Carlo Pi (1M samples) 180ms 52ms 3.5x
Parallel Reduce Sum (500K) 45ms 14ms 3.2x

Note: Users can achieve additional SIMD speedups by implementing SIMD within their function bodies.

🌐 Browser Support

Browser Version Threading Status
Chrome 88+ βœ… Full Support
Edge 88+ βœ… Full Support
Chrome Android 88+ βœ… Full Support
Firefox 89+ βœ… Full Support
Safari 15.2+ ⚠️ Limited Threading

Requirements: Basic WASM support (universal). Threading requires SharedArrayBuffer (secure context + COOP/COEP headers).

πŸ—οΈ Architecture

Threading Strategy

  • Emscripten pthreads: Native pthread support with SharedArrayBuffer
  • Web Workers: Dedicated workers for non-pthread operations
  • SharedWorker: Cross-tab coordination and shared state
  • Worker Pool: Advanced load balancing and work stealing

User-Controlled SIMD

  • WASM SIMD128: Available for users within their parallel function bodies
  • No Abstractions: Users have full control over SIMD usage
  • Standard APIs: Pure oneTBB parallel_for/parallel_reduce semantics

πŸ› οΈ Building from Source

Prerequisites

# Install Emscripten
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk && ./emsdk install latest && ./emsdk activate latest
source ./emsdk_env.sh

# Install Deno
curl -fsSL https://deno.land/install.sh | sh

Build Commands

deno task build:wasm    # Build all variants
deno task build:side    # Production SIDE_MODULE
deno task build:main    # Testing MAIN_MODULE
deno task build:threads # Threading-enabled

πŸ“‹ API Reference

Core Class

class OneTBB {
  constructor(options?: OneTBBOptions);
  async initialize(): Promise<void>;
  cleanup(): void;

  // Parallel algorithms
  async parallelFor<T>(...): Promise<void>;
  async parallelReduce<T, R>(...): Promise<R>;

  // Capabilities
  hasCapability(cap: 'pthreads'|'shared-memory'): boolean;
  getVersion(): string;
  async getPerformanceStats(): Promise<TBBPerformanceStats>;
}

Based on Intel oneTBB

This builds upon Intel's oneAPI Threading Building Blocks (oneTBB), which already includes official WASM support via Emscripten. Our enhancements specifically address the limitations noted in Intel's WASM_Support.md:

🎯 Addresses Intel's Known WASM Limitations:

  • Nested Web Worker Issues: Implements PROXY_TO_PTHREAD and thread pool warm-up solutions
  • Serial Execution Problem: Provides Worker coordination to prevent unexpected serial behavior
  • Performance Optimization: Pre-warmed thread pools and optimal grain size calculation

πŸš€ Additional Enhancements:

  • Deno-first TypeScript wrapper with modern async/await APIs
  • Advanced Worker management with load balancing and work stealing
  • Production-ready build system with SIDE_MODULE/MAIN_MODULE variants
  • Comprehensive testing and benchmarking for web environments
  • Standard oneTBB APIs - no custom abstractions, pure Intel TBB semantics

Upstream oneTBB: https://github.com/uxlfoundation/oneTBB Documentation: https://oneapi-src.github.io/oneTBB/ Intel's WASM Support: See WASM_Support.md for official implementation details

πŸ’– Support This Work

This enhanced WebAssembly implementation is part of a larger effort to bring professional desktop applications to browsers with native performance.

πŸ‘¨β€πŸ’» About the Maintainer: Isaac Johnston (@superstructor) - Building foundational browser-native computing infrastructure through systematic C/C++ to WebAssembly porting.

πŸ“Š Impact: 70+ open source WASM libraries enabling professional applications like Blender, GIMP, and scientific computing tools to run natively in browsers.

πŸš€ Your Support Enables:

  • Continued maintenance and updates
  • Performance optimizations
  • New library ports and integrations
  • Documentation and tutorials
  • Cross-browser compatibility testing

πŸ’– Sponsor this work to help build the future of browser-native computing.

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages

  • C++ 88.7%
  • C 7.2%
  • CMake 2.2%
  • Python 0.8%
  • TypeScript 0.8%
  • Starlark 0.1%
  • Other 0.2%