2023-12-04 23:02:40 +04:00
|
|
|
|
import multiprocessing
|
|
|
|
|
|
2023-12-04 21:25:59 +04:00
|
|
|
|
import numpy as np
|
|
|
|
|
import time
|
|
|
|
|
|
2023-12-04 22:40:49 +04:00
|
|
|
|
|
2023-12-04 21:25:59 +04:00
|
|
|
|
def multiply_matrices(matrix_a, matrix_b):
|
2023-12-04 22:40:49 +04:00
|
|
|
|
if len(matrix_a[0]) != len(matrix_b):
|
2023-12-04 23:02:40 +04:00
|
|
|
|
raise ValueError("матрицы имеют разную длину")
|
2023-12-04 22:40:49 +04:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2023-12-04 23:02:40 +04:00
|
|
|
|
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, i
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def multiply_matrices_parallel(matrix_a, matrix_b, threads):
|
2023-12-04 22:40:49 +04:00
|
|
|
|
if len(matrix_a[0]) != len(matrix_b):
|
2023-12-04 23:02:40 +04:00
|
|
|
|
raise ValueError("матрицы имеют разную длину")
|
2023-12-04 22:40:49 +04:00
|
|
|
|
|
|
|
|
|
result = [[0 for _ in range(len(matrix_b[0]))] for _ in range(len(matrix_a))]
|
|
|
|
|
|
2023-12-04 23:02:40 +04:00
|
|
|
|
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)
|
2023-12-04 22:40:49 +04:00
|
|
|
|
|
2023-12-04 23:02:40 +04:00
|
|
|
|
for row_result, row_index in rows_results:
|
|
|
|
|
result[row_index] = row_result
|
2023-12-04 22:40:49 +04:00
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
2023-12-04 21:25:59 +04:00
|
|
|
|
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
|
|
|
|
|
|
2023-12-04 22:40:49 +04:00
|
|
|
|
|
|
|
|
|
def benchmark_parallel(size, num_threads):
|
2023-12-04 21:25:59 +04:00
|
|
|
|
matrix_a = np.random.rand(size, size)
|
|
|
|
|
matrix_b = np.random.rand(size, size)
|
|
|
|
|
|
|
|
|
|
start_time = time.time()
|
2023-12-04 22:40:49 +04:00
|
|
|
|
multiply_matrices_parallel(matrix_a, matrix_b, num_threads)
|
2023-12-04 21:25:59 +04:00
|
|
|
|
end_time = time.time()
|
|
|
|
|
|
|
|
|
|
return end_time - start_time
|
|
|
|
|
|
2023-12-04 22:40:49 +04:00
|
|
|
|
|
2023-12-04 21:25:59 +04:00
|
|
|
|
if __name__ == "__main__":
|
2023-12-05 13:01:36 +04:00
|
|
|
|
sizes = [100, 300, 500]
|
2023-12-04 23:02:40 +04:00
|
|
|
|
threads = [2, 16, 32]
|
2023-12-05 13:01:36 +04:00
|
|
|
|
|
|
|
|
|
for size in sizes:
|
|
|
|
|
sequential_time = benchmark_sequential(size)
|
|
|
|
|
print(f"Время обычное: {sequential_time} с")
|
|
|
|
|
print(f"Размер матрицы: {size}x{size}")
|
2023-12-04 22:40:49 +04:00
|
|
|
|
|
|
|
|
|
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}")
|