Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

scatter

RLH edited this page Sep 7, 2012 · 9 revisions

Synopsis

myArray.scatter(indices, defaultValue, conflictFunction, length)

##Arguments indices array of indices in the resulting array

defaultValue optional argument indicating the value of elements not set by scatter When not present, the default value is undefined

conflictFunction optional function to resolve conflicts, details below.

length optional argument indicating the length of the resulting array. If absent, the length is the same as the length of the original ParallelArray.

##Returns

A freshly minted ParallelArray A where each element A[i] is defined as

  • A[indices[i]] = this[i], when indices[i] is unique

  • A[indices[i]] = conflictFunction(valA, valB) when multiple elements are scattered to the same location. see below

  • defaultValue, when i is not present in indices array

##Example:

 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>
 
 var reorder = source.scatter([4,0,3,4,2], 33, function max(a, b) {return a>b?a:b; }); // <2, 33, 5, 3, 4>
 
 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>

Handling conflicts with the Conflict Function

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, conflicts are non-deterministically resolved between the two values.

###Synopsis conflictFunction(valA, valB) ###Arguments

this the entire ParallelArray

valA, valB the two values that conflict

To ensure determinism it is the programmer’s responsibility to provide a conflictFunction that is associative and commutative since there is no guarantee in what order the conflicts will be resolved.

###Returns Value to place in result[indices[index]]

Clone this wiki locally