@@ -102,7 +102,7 @@ private static class ReciprocalArraySumTask extends RecursiveAction {
102
102
* Intermediate value produced by this task.
103
103
*/
104
104
private double value ;
105
- static int SEQ_FACTOR = 4 ;
105
+ private static final int SEQUENTIAL_THRESHOLD = 1000 ;
106
106
107
107
/**
108
108
* Constructor.
@@ -128,30 +128,18 @@ public double getValue() {
128
128
129
129
@ Override
130
130
protected void compute () {
131
- int range = endIndexExclusive - startIndexInclusive ;
132
- if (range <= (input .length / SEQ_FACTOR )) {
133
- System .out .println ("Computing " + startIndexInclusive + " " + endIndexExclusive );
134
- double result = 0 ;
131
+ if ((endIndexExclusive - startIndexInclusive ) <= SEQUENTIAL_THRESHOLD ) {
135
132
for (int i = startIndexInclusive ; i < endIndexExclusive ; i ++) {
136
- result += 1 / input [i ];
133
+ value += 1 / input [i ];
137
134
}
138
- value = result ;
139
- System .out .println ("Computing done" );
140
135
} else {
141
- int half = (endIndexExclusive - startIndexInclusive ) / 2 ;
142
- int a = startIndexInclusive ;
143
- int b = startIndexInclusive + half ;
144
- int c = endIndexExclusive ;
145
- System .out .println (("Forking" + a + " " + b + " " + c ));
146
- ReciprocalArraySumTask left = new ReciprocalArraySumTask (a , b , input );
147
- ReciprocalArraySumTask right = new ReciprocalArraySumTask (b , c , input );
136
+ final int midIndex = (startIndexInclusive + endIndexExclusive ) / 2 ;
137
+ final ReciprocalArraySumTask left = new ReciprocalArraySumTask (startIndexInclusive , midIndex , input );
138
+ final ReciprocalArraySumTask right = new ReciprocalArraySumTask (midIndex , endIndexExclusive , input );
148
139
left .fork ();
149
- right .fork ();
140
+ right .compute ();
150
141
left .join ();
151
- right .join ();
152
- System .out .println ("Joined" );
153
- double result = left .getValue () + right .getValue ();
154
- value = result ;
142
+ value = left .value + right .value ;
155
143
}
156
144
}
157
145
}
@@ -168,27 +156,10 @@ protected void compute() {
168
156
protected static double parArraySum (final double [] input ) {
169
157
assert input .length % 2 == 0 ;
170
158
171
- return parManyTaskArraySum (input , 2 );
172
-
173
- // double sum = 0;
174
- //
175
- // ReciprocalArraySumTask left = new ReciprocalArraySumTask(0, input.length / 2,
176
- // input);
177
- // ReciprocalArraySumTask right = new ReciprocalArraySumTask(input.length / 2,
178
- // input.length, input);
179
- //
180
- // left.fork();
181
- // right.compute();
182
- // left.join();
183
- //
184
- // sum = left.getValue() + right.getValue();
185
- //
186
- //// // Compute sum of reciprocals of array elements
187
- //// for (int i = 0; i < input.length; i++) {
188
- //// sum += 1 / input[i];
189
- //// }
190
- //
191
- // return sum;
159
+ double sum =0 ;
160
+
161
+ sum = parManyTaskArraySum (input , 2 );
162
+ return sum ;
192
163
}
193
164
194
165
/**
@@ -202,19 +173,27 @@ protected static double parArraySum(final double[] input) {
202
173
* @return The sum of the reciprocals of the array input
203
174
*/
204
175
protected static double parManyTaskArraySum (final double [] input , final int numTasks ) {
176
+
205
177
double sum = 0 ;
206
178
207
- System . setProperty ( "java.util.concurrent.ForkJoinPool.common.parallelism" , "4" );
208
- System . out . println ( "numTasks" + numTasks ) ;
179
+ // Compute sum of reciprocals of array elements
180
+ final ReciprocalArraySumTask [] tasks = new ReciprocalArraySumTask [ numTasks ] ;
209
181
210
- ReciprocalArraySumTask task = new ReciprocalArraySumTask (0 , input .length , input );
211
- ForkJoinPool .commonPool ().invoke (task );
212
- sum = task .getValue ();
182
+ for (int i = 0 ; i < numTasks ; i ++) {
183
+ final int startIndexInclusive = getChunkStartInclusive (i , numTasks , input .length );
184
+ final int endIndexExclusive = getChunkEndExclusive (i , numTasks , input .length );
185
+ tasks [i ] = new ReciprocalArraySumTask (startIndexInclusive , endIndexExclusive , input );
186
+ }
213
187
214
- // Compute sum of reciprocals of array elements
215
- // for (int i = 0; i < input.length; i++) {
216
- // sum += 1 / input[i];
217
- // }
188
+ for (int i = 0 ; i < (numTasks - 1 ); i ++) {
189
+ tasks [i ].fork ();
190
+ }
191
+ tasks [numTasks - 1 ].compute ();
192
+ for (int i = 0 ; i < (numTasks - 1 ); i ++) {
193
+ tasks [i ].join ();
194
+ sum += tasks [i ].getValue ();
195
+ }
196
+ sum += tasks [numTasks - 1 ].getValue ();
218
197
219
198
return sum ;
220
199
}
0 commit comments