1

Recently in school, I've been taught C++/OpenMPI in a parallel computing class. I don't really like to program in C++ as its low level and harder to program, easier to make mistakes etc.

So I've been thinking, is JavaScript/NodeJS (something I've started to like) actually truly parallel? Or is it simply using non-blocking operations to simulate parallel execution (which I think it is)? There are libraries like async which gives similar functions to what I've used in OpenMPI: gather, scatter even "parallel". But I've got a feeling its just simulating parallelism using non-blocking IO?

Perhaps only node-webcl is truly parallel?

UPDATE: Seems possible via web workers (~31 min): watching http://www.infoq.com/presentations/Parallel-Programming-with-Nodejs

Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • The reason for the popularity of things like Node.js or Python's twisted framework is that it turns out that for the kinds of loads experienced by webservers (multiple connections per client, small packets, responses in under 5 seconds etc.) multi-threading and multi-processing has proven (in the real world) to be significantly slower than multiplexed, non-blocking I/O. Especially once you have more threads than CPU cores (which is typical for a busy website). – slebetman Nov 22 '12 at 05:45
  • See this for a historical perspective of the problem of web servers: http://stackoverflow.com/questions/3759683/how-node-js-server-is-better-than-thread-based-server/3759991#3759991 – slebetman Nov 22 '12 at 05:45
  • possible duplicate of [Is javascript guaranteed to be single-threaded?](http://stackoverflow.com/questions/2734025/is-javascript-guaranteed-to-be-single-threaded) – user123444555621 Nov 22 '12 at 07:34

2 Answers2

1

In deed, JavaScript is single-threaded by it's design. But you're not the first person who wants some parallelism in it, so there're some things that can work truly parallel:

  1. WebWorkers - run in threads, which means that they quite cheap to create. They limited in data interchange abilities. At first, you could only send messages between workers, but now they are a lot better, you can even use SharedArrayBuffer for concurrent memory access. Not supported in NodeJs, only in browsers.
  2. WebGL/WebCL - utilize graphic subsystems for parallel computing. Very fast, but effective for a limited set of problems. Not all tasks can be computed effectively on GPU-like subsystem. It also requires additional data transformations for presenting you data in pixel-like format. Has decent browsers support as WebGL, but as you've already mentioned has only experimental implementations for NodeJs.
  3. SIMD - parallelism over data. It was a promising thing, but it is no longer on the roadmap for JavaScript and it will be a part of the WebAssembly standard.
  4. Cluster - a NodeJs solution for parallelism. Allows to run multiple processes (not threads) and even supports SharedArrayBuffer for communication since 9th version.

That's pretty much it. There's also the WebAssembly threads proposal, but, firstly, it is a proposal and, secondly, WebAssembly is not JavaScript.

In general, JavaScript is by far not the best tool for low level parallel computing. There're a lot of other tools that suit better for this: Java, C#, Go...

  • 1
    "*multiple processes (not threads)*" - well, each of those processes has its own thread(s) – Bergi Dec 18 '17 at 01:49
0

With Node.js, your JavaScript runs in a single thread. IO is non blocking.

Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
  • Hmm, NodeJS is single threaded, so its NOT *truly* parallel? Meaning it doesn't benefit from multicore? But I suppose I can still use distributed systems (like the cloud)? – Jiew Meng Nov 22 '12 at 04:08
  • Not parallel in any sense of the word, is what I think he's saying. – Stoive Nov 22 '12 at 04:13
  • That is correct. Node.js manages some threads under the hood for IO and other async operations (and if you write a C++ extension, you can get access to them), but to take advantage of multiple cores in your JavaScript code you'll either need to run multiple processes, or use a module that creates threads for you (via C++). – Michelle Tilley Nov 22 '12 at 04:37