gusev_vladislav_lab_4 is ready

This commit is contained in:
vladg 2023-12-13 15:02:01 +04:00
parent b75a601ab9
commit 775bd41749

View File

@ -1,49 +0,0 @@
import time
import random
from scipy.linalg import det
from multiprocessing import Pool
def sequential_matrix_determinant(matrix):
return det(matrix)
def parallel_matrix_determinant_worker(args):
matrix, row_start, row_end = args
submatrix = [row[:min(row_end - row_start, len(matrix[0]))] for row in matrix[row_start:row_end]]
return det(submatrix)
def parallel_matrix_determinant(matrix, num_processes=2):
num_rows = len(matrix)
chunk_size = num_rows // num_processes
pool = Pool(processes=num_processes)
results = pool.map(parallel_matrix_determinant_worker, [(matrix, i * chunk_size, (i + 1) * chunk_size) for i in range(num_processes)])
pool.close()
pool.join()
return results[0]
def run_determinant_test(matrix_size, num_processes=2):
matrix = [[random.random() for _ in range(matrix_size)] for _ in range(matrix_size)]
start_time = time.time()
result_sequential = sequential_matrix_determinant(matrix)
sequential_time = time.time() - start_time
print(f"Sequential determinant calculation time ({matrix_size}x{matrix_size}): {sequential_time} seconds")
print(f"Sequential determinant result: {result_sequential}")
start_time = time.time()
result_parallel = parallel_matrix_determinant(matrix, num_processes)
parallel_time = time.time() - start_time
print(f"Parallel determinant calculation time ({matrix_size}x{matrix_size}) with {num_processes} processes: {parallel_time} seconds")
print(f"Parallel determinant result: {result_parallel}")
if __name__ == '__main__':
# Тесты для квадратных матриц размером 100x100, 300x300 и 500x500 с разным числом процессов
run_determinant_test(100, num_processes=2)
run_determinant_test(100, num_processes=4)
run_determinant_test(300, num_processes=2)
run_determinant_test(300, num_processes=4)
run_determinant_test(500, num_processes=2)
run_determinant_test(500, num_processes=4)