tepechin_kirill_lab_5 #48
65
tepechin_kirill_lab_5/README.md
Normal file
65
tepechin_kirill_lab_5/README.md
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
## Лабораторная работа №5, Тепечин Кирилл
|
||||||
|
|
||||||
|
### Код
|
||||||
|
|
||||||
|
#### Обычный код
|
||||||
|
|
||||||
|
````java
|
||||||
|
public static int[][] multiplySync(int[][] a, int[][] b) {
|
||||||
|
final int[][] result = new int[a.length][b[0].length];
|
||||||
|
for (int i = 0; i < a.length; ++i) {
|
||||||
|
for (int j = 0; j < b[0].length; ++j) {
|
||||||
|
result[i][j] = calculateSingleValue(i, j, a, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int calculateSingleValue(int i, int j, int[][] a, int[][] b) {
|
||||||
|
int result = 0;
|
||||||
|
for (int k = 0; k < a[0].length; ++k) {
|
||||||
|
result += a[i][k] * b[k][j];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
````
|
||||||
|
#### Параллельный код
|
||||||
|
|
||||||
|
````java
|
||||||
|
final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||||
|
|
||||||
|
public static int[][] multiplyAsync(int[][] a, int[][] b, ExecutorService executor) throws InterruptedException {
|
||||||
|
final int[][] result = new int[a.length][b[0].length];
|
||||||
|
for (int i = 0; i < a.length; ++i) {
|
||||||
|
final int fi = i;
|
||||||
|
executor.execute(() -> {
|
||||||
|
for (int j = 0; j < b[0].length; ++j) {
|
||||||
|
for (int k = 0; k < a[0].length; ++k) {
|
||||||
|
result[fi][j] += a[fi][k] * b[k][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
executor.shutdown();
|
||||||
|
executor.awaitTermination(1, TimeUnit.DAYS);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
````
|
||||||
|
|
||||||
|
### Тесты
|
||||||
|
|
||||||
|
Тесты проводились на 16 потоках
|
||||||
|
|
||||||
|
![result](result.png)
|
||||||
|
|
||||||
|
### Выводы
|
||||||
|
|
||||||
|
* На матрице 100x100 последовательное вычисление быстрее, затрачивается всего 3 мс.
|
||||||
|
|
||||||
|
* На матрице 300x300 вычисления занимают примерно одинаковое время.
|
||||||
|
|
||||||
|
* На матрице 500x500 параллельное вычисление выполняется уже в 4 раза быстрее
|
||||||
|
|
||||||
|
### Ссылка на видео
|
||||||
|
|
||||||
|
https://youtu.be/GPjedqzwvt4
|
BIN
tepechin_kirill_lab_5/result.png
Normal file
BIN
tepechin_kirill_lab_5/result.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 9.4 KiB |
81
tepechin_kirill_lab_5/src/MatrixMultiply.java
Normal file
81
tepechin_kirill_lab_5/src/MatrixMultiply.java
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class MatrixMultiply {
|
||||||
|
|
||||||
|
public static int[][] generateMatrix(int n) {
|
||||||
|
int[][] matrix = new int[n][n];
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < n; j++) {
|
||||||
|
matrix[i][j] = (int) (Math.random() * 100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static int calculateSingleValue(int i, int j, int[][] a, int[][] b) {
|
||||||
|
int result = 0;
|
||||||
|
for (int k = 0; k < a[0].length; ++k) {
|
||||||
|
result += a[i][k] * b[k][j];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static int[][] multiplySync(int[][] a, int[][] b) {
|
||||||
|
final int[][] result = new int[a.length][b[0].length];
|
||||||
|
for (int i = 0; i < a.length; ++i) {
|
||||||
|
for (int j = 0; j < b[0].length; ++j) {
|
||||||
|
result[i][j] = calculateSingleValue(i, j, a, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[][] multiplyAsync(int[][] a, int[][] b, ExecutorService executor) throws InterruptedException {
|
||||||
|
final int[][] result = new int[a.length][b[0].length];
|
||||||
|
for (int i = 0; i < a.length; ++i) {
|
||||||
|
final int fi = i;
|
||||||
|
executor.execute(() -> {
|
||||||
|
for (int j = 0; j < b[0].length; ++j) {
|
||||||
|
for (int k = 0; k < a[0].length; ++k) {
|
||||||
|
result[fi][j] += a[fi][k] * b[k][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
executor.shutdown();
|
||||||
|
executor.awaitTermination(1, TimeUnit.DAYS);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
run(100);
|
||||||
|
run(300);
|
||||||
|
run(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void run(int n) throws InterruptedException {
|
||||||
|
System.out.println("N = " + n);
|
||||||
|
final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||||
|
|
||||||
|
final int[][] a = generateMatrix(n);
|
||||||
|
final int[][] b = generateMatrix(n);
|
||||||
|
|
||||||
|
long time = System.currentTimeMillis();
|
||||||
|
final int[][] productSync = multiplySync(a, b);
|
||||||
|
System.out.println("Time sync: " + (System.currentTimeMillis() - time));
|
||||||
|
|
||||||
|
time = System.currentTimeMillis();
|
||||||
|
final int[][] productAsync = multiplyAsync(a, b, executorService);
|
||||||
|
System.out.println("Time async: " + (System.currentTimeMillis() - time));
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
// System.out.println(Arrays.deepEquals(productAsync, productSync));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user