Spliterator that describes the portion
of the input associated with the subtree rooted at this task.
Tasks may be leaf nodes (which will traverse the elements of
the Spliterator) or internal nodes (which split the
Spliterator into multiple child tasks).
Implementation Note
This class is based on CountedCompleter, a form of fork-join task
where each task has a semaphore-like count of uncompleted children, and the
task is implicitly completed and notified when its last child completes.
Internal node tasks will likely override the onCompletion method from
CountedCompleter to merge the results from child tasks into the
current task's result.
Splitting and setting up the child task links is done by compute()
for internal nodes. At compute() time for leaf nodes, it is
guaranteed that the parent's child-related fields (including sibling links
for the parent's children) will be set up for all children.
For example, a task that performs a reduce would override doLeaf()
to perform a reduction on that leaf node's chunk using the
Spliterator, and override onCompletion() to merge the results
of the child tasks for internal nodes:
protected S doLeaf() {
spliterator.forEach(...);
return localReductionResult;
}
public void onCompletion(CountedCompleter caller) {
if (!isLeaf()) {
ReduceTask<P_IN, P_OUT, T, R> child = children;
R result = child.getLocalResult();
child = child.nextSibling;
for (; child != null; child = child.nextSibling)
result = combine(result, child.getLocalResult());
setLocalResult(result);
}
}
Serialization is not supported as there is no intention to serialize tasks managed by stream ops.
| Modifier and Type | Field and Description |
|---|---|
| protected final PipelineHelper | helper
The pipeline helper, common to all tasks in a computation |
| private static final int | |
| protected K | leftChild
The left child. |
| private R | localResult
The result of this node, if completed |
| protected K | rightChild
The right child. |
| protected Spliterator | spliterator
The spliterator for the portion of the input associated with the subtree rooted at this task |
| protected long | targetSize
Target leaf size, common to all tasks in a computation |
| Access | Constructor and Description |
|---|---|
| protected | AbstractTask(PipelineHelper<P_OUT>
The helper, Spliterator<P_IN> PipelineHelper describing the stream pipeline
up to this operationThe spliterator)Spliterator describing the source for this
pipelineConstructor for root nodes. |
| protected | AbstractTask(K
this node's parent task parent, Spliterator<P_IN> Spliterator describing the subtree rooted at
this node, obtained by splitting the parent SpliteratorConstructor for non-root nodes. |
| Modifier and Type | Method and Description |
|---|---|
| public void | compute()
Implements abstract java. Decides whether or not to split a task further or compute it directly. |
| protected abstract R | |
| public static int | |
| protected R | Returns: local result for this node previously stored withsetLocalResultRetrieves a result previously stored with |
| protected K | Returns: the parent of this task, or null if this task is the rootReturns the parent of this task, or null if this task is the root |
| public R | Returns: local result for this node previously stored withsetLocalResultOverrides java. Returns the local result, if any. |
| protected final long | getTargetSize(long sizeEstimate)
Returns the targetSize, initializing it via the supplied size estimate if not already initialized. |
| protected boolean | |
| protected boolean | Returns: true if this node is a "leftmost" nodeReturns whether this node is a "leftmost" node -- whether the path from the root to this node involves only traversing leftmost child links. |
| protected boolean | |
| protected abstract K | Returns: newly constructed child nodeSpliterator describing the subtree rooted at
this node, obtained by splitting the parent SpliteratorConstructs a new node of type T whose parent is the receiver; must call the AbstractTask(T, Spliterator) constructor with the receiver and the provided Spliterator. |
| public void | onCompletion(CountedCompleter<?> caller)
Overrides java. Performs an action when method |
| protected void | setLocalResult(R
local result for this node localResult)Associates the result with the task, can be retrieved with
|
| protected void | setRawResult(R
must be null, or an exception is thrown (this is a safety
tripwire to detect when result)setRawResult() is being used
instead of setLocalResult()Overrides java. Does nothing; instead, subclasses should use
|
| public static long | Returns: suggested target leaf sizeReturns a suggested target leaf size based on the initial size estimate. |
| helper | back to summary |
|---|---|
| protected final PipelineHelper<P_OUT> helper The pipeline helper, common to all tasks in a computation | |
| LEAF_TARGET | back to summary |
|---|---|
| private static final int LEAF_TARGET | |
| leftChild | back to summary |
|---|---|
| protected K leftChild The left child. null if no children if non-null rightChild is non-null | |
| localResult | back to summary |
|---|---|
| private R localResult The result of this node, if completed | |
| rightChild | back to summary |
|---|---|
| protected K rightChild The right child. null if no children if non-null leftChild is non-null | |
| spliterator | back to summary |
|---|---|
| protected Spliterator<P_IN> spliterator The spliterator for the portion of the input associated with the subtree rooted at this task | |
| targetSize | back to summary |
|---|---|
| protected long targetSize Target leaf size, common to all tasks in a computation | |
| AbstractTask | back to summary |
|---|---|
| protected AbstractTask(PipelineHelper<P_OUT> helper, Spliterator<P_IN> spliterator) Constructor for root nodes.
| |
| AbstractTask | back to summary |
|---|---|
| protected AbstractTask(K parent, Spliterator<P_IN> spliterator) Constructor for non-root nodes.
| |
| compute | back to summary |
|---|---|
| public void compute() Implements abstract java. Decides whether or not to split a task further or compute it
directly. If computing directly, calls The method is structured to conserve resources across a range of uses. The loop continues with one of the child tasks when split, to avoid deep recursion. To cope with spliterators that may be systematically biased toward left-heavy or right-heavy splits, we alternate which child is forked versus continued in the loop.
| |
| doLeaf | back to summary |
|---|---|
| protected abstract R doLeaf() Computes the result associated with a leaf node. Will be called by
| |
| getLeafTarget | back to summary |
|---|---|
| public static int getLeafTarget() Default target of leaf tasks for parallel decomposition. To allow load balancing, we over-partition, currently to approximately four tasks per processor, which enables others to help out if leaf tasks are uneven or some processors are otherwise busy. | |
| getLocalResult | back to summary |
|---|---|
| protected R getLocalResult() Retrieves a result previously stored with
| |
| getParent | back to summary |
|---|---|
| protected K getParent() Returns the parent of this task, or null if this task is the root
| |
| getRawResult | back to summary |
|---|---|
| public R getRawResult() Overrides java. Returns the local result, if any. Subclasses should use
| |
| getTargetSize | back to summary |
|---|---|
| protected final long getTargetSize(long sizeEstimate) Returns the targetSize, initializing it via the supplied size estimate if not already initialized. | |
| isLeaf | back to summary |
|---|---|
| protected boolean isLeaf() Indicates whether this task is a leaf node. (Only valid after
| |
| isLeftmostNode | back to summary |
|---|---|
| protected boolean isLeftmostNode() Returns whether this node is a "leftmost" node -- whether the path from the root to this node involves only traversing leftmost child links. For a leaf node, this means it is the first leaf node in the encounter order.
| |
| isRoot | back to summary |
|---|---|
| protected boolean isRoot() Indicates whether this task is the root node
| |
| makeChild | back to summary |
|---|---|
| protected abstract K makeChild(Spliterator<P_IN> spliterator) Constructs a new node of type T whose parent is the receiver; must call the AbstractTask(T, Spliterator) constructor with the receiver and the provided Spliterator.
| |
| onCompletion | back to summary |
|---|---|
| public void onCompletion(CountedCompleter<?> caller) Overrides java. Doc from java. Performs an action when method Implementation Note Clears spliterator and children fields. Overriders MUST call
| |
| setLocalResult | back to summary |
|---|---|
| protected void setLocalResult(R localResult) Associates the result with the task, can be retrieved with
| |
| setRawResult | back to summary |
|---|---|
| protected void setRawResult(R result) Overrides java. Does nothing; instead, subclasses should use
| |
| suggestTargetSize | back to summary |
|---|---|
| public static long suggestTargetSize(long sizeEstimate) Returns a suggested target leaf size based on the initial size estimate.
| |