86 *
87 * To better exploit locality and reduce overhead, the compute
88 * method loops starting with the current task, moving if possible
89 * to one of its subtasks rather than forking.
90 *
91 * As usual for this sort of utility, there are 4 versions, that
92 * are simple copy/paste/adapt variants of each other. (The
93 * double and int versions differ from long version solely by
94 * replacing "long" (with case-matching)).
95 */
96
97 // see above
98 static final int CUMULATE = 1;
99 static final int SUMMED = 2;
100 static final int FINISHED = 4;
101
102 /** The smallest subtask array partition size to use as threshold */
103 static final int MIN_PARTITION = 16;
104
105 static final class CumulateTask<T> extends CountedCompleter<Void> {
106 final T[] array;
107 final BinaryOperator<T> function;
108 CumulateTask<T> left, right;
109 T in, out;
110 final int lo, hi, origin, fence, threshold;
111
112 /** Root task constructor */
113 public CumulateTask(CumulateTask<T> parent,
114 BinaryOperator<T> function,
115 T[] array, int lo, int hi) {
116 super(parent);
117 this.function = function; this.array = array;
118 this.lo = this.origin = lo; this.hi = this.fence = hi;
119 int p;
120 this.threshold =
121 (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
122 <= MIN_PARTITION ? MIN_PARTITION : p;
123 }
124
125 /** Subtask constructor */
126 CumulateTask(CumulateTask<T> parent, BinaryOperator<T> function,
127 T[] array, int origin, int fence, int threshold,
128 int lo, int hi) {
129 super(parent);
240 if ((nextState = b|state|refork) == b ||
241 par.compareAndSetPendingCount(b, nextState)) {
242 state = SUMMED; // drop finished
243 t = par;
244 if (refork != 0)
245 par.fork();
246 }
247 }
248 else if (par.compareAndSetPendingCount(b, b|state))
249 break outer; // sib not ready
250 }
251 }
252 }
253 }
254 @java.io.Serial
255 private static final long serialVersionUID = 5293554502939613543L;
256 }
257
258 static final class LongCumulateTask extends CountedCompleter<Void> {
259 final long[] array;
260 final LongBinaryOperator function;
261 LongCumulateTask left, right;
262 long in, out;
263 final int lo, hi, origin, fence, threshold;
264
265 /** Root task constructor */
266 public LongCumulateTask(LongCumulateTask parent,
267 LongBinaryOperator function,
268 long[] array, int lo, int hi) {
269 super(parent);
270 this.function = function; this.array = array;
271 this.lo = this.origin = lo; this.hi = this.fence = hi;
272 int p;
273 this.threshold =
274 (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
275 <= MIN_PARTITION ? MIN_PARTITION : p;
276 }
277
278 /** Subtask constructor */
279 LongCumulateTask(LongCumulateTask parent, LongBinaryOperator function,
391 if ((nextState = b|state|refork) == b ||
392 par.compareAndSetPendingCount(b, nextState)) {
393 state = SUMMED; // drop finished
394 t = par;
395 if (refork != 0)
396 par.fork();
397 }
398 }
399 else if (par.compareAndSetPendingCount(b, b|state))
400 break outer; // sib not ready
401 }
402 }
403 }
404 }
405 @java.io.Serial
406 private static final long serialVersionUID = -5074099945909284273L;
407 }
408
409 static final class DoubleCumulateTask extends CountedCompleter<Void> {
410 final double[] array;
411 final DoubleBinaryOperator function;
412 DoubleCumulateTask left, right;
413 double in, out;
414 final int lo, hi, origin, fence, threshold;
415
416 /** Root task constructor */
417 public DoubleCumulateTask(DoubleCumulateTask parent,
418 DoubleBinaryOperator function,
419 double[] array, int lo, int hi) {
420 super(parent);
421 this.function = function; this.array = array;
422 this.lo = this.origin = lo; this.hi = this.fence = hi;
423 int p;
424 this.threshold =
425 (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
426 <= MIN_PARTITION ? MIN_PARTITION : p;
427 }
428
429 /** Subtask constructor */
430 DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function,
542 if ((nextState = b|state|refork) == b ||
543 par.compareAndSetPendingCount(b, nextState)) {
544 state = SUMMED; // drop finished
545 t = par;
546 if (refork != 0)
547 par.fork();
548 }
549 }
550 else if (par.compareAndSetPendingCount(b, b|state))
551 break outer; // sib not ready
552 }
553 }
554 }
555 }
556 @java.io.Serial
557 private static final long serialVersionUID = -586947823794232033L;
558 }
559
560 static final class IntCumulateTask extends CountedCompleter<Void> {
561 final int[] array;
562 final IntBinaryOperator function;
563 IntCumulateTask left, right;
564 int in, out;
565 final int lo, hi, origin, fence, threshold;
566
567 /** Root task constructor */
568 public IntCumulateTask(IntCumulateTask parent,
569 IntBinaryOperator function,
570 int[] array, int lo, int hi) {
571 super(parent);
572 this.function = function; this.array = array;
573 this.lo = this.origin = lo; this.hi = this.fence = hi;
574 int p;
575 this.threshold =
576 (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
577 <= MIN_PARTITION ? MIN_PARTITION : p;
578 }
579
580 /** Subtask constructor */
581 IntCumulateTask(IntCumulateTask parent, IntBinaryOperator function,
|
86 *
87 * To better exploit locality and reduce overhead, the compute
88 * method loops starting with the current task, moving if possible
89 * to one of its subtasks rather than forking.
90 *
91 * As usual for this sort of utility, there are 4 versions, that
92 * are simple copy/paste/adapt variants of each other. (The
93 * double and int versions differ from long version solely by
94 * replacing "long" (with case-matching)).
95 */
96
97 // see above
98 static final int CUMULATE = 1;
99 static final int SUMMED = 2;
100 static final int FINISHED = 4;
101
102 /** The smallest subtask array partition size to use as threshold */
103 static final int MIN_PARTITION = 16;
104
105 static final class CumulateTask<T> extends CountedCompleter<Void> {
106 @SuppressWarnings("serial") // Not statically typed as Serializable
107 final T[] array;
108 @SuppressWarnings("serial") // Not statically typed as Serializable
109 final BinaryOperator<T> function;
110 CumulateTask<T> left, right;
111 @SuppressWarnings("serial") // Not statically typed as Serializable
112 T in;
113 @SuppressWarnings("serial") // Not statically typed as Serializable
114 T out;
115 final int lo, hi, origin, fence, threshold;
116
117 /** Root task constructor */
118 public CumulateTask(CumulateTask<T> parent,
119 BinaryOperator<T> function,
120 T[] array, int lo, int hi) {
121 super(parent);
122 this.function = function; this.array = array;
123 this.lo = this.origin = lo; this.hi = this.fence = hi;
124 int p;
125 this.threshold =
126 (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
127 <= MIN_PARTITION ? MIN_PARTITION : p;
128 }
129
130 /** Subtask constructor */
131 CumulateTask(CumulateTask<T> parent, BinaryOperator<T> function,
132 T[] array, int origin, int fence, int threshold,
133 int lo, int hi) {
134 super(parent);
245 if ((nextState = b|state|refork) == b ||
246 par.compareAndSetPendingCount(b, nextState)) {
247 state = SUMMED; // drop finished
248 t = par;
249 if (refork != 0)
250 par.fork();
251 }
252 }
253 else if (par.compareAndSetPendingCount(b, b|state))
254 break outer; // sib not ready
255 }
256 }
257 }
258 }
259 @java.io.Serial
260 private static final long serialVersionUID = 5293554502939613543L;
261 }
262
263 static final class LongCumulateTask extends CountedCompleter<Void> {
264 final long[] array;
265 @SuppressWarnings("serial") // Not statically typed as Serializable
266 final LongBinaryOperator function;
267 LongCumulateTask left, right;
268 long in, out;
269 final int lo, hi, origin, fence, threshold;
270
271 /** Root task constructor */
272 public LongCumulateTask(LongCumulateTask parent,
273 LongBinaryOperator function,
274 long[] array, int lo, int hi) {
275 super(parent);
276 this.function = function; this.array = array;
277 this.lo = this.origin = lo; this.hi = this.fence = hi;
278 int p;
279 this.threshold =
280 (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
281 <= MIN_PARTITION ? MIN_PARTITION : p;
282 }
283
284 /** Subtask constructor */
285 LongCumulateTask(LongCumulateTask parent, LongBinaryOperator function,
397 if ((nextState = b|state|refork) == b ||
398 par.compareAndSetPendingCount(b, nextState)) {
399 state = SUMMED; // drop finished
400 t = par;
401 if (refork != 0)
402 par.fork();
403 }
404 }
405 else if (par.compareAndSetPendingCount(b, b|state))
406 break outer; // sib not ready
407 }
408 }
409 }
410 }
411 @java.io.Serial
412 private static final long serialVersionUID = -5074099945909284273L;
413 }
414
415 static final class DoubleCumulateTask extends CountedCompleter<Void> {
416 final double[] array;
417 @SuppressWarnings("serial") // Not statically typed as Serializable
418 final DoubleBinaryOperator function;
419 DoubleCumulateTask left, right;
420 double in, out;
421 final int lo, hi, origin, fence, threshold;
422
423 /** Root task constructor */
424 public DoubleCumulateTask(DoubleCumulateTask parent,
425 DoubleBinaryOperator function,
426 double[] array, int lo, int hi) {
427 super(parent);
428 this.function = function; this.array = array;
429 this.lo = this.origin = lo; this.hi = this.fence = hi;
430 int p;
431 this.threshold =
432 (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
433 <= MIN_PARTITION ? MIN_PARTITION : p;
434 }
435
436 /** Subtask constructor */
437 DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function,
549 if ((nextState = b|state|refork) == b ||
550 par.compareAndSetPendingCount(b, nextState)) {
551 state = SUMMED; // drop finished
552 t = par;
553 if (refork != 0)
554 par.fork();
555 }
556 }
557 else if (par.compareAndSetPendingCount(b, b|state))
558 break outer; // sib not ready
559 }
560 }
561 }
562 }
563 @java.io.Serial
564 private static final long serialVersionUID = -586947823794232033L;
565 }
566
567 static final class IntCumulateTask extends CountedCompleter<Void> {
568 final int[] array;
569 @SuppressWarnings("serial") // Not statically typed as Serializable
570 final IntBinaryOperator function;
571 IntCumulateTask left, right;
572 int in, out;
573 final int lo, hi, origin, fence, threshold;
574
575 /** Root task constructor */
576 public IntCumulateTask(IntCumulateTask parent,
577 IntBinaryOperator function,
578 int[] array, int lo, int hi) {
579 super(parent);
580 this.function = function; this.array = array;
581 this.lo = this.origin = lo; this.hi = this.fence = hi;
582 int p;
583 this.threshold =
584 (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
585 <= MIN_PARTITION ? MIN_PARTITION : p;
586 }
587
588 /** Subtask constructor */
589 IntCumulateTask(IntCumulateTask parent, IntBinaryOperator function,
|