--- jsr166/src/main/java/util/ArrayPrefixHelpers.java 2015/09/20 15:33:12 1.7 +++ jsr166/src/main/java/util/ArrayPrefixHelpers.java 2015/09/21 01:12:41 1.10 @@ -7,6 +7,7 @@ package java.util; import java.util.concurrent.CountedCompleter; +import java.util.concurrent.ForkJoinPool; import java.util.function.BinaryOperator; import java.util.function.DoubleBinaryOperator; import java.util.function.IntBinaryOperator; @@ -72,14 +73,27 @@ class ArrayPrefixHelpers { /** The smallest subtask array partition size to use as threshold */ static final int MIN_PARTITION = 16; - private static final class CumulateTask - extends CountedCompleter { + static final class CumulateTask extends CountedCompleter { final T[] array; final BinaryOperator function; CumulateTask left, right; T in, out; final int lo, hi, origin, fence, threshold; + /** Root task constructor */ + public CumulateTask(CumulateTask parent, + BinaryOperator function, + T[] array, int lo, int hi) { + super(parent); + this.function = function; this.array = array; + this.lo = this.origin = lo; this.hi = this.fence = hi; + int p; + this.threshold = + (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3)) + <= MIN_PARTITION ? MIN_PARTITION : p; + } + + /** Subtask constructor */ CumulateTask(CumulateTask parent, BinaryOperator function, T[] array, int origin, int fence, int threshold, int lo, int hi) { @@ -104,7 +118,7 @@ class ArrayPrefixHelpers { int mid = (l + h) >>> 1; f = rt = t.right = new CumulateTask(t, fn, a, org, fnc, th, mid, h); - t = lt = t.left = + t = lt = t.left = new CumulateTask(t, fn, a, org, fnc, th, l, mid); } else { // possibly refork @@ -211,14 +225,27 @@ class ArrayPrefixHelpers { private static final long serialVersionUID = 5293554502939613543L; } - private static final class LongCumulateTask - extends CountedCompleter { + static final class LongCumulateTask extends CountedCompleter { final long[] array; final LongBinaryOperator function; LongCumulateTask left, right; long in, out; final int lo, hi, origin, fence, threshold; + /** Root task constructor */ + public LongCumulateTask(LongCumulateTask parent, + LongBinaryOperator function, + long[] array, int lo, int hi) { + super(parent); + this.function = function; this.array = array; + this.lo = this.origin = lo; this.hi = this.fence = hi; + int p; + this.threshold = + (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3)) + <= MIN_PARTITION ? MIN_PARTITION : p; + } + + /** Subtask constructor */ LongCumulateTask(LongCumulateTask parent, LongBinaryOperator function, long[] array, int origin, int fence, int threshold, int lo, int hi) { @@ -243,7 +270,7 @@ class ArrayPrefixHelpers { int mid = (l + h) >>> 1; f = rt = t.right = new LongCumulateTask(t, fn, a, org, fnc, th, mid, h); - t = lt = t.left = + t = lt = t.left = new LongCumulateTask(t, fn, a, org, fnc, th, l, mid); } else { // possibly refork @@ -348,14 +375,27 @@ class ArrayPrefixHelpers { private static final long serialVersionUID = -5074099945909284273L; } - private static final class DoubleCumulateTask - extends CountedCompleter { + static final class DoubleCumulateTask extends CountedCompleter { final double[] array; final DoubleBinaryOperator function; DoubleCumulateTask left, right; double in, out; final int lo, hi, origin, fence, threshold; + /** Root task constructor */ + public DoubleCumulateTask(DoubleCumulateTask parent, + DoubleBinaryOperator function, + double[] array, int lo, int hi) { + super(parent); + this.function = function; this.array = array; + this.lo = this.origin = lo; this.hi = this.fence = hi; + int p; + this.threshold = + (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3)) + <= MIN_PARTITION ? MIN_PARTITION : p; + } + + /** Subtask constructor */ DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function, double[] array, int origin, int fence, int threshold, int lo, int hi) { @@ -380,7 +420,7 @@ class ArrayPrefixHelpers { int mid = (l + h) >>> 1; f = rt = t.right = new DoubleCumulateTask(t, fn, a, org, fnc, th, mid, h); - t = lt = t.left = + t = lt = t.left = new DoubleCumulateTask(t, fn, a, org, fnc, th, l, mid); } else { // possibly refork @@ -485,14 +525,27 @@ class ArrayPrefixHelpers { private static final long serialVersionUID = -586947823794232033L; } - private static final class IntCumulateTask - extends CountedCompleter { + static final class IntCumulateTask extends CountedCompleter { final int[] array; final IntBinaryOperator function; IntCumulateTask left, right; int in, out; final int lo, hi, origin, fence, threshold; + /** Root task constructor */ + public IntCumulateTask(IntCumulateTask parent, + IntBinaryOperator function, + int[] array, int lo, int hi) { + super(parent); + this.function = function; this.array = array; + this.lo = this.origin = lo; this.hi = this.fence = hi; + int p; + this.threshold = + (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3)) + <= MIN_PARTITION ? MIN_PARTITION : p; + } + + /** Subtask constructor */ IntCumulateTask(IntCumulateTask parent, IntBinaryOperator function, int[] array, int origin, int fence, int threshold, int lo, int hi) { @@ -517,7 +570,7 @@ class ArrayPrefixHelpers { int mid = (l + h) >>> 1; f = rt = t.right = new IntCumulateTask(t, fn, a, org, fnc, th, mid, h); - t = lt = t.left = + t = lt = t.left = new IntCumulateTask(t, fn, a, org, fnc, th, l, mid); } else { // possibly refork @@ -621,5 +674,4 @@ class ArrayPrefixHelpers { } private static final long serialVersionUID = 3731755594596840961L; } - }