61 lines
2.4 KiB
Python
61 lines
2.4 KiB
Python
import numpy as np
|
|
import time
|
|
from multiprocessing import Pool
|
|
|
|
def determinant(matrix):
|
|
return np.linalg.det(matrix)
|
|
|
|
def parallel_determinant(matrix, num_threads):
|
|
size = matrix.shape[0]
|
|
step = size // num_threads
|
|
pool = Pool(processes=num_threads)
|
|
blocks = []
|
|
for i in range(0, size, step):
|
|
blocks.append(matrix[i:i+step, i:i+step])
|
|
dets = pool.map(determinant, blocks)
|
|
return np.prod(dets)
|
|
|
|
def benchmark(num_threads):
|
|
matrix1 = np.random.rand(100, 100)
|
|
start_time = time.time()
|
|
det1 = determinant(matrix1)
|
|
end_time = time.time()
|
|
execution_time = end_time - start_time
|
|
print("Матрица 100x100:", matrix1)
|
|
print("Детерминант матрицы 100x100:", det1)
|
|
print("Последовательное время выполнения:", execution_time, "сек")
|
|
start_time = time.time()
|
|
detp1 = parallel_determinant(matrix1, num_threads)
|
|
end_time = time.time()
|
|
execution_time = end_time - start_time
|
|
print("Пареллельное время выполнения:", execution_time, "сек")
|
|
matrix2 = np.random.rand(300, 300)
|
|
start_time = time.time()
|
|
det2 = determinant(matrix2)
|
|
end_time = time.time()
|
|
execution_time = end_time - start_time
|
|
print("Матрица 300x300:", matrix2)
|
|
print("Детерминант матрицы 300x300:", det2)
|
|
print("Последовательное время выполнения:", execution_time, "сек")
|
|
start_time = time.time()
|
|
detp2 = parallel_determinant(matrix2, num_threads)
|
|
end_time = time.time()
|
|
execution_time = end_time - start_time
|
|
print("Пареллельное время выполнения:", execution_time, "сек")
|
|
matrix3 = np.random.rand(500, 500)
|
|
det3 = determinant(matrix3)
|
|
end_time = time.time()
|
|
execution_time = end_time - start_time
|
|
print("Матрица 500x500:", matrix3)
|
|
print("Детерминант матрицы 500x500:", det3)
|
|
print("Последовательное время выполнения:", execution_time, "сек")
|
|
start_time = time.time()
|
|
detp3 = parallel_determinant(matrix3, num_threads)
|
|
end_time = time.time()
|
|
execution_time = end_time - start_time
|
|
print("Пареллельное время выполнения:", execution_time, "сек")
|
|
|
|
if __name__ == "__main__":
|
|
print("Введите кол-во поток")
|
|
n = int(input())
|
|
benchmark(n) |