-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit2.py
More file actions
73 lines (57 loc) · 1.99 KB
/
split2.py
File metadata and controls
73 lines (57 loc) · 1.99 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#Divides the matrix column-wise.
#It uses SEND and RECV directives.
#Programmers have to compute the distribution of data among processes.
#A process can compute several columns.
#Each process receives the data in a matrix shape of 2Dimensions.
#mpiexec -n 10 python split2.py > output_2
#In this example, each process gets 480 columns (except the last process, which gets 481).
#Later, all the processes (in this example: 10 processes) computes in parallel the sum of their 480 colums. Each column has 1000 elements.
from mpi4py import MPI
import numpy
from numpy import random
comm=MPI.COMM_WORLD
rank=comm.Get_rank()
size=comm.Get_size()
if(rank==0):
v=random.random((1000,4801))
print "The size of v is:", len(v)
if size>=len(v[0]):
size=len(v[0])
slice_size=int(numpy.ceil(float(len(v[0]))/float(size)))
slice_for_last_node=len(v[0])-(size-1)*slice_size
#xtra_slices=len(v[0])%size
cols=len(v[0])
print "slice_size:",slice_size
print "slice_for_last_node:",slice_for_last_node
else:
slice_size=slice_for_last_node=cols=None
size=comm.bcast(size,root=0)
slice_size=comm.bcast(slice_size,root=0)
slice_for_last_node=comm.bcast(slice_for_last_node,root=0)
cols=comm.bcast(cols,root=0)
def doSum(x):
return numpy.sum(x)
if rank==0:
print 'thats v_random:\n', v
count=1
cur_dest=0
for i in range(len(v[0])):
if(count>slice_size and cur_dest<size-1):
cur_dest+=1
count=1
if(cur_dest>=size-1):
cur_dest=size-1
comm.send(v[:,i],dest=cur_dest)
count+=1
if rank<size-1:
count=1
while count<=slice_size: #slices per proc
data=comm.recv(source=0)
count+=1
print 'my rank is {0} and my output is {1}\n'.format(rank,doSum(data))
elif rank==size-1:
count=1
while count<=slice_for_last_node:
data=comm.recv(source=0)
count+=1
print 'my rank is {0} and my output is {1}\n'.format(rank,doSum(data))