30 |
|
System.out.println("Usage: java FJSums threads n reps"); |
31 |
|
return; |
32 |
|
} |
33 |
< |
ForkJoinPool g = procs == 0? new ForkJoinPool() : |
33 |
> |
ForkJoinPool g = procs == 0? new ForkJoinPool() : |
34 |
|
new ForkJoinPool(procs); |
35 |
|
System.out.println("Number of procs=" + g.getParallelism()); |
36 |
|
// for now hardwire Cumulate threshold to 8 * #CPUs leaf tasks |
37 |
|
THRESHOLD = 1 + ((n + 7) >>> 3) / g.getParallelism(); |
38 |
< |
|
38 |
> |
|
39 |
|
long[] a = new long[n]; |
40 |
|
for (int i = 0; i < n; ++i) |
41 |
|
a[i] = i; |
46 |
|
long ss = seqSum(a, 0, n); |
47 |
|
double elapsed = elapsedTime(last); |
48 |
|
System.out.printf("sum = %24d time: %7.3f\n", ss, elapsed); |
49 |
< |
if (ss != expected) |
49 |
> |
if (ss != expected) |
50 |
|
throw new Error("expected " + expected + " != " + ss); |
51 |
|
} |
52 |
|
for (int i = 0; i < reps; ++i) { |
57 |
|
long ss = s.result; |
58 |
|
double elapsed = elapsedTime(last); |
59 |
|
System.out.printf("sum = %24d time: %7.3f\n", ss, elapsed); |
60 |
< |
if (i == 0 && ss != expected) |
60 |
> |
if (i == 0 && ss != expected) |
61 |
|
throw new Error("expected " + expected + " != " + ss); |
62 |
|
System.out.print("Cum: "); |
63 |
|
last = System.nanoTime(); |
78 |
|
|
79 |
|
static long seqSum(long[] array, int l, int h) { |
80 |
|
long sum = 0; |
81 |
< |
for (int i = l; i < h; ++i) |
81 |
> |
for (int i = l; i < h; ++i) |
82 |
|
sum += array[i]; |
83 |
|
return sum; |
84 |
|
} |
89 |
|
array[i] = sum += array[i]; |
90 |
|
return sum; |
91 |
|
} |
92 |
< |
|
92 |
> |
|
93 |
|
/** |
94 |
|
* Adapted from Applyer demo in RecursiveAction docs |
95 |
|
*/ |
102 |
|
this.array = array; this.lo = lo; this.hi = hi; |
103 |
|
this.next = next; |
104 |
|
} |
105 |
< |
|
105 |
> |
|
106 |
|
protected void compute() { |
107 |
|
int l = lo; |
108 |
|
int h = hi; |