Compare commits

..

2 Commits

Author SHA1 Message Date
f944b01cca лаба 6 2024-11-07 16:33:18 +04:00
63f09a3c0b лаба 6 2024-11-07 16:32:56 +04:00

View File

@ -0,0 +1,72 @@
import numpy as np
from time import time
import multiprocessing
def do_task(matrix, start_j, stop_j, queue):
size = len(matrix[0] - 1)
if size == 2:
return matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1]
else:
res = 0
for j in range(start_j, stop_j):
tmp = np.delete(matrix, 0, axis=0)
tmp = np.delete(tmp, j, axis=1)
a = matrix[0][j]
b = do_task(tmp, 0, len(tmp[0]), None)
if j % 2 == 0:
res += a * b
else:
res += a * b * (-1)
# print(res)
if queue:
queue.put(res)
return res
def do_threads(matrix, size, threads):
offset = int(size / threads)
offset_last = size % threads + offset
processes = []
queue = multiprocessing.Queue()
start_test = time()
for i in range(threads):
start_ = i * offset
stop_ = start_ + offset_last if i == threads - 1 else start_ + offset
process = multiprocessing.Process(target=do_task, args=(matrix, start_, stop_, queue))
processes.append(process)
process.start()
total_result = 0
for p in processes:
p.join()
total_result += queue.get()
# print(total_result)
stop_test = time()
print(f'{size}x{size}, time: {stop_test - start_test}')
matrix2 = np.array([
[3, -3, -5, 8],
[-3, 2, 4, -6],
[2, -5, -7, 6],
[-4, 3, 5, -6]
])
if __name__ == '__main__':
sizes = [5, 6, 7, 8, 9, 10, 11, 12]
threads_counts = [1, 4, 6, 8, 12]
for threads in threads_counts:
print('-------------------------------------------------')
print(f'Threads:{threads}')
for n in sizes:
m = np.random.randint(3, size=(n, n))
do_threads(m, n, threads)
print('-------------------------------------------------')