-
Notifications
You must be signed in to change notification settings - Fork 0
/
scatter-mpi-multiplication.py
42 lines (33 loc) · 1.12 KB
/
scatter-mpi-multiplication.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import time
from mpi4py import MPI
from functions import line_and_second_matrix_multiplication
from functions import get_matrix
comm = MPI.COMM_WORLD
my_rank = comm.Get_rank()
num_of_processes = comm.Get_size()
second_matrix = get_matrix(num_of_processes, 36.6)
if my_rank == 0:
first_matrix = get_matrix(num_of_processes, 1.23)
first_matrix_row = [x for x in first_matrix]
else:
first_matrix_row = None
if my_rank == 0:
time_start = time.time()
first_matrix_row = comm.scatter(first_matrix_row, root=0)
if my_rank != 0:
line = [my_rank, line_and_second_matrix_multiplication(
first_matrix_row, second_matrix)]
req = comm.isend(line, dest=0)
req.wait()
else:
result_matrix = [[] for i in range(0, num_of_processes)]
result_matrix[0] = line_and_second_matrix_multiplication(
first_matrix_row, second_matrix)
for procid in range(1, num_of_processes):
req = comm.irecv(source=procid)
line = req.wait()
result_matrix[line[0]] = line[1]
# print(result_matrix)
if my_rank == 0:
print("Finished in %s seconds" % (time.time() - time_start))
MPI.Finalize