|
1 | | -thread.js |
2 | | -=== |
| 1 | +Parallel.js |
| 2 | +=========== |
3 | 3 |
|
4 | | -Parallel Computing with Javascript |
5 | | ---- |
| 4 | +### Parallel Computing with Javascript |
| 5 | +******* |
6 | 6 |
|
7 | | -thread.js allows you to spawn webworkers without any hassle in node.js and in the webbrowser. |
8 | | -The API is really simple: |
9 | 7 |
|
10 | | -### API |
11 | | -#### var thread = new Thread(data, opts) |
12 | | -This initializes a new thread.js instance (note: not a webworker). ```data``` is any data you want to work with in your operations. |
13 | | -In node.js data has to be ```JSON.serialize()```able, in a webbrowser, the object you want to transmit, has to be a transferable. |
| 8 | +Paralell.js is a simple library for parallel computing in Javascript, either in Node.js or in the Web Browser. |
| 9 | +Parallel takes advantage of Web Workers for the web, and child processes for Node. |
14 | 10 |
|
15 | | -The following options are available: |
| 11 | +# Installation |
| 12 | +You can download the raw javascript file [here](https://raw.github.com/adambom/parallel.js/master/lib/parallel.js) |
16 | 13 |
|
17 | | -* path |
| 14 | +Just include it via a script tag in your HTML page |
18 | 15 |
|
19 | | - default value: ```(isNode ? __dirname + '/' : '') + 'eval.js'``` |
20 | | - |
| 16 | +Parallel.js is also available as a node module: |
| 17 | + |
| 18 | +```bash |
| 19 | +npm install parallel.js -g |
| 20 | +``` |
| 21 | + |
| 22 | +# Usage |
| 23 | + |
| 24 | +#### `Parallel(data, opts)` |
| 25 | +This is the constructor. Use it to new up any parallel jobs. The constructor takes an array of data you want to |
| 26 | +operate on. This data will be held in memory until you finish your job, and can be accessed via the `.data` attribute |
| 27 | +of your job. |
| 28 | + |
| 29 | +The object returned by the `Parallel` constructor is meant to be chained, so you can produce a chain of |
| 30 | +operations on the provided data. |
| 31 | + |
| 32 | +*Arguments* |
| 33 | +* `data`: This is the data you wish to operate on. Will often be an array, but the only restrictions are that your values are serializable as JSON. |
| 34 | +* `options` (optional): Some options for your job |
| 35 | + * `legacyEvalPath` (optional): The file eval.js must be included for IE 10 support. This option specifies the path to the eval.js file. By default it will look in the same location as parallel.js |
| 36 | + * `maxWorkers` (optional): The maximum number of permitted worker threads. This will default to 4, or the number of cpus on your computer if you're running node |
| 37 | + |
| 38 | +*Example* |
| 39 | +```javascript |
| 40 | +var p = new Parallel([1, 2, 3, 4, 5]); |
21 | 41 |
|
22 | | - This is the path to the eval.js file on your webserver (webbrowser-only). |
| 42 | +console.log(p.data); // prints [1, 2, 3, 4, 5] |
| 43 | +``` |
| 44 | + |
| 45 | +#### `spawn(fn)` |
| 46 | +This function will spawn a new process on a worker thread. Pass it the function you want to call. Your |
| 47 | +function will receive one argument, which is the current data. The value returned from your spawned function will |
| 48 | +update the current data. |
| 49 | + |
| 50 | +*Arguments* |
| 51 | +* `fn`: A function to execute on a worker thread. Receives the wrapped data as an argument. The value returned will be assigned to the wrapped data. |
| 52 | + |
| 53 | +*Example* |
| 54 | +```javascript |
| 55 | +var p = new Parallel('forwards'); |
| 56 | + |
| 57 | +// Spawn a remote job (we'll see more on how to use then later) |
| 58 | +p.spawn(function (data) { |
| 59 | + data = data.reverse(); |
| 60 | + |
| 61 | + console.log(data); // logs sdrawrof |
23 | 62 |
|
24 | | -* maxWorkers |
| 63 | + return data; |
| 64 | +}).then(function (data) { |
| 65 | + console.log(data) // logs sdrawrof |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +#### `map(fn)` |
| 70 | +Map will apply the supplied function to every element in the wrapped data. Parallel will spawn one worker for |
| 71 | +each array element in the data, or the supplied maxWorkers argument. The values returned will be stored for |
| 72 | +further processing. |
| 73 | + |
| 74 | +*Arguments* |
| 75 | +* `fn`: A function to apply. Receives the wrapped data as an argument. The value returned will be assigned to the wrapped data. |
| 76 | + |
| 77 | +*Example* |
| 78 | +```javascript |
| 79 | +var p = new Parallel([0, 1, 2, 3, 4, 5, 6]), |
| 80 | + log = function () { console.log(arguments); }; |
| 81 | + |
| 82 | +// One gotcha: anonymous functions cannot be serialzed |
| 83 | +// If you want to do recursion, make sure the function |
| 84 | +// is named appropriately |
| 85 | +function fib(n) { |
| 86 | + return n < 2 ? 1 : fib(n - 1) + fib(n - 2); |
| 87 | +}; |
| 88 | + |
| 89 | +p.map(fib).then(log) |
| 90 | + |
| 91 | +// Logs the first 7 Fibonnaci numbers, woot! |
| 92 | +``` |
| 93 | + |
| 94 | +#### `reduce(fn)` |
| 95 | +Reduce applies an operation to every member of the wrapped data, and returns a scalar value produced by the operation. |
| 96 | +Use it for combining the results of a map operation, by summing numbers for example. This takes a reducing function, |
| 97 | +which gets an argument, `data`, an array of the stored value, and the current element. |
| 98 | + |
| 99 | +*Arguments* |
| 100 | +* `fn`: A function to apply. Receives the stored value and current element as argument. The value returned will be stored as the current value for the next iteration. Finally, the current value will be assigned to current data. |
| 101 | + |
| 102 | +*Example* |
| 103 | +```javascript |
| 104 | +var p = new Parallel([0, 1, 2, 3, 4, 5, 6, 7, 8]); |
| 105 | + |
| 106 | +function add(d) { return d[0] + d[1]; } |
| 107 | +function factorial(n) { return n < 2 ? 1 : n * factorial(n - 1); } |
| 108 | +function log() { console.log(arguments); } |
| 109 | + |
| 110 | +p.require(factorial) |
| 111 | + |
| 112 | +// Approximate e^10 |
| 113 | +p.map(function (n) { return Math.pow(10, n); }).reduce(add).then(log); |
| 114 | +``` |
| 115 | + |
| 116 | +#### `then(success, fail)` |
| 117 | +The functions given to `then` are called after the last requested operation has finished. |
| 118 | +`success` receives the resulting data object, while `fail` will receive an error object. |
| 119 | + |
| 120 | +*Arguments* |
| 121 | +- `success`: A function that gets called upon succesful completion. Receives the wrapped data as an argument. |
| 122 | +- `failure` (optional): A function that gets called if the job fails. The function is passed an error object. |
| 123 | + |
| 124 | +*Example* |
| 125 | +```javascript |
| 126 | +var p = new Parallel([1, 2, 3]); |
25 | 127 |
|
26 | | - default value: ```isNode ? require('os').cpus().length : 4``` |
| 128 | +function dbl(n) { return n * 2; } |
27 | 129 |
|
28 | | - The maximum of threads you want to use simultaneously. |
| 130 | +p.map(dbl).map(dbl).map(dbl).then(function (data) { |
| 131 | + console.log(data); // logs [8, 16, 24] |
| 132 | +}); |
29 | 133 |
|
30 | | -Note that every function call returns a promise! |
| 134 | +// Approximate e^10 |
| 135 | +p.map(function (n) { return Math.pow(10, n); }).reduce(add).then(log); |
| 136 | +``` |
31 | 137 |
|
32 | | -#### .spawn(fn) |
33 | | -```.spawn``` spawns a single webworker with your given function as an argument. |
34 | | -The function will receive one parameter with the data which is currently held by the thread.js instance. |
35 | | -The value returned from spawn will be stored in the thread.js instance after execution in the worker. |
| 138 | +#### `require(state)` |
| 139 | +If you have state that you want to share between your main thread and the worker threads, this is how. Require |
| 140 | +takes either a string or a function. A string should point to a file name. |
36 | 141 |
|
37 | | -#### .map(fn) |
38 | | -```.map``` spawns one worker per element in the ```data```-array up to the given ```maxWorkers``` option. |
39 | | -The function given to map will receive a single element of the array and the returned value will be stored at that index of the array again. |
| 142 | +*Example* |
| 143 | +```javascript |
| 144 | +var p = new Parallel([1, 2, 3]); |
40 | 145 |
|
41 | | -#### .then(success, fail) |
42 | | -The functions given to ```.then``` are called after the last requested operation has finished. |
43 | | -```success``` receives the resulting data object, while ```fail``` will receive an error object. |
| 146 | +function cubeRoot(n) { return Math.pow(n, 1 / 3); } |
44 | 147 |
|
45 | | -### Code Style |
| 148 | +// Require a function |
| 149 | +p.require(cubeRoot); |
46 | 150 |
|
47 | | -Code style is equivalent to the Visual Studio default code formatting settings, namely: |
| 151 | +// Require a file |
| 152 | +p.require('blargh.js'); |
48 | 153 |
|
49 | | -* Tabs |
50 | | -* when using anonymous functions, put a space between function and the paranthesis ```function () {``` |
51 | | -* no spaces around parameters, only after commas |
52 | | -* Use semilicons |
53 | | -* no newline before curly braces |
54 | | -* single quotes for strings |
55 | | -* tests, tests, tests. If you change something and it's testable, there **has** to be a test |
| 154 | +p.map(function (d) { |
| 155 | + return blargh(20 * cubeRoot(d)); |
| 156 | +}); |
| 157 | +``` |
0 commit comments