-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProblem$17.java
109 lines (96 loc) · 3.34 KB
/
Problem$17.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package chapter_thirty_two;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
/**
* *32.17 (Parallel matrix multiplication) Programming Exercise 8.6 describes how to
* perform matrix multiplication. Suppose that you have multiple processors, so
* you can speed up the matrix multiplication. Implement the following method in
* parallel:
* public static double[][] parallelMultiplyMatrix(
* double[][] a, double[][] b)
* Write a test program that measures the execution time for multiplying two
* 2,000 * 2,000 matrices using the parallel method and sequential method,
* respectively.
*
*
* @author Sharaf Qeshta
* */
public class Problem$17
{
/**
* output =>
* Sequential multiplication takes: 82746
* Parallel multiplication takes: 24274
* */
public static void main(String[] args)
{
double[][] a = new double[2_000][2_000];
double[][] b = new double[2_000][2_000];
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
a[i][j] = Math.random() * 10;
b[i][j] = Math.random() * 10;
}
}
long start = System.currentTimeMillis();
sequentialMultiplyMatrix(a, b);
long end = System.currentTimeMillis();
System.out.println("Sequential multiplication takes: " + (end - start));
start = System.currentTimeMillis();
parallelMultiplyMatrix(a, b);
end = System.currentTimeMillis();
System.out.println("Parallel multiplication takes: " + (end - start));
}
public static double[][] sequentialMultiplyMatrix(double[][] a, double[][] b)
{
double[][] c = new double[a.length][b[0].length];
for (int i = 0; i < a.length; i++)
for (int j = 0; j < b[0].length; j++)
for (int k = 0; k < b.length; k++)
c[i][j] += a[i][k] * b[k][j];
return c;
}
public static double[][] parallelMultiplyMatrix(double[][] a, double[][] b)
{
double[][] c = new double[a.length][b[0].length];
MultiplyAction action = new MultiplyAction(a, b, c, 0, a.length);
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(action);
return c;
}
private static class MultiplyAction extends RecursiveAction
{
private final static int THRESHOLD = 50;
double[][] a, b, c;
int startRow;
int endRow;
public MultiplyAction(double[][] a, double[][] b,
double[][] c, int startRow, int endRow)
{
this.a = a;
this.b = b;
this.c = c;
this.startRow = startRow;
this.endRow = endRow;
}
@Override
protected void compute()
{
if (endRow - startRow < THRESHOLD)
{
for (int i = startRow; i < endRow; i++)
for (int j = 0; j < b[0].length; j++)
for (int k = 0; k < b.length; k++)
c[i][j] += a[i][k] * b[k][j];
}
else
{
int mid = (endRow + startRow) / 2;
invokeAll(new MultiplyAction(a, b, c, startRow, mid),
new MultiplyAction(a, b, c, mid, endRow));
}
}
}
}