Compare commits
No commits in common. "266432cfda1fee7d821bde14cdcdf2164c83ab65" and "0227f5eaa02d1ee05bbeba0fff0e22dc99d88bc4" have entirely different histories.
266432cfda
...
0227f5eaa0
@ -1,81 +0,0 @@
|
|||||||
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")
|
|
||||||
|
|
||||||
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)
|
|
||||||
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, 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, num_threads)
|
|
||||||
end_time = time.time()
|
|
||||||
|
|
||||||
return end_time - start_time
|
|
||||||
|
|
||||||
|
|
||||||
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}")
|
|
||||||
|
|
||||||
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}")
|
|
@ -1,70 +0,0 @@
|
|||||||
from flask import Flask, render_template, request
|
|
||||||
import numpy as np
|
|
||||||
import concurrent.futures
|
|
||||||
|
|
||||||
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")
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@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)
|
|
@ -1,24 +0,0 @@
|
|||||||
<!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>
|
|
@ -1,19 +0,0 @@
|
|||||||
<!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>
|
|
Loading…
x
Reference in New Issue
Block a user