-
Notifications
You must be signed in to change notification settings - Fork 71
scatter
TODO
myParallelArray.scatter(indices, defaultValue, conflictFunction, length)-
indices: array of indices in the resulting array -
defaultValue: an optional argument indicating the value of elements not set by scatter. When not present, the default value isundefined -
conflictFunction: an optional function to resolve conflicts, details below. -
length: an optional argument indicating the length of the resulting array. If absent, the length is the same as the length of the original ParallelArray.
A conflict occurs when multiple elements are scattered to the same location. It results in a call to conflictFunction, which is an optional third argument to scatter. If conflictFunction is undefined, an exception is thrown.
function(a, b) { <body> }-
a,b: the two values that conflict
The result of the conflict function is the elemnent to place in result[indices[index]], where result is the result ParallelArray that scatter returns.
To ensure determinism, it is the programmer’s responsibility to provide a conflict function that is associative and commutative, since there is no guarantee of the order in which conflicts will be resolved. For instance, if three elements a, b, and c are scattered to the same location, then the final element written to that location could be conflictFunction(a, conflictFunction(b, c)), or it could be conflictFunction(conflictFunction(a, b), c) or conflictFunction(conflictFunction(a, c), b), and so on.
Inside the conflict function, the value of this will be the ParallelArray object on which scatter was invoked. For example, in the invocation of scatter above, this would refer to myParallelArray.
A freshly minted ParallelArray pa, where each element pa[i] is defined as:
-
pa[indices[i]] = this[i]whenindices[i]is unique -
pa[indices[i]] = conflictFunction(a, b)when elementsaandbare scattered to the same location -
defaultValuewheniis not present inindicesarray
// Do the identity function
var source = new ParallelArray([1,2,3,4,5]);
var indices = [0, 1, 2, 3, 4];
var identity = source.scatter(indices); // <1, 2, 3, 4, 5>
var reorder = source.scatter([4,0,3,1,2]); // <2, 4, 5, 3, 1>
// if there is a conflict use the max. use 33 as a default value.
var reorder = source.scatter([4,0,3,4,2], 33, function max(a, b) {return a>b?a:b; }); // <2, 33, 5, 3, 4>
// One way to do histogram
var source = new ParallelArray([1,2,2,4,2,4,5]);
var ones = source.map(function one(v) { return 1; });
var histogram = ones.scatter(source, 0, function plus(a,b) { return a+b;}, 6); // <0, 1, 3, 0, 2 1>