DAS_2024_1/chernyshov_nikita_lab_6/application.py
Никита Чернышов 7f775233bf chernyshov_nikita_lab_6 is ready
2024-12-15 17:13:28 +04:00

51 lines
1.5 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
import argparse
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
blocks = []
for i in range(num_processes):
start_row = i * step
end_row = start_row + step if i < num_processes - 1 else size
blocks.append(matrix[start_row:end_row, start_row:end_row])
pool = Pool(processes=num_processes)
dets = pool.map(determinant_block, blocks)
pool.close()
pool.join()
return np.prod(dets)
def benchmark(size, num_processes):
matrix = np.random.rand(size, size)
start = time.time()
det_parallel = determinant_parallel(matrix, num_processes)
end = time.time()
print(f"Матрица {size}x{size} с {num_processes} процессами заняла {end - start:.5f} сек (Параллельно)")
start = time.time()
det_seq = determinant_block(matrix)
end = time.time()
print(f"Матрица {size}x{size} последовательный вычисление заняло {end - start:.5f} сек (Последовательно)")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Вычисление детерминанта с параллельной обработкой")
parser.add_argument("--processes", type=int, default=4)
args = parser.parse_args()
sizes = [100, 300, 500]
for size in sizes:
benchmark(size, args.processes)