From a4a35b552985ed38e6db4abdca920a39ea32582e Mon Sep 17 00:00:00 2001 From: DmitriyAntonov Date: Mon, 4 Dec 2023 23:02:40 +0400 Subject: [PATCH] good start2 --- antonov_dmitry_lab_5/benchmark.py | 55 +++++++++++++++--------------- antonov_dmitry_lab_5/simple_app.py | 41 +++++++++++----------- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/antonov_dmitry_lab_5/benchmark.py b/antonov_dmitry_lab_5/benchmark.py index 8221303..1d4db87 100644 --- a/antonov_dmitry_lab_5/benchmark.py +++ b/antonov_dmitry_lab_5/benchmark.py @@ -1,11 +1,12 @@ +import multiprocessing + import numpy as np import time -import concurrent.futures def multiply_matrices(matrix_a, matrix_b): if len(matrix_a[0]) != len(matrix_b): - raise ValueError("Incompatible matrix dimensions for multiplication") + raise ValueError("матрицы имеют разную длину") result = [[0 for _ in range(len(matrix_b[0]))] for _ in range(len(matrix_a))] @@ -17,29 +18,29 @@ def multiply_matrices(matrix_a, matrix_b): return result -def multiply_matrices_parallel(matrix_a, matrix_b, num_threads): - if len(matrix_a[0]) != len(matrix_b): - raise ValueError("Incompatible matrix dimensions for multiplication") - - result = [[0 for _ in range(len(matrix_b[0]))] for _ in range(len(matrix_a))] - - with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor: - futures = [] - for i in range(len(matrix_a)): - futures.append(executor.submit(_multiply_row, matrix_a, matrix_b, i)) - - for i, future in enumerate(concurrent.futures.as_completed(futures)): - result[i] = future.result() - - return result - - -def _multiply_row(matrix_a, matrix_b, i): +def multiply_row(args): + matrix_a, matrix_b, i = args row_result = [0 for _ in range(len(matrix_b[0]))] for j in range(len(matrix_b[0])): for k in range(len(matrix_b)): row_result[j] += matrix_a[i][k] * matrix_b[k][j] - return row_result + return row_result, i + + +def multiply_matrices_parallel(matrix_a, matrix_b, threads): + if len(matrix_a[0]) != len(matrix_b): + raise ValueError("матрицы имеют разную длину") + + result = [[0 for _ in range(len(matrix_b[0]))] for _ in range(len(matrix_a))] + + with multiprocessing.Pool(processes=threads) as pool: + args_list = [(matrix_a, matrix_b, i) for i in range(len(matrix_a))] + rows_results = pool.map(multiply_row, args_list) + + for row_result, row_index in rows_results: + result[row_index] = row_result + + return result def benchmark_sequential(size): @@ -66,12 +67,12 @@ def benchmark_parallel(size, num_threads): if __name__ == "__main__": sizes = [300] - threads = [2, 8] - - for size in sizes: - sequential_time = benchmark_sequential(size) - print(f"Время обычное: {sequential_time} с") - print(f"Размер матрицы: {size}x{size}") + threads = [2, 16, 32] + # + # for size in sizes: + # sequential_time = benchmark_sequential(size) + # print(f"Время обычное: {sequential_time} с") + # print(f"Размер матрицы: {size}x{size}") for thread in threads: for size in sizes: diff --git a/antonov_dmitry_lab_5/simple_app.py b/antonov_dmitry_lab_5/simple_app.py index 87d173e..740bcda 100644 --- a/antonov_dmitry_lab_5/simple_app.py +++ b/antonov_dmitry_lab_5/simple_app.py @@ -1,3 +1,5 @@ +import multiprocessing + from flask import Flask, render_template, request import numpy as np import concurrent.futures @@ -7,7 +9,7 @@ app = Flask(__name__) def multiply_matrices(matrix_a, matrix_b): if len(matrix_a[0]) != len(matrix_b): - raise ValueError("Incompatible matrix dimensions for multiplication") + raise ValueError("матрицы имеют разную длину") result = [[0 for _ in range(len(matrix_b[0]))] for _ in range(len(matrix_a))] @@ -18,30 +20,31 @@ def multiply_matrices(matrix_a, matrix_b): return result -def multiply_matrices_parallel(matrix_a, matrix_b, num_threads): - if len(matrix_a[0]) != len(matrix_b): - raise ValueError("Incompatible matrix dimensions for multiplication") - result = [[0 for _ in range(len(matrix_b[0]))] for _ in range(len(matrix_a))] - - with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor: - futures = [] - for i in range(len(matrix_a)): - futures.append(executor.submit(_multiply_row, matrix_a, matrix_b, i)) - - for i, future in enumerate(concurrent.futures.as_completed(futures)): - result[i] = future.result() - - return result - -def _multiply_row(matrix_a, matrix_b, i): +def multiply_row(args): + matrix_a, matrix_b, i = args row_result = [0 for _ in range(len(matrix_b[0]))] for j in range(len(matrix_b[0])): for k in range(len(matrix_b)): row_result[j] += matrix_a[i][k] * matrix_b[k][j] - return row_result + return row_result, i +def multiply_matrices_parallel(matrix_a, matrix_b, threads): + if len(matrix_a[0]) != len(matrix_b): + raise ValueError("матрицы имеют разную длину") + + result = [[0 for _ in range(len(matrix_b[0]))] for _ in range(len(matrix_a))] + + with multiprocessing.Pool(processes=threads) as pool: + args_list = [(matrix_a, matrix_b, i) for i in range(len(matrix_a))] + rows_results = pool.map(multiply_row, args_list) + + for row_result, row_index in rows_results: + result[row_index] = row_result + + return result + @app.route('/') def index(): @@ -59,7 +62,7 @@ def multiply(): if operation_type == 'sequential': result = multiply_matrices(matrix_a, matrix_b) elif operation_type == 'parallel': - result = multiply_matrices_parallel(matrix_a, matrix_b) + result = multiply_matrices_parallel(matrix_a, matrix_b, 16) else: return "Invalid operation type"