-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixDriver.java
More file actions
90 lines (76 loc) · 2.15 KB
/
Copy pathMatrixDriver.java
File metadata and controls
90 lines (76 loc) · 2.15 KB
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
import java.util.*;
import java.util.concurrent.TimeUnit;
class MatrixDriver extends Thread {
private static int[][] A;
private static int[][] B;
private static int[][] C;
private static int n;
private static int threads;
private int threadNumber;
public MatrixDriver(int threadNumber)
{
this.threadNumber=threadNumber;
}
public void run()
{
for (int i = threadNumber; i < n; i+= threads) { // aRow
for (int j = 0; j < n; j++) { // bColumn
for (int k = 0; k < n; k++) { // aColumn
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
public static void main(String[] args)
{
threads = 8;
n = 10000;
int max = 100;
int min = 0;
Random rand = new Random();
A=new int[n][n];
B=new int[n][n];
C=new int[n][n];
MatrixDriver[] thrd = new MatrixDriver[threads];
//Create A
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
A[i][j]=rand.nextInt((max - min) + 1) + min;
}
}
System.out.println("Matrix A generated.");
//Create B
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
B[i][j]=rand.nextInt((max - min) + 1) + min;
}
}
System.out.println("Matrix B generated.");
//Start clock
long startTime = System.currentTimeMillis();
//Run calculation
for(int i=0;i<threads;i++)
{
thrd[i] = new MatrixDriver(i);
thrd[i].start();
}
for(int i=0;i<threads;i++)
{
try
{
thrd[i].join();
}
catch(InterruptedException e){}
}
//Stop clock
long endTime = System.currentTimeMillis();
//Calculate time elapsed
long timeElapsedSeconds = TimeUnit.MILLISECONDS.toSeconds(endTime - startTime);
//Print results
System.out.println("The calculation of the matrix of dimension " + n + " took " + timeElapsedSeconds + " seconds.");
}
}