-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit6.py
More file actions
72 lines (56 loc) · 1.79 KB
/
Copy pathsplit6.py
File metadata and controls
72 lines (56 loc) · 1.79 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
#Divides the matrix row-wise.
#Uses SCATTERV.
#The programmers have to compute the distribution of data.
#One process can have several columns. One process can have more columns than others.
#The data can have gaps (if the programmer desires that). Each process receives the data (several columns) in a array (1DIMENSION).
#mpiexec -n 10 python split6.py > output_6
from mpi4py import MPI
import numpy
from numpy import random
comm=MPI.COMM_WORLD
rank=comm.Get_rank()
size=comm.Get_size()
sendbuf=[]
data=[]
if(rank==0):
v=random.random((1000,4801))
sendbuf=v
print 'thats v_random:\n', 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
rows=len(v)
cols=len(v[0])
tup_elem =[]
tup_disp =[]
disp=0
for i in range(size-1):
tup_elem.append(slice_size*rows)
tup_disp.append(disp)
disp = disp + slice_size*rows
tup_elem.append(slice_for_last_node*rows)
tup_disp.append(disp)
print "tup_elem:",tup_elem
print "tup_disp:",tup_disp
else:
slice_size=slice_for_last_node=rows=cols=tup_elem=tup_disp=None
def doSum(x):
return numpy.sum(x)
#############################
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)
rows=comm.bcast(rows,root=0)
tup_elem=comm.bcast(tup_elem,root=0)
tup_disp=comm.bcast(tup_disp,root=0)
if rank<size-1:
data=numpy.zeros(slice_size*rows)
else:
data=numpy.zeros(slice_for_last_node*rows)
tup_elem= tuple(tup_elem)
tup_disp = tuple(tup_disp)
#############################
comm.Scatterv([sendbuf,tup_elem,tup_disp,MPI.DOUBLE],data)
comm.Barrier()
print 'my rank is {0} and my output is {1}\n'.format(rank,doSum(data))