DAS_2023_1/shadaev_anton_lab_6/main.py

40 lines
1.1 KiB
Python
Raw Normal View History

2023-12-23 19:56:19 +04:00
import numpy as np
from multiprocessing import Pool
import time
def determinant_block(matrix_block):
return np.linalg.det(matrix_block)
def determinant_parallel(matrix, num_processes):
size = matrix.shape[0]
step = size // num_processes
pool = Pool(processes=num_processes)
blocks = []
for i in range(0, size, step):
blocks.append(matrix[i:i+step, i:i+step])
dets = pool.map(determinant_block, blocks)
return np.product(dets)
if __name__ == "__main__":
sizes = [100, 300, 500]
processes = [2, 4, 8]
for size in sizes:
matrix = np.random.rand(size, size)
for p in processes:
start = time.time()
det = determinant_parallel(matrix, p)
end = time.time()
print(f"{size}x{size} матрица с {p} процессами заняла {end - start:.5f} сек")
start = time.time()
det_seq = determinant_block(matrix)
end = time.time()
print(f"{size}x{size} умн. последовательно заняло {end - start:.5f} сек")
print("=======================================")