From ad8894c0ca7f44a97830050a9f39b29a93644cd1 Mon Sep 17 00:00:00 2001 From: Inohara Date: Fri, 25 Oct 2024 16:56:56 +0400 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BE,=20?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D0=B0=D0=BB=D0=BE=D1=81=D1=8C=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=B0=D1=82=D1=8C=20=D1=80=D0=B8=D0=B4=D0=BC?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tsukanova_irina_lab_5/README.md | 11 +++++++ tsukanova_irina_lab_5/main.py | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tsukanova_irina_lab_5/README.md create mode 100644 tsukanova_irina_lab_5/main.py diff --git a/tsukanova_irina_lab_5/README.md b/tsukanova_irina_lab_5/README.md new file mode 100644 index 0000000..512c50b --- /dev/null +++ b/tsukanova_irina_lab_5/README.md @@ -0,0 +1,11 @@ +# Цуканова Ирина ПИбд-42 +# Лабораторная работа №5 - Параллельное перемножение матриц + + +## Результат: + + +## Вывод: + + +## [Видео]() \ No newline at end of file diff --git a/tsukanova_irina_lab_5/main.py b/tsukanova_irina_lab_5/main.py new file mode 100644 index 0000000..2f6ffd2 --- /dev/null +++ b/tsukanova_irina_lab_5/main.py @@ -0,0 +1,52 @@ +import multiprocessing +import time +from time import time + +import numpy as np + + +def matrix_multi(first, second, res, start_i, stop_i, size): + for i in range(start_i, stop_i): + for j in range(size): + res[i][j] = 0 + for k in range(size): + res[i][j] += first[i][k] * second[k][j] + + +def do(first, second, size, threads): + offset = int(size / threads) + offset_last = size % threads + offset + + processes = [] + res = np.zeros((size, size)) + start_test = time() + for i in range(threads): + start_ = i * offset + stop_ = start_ + offset_last if i == threads - 1 else start_ + offset + + process = multiprocessing.Process(target=matrix_multi, args=(first, second, res, start_, stop_, size)) + processes.append(process) + process.start() + + for p in processes: + p.join() + stop_test = time() + print(f'{n}x{n}, time: {stop_test - start_test}') + + +if __name__ == "__main__": + threads_count = 20 # задание кол-ва потоков + sizes = [100, 300, 500] + test = [[5, 3, 4], [2, 8, 3], [6, 3, 4]] + test2 = [[4, 3, 6], [2, 8, 3], [5, 6, 4]] + + threads_counts = [1, 4, 6, 8] + for threads in threads_counts: + print('-------------------------------------------------') + print(f'Threads:{threads}') + for n in sizes: + first_matrix = np.random.randint(3, size=(n, n)) + second_matrix = np.random.randint(3, size=(n, n)) + do(first_matrix, second_matrix, n, threads) + + print('-------------------------------------------------')