|
org.codehaus.gpars | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object AbstractParallelAnyArray.OUPap groovyx.gpars.extra166y.ParallelArray
public class ParallelArray extends OUPap
An array supporting parallel operations.
A ParallelArray maintains a ForkJoinPool and an array in order to provide parallel aggregate operations. The main operations are to apply some procedure to each element, to map each element to a new element, to replace each element, to select a subset of elements based on matching a predicate or ranges of indices, and to reduce all elements into a single value such as a sum.
A ParallelArray is constructed by allocating, using, or copying an array, using one of the static factory methods create, createEmpty, createUsingHandoff and createFromCopy. Upon construction, the encapsulated array managed by the ParallelArray must not be shared between threads without external synchronization. In particular, as is the case with any array, access by another thread of an element of a ParallelArray while another operation is in progress has undefined effects.
The ForkJoinPool used to construct a ParallelArray can be shared safely by other threads (and used in other ParallelArrays). To avoid the overhead associated with creating multiple executors, it is often a good idea to use the defaultExecutor() across all ParallelArrays. However, you might choose to use different ones for the sake of controlling processor usage, isolating faults, and/or ensuring progress.
A ParallelArray is not a List. It relies on random access across array elements to support efficient parallel operations. However, a ParallelArray can be viewed and manipulated as a List, via method ParallelArray#asList#asList. The asList view allows incremental insertion and modification of elements while setting up a ParallelArray, generally before using it for parallel operations. Similarly, the list view may be useful when accessing the results of computations in sequential contexts. A ParallelArray may also be created using the elements of any other Collection, by constructing from the array returned by the Collection's toArray method. The effects of mutative asList operations may also be achieved directly using method setLimit along with element-by-element access methods get and set.
While ParallelArrays can be based on any kind of an object array, including "boxed" types such as Long, parallel operations on scalar "unboxed" type are likely to be substantially more efficient. For this reason, classes ParallelLongArray and ParallelDoubleArray are also supplied, and designed to smoothly interoperate with ParallelArrays. You should also use a ParallelLongArray for processing other integral scalar data (int, short, etc). And similarly use a ParallelDoubleArray for float data. (Further specializations for these other types would add clutter without significantly improving performance beyond that of the Long and Double versions.)
Most usages of ParallelArray involve sets of operations prefixed with range bounds, filters, and mappings (including mappings that combine elements from other ParallelArrays), using withBounds, withFilter, and withMapping, respectively. For example, aParallelArray.withFilter(aPredicate).all() creates a new ParallelArray containing only those elements matching the predicate. And for ParallelLongArrays a, b, and c, a.withMapping(CommonOps.longAdder(),b).withMapping(CommonOps.longAdder(),c).min() returns the minimum value of a[i]+b[i]+c[i] for all i. As illustrated below, a mapping often represents accessing some field or invoking some method of an element. These versions are typically more efficient than performing selections, then mappings, then other operations in multiple (parallel) steps. The basic ideas and usages of filtering and mapping are similar to those in database query systems such as SQL, but take a more restrictive form. Series of filter and mapping prefixes may each be cascaded, but all filter prefixes must precede all mapping prefixes, to ensure efficient execution in a single parallel step. In cases of combined mapping expressions, this rule is only dynamically enforced. For example, pa.withMapping(op, pb.withFilter(f)) will compile but throw an exception upon execution because the filter precedes the mapping.
While series of filters and mappings are allowed, it is usually more efficient to combine them into single filters or mappings when possible. For example pa.withMapping(addOne).withMapping(addOne) is generally less efficient than pa.withMapping(addTwo). Methods withIndexedFilter and withIndexedMapping may be useful when combining such expressions.
This class includes some reductions, such as min, that are commonly useful for most element types, as well as a combined version, summary, that computes all of them in a single parallel step, which is normally more efficient than computing each in turn.
The methods in this class are designed to perform efficiently with both large and small pools, even with single-thread pools on uniprocessors. However, there is some overhead in parallelizing operations, so short computations on small arrays might not execute faster than sequential versions, and might even be slower.
Sample usages. The main difference between programming with plain arrays and programming with aggregates is that you must separately define each of the component functions on elements. For example, the following returns the maximum Grade Point Average across all senior students, given a (fictional) Student class:
import static Ops.*; class StudentStatistics { ParallelArray<Student> students = ... // ... public double getMaxSeniorGpa() { return students.withFilter(isSenior).withMapping(gpaField).max(); } // helpers: static final class IsSenior implements Predicate<Student> { public boolean op(Student s) { return s.credits > 90; } } static final IsSenior isSenior = new IsSenior(); static final class GpaField implements ObjectToDouble<Student> { public double op(Student s) { return s.gpa; } } static final GpaField gpaField = new GpaField(); }
Nested Class Summary | |
---|---|
class |
ParallelArray.AsList
|
class |
ParallelArray.ListIter
|
static class |
ParallelArray.ParallelArrayIterator
|
static interface |
ParallelArray.SummaryStatistics
Summary statistics for a possibly bounded, filtered, and/or mapped ParallelArray. |
Field Summary | |
---|---|
ParallelArray.AsList |
listView
Lazily constructed list view |
Constructor Summary | |
protected ParallelArray(ForkJoinPool executor, java.lang.Object[] array, int limit)
Constructor for use by subclasses to create a new ParallelArray using the given executor, and initially using the supplied array, with effective size bound by the given limit. |
|
ParallelArray(ForkJoinPool executor, java.lang.Object[] array)
Trusted internal version of protected constructor. |
Method Summary | |
---|---|
ParallelArray
|
addAll(java.lang.Object[] other)
Equivalent to asList().addAll but specialized for array arguments and likely to be more efficient. |
ParallelArray
|
addAll(ParallelArrayWithMapping other)
Appends all (possibly bounded, filtered, or mapped) elements of the given ParallelArray, resizing and/or reallocating this array if necessary. |
ParallelArray
|
all()
Returns a new ParallelArray holding all elements |
ParallelArray
|
all(java.lang.Class elementType)
Returns a new ParallelArray with the given element type holding all elements |
ParallelArray
|
allNonidenticalElements()
Returns a new ParallelArray containing only the non-null unique elements of this array (that is, without any duplicates), using reference identity to test for duplication. |
ParallelArray
|
allUniqueElements()
Returns a new ParallelArray containing only the non-null unique elements of this array (that is, without any duplicates), using each element's equals method to test for duplication. |
void
|
appendElement(java.lang.Object e)
|
void
|
apply(Procedure procedure)
Applies the given procedure to elements |
java.util.List
|
asList()
Returns a view of this ParallelArray as a List. |
int
|
binarySearch(java.lang.Object target)
Assuming this array is sorted, returns the index of an element equal to given target, or -1 if not present. |
int
|
binarySearch(java.lang.Object target, java.util.Comparator comparator)
Assuming this array is sorted with respect to the given comparator, returns the index of an element equal to given target, or -1 if not present. |
static ParallelArray
|
create(int size, java.lang.Class elementType, ForkJoinPool executor)
Creates a new ParallelArray using the given executor and an array of the given size constructed using the indicated base element type. |
static ParallelArray
|
createEmpty(int size, java.lang.Class elementType, ForkJoinPool executor)
Creates a new ParallelArray using the given executor and an array of the given size constructed using the indicated base element type, but with an initial effective size of zero, enabling incremental insertion via ParallelArray#asList#asList operations. |
static ParallelArray
|
createFromCopy(java.lang.Object[] source, ForkJoinPool executor)
Creates a new ParallelArray using the given executor and initially holding copies of the given source elements. |
static ParallelArray
|
createFromCopy(int size, java.lang.Object[] source, ForkJoinPool executor)
Creates a new ParallelArray using an array of the given size, initially holding copies of the given source truncated or padded with nulls to obtain the specified length. |
static ParallelArray
|
createUsingHandoff(java.lang.Object[] handoff, ForkJoinPool executor)
Creates a new ParallelArray initially using the given array and executor. |
ParallelArray
|
cumulate(Reducer reducer, java.lang.Object base)
Replaces each element with the running cumulation of applying the given reducer. |
static ForkJoinPool
|
defaultExecutor()
Returns a common default executor for use in ParallelArrays. |
java.lang.Object
|
get(int i)
Returns the element of the array at the given index |
java.lang.Object[]
|
getArray()
Returns the underlying array used for computations |
ForkJoinPool
|
getExecutor()
Returns the executor used for computations. |
boolean
|
hasAllEqualElements(ParallelArrayWithMapping other)
Returns true if all elements at the same relative positions of this and other array are equal. |
boolean
|
hasAllIdenticalElements(ParallelArrayWithMapping other)
Returns true if all elements at the same relative positions of this and other array are identical. |
int
|
indexOf(java.lang.Object target)
Returns the index of some element equal to given target, or -1 if not present |
void
|
insertElementAt(int index, java.lang.Object e)
|
void
|
insertSlotsAt(int index, int len)
Makes len slots available at index. |
java.util.Iterator
|
iterator()
Returns an iterator stepping through each element of the array up to the current limit. |
java.lang.Object
|
max(java.util.Comparator comparator)
Returns the maximum element, or null if empty |
java.lang.Object
|
max()
Returns the maximum element, or null if empty assuming that all elements are Comparables |
java.lang.Object
|
min(java.util.Comparator comparator)
Returns the minimum element, or null if empty |
java.lang.Object
|
min()
Returns the minimum element, or null if empty, assuming that all elements are Comparables |
java.lang.Object
|
precumulate(Reducer reducer, java.lang.Object base)
Replaces each element with the cumulation of applying the given reducer to all previous values, and returns the total reduction. |
java.lang.Object
|
reduce(Reducer reducer, java.lang.Object base)
Returns reduction of elements |
ParallelArray
|
removeAll(Predicate selector)
Removes from the array all elements for which the given selector holds. |
ParallelArray
|
removeConsecutiveDuplicates()
Removes consecutive elements that are equal (or null), shifting others leftward, and possibly decreasing size. |
ParallelArray
|
removeNulls()
Removes null elements, shifting others leftward, and possibly decreasing size. |
void
|
removeSlotAt(int index)
|
void
|
removeSlotsAt(int fromIndex, int toIndex)
|
void
|
replaceElementsWith(java.lang.Object[] a)
|
ParallelArray
|
replaceWithGeneratedValue(Generator generator)
Replaces elements with the results of applying the given generator. |
ParallelArray
|
replaceWithMappedIndex(IntToObject op)
Replaces elements with the results of applying the given mapping to their indices. |
ParallelArray
|
replaceWithMappedIndex(IntAndObjectToObject op)
Replaces elements with the results of applying the given mapping to each index and current element value |
ParallelArray
|
replaceWithMapping(Op op)
Replaces elements with the results of applying the given transform to their current values. |
ParallelArray
|
replaceWithMapping(BinaryOp combiner, ParallelArrayWithMapping other)
Replaces elements with results of applying op(thisElement, otherElement) |
ParallelArray
|
replaceWithMapping(BinaryOp combiner, java.lang.Object[] other)
Replaces elements with results of applying op(thisElement, otherElement) |
ParallelArray
|
replaceWithValue(java.lang.Object value)
Replaces elements with the given value. |
void
|
resizeArray(int newCap)
|
int
|
seqIndexOf(java.lang.Object target)
|
int
|
seqLastIndexOf(java.lang.Object target)
|
void
|
set(int i, java.lang.Object x)
Sets the element of the array at the given index to the given value |
void
|
setLimit(int newLimit)
Ensures that the underlying array can be accessed up to the given upper bound, reallocating and copying the underlying array to expand if necessary. |
int
|
size()
Returns the effective size of the underlying array. |
ParallelArray
|
sort(java.util.Comparator comparator)
Sorts the array. |
ParallelArray
|
sort()
Sorts the array, assuming all elements are Comparable. |
SummaryStatistics
|
summary(java.util.Comparator comparator)
Returns summary statistics, using the given comparator to locate minimum and maximum elements. |
SummaryStatistics
|
summary()
Returns summary statistics, assuming that all elements are Comparables |
java.lang.String
|
toString()
Equivalent to asList().toString() |
ParallelArrayWithBounds
|
withBounds(int firstIndex, int upperBound)
Returns an operation prefix that causes a method to operate only on the elements of the array between firstIndex (inclusive) and upperBound (exclusive). |
ParallelArrayWithFilter
|
withFilter(Predicate selector)
Returns an operation prefix that causes a method to operate only on the elements of the array for which the given selector returns true |
ParallelArrayWithFilter
|
withFilter(BinaryPredicate selector, ParallelArrayWithMapping other)
Returns an operation prefix that causes a method to operate only on elements for which the given binary selector returns true |
ParallelArrayWithFilter
|
withIndexedFilter(IntAndObjectPredicate selector)
Returns an operation prefix that causes a method to operate only on elements for which the given indexed selector returns true |
ParallelArrayWithMapping
|
withIndexedMapping(IntAndObjectToObject mapper)
Returns an operation prefix that causes a method to operate on mappings of this array using the given mapper that accepts as arguments an element's current index and value, and produces a new value. |
ParallelArrayWithDoubleMapping
|
withIndexedMapping(IntAndObjectToDouble mapper)
Returns an operation prefix that causes a method to operate on mappings of this array using the given mapper that accepts as arguments an element's current index and value, and produces a new value. |
ParallelArrayWithLongMapping
|
withIndexedMapping(IntAndObjectToLong mapper)
Returns an operation prefix that causes a method to operate on mappings of this array using the given mapper that accepts as arguments an element's current index and value, and produces a new value. |
ParallelArrayWithMapping
|
withMapping(Op op)
Returns an operation prefix that causes a method to operate on mapped elements of the array using the given op. |
ParallelArrayWithDoubleMapping
|
withMapping(ObjectToDouble op)
Returns an operation prefix that causes a method to operate on mapped elements of the array using the given op. |
ParallelArrayWithLongMapping
|
withMapping(ObjectToLong op)
Returns an operation prefix that causes a method to operate on mapped elements of the array using the given op. |
ParallelArrayWithMapping
|
withMapping(BinaryOp combiner, ParallelArrayWithMapping other)
Returns an operation prefix that causes a method to operate on binary mappings of this array and the other array. |
ParallelArrayWithMapping
|
withMapping(ObjectAndDoubleToObject combiner, ParallelDoubleArrayWithDoubleMapping other)
Returns an operation prefix that causes a method to operate on binary mappings of this array and the other array. |
ParallelArrayWithMapping
|
withMapping(ObjectAndLongToObject combiner, ParallelLongArrayWithLongMapping other)
Returns an operation prefix that causes a method to operate on binary mappings of this array and the other array. |
ParallelArrayWithDoubleMapping
|
withMapping(ObjectAndObjectToDouble combiner, ParallelArrayWithMapping other)
Returns an operation prefix that causes a method to operate on binary mappings of this array and the other array. |
ParallelArrayWithDoubleMapping
|
withMapping(ObjectAndDoubleToDouble combiner, ParallelDoubleArrayWithDoubleMapping other)
Returns an operation prefix that causes a method to operate on binary mappings of this array and the other array. |
ParallelArrayWithDoubleMapping
|
withMapping(ObjectAndLongToDouble combiner, ParallelLongArrayWithLongMapping other)
Returns an operation prefix that causes a method to operate on binary mappings of this array and the other array. |
ParallelArrayWithLongMapping
|
withMapping(ObjectAndObjectToLong combiner, ParallelArrayWithMapping other)
Returns an operation prefix that causes a method to operate on binary mappings of this array and the other array. |
ParallelArrayWithLongMapping
|
withMapping(ObjectAndDoubleToLong combiner, ParallelDoubleArrayWithDoubleMapping other)
Returns an operation prefix that causes a method to operate on binary mappings of this array and the other array. |
ParallelArrayWithLongMapping
|
withMapping(ObjectAndLongToLong combiner, ParallelLongArrayWithLongMapping other)
Returns an operation prefix that causes a method to operate on binary mappings of this array and the other array. |
Field Detail |
---|
ParallelArray.AsList listView
Constructor Detail |
---|
protected ParallelArray(ForkJoinPool executor, java.lang.Object[] array, int limit)
executor
- the executorarray
- the arraylimit
- the upper bound limit
ParallelArray(ForkJoinPool executor, java.lang.Object[] array)
Method Detail |
---|
public ParallelArray addAll(java.lang.Object[] other)
other
- the elements to add
public ParallelArray addAll(ParallelArrayWithMapping other)
other
- the elements to add
public ParallelArray all()
public ParallelArray all(java.lang.Class elementType)
elementType
- the type of the elements
public ParallelArray allNonidenticalElements()
public ParallelArray allUniqueElements()
final void appendElement(java.lang.Object e)
public void apply(Procedure procedure)
procedure
- the procedure
public java.util.List asList()
public int binarySearch(java.lang.Object target)
target
- the element to search for
public int binarySearch(java.lang.Object target, java.util.Comparator comparator)
target
- the element to search forcomparator
- the comparator
public static ParallelArray create(int size, java.lang.Class elementType, ForkJoinPool executor)
size
- the array sizeelementType
- the type of the elementsexecutor
- the executor
public static ParallelArray createEmpty(int size, java.lang.Class elementType, ForkJoinPool executor)
size
- the array sizeelementType
- the type of the elementsexecutor
- the executor
public static ParallelArray createFromCopy(java.lang.Object[] source, ForkJoinPool executor)
source
- the source of initial elementsexecutor
- the executor
public static ParallelArray createFromCopy(int size, java.lang.Object[] source, ForkJoinPool executor)
source
- the source of initial elementssize
- the array sizeexecutor
- the executor
public static ParallelArray createUsingHandoff(java.lang.Object[] handoff, ForkJoinPool executor)
handoff
- the arrayexecutor
- the executor
public ParallelArray cumulate(Reducer reducer, java.lang.Object base)
reducer
- the reducerbase
- the result for an empty array
public static ForkJoinPool defaultExecutor()
public java.lang.Object get(int i)
i
- the index
public java.lang.Object[] getArray()
public ForkJoinPool getExecutor()
public boolean hasAllEqualElements(ParallelArrayWithMapping other)
other
- the other array
public boolean hasAllIdenticalElements(ParallelArrayWithMapping other)
other
- the other array
public int indexOf(java.lang.Object target)
target
- the element to search for
final void insertElementAt(int index, java.lang.Object e)
final void insertSlotsAt(int index, int len)
public java.util.Iterator iterator()
public java.lang.Object max(java.util.Comparator comparator)
comparator
- the comparator
public java.lang.Object max()
public java.lang.Object min(java.util.Comparator comparator)
comparator
- the comparator
public java.lang.Object min()
public java.lang.Object precumulate(Reducer reducer, java.lang.Object base)
reducer
- the reducerbase
- the result for an empty array
public java.lang.Object reduce(Reducer reducer, java.lang.Object base)
reducer
- the reducerbase
- the result for an empty array
public ParallelArray removeAll(Predicate selector)
selector
- the selector
public ParallelArray removeConsecutiveDuplicates()
public ParallelArray removeNulls()
final void removeSlotAt(int index)
final void removeSlotsAt(int fromIndex, int toIndex)
final void replaceElementsWith(java.lang.Object[] a)
public ParallelArray replaceWithGeneratedValue(Generator generator)
generator
- the generator
public ParallelArray replaceWithMappedIndex(IntToObject op)
op
- the op
public ParallelArray replaceWithMappedIndex(IntAndObjectToObject op)
op
- the op
public ParallelArray replaceWithMapping(Op op)
op
- the op
public ParallelArray replaceWithMapping(BinaryOp combiner, ParallelArrayWithMapping other)
other
- the other arraycombiner
- the combiner
public ParallelArray replaceWithMapping(BinaryOp combiner, java.lang.Object[] other)
other
- the other arraycombiner
- the combiner
public ParallelArray replaceWithValue(java.lang.Object value)
value
- the value
final void resizeArray(int newCap)
final int seqIndexOf(java.lang.Object target)
final int seqLastIndexOf(java.lang.Object target)
public void set(int i, java.lang.Object x)
i
- the indexx
- the value
public final void setLimit(int newLimit)
newLimit
- the new upper bound
public int size()
public ParallelArray sort(java.util.Comparator comparator)
comparator
- the comparator to use
public ParallelArray sort()
public SummaryStatistics summary(java.util.Comparator comparator)
comparator
- the comparator to use for
locating minimum and maximum elements
public SummaryStatistics summary()
public java.lang.String toString()
public ParallelArrayWithBounds withBounds(int firstIndex, int upperBound)
firstIndex
- the lower bound (inclusive)upperBound
- the upper bound (exclusive)
public ParallelArrayWithFilter withFilter(Predicate selector)
selector
- the selector
public ParallelArrayWithFilter withFilter(BinaryPredicate selector, ParallelArrayWithMapping other)
selector
- the selector
public ParallelArrayWithFilter withIndexedFilter(IntAndObjectPredicate selector)
selector
- the selector
public ParallelArrayWithMapping withIndexedMapping(IntAndObjectToObject mapper)
mapper
- the mapper
public ParallelArrayWithDoubleMapping withIndexedMapping(IntAndObjectToDouble mapper)
mapper
- the mapper
public ParallelArrayWithLongMapping withIndexedMapping(IntAndObjectToLong mapper)
mapper
- the mapper
public ParallelArrayWithMapping withMapping(Op op)
op
- the op
public ParallelArrayWithDoubleMapping withMapping(ObjectToDouble op)
op
- the op
public ParallelArrayWithLongMapping withMapping(ObjectToLong op)
op
- the op
public ParallelArrayWithMapping withMapping(BinaryOp combiner, ParallelArrayWithMapping other)
combiner
- the combinerother
- the other array
public ParallelArrayWithMapping withMapping(ObjectAndDoubleToObject combiner, ParallelDoubleArrayWithDoubleMapping other)
combiner
- the combinerother
- the other array
public ParallelArrayWithMapping withMapping(ObjectAndLongToObject combiner, ParallelLongArrayWithLongMapping other)
combiner
- the combinerother
- the other array
public ParallelArrayWithDoubleMapping withMapping(ObjectAndObjectToDouble combiner, ParallelArrayWithMapping other)
combiner
- the combinerother
- the other array
public ParallelArrayWithDoubleMapping withMapping(ObjectAndDoubleToDouble combiner, ParallelDoubleArrayWithDoubleMapping other)
combiner
- the combinerother
- the other array
public ParallelArrayWithDoubleMapping withMapping(ObjectAndLongToDouble combiner, ParallelLongArrayWithLongMapping other)
combiner
- the combinerother
- the other array
public ParallelArrayWithLongMapping withMapping(ObjectAndObjectToLong combiner, ParallelArrayWithMapping other)
combiner
- the combinerother
- the other array
public ParallelArrayWithLongMapping withMapping(ObjectAndDoubleToLong combiner, ParallelDoubleArrayWithDoubleMapping other)
combiner
- the combinerother
- the other array
public ParallelArrayWithLongMapping withMapping(ObjectAndLongToLong combiner, ParallelLongArrayWithLongMapping other)
combiner
- the combinerother
- the other array
Copyright © 2008–2012 Václav Pech. All Rights Reserved.