简体   繁体   中英

Mpi4py scatter, gather and send-recv

I'm trying to communicate between processes as in below code, such that every processor has two data dictionaries viz. "data" & "dataPrev". dataPrev is the dictionary from the previous ranked processor

    from Mpi4py import MPI

    comm = MPI.COMM_WORLD
    data = {'a': 11, 'b': b, 'c': c}
    comm.barrier()
    if rank == size-1: # last rank
            reqSend = comm.isend(data, dest=0, tag=rank)
            reqSend.wait()
    else:
            reqSend = comm.isend(data, dest=rank+1, tag=rank)
            reqSend.wait()

    if rank == 0:
            reqRecv = comm.irecv(source=size-1, tag=rank)
            dataPrev = reqRecv.wait()
    else:
            reqRecv = comm.irecv(source=rank-1, tag=rank)
            dataPrev = reqRecv.wait()
    comm.barrier()

Once that sent and recv has happened, I am making some changes to the values stored in dataPrev keys. This step is done.

Then, I'm gathering all the dictionaries from all the processors onto root = 0: if there are P processors, then in total I have 2P dictionaries.

    senddata=[]
    for j in range(3):  # 3 keys in the data
          senddata.append([1, data[j]])
    for j in range(3):  # 3 keys in the data
          senddata.append([2, data[j]])
    recvdata = None
    if rank == 0:
            comm.Gatherv(senddata, recvdata, root=0)

And then scatter the dictionaries of the ones with highest values in key data['a'] into different processors. I am not sure, how to "scatter" & if the code logic that I am using is correct or is there a simpler way.

Figured out a solution,

For sending and receiving:

            reqSend1 = self.comm.isend(data, dest=((self.size+self.rank+1)%self.size), tag=self.rank)
            reqRecv2 = self.comm.irecv(source=((self.size+self.rank-1)%self.size), tag=self.rank-1)
            dataPrev = reqRecv2.wait()
            reqSend1.wait()

And for Gather and scatter for python dictionaries, code logic is as follows: Gather function:

if rank == 0:
        comm.Gather(data, root=0)

Scatter function:

if rank == 0:
        comm.Scatter(data, root=0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM