I've got a problem regarding my code. I want this code to send elements of an array to certain workers. The workers 0-7 should receive 12, 18, 30, 36, 48, 54, 66, 72. So I used send and receive in order to do that but when I execute my code I just get to part 66 but not 72. So there is one missing and it does not start at 0. Maybe you can help me.
Here is my code:
import numpy as np
import pandas as pd
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
a=np.random.rand(336,1) #array
sizedarray=[]
starting_point=0
ending_point=12
sizes=[]
q=[]
if rank == 0:#masterrrank
for i in range(0,size-1):
take_out=0
sizedarray.append(a[starting_point:ending_point])
starting_point=ending_point
if i % 2 == 0:
ending_point= ending_point + len(sizedarray[i]) + 6
sizes.append(len(sizedarray[i]))
elif i % 2 !=0:
ending_point=len(sizedarray[i]) +12+ ending_point
sizes.append(len(sizedarray[i]))
comm.send(a[take_out:sizes[i]+take_out],dest=i+1)
take_out=take_out+sizes[i]
else:
p=comm.recv(source=0)
print(len(p)," ", rank)
and my outcome:
48 5
36 4
54 6
30 3
12 1
18 2
66 7