antonov_dmitry_lab_5 #35

Merged
Alexey merged 4 commits from antonov_dmitry_lab_5 into main 2023-12-05 22:35:42 +04:00
4 changed files with 129 additions and 0 deletions
Showing only changes of commit f279209740 - Show all commits

View File

@ -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")

View File

@ -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)

View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Matrix Multiplication</title>
</head>
<body>
<h1>Matrix Multiplication</h1>
<form action="/multiply" method="post">
<label for="matrix_a">Matrix размер:</label>
<input type="text" name="matrix_a" required><br>
<label for="operation_type">Как умножить:</label>
<select name="operation_type">
<option value="sequential">Последовательно</option>
<option value="parallel">Параллельно</option>
</select><br>
<button type="submit">Умножение матриц</button>
</form>
</body>
</html>

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Matrix Multiplication Result</title>
</head>
<body>
<h1>Результат</h1>
<p>Matrix A:</p>
<pre>{{ matrix_a }}</pre>
<p>Matrix B:</p>
<pre>{{ matrix_b }}</pre>
<p>Result:</p>
<pre>{{ result }}</pre>
<a href="/">Назад</a>
</body>
</html>