From 266432cfda1fee7d821bde14cdcdf2164c83ab65 Mon Sep 17 00:00:00 2001 From: DmitriyAntonov Date: Mon, 4 Dec 2023 22:40:49 +0400 Subject: [PATCH] good start2 --- antonov_dmitry_lab_5/benchmark.py | 66 +++++++++++++++++++++++------- antonov_dmitry_lab_5/simple_app.py | 37 ++++++++++++++--- 2 files changed, 84 insertions(+), 19 deletions(-) diff --git a/antonov_dmitry_lab_5/benchmark.py b/antonov_dmitry_lab_5/benchmark.py index bb21479..8221303 100644 --- a/antonov_dmitry_lab_5/benchmark.py +++ b/antonov_dmitry_lab_5/benchmark.py @@ -2,13 +2,45 @@ import numpy as np import time import concurrent.futures -def multiply_matrices(matrix_a, matrix_b): - return np.dot(matrix_a, matrix_b) -def multiply_matrices_parallel(matrix_a, matrix_b): - with concurrent.futures.ThreadPoolExecutor() as executor: - result = executor.submit(np.dot, matrix_a, matrix_b) - return result.result() +def multiply_matrices(matrix_a, matrix_b): + 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))] + + for i in range(len(matrix_a)): + for j in range(len(matrix_b[0])): + for k in range(len(matrix_b)): + result[i][j] += matrix_a[i][k] * matrix_b[k][j] + + 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): + 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 + def benchmark_sequential(size): matrix_a = np.random.rand(size, size) @@ -20,24 +52,30 @@ def benchmark_sequential(size): return end_time - start_time -def benchmark_parallel(size): + +def benchmark_parallel(size, num_threads): matrix_a = np.random.rand(size, size) matrix_b = np.random.rand(size, size) start_time = time.time() - multiply_matrices_parallel(matrix_a, matrix_b) + multiply_matrices_parallel(matrix_a, matrix_b, num_threads) end_time = time.time() return end_time - start_time + if __name__ == "__main__": - sizes = [100, 300, 500, 700, 900, 1000, 1200, 1400, 1700, 2000] + sizes = [300] + threads = [2, 8] for size in sizes: sequential_time = benchmark_sequential(size) - parallel_time = benchmark_parallel(size) - - print(f"Размер матрицы: {size}x{size}") print(f"Время обычное: {sequential_time} с") - print(f"Время параллельное: {parallel_time} с") - print(f"Ускорение: {sequential_time / parallel_time}\n") + print(f"Размер матрицы: {size}x{size}") + + for thread in threads: + for size in sizes: + parallel_time = benchmark_parallel(size, thread) + print(f"Размер матрицы: {size}x{size}") + print(f"Время параллельное: {parallel_time} с") + print(f"Потоков: {thread}") diff --git a/antonov_dmitry_lab_5/simple_app.py b/antonov_dmitry_lab_5/simple_app.py index b714a15..87d173e 100644 --- a/antonov_dmitry_lab_5/simple_app.py +++ b/antonov_dmitry_lab_5/simple_app.py @@ -6,14 +6,41 @@ app = Flask(__name__) def multiply_matrices(matrix_a, matrix_b): - result = np.dot(matrix_a, matrix_b) + 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))] + + for i in range(len(matrix_a)): + for j in range(len(matrix_b[0])): + for k in range(len(matrix_b)): + result[i][j] += matrix_a[i][k] * matrix_b[k][j] + 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): + 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 -def multiply_matrices_parallel(matrix_a, matrix_b): - with concurrent.futures.ThreadPoolExecutor() as executor: - result = executor.submit(np.dot, matrix_a, matrix_b) - return result.result() @app.route('/')