DAS_2023_1/martysheva_tamara_lab_5/myapp/Main.java

104 lines
8.5 KiB
Java
Raw Normal View History

2023-12-21 20:43:51 +04:00
<EFBFBD><EFBFBD>import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws InterruptedException {
int nThreads = 8;
//100x100
var matrix1 = Matrix(100);
var matrix2 = Matrix(100);
//>A;54>20B5;L=K9 0;3>@8B<
long startTime = System.currentTimeMillis();
int[][] matrixResult = SequentialMult(matrix1, matrix2, matrix1.length);
long endTime = System.currentTimeMillis();
System.out.println(">A;54>20B5;L=>, 100x100, 2@5<O 2K?>;=5=8O: " + (endTime - startTime) + " <8;;8A5:C=4");
//0@0;;5;L=K9 0;3>@8B<
startTime = System.currentTimeMillis();
matrixResult = ParallelMult(matrix1, matrix2, matrix1.length, nThreads);
endTime = System.currentTimeMillis();
System.out.println("0@0;;5;L=>, 100x100, (" + nThreads + " ?>B>:>2), 2@5<O 2K?>;=5=8O : " + (endTime - startTime) + " <8;;8A5:C=4\n");
//300x300
matrix1 = Matrix(300);
matrix2 = Matrix(300);
//>A;54>20B5;L=K9 0;3>@8B<
startTime = System.currentTimeMillis();
matrixResult = SequentialMult(matrix1, matrix2, matrix1.length);
endTime = System.currentTimeMillis();
System.out.println(">A;54>20B5;L=>, 300x300, 2@5<O 2K?>;=5=8O: " + (endTime - startTime) + " <8;;8A5:C=4");
//0@0;;5;L=K9 0;3>@8B<
startTime = System.currentTimeMillis();
matrixResult = ParallelMult(matrix1, matrix2, matrix1.length, nThreads);
endTime = System.currentTimeMillis();
System.out.println("0@0;;5;L=>, 300x300, (" + nThreads + " ?>B>:>2), 2@5<O 2K?>;=5=8O : " + (endTime - startTime) + " <8;;8A5:C=4\n");
//500x500
matrix1 = Matrix(500);
matrix2 = Matrix(500);
//>A;54>20B5;L=K9 0;3>@8B<
startTime = System.currentTimeMillis();
matrixResult = SequentialMult(matrix1, matrix2, matrix1.length);
endTime = System.currentTimeMillis();
System.out.println(">A;54>20B5;L=>, 500x500, 2@5<O 2K?>;=5=8O: " + (endTime - startTime) + " <8;;8A5:C=4");
//0@0;;5;L=K9 0;3>@8B<
startTime = System.currentTimeMillis();
matrixResult = ParallelMult(matrix1, matrix2, matrix1.length, nThreads);
endTime = System.currentTimeMillis();
System.out.println("0@0;;5;L=>, 500x500, (" + nThreads + " ?>B>:>2), 2@5<O 2K?>;=5=8O : " + (endTime - startTime) + " <8;;8A5:C=4\n");
}
public static int[][] Matrix(int size) {
int[][] matrix = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
matrix[i][j] = (int) (Math.random() * 1000);
}
}
return matrix;
}
public static int[][] SequentialMult(int[][] matrix1, int[][] matrix2, int size) {
var matrixResult = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
for (int m = 0; m < size; m++) {
matrixResult[i][j] += matrix1[i][m] * matrix2[m][j];
}
}
}
return matrixResult;
}
public static int[][] ParallelMult(int[][] matrix1, int[][] matrix2, int size, int nThreads) throws InterruptedException {
var matrixResult = new int[size][size];
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
int blockSize = size / nThreads;
for (int i = 0; i < nThreads; i++) {
int startRow = i * blockSize;
int endRow = (i + 1) * blockSize;
if (i == nThreads - 1) {
endRow = size;
}
int finalEndRow = endRow;
executorService.submit(() -> {
for (int row = startRow; row < finalEndRow; row++) {
for (int col = 0; col < size; col++) {
for (int m = 0; m < size; m++) {
matrixResult[row][col] += matrix1[row][m] * matrix2[m][col];
}
}
}
});
}
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
return matrixResult;
}
}