51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
import numpy as np
|
|
import multiprocessing
|
|
import time
|
|
from time import time
|
|
|
|
|
|
def multiplication(first, second, res, start, stop, size):
|
|
for i in range(start, stop):
|
|
for j in range(size):
|
|
res[i][j] = 0
|
|
for k in range(size):
|
|
res[i][j] += first[i][k] * second[k][j]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sizes = [100, 300, 500]
|
|
threads_counts = [1, 3, 5, 7, 9]
|
|
for threads in threads_counts:
|
|
print(f'Количество потоков: {threads}')
|
|
for size in sizes:
|
|
A = np.random.randint(10, size=(size, size))
|
|
B = np.random.randint(10, size=(size, size))
|
|
if threads == 1:
|
|
res = np.zeros((size, size))
|
|
start = time()
|
|
multiplication(A, B, res, 0, size, size)
|
|
stop = time()
|
|
print(f'Размер: {size}x{size}, время выполнения: {stop - start}')
|
|
|
|
else:
|
|
offset = int(size / threads)
|
|
offset_last = size % threads + offset
|
|
|
|
processes = []
|
|
res = np.zeros((size, size))
|
|
start = time()
|
|
for i in range(threads):
|
|
start_ = i * offset
|
|
stop_ = start_ + offset_last if i == threads - 1 else start_ + offset
|
|
|
|
process = multiprocessing.Process(target=multiplication, args=(A, B, res, start_, stop_, size))
|
|
processes.append(process)
|
|
process.start()
|
|
|
|
for p in processes:
|
|
p.join()
|
|
|
|
stop = time()
|
|
|
|
print(f'Размер: {size}x{size}, время выполнения: {stop - start}')
|
|
print() |