DAS_2023_1/martysheva_tamara_lab_6/myapp/Main.java

131 lines
9.8 KiB
Java
Raw Normal View History

2023-12-21 20:50:48 +04:00
<EFBFBD><EFBFBD>import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) throws InterruptedException {
int nThreads = 8;
//100x100
var matrix = Matrix(6);
//>A;54>20B5;L=K9 0;3>@8B<
long startTime = System.currentTimeMillis();
int determinant = SequentialDeterminant(matrix, matrix.length);
long endTime = System.currentTimeMillis();
System.out.println(">A;54>20B5;L=>, 6x6, 2@5<O 2K?>;=5=8O: " + (endTime - startTime) + " <8;;8A5:C=4");
//0@0;;5;L=K9 0;3>@8B<
startTime = System.currentTimeMillis();
determinant = ParallelDeterminant(matrix, matrix.length, nThreads);
endTime = System.currentTimeMillis();
System.out.println("0@0;;5;L=>, 6x6, (" + nThreads + " ?>B>:>2), 2@5<O 2K?>;=5=8O : " + (endTime - startTime) + " <8;;8A5:C=4\n");
//300x300
matrix = Matrix(9);
//>A;54>20B5;L=K9 0;3>@8B<
startTime = System.currentTimeMillis();
determinant = SequentialDeterminant(matrix, matrix.length);
endTime = System.currentTimeMillis();
System.out.println(">A;54>20B5;L=>, 9x9, 2@5<O 2K?>;=5=8O: " + (endTime - startTime) + " <8;;8A5:C=4");
//0@0;;5;L=K9 0;3>@8B<
startTime = System.currentTimeMillis();
determinant = ParallelDeterminant(matrix, matrix.length, nThreads);
endTime = System.currentTimeMillis();
System.out.println("0@0;;5;L=>, 9x9, (" + nThreads + " ?>B>:>2), 2@5<O 2K?>;=5=8O : " + (endTime - startTime) + " <8;;8A5:C=4\n");
//500x500
matrix = Matrix(12);
//>A;54>20B5;L=K9 0;3>@8B<
startTime = System.currentTimeMillis();
determinant = SequentialDeterminant(matrix, matrix.length);
endTime = System.currentTimeMillis();
System.out.println(">A;54>20B5;L=>, 12x12, 2@5<O 2K?>;=5=8O: " + (endTime - startTime) + " <8;;8A5:C=4");
//0@0;;5;L=K9 0;3>@8B<
startTime = System.currentTimeMillis();
determinant = ParallelDeterminant(matrix, matrix.length, nThreads);
endTime = System.currentTimeMillis();
System.out.println("0@0;;5;L=>, 12x12, (" + 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 SequentialDeterminant(int[][] matrix, int size) {
if (size == 1) {
return matrix[0][0];
}
else if (size == 2) {
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
}
else {
int det = 0;
for (int i = 0; i < size; i++) {
var submatrix = Submatrix(matrix, i);
det += Math.pow(-1, i) * matrix[0][i] * SequentialDeterminant(submatrix, submatrix.length);
}
return det;
}
}
private static int[][] Submatrix(int[][] matrix, int excludeCol) {
int size = matrix.length - 1;
int[][] submatrix = new int[size][size];
int rowIndex = 0;
for (int i = 0; i < matrix.length; i++) {
if (i == 0) {
continue;
}
int colIndex = 0;
for (int j = 0; j < matrix[i].length; j++) {
if (j == excludeCol) {
continue;
}
submatrix[rowIndex][colIndex] = matrix[i][j];
colIndex++;
}
rowIndex++;
}
return submatrix;
}
public static int ParallelDeterminant(int[][] matrix, int size, int threads) {
if (size == 1) {
return matrix[0][0];
}
else if (size == 2) {
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
}
else {
int det = 0;
ExecutorService executor = Executors.newFixedThreadPool(threads);
List<Callable<Integer>> tasks = new ArrayList<>();
for (int i = 0; i < size; i++) {
var submatrix = Submatrix(matrix, i);
int finalI = i;
tasks.add(() -> (int) Math.pow(-1, finalI) * matrix[0][finalI] * SequentialDeterminant(submatrix, submatrix.length));
}
try {
List<Future<Integer>> futures = executor.invokeAll(tasks);
for (Future<Integer> future : futures) {
det += future.get();
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return -1;
} finally {
executor.shutdown();
}
return det;
}
}
}