131 lines
9.8 KiB
Java
131 lines
9.8 KiB
Java
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);
|
||
//Последовательный алгоритм
|
||
long startTime = System.currentTimeMillis();
|
||
int determinant = SequentialDeterminant(matrix, matrix.length);
|
||
long endTime = System.currentTimeMillis();
|
||
System.out.println("Последовательно, 6x6, время выполнения: " + (endTime - startTime) + " миллисекунд");
|
||
//Параллельный алгоритм
|
||
startTime = System.currentTimeMillis();
|
||
determinant = ParallelDeterminant(matrix, matrix.length, nThreads);
|
||
endTime = System.currentTimeMillis();
|
||
System.out.println("Параллельно, 6x6, (" + nThreads + " потоков), время выполнения : " + (endTime - startTime) + " миллисекунд\n");
|
||
|
||
//300x300
|
||
matrix = Matrix(9);
|
||
//Последовательный алгоритм
|
||
startTime = System.currentTimeMillis();
|
||
determinant = SequentialDeterminant(matrix, matrix.length);
|
||
endTime = System.currentTimeMillis();
|
||
System.out.println("Последовательно, 9x9, время выполнения: " + (endTime - startTime) + " миллисекунд");
|
||
//Параллельный алгоритм
|
||
startTime = System.currentTimeMillis();
|
||
determinant = ParallelDeterminant(matrix, matrix.length, nThreads);
|
||
endTime = System.currentTimeMillis();
|
||
System.out.println("Параллельно, 9x9, (" + nThreads + " потоков), время выполнения : " + (endTime - startTime) + " миллисекунд\n");
|
||
|
||
//500x500
|
||
matrix = Matrix(12);
|
||
//Последовательный алгоритм
|
||
startTime = System.currentTimeMillis();
|
||
determinant = SequentialDeterminant(matrix, matrix.length);
|
||
endTime = System.currentTimeMillis();
|
||
System.out.println("Последовательно, 12x12, время выполнения: " + (endTime - startTime) + " миллисекунд");
|
||
//Параллельный алгоритм
|
||
startTime = System.currentTimeMillis();
|
||
determinant = ParallelDeterminant(matrix, matrix.length, nThreads);
|
||
endTime = System.currentTimeMillis();
|
||
System.out.println("Параллельно, 12x12, (" + nThreads + " потоков), время выполнения : " + (endTime - startTime) + " миллисекунд\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;
|
||
}
|
||
}
|
||
}
|