DAS_2024_1/balakhonov_danila_lab_5/program.py

86 lines
2.8 KiB
Python
Raw Normal View History

import random as rnd
import threading
import time
from multiprocessing import Pool
MAX_SIZE = 500
def generateSquareMatrix(size):
return [[rnd.randint(0, 100) for _ in range(size)] for _ in range(size)]
# Функция для перемножения матриц
def matrixMultiply(matrix1, matrix2, start_i=0, end_i=None):
l1 = len(matrix1)
l2 = len(matrix2)
global result_matrix
result = result_matrix
end_i = end_i if end_i is not None else l1
for i in range(start_i, end_i):
for j in range(len(matrix2[0])):
for k in range(l2):
result[i][j] += matrix1[i - start_i][k] * matrix2[k][j]
return result
# Перемножение без использования потоков
def matrixMultiplyStandard(matrix1, matrix2):
return matrixMultiply(matrix1, matrix2)
# Перемножение в отдельном потоке
def matrixMultiplySingleThread(args):
matrix1, matrix2, start_i, end_i = args
matrixMultiply(matrix1, matrix2, start_i, end_i)
# Параллельное перемножение
def matrixMultiplyWithThreads(matrix1, matrix2, thread_count):
l1 = len(matrix1)
l2 = len(matrix2)
# Кол-во строк на последний поток, если деление по потокам будет неточным
last_rows_count = 0
if l1 % thread_count == 0:
rows_per_thread = l1 // thread_count
else:
rows_per_thread = l1 // thread_count
last_rows_count = l1 % thread_count
for i in range(thread_count):
start_i = i * rows_per_thread
if (i - 1) == thread_count and last_rows_count > 0:
end_i = start_i + last_rows_count
else:
end_i = start_i + rows_per_thread
args = []
args.append((matrix1[start_i:end_i], matrix2, start_i, end_i))
with Pool(processes = thread_count) as pool:
pool.map(matrixMultiplySingleThread, args)
result_matrix = [[0 for _ in range(MAX_SIZE)] for _ in range(MAX_SIZE)]
if __name__ == "__main__":
sizes = [100, 300, 500]
num_threads = [1, 5, 8]
for size in sizes:
matrix1 = generateSquareMatrix(size)
matrix2 = generateSquareMatrix(size)
# Обычное перемножение
start_time = time.time()
matrixMultiplyStandard(matrix1, matrix2)
end_time = time.time()
print(f"Обычное перемножение.\t\tРазмер: {size}\t\t|\t {end_time - start_time}")
# Перемножение в потоках
for threads in num_threads:
start_time = time.time()
matrixMultiplyWithThreads(matrix1, matrix2, threads)
end_time = time.time()
print(f"Параллельное перемножение.\tРазмер: {size}, {threads} потоков\t|\t {end_time - start_time}")
print("=" * 100)