1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
package jsr166y; |
8 |
|
|
|
9 |
|
|
/** |
10 |
|
|
* Recursive resultless ForkJoinTasks. This class establishes |
11 |
jsr166 |
1.3 |
* conventions to parameterize resultless actions as {@code Void} |
12 |
|
|
* ForkJoinTasks. Because {@code null} is the only valid value of |
13 |
jsr166 |
1.5 |
* type {@code Void}, methods such as join always return {@code null} |
14 |
dl |
1.1 |
* upon completion. |
15 |
jsr166 |
1.2 |
* |
16 |
dl |
1.1 |
* <p><b>Sample Usages.</b> Here is a sketch of a ForkJoin sort that |
17 |
jsr166 |
1.3 |
* sorts a given {@code long[]} array: |
18 |
dl |
1.1 |
* |
19 |
jsr166 |
1.4 |
* <pre> {@code |
20 |
dl |
1.1 |
* class SortTask extends RecursiveAction { |
21 |
|
|
* final long[] array; final int lo; final int hi; |
22 |
|
|
* SortTask(long[] array, int lo, int hi) { |
23 |
|
|
* this.array = array; this.lo = lo; this.hi = hi; |
24 |
|
|
* } |
25 |
|
|
* protected void compute() { |
26 |
jsr166 |
1.4 |
* if (hi - lo < THRESHOLD) |
27 |
dl |
1.1 |
* sequentiallySort(array, lo, hi); |
28 |
|
|
* else { |
29 |
jsr166 |
1.4 |
* int mid = (lo + hi) >>> 1; |
30 |
dl |
1.1 |
* invokeAll(new SortTask(array, lo, mid), |
31 |
|
|
* new SortTask(array, mid, hi)); |
32 |
|
|
* merge(array, lo, hi); |
33 |
|
|
* } |
34 |
|
|
* } |
35 |
jsr166 |
1.4 |
* }}</pre> |
36 |
dl |
1.1 |
* |
37 |
jsr166 |
1.3 |
* You could then sort anArray by creating {@code new SortTask(anArray, 0, |
38 |
|
|
* anArray.length-1) } and invoking it in a ForkJoinPool. |
39 |
dl |
1.1 |
* As a more concrete simple example, the following task increments |
40 |
|
|
* each element of an array: |
41 |
jsr166 |
1.4 |
* <pre> {@code |
42 |
dl |
1.1 |
* class IncrementTask extends RecursiveAction { |
43 |
|
|
* final long[] array; final int lo; final int hi; |
44 |
|
|
* IncrementTask(long[] array, int lo, int hi) { |
45 |
|
|
* this.array = array; this.lo = lo; this.hi = hi; |
46 |
|
|
* } |
47 |
|
|
* protected void compute() { |
48 |
jsr166 |
1.4 |
* if (hi - lo < THRESHOLD) { |
49 |
|
|
* for (int i = lo; i < hi; ++i) |
50 |
dl |
1.1 |
* array[i]++; |
51 |
|
|
* } |
52 |
|
|
* else { |
53 |
jsr166 |
1.4 |
* int mid = (lo + hi) >>> 1; |
54 |
dl |
1.1 |
* invokeAll(new IncrementTask(array, lo, mid), |
55 |
|
|
* new IncrementTask(array, mid, hi)); |
56 |
|
|
* } |
57 |
|
|
* } |
58 |
jsr166 |
1.4 |
* }}</pre> |
59 |
dl |
1.1 |
* |
60 |
|
|
* <p>The following example illustrates some refinements and idioms |
61 |
|
|
* that may lead to better performance: RecursiveActions need not be |
62 |
|
|
* fully recursive, so long as they maintain the basic |
63 |
|
|
* divide-and-conquer approach. Here is a class that sums the squares |
64 |
|
|
* of each element of a double array, by subdividing out only the |
65 |
|
|
* right-hand-sides of repeated divisions by two, and keeping track of |
66 |
jsr166 |
1.3 |
* them with a chain of {@code next} references. It uses a dynamic |
67 |
|
|
* threshold based on method {@code surplus}, but counterbalances |
68 |
dl |
1.1 |
* potential excess partitioning by directly performing leaf actions |
69 |
|
|
* on unstolen tasks rather than further subdividing. |
70 |
|
|
* |
71 |
jsr166 |
1.4 |
* <pre> {@code |
72 |
dl |
1.1 |
* double sumOfSquares(ForkJoinPool pool, double[] array) { |
73 |
|
|
* int n = array.length; |
74 |
|
|
* int seqSize = 1 + n / (8 * pool.getParallelism()); |
75 |
|
|
* Applyer a = new Applyer(array, 0, n, seqSize, null); |
76 |
|
|
* pool.invoke(a); |
77 |
|
|
* return a.result; |
78 |
|
|
* } |
79 |
|
|
* |
80 |
|
|
* class Applyer extends RecursiveAction { |
81 |
|
|
* final double[] array; |
82 |
|
|
* final int lo, hi, seqSize; |
83 |
|
|
* double result; |
84 |
|
|
* Applyer next; // keeps track of right-hand-side tasks |
85 |
|
|
* Applyer(double[] array, int lo, int hi, int seqSize, Applyer next) { |
86 |
|
|
* this.array = array; this.lo = lo; this.hi = hi; |
87 |
|
|
* this.seqSize = seqSize; this.next = next; |
88 |
|
|
* } |
89 |
|
|
* |
90 |
|
|
* double atLeaf(int l, int r) { |
91 |
|
|
* double sum = 0; |
92 |
jsr166 |
1.4 |
* for (int i = l; i < h; ++i) // perform leftmost base step |
93 |
dl |
1.1 |
* sum += array[i] * array[i]; |
94 |
|
|
* return sum; |
95 |
|
|
* } |
96 |
|
|
* |
97 |
|
|
* protected void compute() { |
98 |
|
|
* int l = lo; |
99 |
|
|
* int h = hi; |
100 |
|
|
* Applyer right = null; |
101 |
jsr166 |
1.4 |
* while (h - l > 1 && |
102 |
|
|
* ForkJoinWorkerThread.getEstimatedSurplusTaskCount() <= 3) { |
103 |
|
|
* int mid = (l + h) >>> 1; |
104 |
dl |
1.1 |
* right = new Applyer(array, mid, h, seqSize, right); |
105 |
|
|
* right.fork(); |
106 |
|
|
* h = mid; |
107 |
|
|
* } |
108 |
|
|
* double sum = atLeaf(l, h); |
109 |
|
|
* while (right != null) { |
110 |
|
|
* if (right.tryUnfork()) // directly calculate if not stolen |
111 |
|
|
* sum += right.atLeaf(right.lo, right.hi); |
112 |
|
|
* else { |
113 |
|
|
* right.helpJoin(); |
114 |
|
|
* sum += right.result; |
115 |
|
|
* } |
116 |
|
|
* right = right.next; |
117 |
|
|
* } |
118 |
|
|
* result = sum; |
119 |
|
|
* } |
120 |
jsr166 |
1.4 |
* }}</pre> |
121 |
jsr166 |
1.6 |
* |
122 |
|
|
* @since 1.7 |
123 |
|
|
* @author Doug Lea |
124 |
dl |
1.1 |
*/ |
125 |
|
|
public abstract class RecursiveAction extends ForkJoinTask<Void> { |
126 |
|
|
|
127 |
|
|
/** |
128 |
jsr166 |
1.2 |
* The main computation performed by this task. |
129 |
dl |
1.1 |
*/ |
130 |
|
|
protected abstract void compute(); |
131 |
|
|
|
132 |
|
|
/** |
133 |
jsr166 |
1.5 |
* Always returns null. |
134 |
dl |
1.1 |
*/ |
135 |
|
|
public final Void getRawResult() { return null; } |
136 |
|
|
|
137 |
|
|
/** |
138 |
|
|
* Requires null completion value. |
139 |
|
|
*/ |
140 |
|
|
protected final void setRawResult(Void mustBeNull) { } |
141 |
|
|
|
142 |
|
|
/** |
143 |
jsr166 |
1.5 |
* Implements execution conventions for RecursiveActions. |
144 |
dl |
1.1 |
*/ |
145 |
|
|
protected final boolean exec() { |
146 |
|
|
compute(); |
147 |
|
|
return true; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
} |