From 0eb56e61dac4dc2b32942d89d9454509cabc5a9c Mon Sep 17 00:00:00 2001 From: KirillTepechin Date: Wed, 13 Dec 2023 12:22:31 +0400 Subject: [PATCH 1/2] tepechin_kirill_lab_5 --- tepechin_kirill_lab_5/README.md | 65 +++++++++++++++++++++++++++++++ tepechin_kirill_lab_5/result.png | Bin 0 -> 9608 bytes 2 files changed, 65 insertions(+) create mode 100644 tepechin_kirill_lab_5/README.md create mode 100644 tepechin_kirill_lab_5/result.png diff --git a/tepechin_kirill_lab_5/README.md b/tepechin_kirill_lab_5/README.md new file mode 100644 index 0000000..37e5c51 --- /dev/null +++ b/tepechin_kirill_lab_5/README.md @@ -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 \ No newline at end of file diff --git a/tepechin_kirill_lab_5/result.png b/tepechin_kirill_lab_5/result.png new file mode 100644 index 0000000000000000000000000000000000000000..a309b8c8d70f4feb28f833c52b101ccd14962e33 GIT binary patch literal 9608 zcmd6tcT`hRy0=wSq$^E2BGN)v=@5|6Yk*J%r1v77P(+X>Qlujy5D*EW_Zm=&U?_qR z5CTC21c88t5+IZt=g!iYe)I+QXPk7Dp*c1 zsa!{R^?HBYwT3XYi@HLTs=se|8C`~HFecr8k)?iFox0(U$ZQ?o=kxuuu{$D}O0ktc z(g}36+aSKZ!!?0@_Y%Gqnq(Ca=4hV zrn|R*Evt+@N7aLkX+K;Ur_rqspnaFC3}(Dm%{9*HjEDkh-f3!aqs;8&e#DI)*cTM8 zYB#MoWx*yFJTE}-32>8b?-ZE|R*Sw&hyJ?b#&&YK?-|wjP|-Az!W80ln|46p=;hNq zIMP?Vw2s~~!KP%%=Q&_tnxSyzY6?PGcP57 zDBQ*Bv2kYN7h4794yqf-G;pK=PaFVtjgx!&*AJ|^8abuu(_pm^u`CET4#3aBqzdw* zH7*C>2>5xSNktqzpmp`4DifU3NP;IpCrLQ^2|8(ek;_4P1RVKB&a{~7`gw8ZV+9E8 z&$afz?%%=1GJUdg&t6G@_N9J6D9>|IG;SmFHq0IhB`T`4+4z8DgEZ$=m&bW9dQqXq z>v8I32CF#VRWMThVf*HquBB$koRCb=;2W8sozf0Sdj%%?#xlQNtq;$_@(*7@T;io! z6Zl&ePWcp651G0KyyZLvl`9qb(?DUZhkJFpG<@pm5t^ooMcJXPlWX?VHioL1blWe2 z)YZZV|N1okc_ROv;#Y)Z{J8GmfXrp!WPAeRs6P(Sh|!@d(QXq8Q^HG2ZKB`HLE+3?`sQ z6{rS{+mHp2o7H>dt8#kEM@UU*Hh1o1>Y49SH@4_^?TG9h-@5brBwk1Vi}ia@DcQY{ zD7=+sy1tH>%HWoO2yC1{1q)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}`5Z;^KLi)PmsnPuy_?NzAMOeBIv1h)&!HYQu# zjr{uVOyPx(rWjA$#`~iRn#SOJ9h9`tT3*T+7`}K<%oVfBvxRqDiIbeV;{aFGs*hX0 z8+P7FZv_8$da$txNzO$-i+9;cPU7r9Xwg~sFS~*wux7T-Ei|FWs`k6uQ(yCF*pL(nSg#0mAh0$C_7c zuLQW|E|PLL@rU^7j!{@eNg<6z`Oa<7KKcw&mY)^LIHQERCfIj8Pp*p>bfV67r-s~8 zRb~3=T*Q>n+CAOwpl5(QGwrJi?t0GegUzT z|7ziLT{|3-k%T4vYWSRQl9C5ba;H^R336vm@@=wrn~2Vy2qUQGlD8@DwJg(2;(yZ? zt2Nt@G@1FaXkET-ap>lb${7Vkw$(R!=SSsR0S;LG`A+j^_Ym}-wM}{P5}MdTgx>B0 zF*;n9S=^IJer)p_&E(@#;_r~IW?u&%_|3>ke{mnH0gSofuK0-(u5r(_to zvju{1d~azrpIWv#UB8fR4<`YSa?uao!7Ty$ILe zy8AnvFG*wlouKFsubo*b?OTyt3ldXKkcQeGpAoEU7v Date: Wed, 13 Dec 2023 12:23:00 +0400 Subject: [PATCH 2/2] tepechin_kirill_lab_5 --- tepechin_kirill_lab_5/src/MatrixMultiply.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tepechin_kirill_lab_5/src/MatrixMultiply.java diff --git a/tepechin_kirill_lab_5/src/MatrixMultiply.java b/tepechin_kirill_lab_5/src/MatrixMultiply.java new file mode 100644 index 0000000..0dbdb63 --- /dev/null +++ b/tepechin_kirill_lab_5/src/MatrixMultiply.java @@ -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)); + } +}