author | lana |
Thu, 26 Dec 2013 12:04:16 -0800 | |
changeset 23010 | 6dadb192ad81 |
parent 22994 | 0390009c367f |
permissions | -rw-r--r-- |
18555
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
22994
diff
changeset
|
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
18555
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
4 |
* |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
10 |
* |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
15 |
* accompanied this code). |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
16 |
* |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
20 |
* |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
23 |
* questions. |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
24 |
*/ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
25 |
package java.util; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
26 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
27 |
/* |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
28 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
29 |
* Expert Group and released to the public domain, as explained at |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
30 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
31 |
*/ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
32 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
33 |
import java.util.concurrent.ForkJoinPool; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
34 |
import java.util.concurrent.CountedCompleter; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
35 |
import java.util.function.BinaryOperator; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
36 |
import java.util.function.IntBinaryOperator; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
37 |
import java.util.function.LongBinaryOperator; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
38 |
import java.util.function.DoubleBinaryOperator; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
39 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
40 |
/** |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
41 |
* ForkJoin tasks to perform Arrays.parallelPrefix operations. |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
42 |
* |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
43 |
* @author Doug Lea |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
44 |
* @since 1.8 |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
45 |
*/ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
46 |
class ArrayPrefixHelpers { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
47 |
private ArrayPrefixHelpers() {}; // non-instantiable |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
48 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
49 |
/* |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
50 |
* Parallel prefix (aka cumulate, scan) task classes |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
51 |
* are based loosely on Guy Blelloch's original |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
52 |
* algorithm (http://www.cs.cmu.edu/~scandal/alg/scan.html): |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
53 |
* Keep dividing by two to threshold segment size, and then: |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
54 |
* Pass 1: Create tree of partial sums for each segment |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
55 |
* Pass 2: For each segment, cumulate with offset of left sibling |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
56 |
* |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
57 |
* This version improves performance within FJ framework mainly by |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
58 |
* allowing the second pass of ready left-hand sides to proceed |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
59 |
* even if some right-hand side first passes are still executing. |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
60 |
* It also combines first and second pass for leftmost segment, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
61 |
* and skips the first pass for rightmost segment (whose result is |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
62 |
* not needed for second pass). It similarly manages to avoid |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
63 |
* requiring that users supply an identity basis for accumulations |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
64 |
* by tracking those segments/subtasks for which the first |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
65 |
* existing element is used as base. |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
66 |
* |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
67 |
* Managing this relies on ORing some bits in the pendingCount for |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
68 |
* phases/states: CUMULATE, SUMMED, and FINISHED. CUMULATE is the |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
69 |
* main phase bit. When false, segments compute only their sum. |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
70 |
* When true, they cumulate array elements. CUMULATE is set at |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
71 |
* root at beginning of second pass and then propagated down. But |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
72 |
* it may also be set earlier for subtrees with lo==0 (the left |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
73 |
* spine of tree). SUMMED is a one bit join count. For leafs, it |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
74 |
* is set when summed. For internal nodes, it becomes true when |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
75 |
* one child is summed. When the second child finishes summing, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
76 |
* we then moves up tree to trigger the cumulate phase. FINISHED |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
77 |
* is also a one bit join count. For leafs, it is set when |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
78 |
* cumulated. For internal nodes, it becomes true when one child |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
79 |
* is cumulated. When the second child finishes cumulating, it |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
80 |
* then moves up tree, completing at the root. |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
81 |
* |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
82 |
* To better exploit locality and reduce overhead, the compute |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
83 |
* method loops starting with the current task, moving if possible |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
84 |
* to one of its subtasks rather than forking. |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
85 |
* |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
86 |
* As usual for this sort of utility, there are 4 versions, that |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
87 |
* are simple copy/paste/adapt variants of each other. (The |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
88 |
* double and int versions differ from long version soley by |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
89 |
* replacing "long" (with case-matching)). |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
90 |
*/ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
91 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
92 |
// see above |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
93 |
static final int CUMULATE = 1; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
94 |
static final int SUMMED = 2; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
95 |
static final int FINISHED = 4; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
96 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
97 |
/** The smallest subtask array partition size to use as threshold */ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
98 |
static final int MIN_PARTITION = 16; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
99 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
100 |
static final class CumulateTask<T> extends CountedCompleter<Void> { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
101 |
final T[] array; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
102 |
final BinaryOperator<T> function; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
103 |
CumulateTask<T> left, right; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
104 |
T in, out; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
105 |
final int lo, hi, origin, fence, threshold; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
106 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
107 |
/** Root task constructor */ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
108 |
public CumulateTask(CumulateTask<T> parent, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
109 |
BinaryOperator<T> function, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
110 |
T[] array, int lo, int hi) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
111 |
super(parent); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
112 |
this.function = function; this.array = array; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
113 |
this.lo = this.origin = lo; this.hi = this.fence = hi; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
114 |
int p; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
115 |
this.threshold = |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
116 |
(p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3)) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
117 |
<= MIN_PARTITION ? MIN_PARTITION : p; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
118 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
119 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
120 |
/** Subtask constructor */ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
121 |
CumulateTask(CumulateTask<T> parent, BinaryOperator<T> function, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
122 |
T[] array, int origin, int fence, int threshold, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
123 |
int lo, int hi) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
124 |
super(parent); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
125 |
this.function = function; this.array = array; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
126 |
this.origin = origin; this.fence = fence; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
127 |
this.threshold = threshold; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
128 |
this.lo = lo; this.hi = hi; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
129 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
130 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
131 |
public final void compute() { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
132 |
final BinaryOperator<T> fn; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
133 |
final T[] a; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
134 |
if ((fn = this.function) == null || (a = this.array) == null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
135 |
throw new NullPointerException(); // hoist checks |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
136 |
int th = threshold, org = origin, fnc = fence, l, h; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
137 |
CumulateTask<T> t = this; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
138 |
outer: while ((l = t.lo) >= 0 && (h = t.hi) <= a.length) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
139 |
if (h - l > th) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
140 |
CumulateTask<T> lt = t.left, rt = t.right, f; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
141 |
if (lt == null) { // first pass |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
142 |
int mid = (l + h) >>> 1; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
143 |
f = rt = t.right = |
22994
0390009c367f
8022852: Fix serial javac warnings in ArrayPrefixHelpers
chegar
parents:
22078
diff
changeset
|
144 |
new CumulateTask<T>(t, fn, a, org, fnc, th, mid, h); |
18555
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
145 |
t = lt = t.left = |
22994
0390009c367f
8022852: Fix serial javac warnings in ArrayPrefixHelpers
chegar
parents:
22078
diff
changeset
|
146 |
new CumulateTask<T>(t, fn, a, org, fnc, th, l, mid); |
18555
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
147 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
148 |
else { // possibly refork |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
149 |
T pin = t.in; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
150 |
lt.in = pin; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
151 |
f = t = null; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
152 |
if (rt != null) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
153 |
T lout = lt.out; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
154 |
rt.in = (l == org ? lout : |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
155 |
fn.apply(pin, lout)); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
156 |
for (int c;;) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
157 |
if (((c = rt.getPendingCount()) & CUMULATE) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
158 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
159 |
if (rt.compareAndSetPendingCount(c, c|CUMULATE)){ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
160 |
t = rt; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
161 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
162 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
163 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
164 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
165 |
for (int c;;) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
166 |
if (((c = lt.getPendingCount()) & CUMULATE) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
167 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
168 |
if (lt.compareAndSetPendingCount(c, c|CUMULATE)) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
169 |
if (t != null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
170 |
f = t; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
171 |
t = lt; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
172 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
173 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
174 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
175 |
if (t == null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
176 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
177 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
178 |
if (f != null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
179 |
f.fork(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
180 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
181 |
else { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
182 |
int state; // Transition to sum, cumulate, or both |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
183 |
for (int b;;) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
184 |
if (((b = t.getPendingCount()) & FINISHED) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
185 |
break outer; // already done |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
186 |
state = ((b & CUMULATE) != 0? FINISHED : |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
187 |
(l > org) ? SUMMED : (SUMMED|FINISHED)); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
188 |
if (t.compareAndSetPendingCount(b, b|state)) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
189 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
190 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
191 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
192 |
T sum; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
193 |
if (state != SUMMED) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
194 |
int first; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
195 |
if (l == org) { // leftmost; no in |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
196 |
sum = a[org]; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
197 |
first = org + 1; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
198 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
199 |
else { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
200 |
sum = t.in; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
201 |
first = l; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
202 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
203 |
for (int i = first; i < h; ++i) // cumulate |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
204 |
a[i] = sum = fn.apply(sum, a[i]); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
205 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
206 |
else if (h < fnc) { // skip rightmost |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
207 |
sum = a[l]; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
208 |
for (int i = l + 1; i < h; ++i) // sum only |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
209 |
sum = fn.apply(sum, a[i]); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
210 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
211 |
else |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
212 |
sum = t.in; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
213 |
t.out = sum; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
214 |
for (CumulateTask<T> par;;) { // propagate |
22994
0390009c367f
8022852: Fix serial javac warnings in ArrayPrefixHelpers
chegar
parents:
22078
diff
changeset
|
215 |
@SuppressWarnings("unchecked") CumulateTask<T> partmp |
0390009c367f
8022852: Fix serial javac warnings in ArrayPrefixHelpers
chegar
parents:
22078
diff
changeset
|
216 |
= (CumulateTask<T>)t.getCompleter(); |
0390009c367f
8022852: Fix serial javac warnings in ArrayPrefixHelpers
chegar
parents:
22078
diff
changeset
|
217 |
if ((par = partmp) == null) { |
18555
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
218 |
if ((state & FINISHED) != 0) // enable join |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
219 |
t.quietlyComplete(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
220 |
break outer; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
221 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
222 |
int b = par.getPendingCount(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
223 |
if ((b & state & FINISHED) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
224 |
t = par; // both done |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
225 |
else if ((b & state & SUMMED) != 0) { // both summed |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
226 |
int nextState; CumulateTask<T> lt, rt; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
227 |
if ((lt = par.left) != null && |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
228 |
(rt = par.right) != null) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
229 |
T lout = lt.out; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
230 |
par.out = (rt.hi == fnc ? lout : |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
231 |
fn.apply(lout, rt.out)); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
232 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
233 |
int refork = (((b & CUMULATE) == 0 && |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
234 |
par.lo == org) ? CUMULATE : 0); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
235 |
if ((nextState = b|state|refork) == b || |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
236 |
par.compareAndSetPendingCount(b, nextState)) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
237 |
state = SUMMED; // drop finished |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
238 |
t = par; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
239 |
if (refork != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
240 |
par.fork(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
241 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
242 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
243 |
else if (par.compareAndSetPendingCount(b, b|state)) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
244 |
break outer; // sib not ready |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
245 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
246 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
247 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
248 |
} |
22994
0390009c367f
8022852: Fix serial javac warnings in ArrayPrefixHelpers
chegar
parents:
22078
diff
changeset
|
249 |
private static final long serialVersionUID = 5293554502939613543L; |
18555
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
250 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
251 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
252 |
static final class LongCumulateTask extends CountedCompleter<Void> { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
253 |
final long[] array; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
254 |
final LongBinaryOperator function; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
255 |
LongCumulateTask left, right; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
256 |
long in, out; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
257 |
final int lo, hi, origin, fence, threshold; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
258 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
259 |
/** Root task constructor */ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
260 |
public LongCumulateTask(LongCumulateTask parent, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
261 |
LongBinaryOperator function, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
262 |
long[] array, int lo, int hi) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
263 |
super(parent); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
264 |
this.function = function; this.array = array; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
265 |
this.lo = this.origin = lo; this.hi = this.fence = hi; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
266 |
int p; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
267 |
this.threshold = |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
268 |
(p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3)) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
269 |
<= MIN_PARTITION ? MIN_PARTITION : p; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
270 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
271 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
272 |
/** Subtask constructor */ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
273 |
LongCumulateTask(LongCumulateTask parent, LongBinaryOperator function, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
274 |
long[] array, int origin, int fence, int threshold, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
275 |
int lo, int hi) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
276 |
super(parent); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
277 |
this.function = function; this.array = array; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
278 |
this.origin = origin; this.fence = fence; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
279 |
this.threshold = threshold; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
280 |
this.lo = lo; this.hi = hi; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
281 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
282 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
283 |
public final void compute() { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
284 |
final LongBinaryOperator fn; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
285 |
final long[] a; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
286 |
if ((fn = this.function) == null || (a = this.array) == null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
287 |
throw new NullPointerException(); // hoist checks |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
288 |
int th = threshold, org = origin, fnc = fence, l, h; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
289 |
LongCumulateTask t = this; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
290 |
outer: while ((l = t.lo) >= 0 && (h = t.hi) <= a.length) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
291 |
if (h - l > th) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
292 |
LongCumulateTask lt = t.left, rt = t.right, f; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
293 |
if (lt == null) { // first pass |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
294 |
int mid = (l + h) >>> 1; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
295 |
f = rt = t.right = |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
296 |
new LongCumulateTask(t, fn, a, org, fnc, th, mid, h); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
297 |
t = lt = t.left = |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
298 |
new LongCumulateTask(t, fn, a, org, fnc, th, l, mid); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
299 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
300 |
else { // possibly refork |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
301 |
long pin = t.in; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
302 |
lt.in = pin; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
303 |
f = t = null; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
304 |
if (rt != null) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
305 |
long lout = lt.out; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
306 |
rt.in = (l == org ? lout : |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
307 |
fn.applyAsLong(pin, lout)); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
308 |
for (int c;;) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
309 |
if (((c = rt.getPendingCount()) & CUMULATE) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
310 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
311 |
if (rt.compareAndSetPendingCount(c, c|CUMULATE)){ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
312 |
t = rt; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
313 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
314 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
315 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
316 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
317 |
for (int c;;) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
318 |
if (((c = lt.getPendingCount()) & CUMULATE) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
319 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
320 |
if (lt.compareAndSetPendingCount(c, c|CUMULATE)) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
321 |
if (t != null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
322 |
f = t; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
323 |
t = lt; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
324 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
325 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
326 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
327 |
if (t == null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
328 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
329 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
330 |
if (f != null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
331 |
f.fork(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
332 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
333 |
else { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
334 |
int state; // Transition to sum, cumulate, or both |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
335 |
for (int b;;) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
336 |
if (((b = t.getPendingCount()) & FINISHED) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
337 |
break outer; // already done |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
338 |
state = ((b & CUMULATE) != 0? FINISHED : |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
339 |
(l > org) ? SUMMED : (SUMMED|FINISHED)); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
340 |
if (t.compareAndSetPendingCount(b, b|state)) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
341 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
342 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
343 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
344 |
long sum; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
345 |
if (state != SUMMED) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
346 |
int first; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
347 |
if (l == org) { // leftmost; no in |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
348 |
sum = a[org]; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
349 |
first = org + 1; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
350 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
351 |
else { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
352 |
sum = t.in; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
353 |
first = l; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
354 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
355 |
for (int i = first; i < h; ++i) // cumulate |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
356 |
a[i] = sum = fn.applyAsLong(sum, a[i]); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
357 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
358 |
else if (h < fnc) { // skip rightmost |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
359 |
sum = a[l]; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
360 |
for (int i = l + 1; i < h; ++i) // sum only |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
361 |
sum = fn.applyAsLong(sum, a[i]); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
362 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
363 |
else |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
364 |
sum = t.in; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
365 |
t.out = sum; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
366 |
for (LongCumulateTask par;;) { // propagate |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
367 |
if ((par = (LongCumulateTask)t.getCompleter()) == null) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
368 |
if ((state & FINISHED) != 0) // enable join |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
369 |
t.quietlyComplete(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
370 |
break outer; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
371 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
372 |
int b = par.getPendingCount(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
373 |
if ((b & state & FINISHED) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
374 |
t = par; // both done |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
375 |
else if ((b & state & SUMMED) != 0) { // both summed |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
376 |
int nextState; LongCumulateTask lt, rt; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
377 |
if ((lt = par.left) != null && |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
378 |
(rt = par.right) != null) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
379 |
long lout = lt.out; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
380 |
par.out = (rt.hi == fnc ? lout : |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
381 |
fn.applyAsLong(lout, rt.out)); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
382 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
383 |
int refork = (((b & CUMULATE) == 0 && |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
384 |
par.lo == org) ? CUMULATE : 0); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
385 |
if ((nextState = b|state|refork) == b || |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
386 |
par.compareAndSetPendingCount(b, nextState)) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
387 |
state = SUMMED; // drop finished |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
388 |
t = par; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
389 |
if (refork != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
390 |
par.fork(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
391 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
392 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
393 |
else if (par.compareAndSetPendingCount(b, b|state)) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
394 |
break outer; // sib not ready |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
395 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
396 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
397 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
398 |
} |
22994
0390009c367f
8022852: Fix serial javac warnings in ArrayPrefixHelpers
chegar
parents:
22078
diff
changeset
|
399 |
private static final long serialVersionUID = -5074099945909284273L; |
18555
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
400 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
401 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
402 |
static final class DoubleCumulateTask extends CountedCompleter<Void> { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
403 |
final double[] array; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
404 |
final DoubleBinaryOperator function; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
405 |
DoubleCumulateTask left, right; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
406 |
double in, out; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
407 |
final int lo, hi, origin, fence, threshold; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
408 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
409 |
/** Root task constructor */ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
410 |
public DoubleCumulateTask(DoubleCumulateTask parent, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
411 |
DoubleBinaryOperator function, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
412 |
double[] array, int lo, int hi) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
413 |
super(parent); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
414 |
this.function = function; this.array = array; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
415 |
this.lo = this.origin = lo; this.hi = this.fence = hi; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
416 |
int p; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
417 |
this.threshold = |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
418 |
(p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3)) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
419 |
<= MIN_PARTITION ? MIN_PARTITION : p; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
420 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
421 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
422 |
/** Subtask constructor */ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
423 |
DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
424 |
double[] array, int origin, int fence, int threshold, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
425 |
int lo, int hi) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
426 |
super(parent); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
427 |
this.function = function; this.array = array; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
428 |
this.origin = origin; this.fence = fence; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
429 |
this.threshold = threshold; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
430 |
this.lo = lo; this.hi = hi; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
431 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
432 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
433 |
public final void compute() { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
434 |
final DoubleBinaryOperator fn; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
435 |
final double[] a; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
436 |
if ((fn = this.function) == null || (a = this.array) == null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
437 |
throw new NullPointerException(); // hoist checks |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
438 |
int th = threshold, org = origin, fnc = fence, l, h; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
439 |
DoubleCumulateTask t = this; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
440 |
outer: while ((l = t.lo) >= 0 && (h = t.hi) <= a.length) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
441 |
if (h - l > th) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
442 |
DoubleCumulateTask lt = t.left, rt = t.right, f; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
443 |
if (lt == null) { // first pass |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
444 |
int mid = (l + h) >>> 1; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
445 |
f = rt = t.right = |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
446 |
new DoubleCumulateTask(t, fn, a, org, fnc, th, mid, h); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
447 |
t = lt = t.left = |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
448 |
new DoubleCumulateTask(t, fn, a, org, fnc, th, l, mid); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
449 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
450 |
else { // possibly refork |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
451 |
double pin = t.in; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
452 |
lt.in = pin; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
453 |
f = t = null; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
454 |
if (rt != null) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
455 |
double lout = lt.out; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
456 |
rt.in = (l == org ? lout : |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
457 |
fn.applyAsDouble(pin, lout)); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
458 |
for (int c;;) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
459 |
if (((c = rt.getPendingCount()) & CUMULATE) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
460 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
461 |
if (rt.compareAndSetPendingCount(c, c|CUMULATE)){ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
462 |
t = rt; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
463 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
464 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
465 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
466 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
467 |
for (int c;;) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
468 |
if (((c = lt.getPendingCount()) & CUMULATE) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
469 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
470 |
if (lt.compareAndSetPendingCount(c, c|CUMULATE)) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
471 |
if (t != null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
472 |
f = t; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
473 |
t = lt; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
474 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
475 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
476 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
477 |
if (t == null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
478 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
479 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
480 |
if (f != null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
481 |
f.fork(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
482 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
483 |
else { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
484 |
int state; // Transition to sum, cumulate, or both |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
485 |
for (int b;;) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
486 |
if (((b = t.getPendingCount()) & FINISHED) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
487 |
break outer; // already done |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
488 |
state = ((b & CUMULATE) != 0? FINISHED : |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
489 |
(l > org) ? SUMMED : (SUMMED|FINISHED)); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
490 |
if (t.compareAndSetPendingCount(b, b|state)) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
491 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
492 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
493 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
494 |
double sum; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
495 |
if (state != SUMMED) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
496 |
int first; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
497 |
if (l == org) { // leftmost; no in |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
498 |
sum = a[org]; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
499 |
first = org + 1; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
500 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
501 |
else { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
502 |
sum = t.in; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
503 |
first = l; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
504 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
505 |
for (int i = first; i < h; ++i) // cumulate |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
506 |
a[i] = sum = fn.applyAsDouble(sum, a[i]); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
507 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
508 |
else if (h < fnc) { // skip rightmost |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
509 |
sum = a[l]; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
510 |
for (int i = l + 1; i < h; ++i) // sum only |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
511 |
sum = fn.applyAsDouble(sum, a[i]); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
512 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
513 |
else |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
514 |
sum = t.in; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
515 |
t.out = sum; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
516 |
for (DoubleCumulateTask par;;) { // propagate |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
517 |
if ((par = (DoubleCumulateTask)t.getCompleter()) == null) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
518 |
if ((state & FINISHED) != 0) // enable join |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
519 |
t.quietlyComplete(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
520 |
break outer; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
521 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
522 |
int b = par.getPendingCount(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
523 |
if ((b & state & FINISHED) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
524 |
t = par; // both done |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
525 |
else if ((b & state & SUMMED) != 0) { // both summed |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
526 |
int nextState; DoubleCumulateTask lt, rt; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
527 |
if ((lt = par.left) != null && |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
528 |
(rt = par.right) != null) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
529 |
double lout = lt.out; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
530 |
par.out = (rt.hi == fnc ? lout : |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
531 |
fn.applyAsDouble(lout, rt.out)); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
532 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
533 |
int refork = (((b & CUMULATE) == 0 && |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
534 |
par.lo == org) ? CUMULATE : 0); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
535 |
if ((nextState = b|state|refork) == b || |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
536 |
par.compareAndSetPendingCount(b, nextState)) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
537 |
state = SUMMED; // drop finished |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
538 |
t = par; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
539 |
if (refork != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
540 |
par.fork(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
541 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
542 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
543 |
else if (par.compareAndSetPendingCount(b, b|state)) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
544 |
break outer; // sib not ready |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
545 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
546 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
547 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
548 |
} |
22994
0390009c367f
8022852: Fix serial javac warnings in ArrayPrefixHelpers
chegar
parents:
22078
diff
changeset
|
549 |
private static final long serialVersionUID = -586947823794232033L; |
18555
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
550 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
551 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
552 |
static final class IntCumulateTask extends CountedCompleter<Void> { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
553 |
final int[] array; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
554 |
final IntBinaryOperator function; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
555 |
IntCumulateTask left, right; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
556 |
int in, out; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
557 |
final int lo, hi, origin, fence, threshold; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
558 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
559 |
/** Root task constructor */ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
560 |
public IntCumulateTask(IntCumulateTask parent, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
561 |
IntBinaryOperator function, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
562 |
int[] array, int lo, int hi) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
563 |
super(parent); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
564 |
this.function = function; this.array = array; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
565 |
this.lo = this.origin = lo; this.hi = this.fence = hi; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
566 |
int p; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
567 |
this.threshold = |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
568 |
(p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3)) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
569 |
<= MIN_PARTITION ? MIN_PARTITION : p; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
570 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
571 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
572 |
/** Subtask constructor */ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
573 |
IntCumulateTask(IntCumulateTask parent, IntBinaryOperator function, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
574 |
int[] array, int origin, int fence, int threshold, |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
575 |
int lo, int hi) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
576 |
super(parent); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
577 |
this.function = function; this.array = array; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
578 |
this.origin = origin; this.fence = fence; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
579 |
this.threshold = threshold; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
580 |
this.lo = lo; this.hi = hi; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
581 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
582 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
583 |
public final void compute() { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
584 |
final IntBinaryOperator fn; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
585 |
final int[] a; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
586 |
if ((fn = this.function) == null || (a = this.array) == null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
587 |
throw new NullPointerException(); // hoist checks |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
588 |
int th = threshold, org = origin, fnc = fence, l, h; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
589 |
IntCumulateTask t = this; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
590 |
outer: while ((l = t.lo) >= 0 && (h = t.hi) <= a.length) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
591 |
if (h - l > th) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
592 |
IntCumulateTask lt = t.left, rt = t.right, f; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
593 |
if (lt == null) { // first pass |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
594 |
int mid = (l + h) >>> 1; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
595 |
f = rt = t.right = |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
596 |
new IntCumulateTask(t, fn, a, org, fnc, th, mid, h); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
597 |
t = lt = t.left = |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
598 |
new IntCumulateTask(t, fn, a, org, fnc, th, l, mid); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
599 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
600 |
else { // possibly refork |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
601 |
int pin = t.in; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
602 |
lt.in = pin; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
603 |
f = t = null; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
604 |
if (rt != null) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
605 |
int lout = lt.out; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
606 |
rt.in = (l == org ? lout : |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
607 |
fn.applyAsInt(pin, lout)); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
608 |
for (int c;;) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
609 |
if (((c = rt.getPendingCount()) & CUMULATE) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
610 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
611 |
if (rt.compareAndSetPendingCount(c, c|CUMULATE)){ |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
612 |
t = rt; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
613 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
614 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
615 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
616 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
617 |
for (int c;;) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
618 |
if (((c = lt.getPendingCount()) & CUMULATE) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
619 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
620 |
if (lt.compareAndSetPendingCount(c, c|CUMULATE)) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
621 |
if (t != null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
622 |
f = t; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
623 |
t = lt; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
624 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
625 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
626 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
627 |
if (t == null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
628 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
629 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
630 |
if (f != null) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
631 |
f.fork(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
632 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
633 |
else { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
634 |
int state; // Transition to sum, cumulate, or both |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
635 |
for (int b;;) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
636 |
if (((b = t.getPendingCount()) & FINISHED) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
637 |
break outer; // already done |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
638 |
state = ((b & CUMULATE) != 0? FINISHED : |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
639 |
(l > org) ? SUMMED : (SUMMED|FINISHED)); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
640 |
if (t.compareAndSetPendingCount(b, b|state)) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
641 |
break; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
642 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
643 |
|
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
644 |
int sum; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
645 |
if (state != SUMMED) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
646 |
int first; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
647 |
if (l == org) { // leftmost; no in |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
648 |
sum = a[org]; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
649 |
first = org + 1; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
650 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
651 |
else { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
652 |
sum = t.in; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
653 |
first = l; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
654 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
655 |
for (int i = first; i < h; ++i) // cumulate |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
656 |
a[i] = sum = fn.applyAsInt(sum, a[i]); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
657 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
658 |
else if (h < fnc) { // skip rightmost |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
659 |
sum = a[l]; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
660 |
for (int i = l + 1; i < h; ++i) // sum only |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
661 |
sum = fn.applyAsInt(sum, a[i]); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
662 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
663 |
else |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
664 |
sum = t.in; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
665 |
t.out = sum; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
666 |
for (IntCumulateTask par;;) { // propagate |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
667 |
if ((par = (IntCumulateTask)t.getCompleter()) == null) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
668 |
if ((state & FINISHED) != 0) // enable join |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
669 |
t.quietlyComplete(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
670 |
break outer; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
671 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
672 |
int b = par.getPendingCount(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
673 |
if ((b & state & FINISHED) != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
674 |
t = par; // both done |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
675 |
else if ((b & state & SUMMED) != 0) { // both summed |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
676 |
int nextState; IntCumulateTask lt, rt; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
677 |
if ((lt = par.left) != null && |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
678 |
(rt = par.right) != null) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
679 |
int lout = lt.out; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
680 |
par.out = (rt.hi == fnc ? lout : |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
681 |
fn.applyAsInt(lout, rt.out)); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
682 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
683 |
int refork = (((b & CUMULATE) == 0 && |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
684 |
par.lo == org) ? CUMULATE : 0); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
685 |
if ((nextState = b|state|refork) == b || |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
686 |
par.compareAndSetPendingCount(b, nextState)) { |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
687 |
state = SUMMED; // drop finished |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
688 |
t = par; |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
689 |
if (refork != 0) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
690 |
par.fork(); |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
691 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
692 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
693 |
else if (par.compareAndSetPendingCount(b, b|state)) |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
694 |
break outer; // sib not ready |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
695 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
696 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
697 |
} |
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
698 |
} |
22994
0390009c367f
8022852: Fix serial javac warnings in ArrayPrefixHelpers
chegar
parents:
22078
diff
changeset
|
699 |
private static final long serialVersionUID = 3731755594596840961L; |
18555
d69e2a644e38
8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents:
diff
changeset
|
700 |
} |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18555
diff
changeset
|
701 |
} |