-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultiplication.java
More file actions
71 lines (61 loc) · 2 KB
/
Copy pathMatrixMultiplication.java
File metadata and controls
71 lines (61 loc) · 2 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
package Homework;
import java.util.Arrays;
public class MatrixMultiplication {
public static void main(String[] args) {
int size = 500;
double[][] A = generateRandomMatrix(size, size);
double[][] B = generateRandomMatrix(size, size);
double[][] C = new double[size][size];
int numThreads = 4;
Thread[] threads = new Thread[numThreads];
int rowsPerThread = size / numThreads;
for (int i = 0; i < numThreads; i++) {
int startRow = i * rowsPerThread;
int endRow = (i == numThreads - 1) ? size : startRow + rowsPerThread;
threads[i] = new Thread(new Worker(A, B, C, startRow, endRow));
threads[i].start();
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Matrix multiplication completed.");
}
private static double[][] generateRandomMatrix(int rows, int cols) {
double[][] matrix = new double[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = Math.random();
}
}
return matrix;
}
}
class Worker implements Runnable {
private final double[][] A;
private final double[][] B;
private final double[][] C;
private final int startRow;
private final int endRow;
public Worker(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
public void run() {
for (int i = startRow; i < endRow; i++) {
for (int j = 0; j < B[0].length; j++) {
C[i][j] = 0;
for (int k = 0; k < A[0].length; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
}