93 lines
3.8 KiB
Python
93 lines
3.8 KiB
Python
|
import numpy as np
|
|||
|
import concurrent.futures
|
|||
|
import time
|
|||
|
import psutil
|
|||
|
|
|||
|
|
|||
|
def multiply_row(row, matrix_b): # Функция для умножения строки матрицы A на матрицу B
|
|||
|
return np.dot(row, matrix_b)
|
|||
|
|
|||
|
|
|||
|
def parallel_matrix_multiplication(matrix_a, matrix_b, num_threads, interval):
|
|||
|
memory = psutil.virtual_memory()
|
|||
|
num_rows_a, num_cols_a = matrix_a.shape
|
|||
|
num_rows_b, num_cols_b = matrix_b.shape
|
|||
|
assert num_cols_a == num_rows_b, "Размеры матриц несовместимы для перемножения"
|
|||
|
|
|||
|
with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor: # Отправляем задачи в пул потоков
|
|||
|
start_time = time.time()
|
|||
|
results = []
|
|||
|
for i in range(num_rows_a):
|
|||
|
result = executor.submit(multiply_row, matrix_a[i], matrix_b)
|
|||
|
results.append((i, result))
|
|||
|
sorted_results = sorted(results, key=lambda x: x[0])
|
|||
|
result_matrix = np.vstack(
|
|||
|
[result.result() for _, result in sorted_results]) # Получаем результаты выполнения задач
|
|||
|
end_time = time.time()
|
|||
|
|
|||
|
execution_time = end_time - start_time
|
|||
|
return result_matrix, execution_time, psutil.cpu_percent(interval=interval), memory.percent
|
|||
|
|
|||
|
|
|||
|
def test(parallel):
|
|||
|
a = np.array([[1, 2, 3], [4, 5, 6]])
|
|||
|
b = np.array([[7, 8], [9, 10], [11, 12]])
|
|||
|
if parallel:
|
|||
|
result = parallel_matrix_multiplication(b, a, num_threads=2, interval=0.5)
|
|||
|
else:
|
|||
|
result = parallel_matrix_multiplication(a, b, num_threads=1, interval=0.5)
|
|||
|
print("Результат умножения:")
|
|||
|
print(result[0])
|
|||
|
print("Время выполнения: " + str(result[1]) + " сек.")
|
|||
|
print("Загрузка ЦП: " + str(result[2]) + "%")
|
|||
|
print("Использование ОЗУ: " + str(result[3]) + "%")
|
|||
|
|
|||
|
|
|||
|
def bench100x100(parallel):
|
|||
|
a = np.random.randint(0, 100, size=(100, 100))
|
|||
|
b = np.random.randint(0, 100, size=(100, 100))
|
|||
|
if parallel:
|
|||
|
result = parallel_matrix_multiplication(a, b, num_threads=100, interval=1)
|
|||
|
else:
|
|||
|
result = parallel_matrix_multiplication(a, b, num_threads=1, interval=1)
|
|||
|
print("Результат умножения:")
|
|||
|
print(result[0])
|
|||
|
print("Время выполнения: " + str(result[1]) + " сек.")
|
|||
|
print("Загрузка ЦП: " + str(result[2]) + "%")
|
|||
|
print("Использование ОЗУ: " + str(result[3]) + "%")
|
|||
|
|
|||
|
|
|||
|
def bench300x300(parallel):
|
|||
|
a = np.random.randint(0, 100, size=(300, 300))
|
|||
|
b = np.random.randint(0, 100, size=(300, 300))
|
|||
|
if parallel:
|
|||
|
result = parallel_matrix_multiplication(a, b, num_threads=300, interval=1)
|
|||
|
else:
|
|||
|
result = parallel_matrix_multiplication(a, b, num_threads=1, interval=1)
|
|||
|
print("Результат умножения:")
|
|||
|
print(result[0])
|
|||
|
print("Время выполнения: " + str(result[1]) + " сек.")
|
|||
|
print("Загрузка ЦП: " + str(result[2]) + "%")
|
|||
|
print("Использование ОЗУ: " + str(result[3]) + "%")
|
|||
|
|
|||
|
|
|||
|
def bench500x500(parallel):
|
|||
|
a = np.random.randint(0, 100, size=(500, 500))
|
|||
|
b = np.random.randint(0, 100, size=(500, 500))
|
|||
|
if parallel:
|
|||
|
result = parallel_matrix_multiplication(a, b, num_threads=500, interval=1)
|
|||
|
else:
|
|||
|
result = parallel_matrix_multiplication(a, b, num_threads=1, interval=1)
|
|||
|
print("Результат умножения:")
|
|||
|
print(result[0])
|
|||
|
print("Время выполнения: " + str(result[1]) + " сек.")
|
|||
|
print("Загрузка ЦП: " + str(result[2]) + "%")
|
|||
|
print("Использование ОЗУ: " + str(result[3]) + "%")
|
|||
|
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
test(parallel=True)
|
|||
|
# bench100x100(parallel=True)
|
|||
|
# bench300x300(parallel=True)
|
|||
|
# bench500x500(parallel=True)
|