38 |
|
procs = Integer.parseInt(args[0]); |
39 |
|
if (args.length > 1) |
40 |
|
n = Integer.parseInt(args[1]); |
41 |
< |
if (args.length > 2) |
41 |
> |
if (args.length > 2) |
42 |
|
granularity = Integer.parseInt(args[2]); |
43 |
< |
if (args.length > 3) |
43 |
> |
if (args.length > 3) |
44 |
|
runs = Integer.parseInt(args[2]); |
45 |
|
} |
46 |
< |
|
46 |
> |
|
47 |
|
catch (Exception e) { |
48 |
|
System.out.println(usage); |
49 |
|
return; |
50 |
|
} |
51 |
< |
|
52 |
< |
if ( ((n & (n - 1)) != 0) || |
51 |
> |
|
52 |
> |
if ( ((n & (n - 1)) != 0) || |
53 |
|
((granularity & (granularity - 1)) != 0) || |
54 |
|
granularity < 2) { |
55 |
|
System.out.println(usage); |
56 |
|
return; |
57 |
|
} |
58 |
< |
|
59 |
< |
ForkJoinPool pool = procs == 0? new ForkJoinPool() : |
58 |
> |
|
59 |
> |
ForkJoinPool pool = procs == 0? new ForkJoinPool() : |
60 |
|
new ForkJoinPool(procs); |
61 |
< |
System.out.println("procs: " + pool.getParallelism() + |
61 |
> |
System.out.println("procs: " + pool.getParallelism() + |
62 |
|
" n: " + n + " granularity: " + granularity + |
63 |
|
" runs: " + runs); |
64 |
< |
|
64 |
> |
|
65 |
|
float[][] a = new float[n][n]; |
66 |
|
float[][] b = new float[n][n]; |
67 |
|
float[][] c = new float[n][n]; |
68 |
< |
|
68 |
> |
|
69 |
|
for (int i = 0; i < runs; ++i) { |
70 |
|
init(a, b, n); |
71 |
|
long start = System.nanoTime(); |
101 |
|
} |
102 |
|
} |
103 |
|
|
104 |
< |
/** |
104 |
> |
/** |
105 |
|
* Multiply matrices AxB by dividing into quadrants, using algorithm: |
106 |
|
* <pre> |
107 |
< |
* A x B |
107 |
> |
* A x B |
108 |
|
* |
109 |
< |
* A11 | A12 B11 | B12 A11*B11 | A11*B12 A12*B21 | A12*B22 |
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 |
111 |
> |
* A21 | A22 B21 | B21 A21*B11 | A21*B21 A22*B21 | A22*B22 |
112 |
|
* </pre> |
113 |
|
*/ |
114 |
|
|
127 |
|
final int cCol; |
128 |
|
|
129 |
|
final int size; // number of elements in current quadrant |
130 |
< |
|
130 |
> |
|
131 |
|
Multiplier(float[][] A, int aRow, int aCol, |
132 |
|
float[][] B, int bRow, int bCol, |
133 |
|
float[][] C, int cRow, int cCol, |
156 |
|
B, bRow+h, bCol, // B21 |
157 |
|
C, cRow, cCol, // C11 |
158 |
|
h)), |
159 |
< |
|
159 |
> |
|
160 |
|
seq(new Multiplier(A, aRow, aCol, // A11 |
161 |
|
B, bRow, bCol+h, // B12 |
162 |
|
C, cRow, cCol+h, // C12 |
165 |
|
B, bRow+h, bCol+h, // B22 |
166 |
|
C, cRow, cCol+h, // C12 |
167 |
|
h)), |
168 |
< |
|
168 |
> |
|
169 |
|
seq(new Multiplier(A, aRow+h, aCol, // A21 |
170 |
|
B, bRow, bCol, // B11 |
171 |
|
C, cRow+h, cCol, // C21 |
174 |
|
B, bRow+h, bCol, // B21 |
175 |
|
C, cRow+h, cCol, // C21 |
176 |
|
h)), |
177 |
< |
|
177 |
> |
|
178 |
|
seq(new Multiplier(A, aRow+h, aCol, // A21 |
179 |
|
B, bRow, bCol+h, // B12 |
180 |
|
C, cRow+h, cCol+h, // C22 |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
< |
/** |
190 |
> |
/** |
191 |
|
* Version of matrix multiplication that steps 2 rows and columns |
192 |
|
* at a time. Adapted from Cilk demos. |
193 |
|
* Note that the results are added into C, not just set into C. |
201 |
|
|
202 |
|
float[] a0 = A[aRow+i]; |
203 |
|
float[] a1 = A[aRow+i+1]; |
204 |
< |
|
205 |
< |
float s00 = 0.0F; |
206 |
< |
float s01 = 0.0F; |
207 |
< |
float s10 = 0.0F; |
208 |
< |
float s11 = 0.0F; |
204 |
> |
|
205 |
> |
float s00 = 0.0F; |
206 |
> |
float s01 = 0.0F; |
207 |
> |
float s10 = 0.0F; |
208 |
> |
float s11 = 0.0F; |
209 |
|
|
210 |
|
for (int k = 0; k < size; k+=2) { |
211 |
|
|
234 |
|
|
235 |
|
} |
236 |
|
|
237 |
< |
static Seq2 seq(RecursiveAction task1, |
238 |
< |
RecursiveAction task2) { |
239 |
< |
return new Seq2(task1, task2); |
237 |
> |
static Seq2 seq(RecursiveAction task1, |
238 |
> |
RecursiveAction task2) { |
239 |
> |
return new Seq2(task1, task2); |
240 |
|
} |
241 |
|
|
242 |
|
static final class Seq2 extends RecursiveAction { |