DAS_2023_1/shadaev_anton_lab_6/main.py
2023-12-23 19:56:19 +04:00

40 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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("=======================================")