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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
//import jsr166y.*; |
8 |
import java.util.concurrent.*; |
9 |
|
10 |
/** |
11 |
* Divide and Conquer matrix multiply demo |
12 |
*/ |
13 |
public class MatrixMultiply { |
14 |
|
15 |
/** for time conversion */ |
16 |
static final long NPS = (1000L * 1000 * 1000); |
17 |
|
18 |
static final int DEFAULT_GRANULARITY = 16; // 32; |
19 |
|
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 |
*/ |
25 |
static int granularity = DEFAULT_GRANULARITY; |
26 |
|
27 |
public static void main(String[] args) throws Exception { |
28 |
|
29 |
final String usage = "Usage: java MatrixMultiply <threads> <matrix size (must be a power of two)> [<granularity>] \n Size and granularity must be powers of two.\n For example, try java MatrixMultiply 2 512 16"; |
30 |
|
31 |
int procs = 0; |
32 |
int n = 2048; |
33 |
int runs = 32; |
34 |
try { |
35 |
if (args.length > 0) |
36 |
procs = Integer.parseInt(args[0]); |
37 |
if (args.length > 1) |
38 |
n = Integer.parseInt(args[1]); |
39 |
if (args.length > 2) |
40 |
granularity = Integer.parseInt(args[2]); |
41 |
if (args.length > 3) |
42 |
runs = Integer.parseInt(args[2]); |
43 |
} |
44 |
|
45 |
catch (Exception e) { |
46 |
System.out.println(usage); |
47 |
return; |
48 |
} |
49 |
|
50 |
if ( ((n & (n - 1)) != 0) || |
51 |
((granularity & (granularity - 1)) != 0) || |
52 |
granularity < 2) { |
53 |
System.out.println(usage); |
54 |
return; |
55 |
} |
56 |
|
57 |
ForkJoinPool pool = (procs == 0) ? ForkJoinPool.commonPool() : |
58 |
new ForkJoinPool(procs); |
59 |
System.out.println("procs: " + pool.getParallelism() + |
60 |
" n: " + n + " granularity: " + granularity + |
61 |
" runs: " + runs); |
62 |
|
63 |
float[][] a = new float[n][n]; |
64 |
float[][] b = new float[n][n]; |
65 |
float[][] c = new float[n][n]; |
66 |
|
67 |
for (int i = 0; i < runs; ++i) { |
68 |
init(a, b, n); |
69 |
long start = System.nanoTime(); |
70 |
new Multiplier(a, 0, 0, b, 0, 0, c, 0, 0, n).invoke(); |
71 |
long time = System.nanoTime() - start; |
72 |
double secs = ((double)time) / NPS; |
73 |
Thread.sleep(100); |
74 |
System.out.printf("Time: %7.3f ", secs); |
75 |
if ((i & 3) == 3) System.out.println(); |
76 |
// check(c, n); |
77 |
} |
78 |
System.out.println(pool.toString()); |
79 |
if (pool != ForkJoinPool.commonPool()) |
80 |
pool.shutdown(); |
81 |
Thread.sleep(100); |
82 |
} |
83 |
|
84 |
// To simplify checking, fill with all 1's. Answer should be all n's. |
85 |
static void init(float[][] a, float[][] b, int n) { |
86 |
for (int i = 0; i < n; ++i) { |
87 |
for (int j = 0; j < n; ++j) { |
88 |
a[i][j] = 1.0F; |
89 |
b[i][j] = 1.0F; |
90 |
} |
91 |
} |
92 |
} |
93 |
|
94 |
static void check(float[][] c, int n) { |
95 |
for (int i = 0; i < n; i++ ) { |
96 |
for (int j = 0; j < n; j++ ) { |
97 |
if (c[i][j] != n) { |
98 |
throw new Error("Check Failed at [" + i +"]["+j+"]: " + c[i][j]); |
99 |
} |
100 |
} |
101 |
} |
102 |
} |
103 |
|
104 |
/** |
105 |
* Multiply matrices AxB by dividing into quadrants, using algorithm: |
106 |
* <pre> |
107 |
* A x B |
108 |
* |
109 |
* A11 | A12 B11 | B12 A11*B11 | A11*B12 A12*B21 | A12*B22 |
110 |
* |----+----| x |----+----| = |--------+--------| + |---------+-------| |
111 |
* A21 | A22 B21 | B21 A21*B11 | A21*B21 A22*B21 | A22*B22 |
112 |
* </pre> |
113 |
*/ |
114 |
static class Multiplier extends RecursiveAction { |
115 |
final float[][] A; // Matrix A |
116 |
final int aRow; // first row of current quadrant of A |
117 |
final int aCol; // first column of current quadrant of A |
118 |
|
119 |
final float[][] B; // Similarly for B |
120 |
final int bRow; |
121 |
final int bCol; |
122 |
|
123 |
final float[][] C; // Similarly for result matrix C |
124 |
final int cRow; |
125 |
final int cCol; |
126 |
|
127 |
final int size; // number of elements in current quadrant |
128 |
|
129 |
Multiplier(float[][] A, int aRow, int aCol, |
130 |
float[][] B, int bRow, int bCol, |
131 |
float[][] C, int cRow, int cCol, |
132 |
int size) { |
133 |
this.A = A; this.aRow = aRow; this.aCol = aCol; |
134 |
this.B = B; this.bRow = bRow; this.bCol = bCol; |
135 |
this.C = C; this.cRow = cRow; this.cCol = cCol; |
136 |
this.size = size; |
137 |
} |
138 |
|
139 |
public void compute() { |
140 |
|
141 |
if (size <= granularity) { |
142 |
multiplyStride2(); |
143 |
} |
144 |
|
145 |
else { |
146 |
int h = size / 2; |
147 |
|
148 |
invokeAll(new Seq2[] { |
149 |
seq(new Multiplier(A, aRow, aCol, // A11 |
150 |
B, bRow, bCol, // B11 |
151 |
C, cRow, cCol, // C11 |
152 |
h), |
153 |
new Multiplier(A, aRow, aCol+h, // A12 |
154 |
B, bRow+h, bCol, // B21 |
155 |
C, cRow, cCol, // C11 |
156 |
h)), |
157 |
|
158 |
seq(new Multiplier(A, aRow, aCol, // A11 |
159 |
B, bRow, bCol+h, // B12 |
160 |
C, cRow, cCol+h, // C12 |
161 |
h), |
162 |
new Multiplier(A, aRow, aCol+h, // A12 |
163 |
B, bRow+h, bCol+h, // B22 |
164 |
C, cRow, cCol+h, // C12 |
165 |
h)), |
166 |
|
167 |
seq(new Multiplier(A, aRow+h, aCol, // A21 |
168 |
B, bRow, bCol, // B11 |
169 |
C, cRow+h, cCol, // C21 |
170 |
h), |
171 |
new Multiplier(A, aRow+h, aCol+h, // A22 |
172 |
B, bRow+h, bCol, // B21 |
173 |
C, cRow+h, cCol, // C21 |
174 |
h)), |
175 |
|
176 |
seq(new Multiplier(A, aRow+h, aCol, // A21 |
177 |
B, bRow, bCol+h, // B12 |
178 |
C, cRow+h, cCol+h, // C22 |
179 |
h), |
180 |
new Multiplier(A, aRow+h, aCol+h, // A22 |
181 |
B, bRow+h, bCol+h, // B22 |
182 |
C, cRow+h, cCol+h, // C22 |
183 |
h)) |
184 |
}); |
185 |
} |
186 |
} |
187 |
|
188 |
/** |
189 |
* Version of matrix multiplication that steps 2 rows and columns |
190 |
* at a time. Adapted from Cilk demos. |
191 |
* Note that the results are added into C, not just set into C. |
192 |
* This works well here because Java array elements |
193 |
* are created with all zero values. |
194 |
*/ |
195 |
void multiplyStride2() { |
196 |
for (int j = 0; j < size; j+=2) { |
197 |
for (int i = 0; i < size; i +=2) { |
198 |
|
199 |
float[] a0 = A[aRow+i]; |
200 |
float[] a1 = A[aRow+i+1]; |
201 |
|
202 |
float s00 = 0.0F; |
203 |
float s01 = 0.0F; |
204 |
float s10 = 0.0F; |
205 |
float s11 = 0.0F; |
206 |
|
207 |
for (int k = 0; k < size; k+=2) { |
208 |
|
209 |
float[] b0 = B[bRow+k]; |
210 |
|
211 |
s00 += a0[aCol+k] * b0[bCol+j]; |
212 |
s10 += a1[aCol+k] * b0[bCol+j]; |
213 |
s01 += a0[aCol+k] * b0[bCol+j+1]; |
214 |
s11 += a1[aCol+k] * b0[bCol+j+1]; |
215 |
|
216 |
float[] b1 = B[bRow+k+1]; |
217 |
|
218 |
s00 += a0[aCol+k+1] * b1[bCol+j]; |
219 |
s10 += a1[aCol+k+1] * b1[bCol+j]; |
220 |
s01 += a0[aCol+k+1] * b1[bCol+j+1]; |
221 |
s11 += a1[aCol+k+1] * b1[bCol+j+1]; |
222 |
} |
223 |
|
224 |
C[cRow+i] [cCol+j] += s00; |
225 |
C[cRow+i] [cCol+j+1] += s01; |
226 |
C[cRow+i+1][cCol+j] += s10; |
227 |
C[cRow+i+1][cCol+j+1] += s11; |
228 |
} |
229 |
} |
230 |
} |
231 |
|
232 |
} |
233 |
|
234 |
static Seq2 seq(RecursiveAction task1, |
235 |
RecursiveAction task2) { |
236 |
return new Seq2(task1, task2); |
237 |
} |
238 |
|
239 |
static final class Seq2 extends RecursiveAction { |
240 |
final RecursiveAction fst; |
241 |
final RecursiveAction snd; |
242 |
public Seq2(RecursiveAction task1, RecursiveAction task2) { |
243 |
fst = task1; |
244 |
snd = task2; |
245 |
} |
246 |
public void compute() { |
247 |
fst.invoke(); |
248 |
snd.invoke(); |
249 |
} |
250 |
} |
251 |
} |