1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
//import jsr166y.*; |
8 |
|
import java.util.concurrent.*; |
9 |
– |
import java.util.concurrent.TimeUnit; |
10 |
– |
|
9 |
|
|
10 |
|
/** |
11 |
|
* Divide and Conquer matrix multiply demo |
12 |
< |
**/ |
15 |
< |
|
12 |
> |
*/ |
13 |
|
public class MatrixMultiply { |
14 |
|
|
15 |
|
/** for time conversion */ |
17 |
|
|
18 |
|
static final int DEFAULT_GRANULARITY = 32; |
19 |
|
|
20 |
< |
/** The quadrant size at which to stop recursing down |
20 |
> |
/** |
21 |
> |
* The quadrant size at which to stop recursing down |
22 |
|
* and instead directly multiply the matrices. |
23 |
|
* Must be a power of two. Minimum value is 2. |
24 |
< |
**/ |
24 |
> |
*/ |
25 |
|
static int granularity = DEFAULT_GRANULARITY; |
26 |
|
|
27 |
|
public static void main(String[] args) throws Exception { |
54 |
|
return; |
55 |
|
} |
56 |
|
|
57 |
< |
ForkJoinPool pool = procs == 0? new ForkJoinPool() : |
57 |
> |
ForkJoinPool pool = (procs == 0) ? new ForkJoinPool() : |
58 |
|
new ForkJoinPool(procs); |
59 |
|
System.out.println("procs: " + pool.getParallelism() + |
60 |
|
" n: " + n + " granularity: " + granularity + |
78 |
|
pool.shutdown(); |
79 |
|
} |
80 |
|
|
83 |
– |
|
81 |
|
// To simplify checking, fill with all 1's. Answer should be all n's. |
82 |
|
static void init(float[][] a, float[][] b, int n) { |
83 |
|
for (int i = 0; i < n; ++i) { |
108 |
|
* A21 | A22 B21 | B21 A21*B11 | A21*B21 A22*B21 | A22*B22 |
109 |
|
* </pre> |
110 |
|
*/ |
114 |
– |
|
115 |
– |
|
111 |
|
static class Multiplier extends RecursiveAction { |
112 |
|
final float[][] A; // Matrix A |
113 |
|
final int aRow; // first row of current quadrant of A |
188 |
|
* Note that the results are added into C, not just set into C. |
189 |
|
* This works well here because Java array elements |
190 |
|
* are created with all zero values. |
191 |
< |
**/ |
197 |
< |
|
191 |
> |
*/ |
192 |
|
void multiplyStride2() { |
193 |
|
for (int j = 0; j < size; j+=2) { |
194 |
|
for (int i = 0; i < size; i +=2) { |
245 |
|
snd.invoke(); |
246 |
|
} |
247 |
|
} |
254 |
– |
|
255 |
– |
|
248 |
|
} |