How to achieve parallelism for SharedArrayBuffer and Atomics?
Asked Answered
G

2

6

ECMA-2017(ES8), just finalized about a month ago, introduces SharedArrayBuffer and Atomics. The link here shows that they have been supported in some browsers.

As we know, they are meant to allow the sharing of data across threads. I wonder how this kind of parallelism is achieved in browsers and Node? Are we supposed to use Web Workers and the 'cluster' package respectively?

Ghost answered 21/7, 2017 at 6:17 Comment(2)
Node is a single thread application, No?Shunt
Well, I think threads can be forked and made to run in parallel on separate cores with the 'cluster' package.Ghost
R
4

Indeed, for browsers SharedArrayBuffers and Atomics are intended to be used with WebWorkers, which is the natural way to have code run in concurrent threads in a browser context.

For Node.js, the threads spawned with the cluster package would indeed be candidates for sharing data, but at the time of writing there is no implementation of SharedArrayBuffers in Node.js yet, so this is theory. You may want to scan several discussions about it:

There already is the ems package which allows sharing data between different threads and processes.

Related:

Rhamnaceous answered 23/7, 2017 at 10:47 Comment(1)
Node released a new version (v10.5.0) including experimental worker threads that can share instances of ArrayBuffer or SharedArrayBuffer : nodejs.org/api/worker_threads.htmlBethanie
C
1

For nodejs the SharedArrayBuffer And Array buffer havee been supported officially since v10.5.0 and they are directly mentioned on the documentation on https://nodejs.org/api/worker_threads.html:

Unlike child_process or cluster, worker_threads can share memory. They do so by transferring ArrayBuffer instances or sharing SharedArrayBuffer instances.

Shared buffer and atomics:

https://www.sitepen.com/blog/the-return-of-sharedarraybuffers-and-atomics/

A good article that tackle directly the shared buffer. And the use with atomic (You must look at Atomic too).

Quick snippet (from the article):

    
// Creating a shared buffer
const length = 10;
 // Get the size we want in bytes for the buffer
const size = Int32Array.BYTES_PER_ELEMENT * length;
 // Create a buffer for 10 integers
const sharedBuffer = new SharedArrayBuffer(size);
const sharedArray = new Int32Array(sharedBuffer);

Now we have a shared buffer that we can pass to a worker context and also have an integer Array that leverages that shared buffer. To pass this buffer reference to the worker:

// main.js
worker.postMessage(sharedBuffer);

This buffer allows us to create another shared array on the worker side:

// worker.js
constsharedArray = new Int32Array(m.data);

The article cover a lot of details and the use of atomic too. Check the last section too about the support in the browser! It may be outdated already! but still relevant!

Check Introducing Atomics title for a good introduction! (too long to site here).

And from MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics. A link to check.

And

https://blog.logrocket.com/a-complete-guide-to-threads-in-node-js-4fa3898fe74f/

And this one take the whole multi-threading including the shared buffer use. With good historical background. And it tackle many many things.

Check this too:

https://mcmap.net/q/485112/-node-js-worker-threads-shared-object-store

For the use of data view!

Cheeseparing answered 16/12, 2019 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.