7 |
|
package java.util; |
8 |
|
|
9 |
|
import java.util.concurrent.CountedCompleter; |
10 |
+ |
import java.util.concurrent.ForkJoinPool; |
11 |
|
import java.util.function.BinaryOperator; |
12 |
|
import java.util.function.DoubleBinaryOperator; |
13 |
|
import java.util.function.IntBinaryOperator; |
73 |
|
/** The smallest subtask array partition size to use as threshold */ |
74 |
|
static final int MIN_PARTITION = 16; |
75 |
|
|
76 |
< |
private static final class CumulateTask<T> |
76 |
< |
extends CountedCompleter<Void> { |
76 |
> |
static final class CumulateTask<T> extends CountedCompleter<Void> { |
77 |
|
final T[] array; |
78 |
|
final BinaryOperator<T> function; |
79 |
|
CumulateTask<T> left, right; |
80 |
|
T in, out; |
81 |
|
final int lo, hi, origin, fence, threshold; |
82 |
|
|
83 |
+ |
/** Root task constructor */ |
84 |
+ |
public CumulateTask(CumulateTask<T> parent, |
85 |
+ |
BinaryOperator<T> function, |
86 |
+ |
T[] array, int lo, int hi) { |
87 |
+ |
super(parent); |
88 |
+ |
this.function = function; this.array = array; |
89 |
+ |
this.lo = this.origin = lo; this.hi = this.fence = hi; |
90 |
+ |
int p; |
91 |
+ |
this.threshold = |
92 |
+ |
(p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3)) |
93 |
+ |
<= MIN_PARTITION ? MIN_PARTITION : p; |
94 |
+ |
} |
95 |
+ |
|
96 |
+ |
/** Subtask constructor */ |
97 |
|
CumulateTask(CumulateTask<T> parent, BinaryOperator<T> function, |
98 |
|
T[] array, int origin, int fence, int threshold, |
99 |
|
int lo, int hi) { |
118 |
|
int mid = (l + h) >>> 1; |
119 |
|
f = rt = t.right = |
120 |
|
new CumulateTask<T>(t, fn, a, org, fnc, th, mid, h); |
121 |
< |
t = lt = t.left = |
121 |
> |
t = lt = t.left = |
122 |
|
new CumulateTask<T>(t, fn, a, org, fnc, th, l, mid); |
123 |
|
} |
124 |
|
else { // possibly refork |
225 |
|
private static final long serialVersionUID = 5293554502939613543L; |
226 |
|
} |
227 |
|
|
228 |
< |
private static final class LongCumulateTask |
215 |
< |
extends CountedCompleter<Void> { |
228 |
> |
static final class LongCumulateTask extends CountedCompleter<Void> { |
229 |
|
final long[] array; |
230 |
|
final LongBinaryOperator function; |
231 |
|
LongCumulateTask left, right; |
232 |
|
long in, out; |
233 |
|
final int lo, hi, origin, fence, threshold; |
234 |
|
|
235 |
+ |
/** Root task constructor */ |
236 |
+ |
public LongCumulateTask(LongCumulateTask parent, |
237 |
+ |
LongBinaryOperator function, |
238 |
+ |
long[] array, int lo, int hi) { |
239 |
+ |
super(parent); |
240 |
+ |
this.function = function; this.array = array; |
241 |
+ |
this.lo = this.origin = lo; this.hi = this.fence = hi; |
242 |
+ |
int p; |
243 |
+ |
this.threshold = |
244 |
+ |
(p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3)) |
245 |
+ |
<= MIN_PARTITION ? MIN_PARTITION : p; |
246 |
+ |
} |
247 |
+ |
|
248 |
+ |
/** Subtask constructor */ |
249 |
|
LongCumulateTask(LongCumulateTask parent, LongBinaryOperator function, |
250 |
|
long[] array, int origin, int fence, int threshold, |
251 |
|
int lo, int hi) { |
270 |
|
int mid = (l + h) >>> 1; |
271 |
|
f = rt = t.right = |
272 |
|
new LongCumulateTask(t, fn, a, org, fnc, th, mid, h); |
273 |
< |
t = lt = t.left = |
273 |
> |
t = lt = t.left = |
274 |
|
new LongCumulateTask(t, fn, a, org, fnc, th, l, mid); |
275 |
|
} |
276 |
|
else { // possibly refork |
375 |
|
private static final long serialVersionUID = -5074099945909284273L; |
376 |
|
} |
377 |
|
|
378 |
< |
private static final class DoubleCumulateTask |
352 |
< |
extends CountedCompleter<Void> { |
378 |
> |
static final class DoubleCumulateTask extends CountedCompleter<Void> { |
379 |
|
final double[] array; |
380 |
|
final DoubleBinaryOperator function; |
381 |
|
DoubleCumulateTask left, right; |
382 |
|
double in, out; |
383 |
|
final int lo, hi, origin, fence, threshold; |
384 |
|
|
385 |
+ |
/** Root task constructor */ |
386 |
+ |
public DoubleCumulateTask(DoubleCumulateTask parent, |
387 |
+ |
DoubleBinaryOperator function, |
388 |
+ |
double[] array, int lo, int hi) { |
389 |
+ |
super(parent); |
390 |
+ |
this.function = function; this.array = array; |
391 |
+ |
this.lo = this.origin = lo; this.hi = this.fence = hi; |
392 |
+ |
int p; |
393 |
+ |
this.threshold = |
394 |
+ |
(p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3)) |
395 |
+ |
<= MIN_PARTITION ? MIN_PARTITION : p; |
396 |
+ |
} |
397 |
+ |
|
398 |
+ |
/** Subtask constructor */ |
399 |
|
DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function, |
400 |
|
double[] array, int origin, int fence, int threshold, |
401 |
|
int lo, int hi) { |
420 |
|
int mid = (l + h) >>> 1; |
421 |
|
f = rt = t.right = |
422 |
|
new DoubleCumulateTask(t, fn, a, org, fnc, th, mid, h); |
423 |
< |
t = lt = t.left = |
423 |
> |
t = lt = t.left = |
424 |
|
new DoubleCumulateTask(t, fn, a, org, fnc, th, l, mid); |
425 |
|
} |
426 |
|
else { // possibly refork |
525 |
|
private static final long serialVersionUID = -586947823794232033L; |
526 |
|
} |
527 |
|
|
528 |
< |
private static final class IntCumulateTask |
489 |
< |
extends CountedCompleter<Void> { |
528 |
> |
static final class IntCumulateTask extends CountedCompleter<Void> { |
529 |
|
final int[] array; |
530 |
|
final IntBinaryOperator function; |
531 |
|
IntCumulateTask left, right; |
532 |
|
int in, out; |
533 |
|
final int lo, hi, origin, fence, threshold; |
534 |
|
|
535 |
+ |
/** Root task constructor */ |
536 |
+ |
public IntCumulateTask(IntCumulateTask parent, |
537 |
+ |
IntBinaryOperator function, |
538 |
+ |
int[] array, int lo, int hi) { |
539 |
+ |
super(parent); |
540 |
+ |
this.function = function; this.array = array; |
541 |
+ |
this.lo = this.origin = lo; this.hi = this.fence = hi; |
542 |
+ |
int p; |
543 |
+ |
this.threshold = |
544 |
+ |
(p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3)) |
545 |
+ |
<= MIN_PARTITION ? MIN_PARTITION : p; |
546 |
+ |
} |
547 |
+ |
|
548 |
+ |
/** Subtask constructor */ |
549 |
|
IntCumulateTask(IntCumulateTask parent, IntBinaryOperator function, |
550 |
|
int[] array, int origin, int fence, int threshold, |
551 |
|
int lo, int hi) { |
570 |
|
int mid = (l + h) >>> 1; |
571 |
|
f = rt = t.right = |
572 |
|
new IntCumulateTask(t, fn, a, org, fnc, th, mid, h); |
573 |
< |
t = lt = t.left = |
573 |
> |
t = lt = t.left = |
574 |
|
new IntCumulateTask(t, fn, a, org, fnc, th, l, mid); |
575 |
|
} |
576 |
|
else { // possibly refork |
674 |
|
} |
675 |
|
private static final long serialVersionUID = 3731755594596840961L; |
676 |
|
} |
624 |
– |
|
677 |
|
} |