From f279209740ce21d60c1b425e04c5bd51f6646fed Mon Sep 17 00:00:00 2001 From: DmitriyAntonov Date: Mon, 4 Dec 2023 21:25:59 +0400 Subject: [PATCH] good start --- antonov_dmitry_lab_5/benchmark.py | 43 ++++++++++++++++++++++ antonov_dmitry_lab_5/simple_app.py | 43 ++++++++++++++++++++++ antonov_dmitry_lab_5/templates/index.html | 24 ++++++++++++ antonov_dmitry_lab_5/templates/result.html | 19 ++++++++++ 4 files changed, 129 insertions(+) create mode 100644 antonov_dmitry_lab_5/benchmark.py create mode 100644 antonov_dmitry_lab_5/simple_app.py create mode 100644 antonov_dmitry_lab_5/templates/index.html create mode 100644 antonov_dmitry_lab_5/templates/result.html diff --git a/antonov_dmitry_lab_5/benchmark.py b/antonov_dmitry_lab_5/benchmark.py new file mode 100644 index 0000000..bb21479 --- /dev/null +++ b/antonov_dmitry_lab_5/benchmark.py @@ -0,0 +1,43 @@ +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 benchmark_sequential(size): + matrix_a = np.random.rand(size, size) + matrix_b = np.random.rand(size, size) + + start_time = time.time() + multiply_matrices(matrix_a, matrix_b) + end_time = time.time() + + return end_time - start_time + +def benchmark_parallel(size): + 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) + end_time = time.time() + + return end_time - start_time + +if __name__ == "__main__": + sizes = [100, 300, 500, 700, 900, 1000, 1200, 1400, 1700, 2000] + + 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") diff --git a/antonov_dmitry_lab_5/simple_app.py b/antonov_dmitry_lab_5/simple_app.py new file mode 100644 index 0000000..b714a15 --- /dev/null +++ b/antonov_dmitry_lab_5/simple_app.py @@ -0,0 +1,43 @@ +from flask import Flask, render_template, request +import numpy as np +import concurrent.futures + +app = Flask(__name__) + + +def multiply_matrices(matrix_a, matrix_b): + result = np.dot(matrix_a, matrix_b) + return 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('/') +def index(): + return render_template('index.html') + + +@app.route('/multiply', methods=['POST']) +def multiply(): + n = int(request.form.get('matrix_a')) + matrix_a = np.random.randint(10, size=(n, n)) + matrix_b = np.random.randint(10, size=(n, n)) + + operation_type = request.form.get('operation_type') + + if operation_type == 'sequential': + result = multiply_matrices(matrix_a, matrix_b) + elif operation_type == 'parallel': + result = multiply_matrices_parallel(matrix_a, matrix_b) + else: + return "Invalid operation type" + + return render_template('result.html', matrix_a=matrix_a, matrix_b=matrix_b, result=result) + + +if __name__ == '__main__': + app.run(debug=True) diff --git a/antonov_dmitry_lab_5/templates/index.html b/antonov_dmitry_lab_5/templates/index.html new file mode 100644 index 0000000..ddec7e9 --- /dev/null +++ b/antonov_dmitry_lab_5/templates/index.html @@ -0,0 +1,24 @@ + + + + + + + Matrix Multiplication + + +

Matrix Multiplication

+
+ +
+ + +
+ + +
+ + diff --git a/antonov_dmitry_lab_5/templates/result.html b/antonov_dmitry_lab_5/templates/result.html new file mode 100644 index 0000000..74775bd --- /dev/null +++ b/antonov_dmitry_lab_5/templates/result.html @@ -0,0 +1,19 @@ + + + + + + + Matrix Multiplication Result + + +

Результат

+

Matrix A:

+
{{ matrix_a }}
+

Matrix B:

+
{{ matrix_b }}
+

Result:

+
{{ result }}
+ Назад + +