emelyanov_artem_lab_6 #77
8
emelyanov_artem_lab_6/.idea/.gitignore
vendored
Normal file
8
emelyanov_artem_lab_6/.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
11
emelyanov_artem_lab_6/.idea/emelyanov_artem_lab_6.iml
Normal file
11
emelyanov_artem_lab_6/.idea/emelyanov_artem_lab_6.iml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
@ -0,0 +1,8 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.lang.foreign.Arena,ofAuto,java.lang.foreign.Arena,global,java.util.concurrent.Executors,newFixedThreadPool" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
6
emelyanov_artem_lab_6/.idea/misc.xml
Normal file
6
emelyanov_artem_lab_6/.idea/misc.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="23" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
emelyanov_artem_lab_6/.idea/modules.xml
Normal file
8
emelyanov_artem_lab_6/.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/emelyanov_artem_lab_6.iml" filepath="$PROJECT_DIR$/.idea/emelyanov_artem_lab_6.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
emelyanov_artem_lab_6/.idea/vcs.xml
Normal file
6
emelyanov_artem_lab_6/.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
27
emelyanov_artem_lab_6/Benchmark.java
Normal file
27
emelyanov_artem_lab_6/Benchmark.java
Normal file
@ -0,0 +1,27 @@
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class Benchmark {
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
int[] sizes = {5, 7, 10};
|
||||
|
||||
for (int size : sizes) {
|
||||
int[][] matrix = MatrixGenerator.generateMatrix(size);
|
||||
|
||||
// Последовательное вычисление
|
||||
long startSequential = System.nanoTime();
|
||||
double detSequential = DeterminantCalculator.calculateDeterminant(matrix, 1);
|
||||
long endSequential = System.nanoTime();
|
||||
System.out.println("Последовательно (" + size + "x" + size + "): " + (endSequential - startSequential) / 1_000_000 + " ms, детерминант: " + detSequential);
|
||||
|
||||
// Параллельное вычисление
|
||||
for (int numThreads : new int[]{2, 4, 8}) {
|
||||
long startParallel = System.nanoTime();
|
||||
double detParallel = DeterminantCalculator.calculateDeterminant(matrix, numThreads);
|
||||
long endParallel = System.nanoTime();
|
||||
System.out.println("Параллельно (" + size + "x" + size + ", " + numThreads + " потоков): " +
|
||||
(endParallel - startParallel) / 1_000_000 + " ms, детерминант: " + detParallel);
|
||||
}
|
||||
System.out.println("--------------------------------------------------");
|
||||
}
|
||||
}
|
||||
}
|
80
emelyanov_artem_lab_6/DeterminantCalculator.java
Normal file
80
emelyanov_artem_lab_6/DeterminantCalculator.java
Normal file
@ -0,0 +1,80 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class DeterminantCalculator {
|
||||
public static int calculateDeterminant(int[][] matrix, int numThreads) throws InterruptedException, ExecutionException {
|
||||
int size = matrix.length;
|
||||
|
||||
// Если размер матрицы 1x1, возвращаем единственный элемент.
|
||||
if (size == 1) {
|
||||
return matrix[0][0];
|
||||
}
|
||||
|
||||
// Если количество потоков равно 1, выполняем последовательный алгоритм.
|
||||
if (numThreads == 1) {
|
||||
return sequentialDeterminant(matrix);
|
||||
}
|
||||
|
||||
// Иначе выполняем параллельный алгоритм.
|
||||
return parallelDeterminant(matrix, numThreads);
|
||||
}
|
||||
|
||||
private static int sequentialDeterminant(int[][] matrix) {
|
||||
int size = matrix.length;
|
||||
if (size == 1) {
|
||||
return matrix[0][0];
|
||||
}
|
||||
if (size == 2) {
|
||||
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
|
||||
}
|
||||
|
||||
int determinant = 0;
|
||||
for (int col = 0; col < size; col++) {
|
||||
determinant += (int) (Math.pow(-1, col) * matrix[0][col] * sequentialDeterminant(getMinor(matrix, 0, col)));
|
||||
}
|
||||
return determinant;
|
||||
}
|
||||
|
||||
private static int parallelDeterminant(int[][] matrix, int numThreads) throws InterruptedException, ExecutionException {
|
||||
int size = matrix.length;
|
||||
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
|
||||
List<Future<Double>> futures = new ArrayList<>();
|
||||
|
||||
for (int col = 0; col < size; col++) {
|
||||
int finalCol = col;
|
||||
futures.add(executor.submit(() -> {
|
||||
double minorDet = sequentialDeterminant(getMinor(matrix, 0, finalCol));
|
||||
return Math.pow(-1, finalCol) * matrix[0][finalCol] * minorDet;
|
||||
}));
|
||||
}
|
||||
|
||||
int determinant = 0;
|
||||
for (Future<Double> future : futures) {
|
||||
determinant += future.get();
|
||||
}
|
||||
|
||||
executor.shutdown();
|
||||
return determinant;
|
||||
}
|
||||
|
||||
private static int[][] getMinor(int[][] matrix, int row, int col) {
|
||||
int size = matrix.length;
|
||||
int[][] minor = new int[size - 1][size - 1];
|
||||
|
||||
for (int i = 0, mi = 0; i < size; i++) {
|
||||
if (i == row) continue;
|
||||
for (int j = 0, mj = 0; j < size; j++) {
|
||||
if (j == col) continue;
|
||||
minor[mi][mj] = matrix[i][j];
|
||||
mj++;
|
||||
}
|
||||
mi++;
|
||||
}
|
||||
return minor;
|
||||
}
|
||||
}
|
||||
|
11
emelyanov_artem_lab_6/MatrixGenerator.java
Normal file
11
emelyanov_artem_lab_6/MatrixGenerator.java
Normal file
@ -0,0 +1,11 @@
|
||||
public class MatrixGenerator {
|
||||
public static int[][] generateMatrix(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() * 100);
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
}
|
12
emelyanov_artem_lab_6/Readme.md
Normal file
12
emelyanov_artem_lab_6/Readme.md
Normal file
@ -0,0 +1,12 @@
|
||||
# Поиск детерминанта
|
||||
|
||||
Данная работа посвящена реализации и сравнению последовательного и параллельного алгоритмов поиска детерминанта матриц на языке Java. Целью является оценка производительности при использовании разного числа потоков.
|
||||
|
||||
## Результаты:
|
||||
|
||||
![img.png](images/img.png)
|
||||
|
||||
Как видим, однозначно сказать нельзя, для маленьких матриц многопоточность особо не повлияла. Начиная с матрицы размерностью 10, есть прибавка в производительности. Посмотрим на сложность алгоритма, и она будет что-то около O(n!), так как алгоритм рекурсивный.
|
||||
Для больших матриц, типа 100 на 100, сложность будет огромной, и не думаю что такое получится посчитать.
|
||||
|
||||
Ссылка на видео: https://drive.google.com/file/d/1eCNcSLLLfWGlOk5Z0y0CfOfkbxeASrdE/view?usp=sharing
|
BIN
emelyanov_artem_lab_6/images/img.png
Normal file
BIN
emelyanov_artem_lab_6/images/img.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 49 KiB |
BIN
emelyanov_artem_lab_6/img.png
Normal file
BIN
emelyanov_artem_lab_6/img.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 49 KiB |
8
emelyanov_artem_lab_6/out/production/emelyanov_artem_lab_6/.idea/.gitignore
vendored
Normal file
8
emelyanov_artem_lab_6/out/production/emelyanov_artem_lab_6/.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
@ -0,0 +1,8 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.lang.foreign.Arena,ofAuto,java.lang.foreign.Arena,global,java.util.concurrent.Executors,newFixedThreadPool" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="23" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/emelyanov_artem_lab_6.iml" filepath="$PROJECT_DIR$/.idea/emelyanov_artem_lab_6.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,6 @@
|
||||
# Поиск детерминанта
|
||||
|
||||
Данная работа посвящена реализации и сравнению последовательного и параллельного алгоритмов поиска детерминанта матриц на языке Java. Целью является оценка производительности при использовании разного числа потоков.
|
||||
|
||||
## Результаты:
|
||||
|
Loading…
Reference in New Issue
Block a user