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; |
9 |
|
|
10 |
|
|
11 |
|
/** |
12 |
|
* Divide and Conquer matrix multiply demo |
13 |
< |
**/ |
15 |
< |
|
13 |
> |
*/ |
14 |
|
public class MatrixMultiply { |
15 |
|
|
16 |
|
/** for time conversion */ |
18 |
|
|
19 |
|
static final int DEFAULT_GRANULARITY = 32; |
20 |
|
|
21 |
< |
/** The quadrant size at which to stop recursing down |
21 |
> |
/** |
22 |
> |
* The quadrant size at which to stop recursing down |
23 |
|
* and instead directly multiply the matrices. |
24 |
|
* Must be a power of two. Minimum value is 2. |
25 |
< |
**/ |
25 |
> |
*/ |
26 |
|
static int granularity = DEFAULT_GRANULARITY; |
27 |
|
|
28 |
|
public static void main(String[] args) throws Exception { |
37 |
|
procs = Integer.parseInt(args[0]); |
38 |
|
if (args.length > 1) |
39 |
|
n = Integer.parseInt(args[1]); |
40 |
< |
if (args.length > 2) |
40 |
> |
if (args.length > 2) |
41 |
|
granularity = Integer.parseInt(args[2]); |
42 |
< |
if (args.length > 3) |
42 |
> |
if (args.length > 3) |
43 |
|
runs = Integer.parseInt(args[2]); |
44 |
|
} |
45 |
< |
|
45 |
> |
|
46 |
|
catch (Exception e) { |
47 |
|
System.out.println(usage); |
48 |
|
return; |
49 |
|
} |
50 |
< |
|
51 |
< |
if ( ((n & (n - 1)) != 0) || |
50 |
> |
|
51 |
> |
if ( ((n & (n - 1)) != 0) || |
52 |
|
((granularity & (granularity - 1)) != 0) || |
53 |
|
granularity < 2) { |
54 |
|
System.out.println(usage); |
55 |
|
return; |
56 |
|
} |
57 |
< |
|
58 |
< |
ForkJoinPool pool = procs == 0? new ForkJoinPool() : |
57 |
> |
|
58 |
> |
ForkJoinPool pool = (procs == 0) ? new ForkJoinPool() : |
59 |
|
new ForkJoinPool(procs); |
60 |
< |
System.out.println("procs: " + pool.getParallelism() + |
60 |
> |
System.out.println("procs: " + pool.getParallelism() + |
61 |
|
" n: " + n + " granularity: " + granularity + |
62 |
|
" runs: " + runs); |
63 |
< |
|
63 |
> |
|
64 |
|
float[][] a = new float[n][n]; |
65 |
|
float[][] b = new float[n][n]; |
66 |
|
float[][] c = new float[n][n]; |
67 |
< |
|
67 |
> |
|
68 |
|
for (int i = 0; i < runs; ++i) { |
69 |
|
init(a, b, n); |
70 |
|
long start = System.nanoTime(); |
100 |
|
} |
101 |
|
} |
102 |
|
|
103 |
< |
/** |
103 |
> |
/** |
104 |
|
* Multiply matrices AxB by dividing into quadrants, using algorithm: |
105 |
|
* <pre> |
106 |
< |
* A x B |
106 |
> |
* A x B |
107 |
|
* |
108 |
< |
* A11 | A12 B11 | B12 A11*B11 | A11*B12 A12*B21 | A12*B22 |
108 |
> |
* A11 | A12 B11 | B12 A11*B11 | A11*B12 A12*B21 | A12*B22 |
109 |
|
* |----+----| x |----+----| = |--------+--------| + |---------+-------| |
110 |
< |
* A21 | A22 B21 | B21 A21*B11 | A21*B21 A22*B21 | A22*B22 |
110 |
> |
* A21 | A22 B21 | B21 A21*B11 | A21*B21 A22*B21 | A22*B22 |
111 |
|
* </pre> |
112 |
|
*/ |
114 |
– |
|
115 |
– |
|
113 |
|
static class Multiplier extends RecursiveAction { |
114 |
|
final float[][] A; // Matrix A |
115 |
|
final int aRow; // first row of current quadrant of A |
124 |
|
final int cCol; |
125 |
|
|
126 |
|
final int size; // number of elements in current quadrant |
127 |
< |
|
127 |
> |
|
128 |
|
Multiplier(float[][] A, int aRow, int aCol, |
129 |
|
float[][] B, int bRow, int bCol, |
130 |
|
float[][] C, int cRow, int cCol, |
153 |
|
B, bRow+h, bCol, // B21 |
154 |
|
C, cRow, cCol, // C11 |
155 |
|
h)), |
156 |
< |
|
156 |
> |
|
157 |
|
seq(new Multiplier(A, aRow, aCol, // A11 |
158 |
|
B, bRow, bCol+h, // B12 |
159 |
|
C, cRow, cCol+h, // C12 |
162 |
|
B, bRow+h, bCol+h, // B22 |
163 |
|
C, cRow, cCol+h, // C12 |
164 |
|
h)), |
165 |
< |
|
165 |
> |
|
166 |
|
seq(new Multiplier(A, aRow+h, aCol, // A21 |
167 |
|
B, bRow, bCol, // B11 |
168 |
|
C, cRow+h, cCol, // C21 |
171 |
|
B, bRow+h, bCol, // B21 |
172 |
|
C, cRow+h, cCol, // C21 |
173 |
|
h)), |
174 |
< |
|
174 |
> |
|
175 |
|
seq(new Multiplier(A, aRow+h, aCol, // A21 |
176 |
|
B, bRow, bCol+h, // B12 |
177 |
|
C, cRow+h, cCol+h, // C22 |
184 |
|
} |
185 |
|
} |
186 |
|
|
187 |
< |
/** |
187 |
> |
/** |
188 |
|
* Version of matrix multiplication that steps 2 rows and columns |
189 |
|
* at a time. Adapted from Cilk demos. |
190 |
|
* Note that the results are added into C, not just set into C. |
191 |
|
* This works well here because Java array elements |
192 |
|
* are created with all zero values. |
193 |
< |
**/ |
197 |
< |
|
193 |
> |
*/ |
194 |
|
void multiplyStride2() { |
195 |
|
for (int j = 0; j < size; j+=2) { |
196 |
|
for (int i = 0; i < size; i +=2) { |
197 |
|
|
198 |
|
float[] a0 = A[aRow+i]; |
199 |
|
float[] a1 = A[aRow+i+1]; |
200 |
< |
|
201 |
< |
float s00 = 0.0F; |
202 |
< |
float s01 = 0.0F; |
203 |
< |
float s10 = 0.0F; |
204 |
< |
float s11 = 0.0F; |
200 |
> |
|
201 |
> |
float s00 = 0.0F; |
202 |
> |
float s01 = 0.0F; |
203 |
> |
float s10 = 0.0F; |
204 |
> |
float s11 = 0.0F; |
205 |
|
|
206 |
|
for (int k = 0; k < size; k+=2) { |
207 |
|
|
230 |
|
|
231 |
|
} |
232 |
|
|
233 |
< |
static Seq2 seq(RecursiveAction task1, |
234 |
< |
RecursiveAction task2) { |
235 |
< |
return new Seq2(task1, task2); |
233 |
> |
static Seq2 seq(RecursiveAction task1, |
234 |
> |
RecursiveAction task2) { |
235 |
> |
return new Seq2(task1, task2); |
236 |
|
} |
237 |
|
|
238 |
|
static final class Seq2 extends RecursiveAction { |