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
2 changed files with 50 additions and 46 deletions
Showing only changes of commit a4a35b5529 - Show all commits

View File

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

View File

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