From f4db3f2d438a17c968d659d927b8e3cbcc1ce128 Mon Sep 17 00:00:00 2001 From: = <=> Date: Mon, 23 Dec 2024 15:53:45 +0400 Subject: [PATCH] =?UTF-8?q?5=20=D0=BB=D0=B0=D0=B1=D0=B0=20=D0=B1=D0=B5?= =?UTF-8?q?=D0=B7=20=D1=80=D0=B8=D0=B4=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- karamushko_maxim_lab_5/main.py | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 karamushko_maxim_lab_5/main.py diff --git a/karamushko_maxim_lab_5/main.py b/karamushko_maxim_lab_5/main.py new file mode 100644 index 0000000..728626f --- /dev/null +++ b/karamushko_maxim_lab_5/main.py @@ -0,0 +1,57 @@ +import random +import time +import multiprocessing + +# Генерация матрицы +def matrix_generate(size): + return [[random.randint(0, 50) for _ in range(size)] for _ in range(size)] + +# Умножение строк +def row_multi(i, A, B, result): + size = len(A) + for j in range(size): + for k in range(size): + result[i][j] += A[i][k] * B[k][j] + +# параллельное умножение с помощью multiprocessing +def matrix_mulit_parallel(A, B, num_processes): + size = len(A) + result = [[0] * size for _ in range(size)] + + with multiprocessing.Pool(processes=num_processes) as pool: + pool.starmap(row_multi, [(i, A, B, result) for i in range(size)]) + + return result + +# Замер времени +def measure_time(size, num_processes=1): + A = matrix_generate(size) + B = matrix_generate(size) + + start_time = time.time() + matrix_mulit_parallel(A, B, num_processes) + par_time = time.time() - start_time + + return par_time + +def main(): + # Размеры матриц + matrix_sizes = [100, 300, 500] + # Количество потоков + num_processes_list = [1, 2, 4, 6, 8] + # Таблица с замерами времени + print("-*" * 40) + print(f"{'Количество потоков':<20}{'|100x100 (сек.)':<20}{'|300x300 (сек.)':<20}{'|500x500 (сек.)'}") + print("-*" * 40) + + for num_processes in num_processes_list: + row = f"{num_processes:<20}" + + for size in matrix_sizes: + par_time = measure_time(size, num_processes) + row += f"|{par_time:.4f}".ljust(20) + print(row) + print("-*" * 40) + +if __name__ == "__main__": + main() \ No newline at end of file