good start2

This commit is contained in:
DmitriyAntonov 2023-12-04 23:02:40 +04:00
parent 266432cfda
commit a4a35b5529
2 changed files with 50 additions and 46 deletions

View File

@ -1,11 +1,12 @@
import multiprocessing
import numpy as np import numpy as np
import time import time
import concurrent.futures
def multiply_matrices(matrix_a, matrix_b): def multiply_matrices(matrix_a, matrix_b):
if len(matrix_a[0]) != len(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))] 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 return result
def multiply_matrices_parallel(matrix_a, matrix_b, num_threads): def multiply_row(args):
if len(matrix_a[0]) != len(matrix_b): matrix_a, matrix_b, i = args
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]))] row_result = [0 for _ in range(len(matrix_b[0]))]
for j in range(len(matrix_b[0])): for j in range(len(matrix_b[0])):
for k in range(len(matrix_b)): for k in range(len(matrix_b)):
row_result[j] += matrix_a[i][k] * matrix_b[k][j] 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): def benchmark_sequential(size):
@ -66,12 +67,12 @@ def benchmark_parallel(size, num_threads):
if __name__ == "__main__": if __name__ == "__main__":
sizes = [300] sizes = [300]
threads = [2, 8] threads = [2, 16, 32]
#
for size in sizes: # for size in sizes:
sequential_time = benchmark_sequential(size) # sequential_time = benchmark_sequential(size)
print(f"Время обычное: {sequential_time} с") # print(f"Время обычное: {sequential_time} с")
print(f"Размер матрицы: {size}x{size}") # print(f"Размер матрицы: {size}x{size}")
for thread in threads: for thread in threads:
for size in sizes: for size in sizes:

View File

@ -1,3 +1,5 @@
import multiprocessing
from flask import Flask, render_template, request from flask import Flask, render_template, request
import numpy as np import numpy as np
import concurrent.futures import concurrent.futures
@ -7,7 +9,7 @@ app = Flask(__name__)
def multiply_matrices(matrix_a, matrix_b): def multiply_matrices(matrix_a, matrix_b):
if len(matrix_a[0]) != len(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))] 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 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))] def multiply_row(args):
matrix_a, matrix_b, i = args
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]))] row_result = [0 for _ in range(len(matrix_b[0]))]
for j in range(len(matrix_b[0])): for j in range(len(matrix_b[0])):
for k in range(len(matrix_b)): for k in range(len(matrix_b)):
row_result[j] += matrix_a[i][k] * matrix_b[k][j] 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('/') @app.route('/')
def index(): def index():
@ -59,7 +62,7 @@ def multiply():
if operation_type == 'sequential': if operation_type == 'sequential':
result = multiply_matrices(matrix_a, matrix_b) result = multiply_matrices(matrix_a, matrix_b)
elif operation_type == 'parallel': elif operation_type == 'parallel':
result = multiply_matrices_parallel(matrix_a, matrix_b) result = multiply_matrices_parallel(matrix_a, matrix_b, 16)
else: else:
return "Invalid operation type" return "Invalid operation type"