Does browser JavaScript allow for SIMD or Vectorized operations?
Asked Answered
M

3

26

I want to write applications in JavaScript that require a large amount of numerical computation. However, I'm very confused about the state of efficient linear-algebra-like computation in client-side JavaScript. There seems to be many approaches, but no clear indication of their readiness. Most of them seem to have restrictions of the size of vectors and matrices allowed for computation.

WebGL

Obviously allows for vector and matrix computations on the GPU, but I'm not clear on the limitations. Attempted wrappers around this library seem to limit size of matrices and vectors. Is this a practical limitation (browsers don't support anything else) or just a development limitation (someone need to write the code)?

WebCL

WebCL is a proposed browser-level implementation of OpenCL, but appears to be stuck in development.

WebGPU

Apple has recently put forth an alternative to WebCL called WebGPU. So far, there is a prototype and demos, but it's not clear to me if this will see wide adoption.

SIMD

Mozilla has put out an API for SIMD operations, but it only has experimental support.

Are vectorized computations on the browser-side supported by JavaScript?


Notes:

  • My question is not "What's a good library for numerical computation in JavaScript" but "Are vectorized operations possible in JavaScript?" An acceptable answer would link to a demo of vectorized computation working in a non-experimental browser.

  • I may be getting SIMD, vectorization and GPU computation confused. I thought it was okay to use them synonymously in this context, given they all allow for efficient computations involving high-dimensional vectors using hardware acceleration.

Minny answered 21/3, 2017 at 2:51 Comment(2)
I'm not good with JavaScript. It's great to have a fat client but I think SIMD instructions in client side might cause many problems. BTW, You must check your web application in different architecture to see the efficiency of that experimental SIMD library for javascript. You must consider Intel support different SIMD technologies 128-bit, 256-bit, and 512-bit instructions with various compatibility problem in different OS, Compilers, ISAs, etc it's just for intel! If you use java in server side there are some way to use SIMDization in server side that might be useful.Charger
Is there an updated answer to this question as of 2024?Penrose
I
18

SIMD.js was part of JavaScript. There was also experimental implementations in Firefox and Chrome but now SIMD.js has been taken out of active development in TC39 and removed from Stage 3. It is not being pursued by web browsers for implementation anymore. SIMD operations exposed to the web are under active development within WebAssembly, with operations based on the SIMD.js operations.

WebAssembly SIMD Status

Status of SIMD in LLVM Wasm backend and web engines:
Proposal: Implementation Status

Interstate answered 10/10, 2017 at 19:50 Comment(0)
M
7

The state of SIMD in JavaScript is partly a practical and a developmental problem.

Practical

Web browsers are kind of like virtual machines. This means they need a ton of drivers for hardware. The drivers for exposing the few shaders and whatnot for WebGL are significantly different than the arbitrary kernel execution required for SIMD operations.

Developmental

Hypothetically, one could just wrap WebGL to make it a general purpose GPU computer and someone has attempted to with gpgpu.js. However, it's got finicky support and is probably slower than just directly piping a kernel to the GPU.

Conclusion

The web isn't ready for SIMD yet. There are quite a few big companies working to make it ready. Until then, you're going to have to rely on WebWorkers for large batches of numerical computations.

Minny answered 13/4, 2017 at 1:13 Comment(0)
G
1

Float32 vs Float64

If you want to do extremely parallelized operations with Float32 values then you can use WebGL, which is supported on almost all phones and computers nowadays (2024). WebGL shaders can do a few hundred Float32 operations simultaneously. A big win!

I don't think browser JS will ever support extremely parallelized operations on Float64 values, because if it did then it would be too easy for bad websites to secretly use browsers to mine bitcoins etc. For example, WebGL does not support Float64 values.

You can use WebAssembly to do two parallel Float64 operations per thread. This is because WebAssembly's SIMD works by packing values into 128 bit registers. SIMD is quite effective with 16 * 8 bit values or 8 * 16 bit values, but it's not really worth the hassle of using WebAssembly for 2 * 64 bit values.

You can use Worker threads to do N parallel Float64 operations simultaneously, where N is the number of CPU cores in the browser. Modern desktop computers have quite a lot of CPU cores. For example, with my Mac Studio I can run 20 Worker threads simultaneously.

Conclusion

  1. For Float32 computations, use WebGL
  2. For Float64 computations, use Worker Threads
  3. For Byte/Short computations, try Web Assembly
Gangster answered 11/4 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.