Concurrency versus parallelism
- Parallelism: more than one task is executed at the same time. That means that the tasks run on different processors or processor cores.
- Concurrency: more than one task makes progress at the same time. Often, tasks depend on each other. Concurrent tasks may run in parallel, but they can also be run via time slicing (virtual parallelism, if you will).
Kinds of parallelismTwo models of parallelism are:
- Data parallelism: The same piece of code is executed several times in parallel. The instances operate on different elements of the same dataset. For example: MapReduce is a data-parallel programming model.
- Task parallelism: Different pieces of code are executed in parallel. Examples: Web Workers and the Unix model of spawning processes.
ParallelJS brings the RiverTrail idea to normal arrays and has recently been included in Firefox Nightly. It adds three methods to arrays (binary data will eventually get those methods, too):
- It must not mutate shared data. It can freely change data that it has created, but can only read data from the outside world.
The rules for when PJS code can be run concurrently will probably never be standardized, because a PJS standard needs to accomodate a variety of implementation approaches (see below). There will, however, be guidelines and recommendations for what works for most implementations.
Given how difficult it is to predict whether your code can be run concurrently or not, an important part of PJS will be diagnostic tools. They will let you find out when and why code is not parallelizable.
ParallelJS is currently tentatively scheduled for ECMAScript 8. If you are worried that that’s too far into the future: ECMAScript 7 and ECMAScript 8 will be smaller and developed more quickly than ECMAScript 6.
Modes of executionThere are several conceivable ways of executing PJS code:
- Sequentially: a shim can make the PJS array methods available on older engines
- Concurrently: run multiple instances of the callback concurrently. The operating system can distribute those instances across cores.
- SIMD: use SIMD processor instructions (such as Intel’s SSE) for faster processing.
- GPU: run vectorized code, but on the GPU instead of the CPU.