如何将文本字符串从mpi4py工作进程传递回root = 0以写入文本输出

时间:2016-03-18 16:45:09

标签: python numpy mpi mpi4py

我正在尝试将包含有关工作进程进度的信息的文本字符串传递回root = 0.我正在使用comm.recv,但是,当我收到错误时,我无法收到包含该文本的列表TypeError: expected a writeable buffer object

我想设置的MWE如下:

from mpi4py import MPI
from mpi4py.MPI import ANY_SOURCE
import numpy as np

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()


data = []
recv_buffer = []


for i in range(0,10,1):
    data = data + ["Logfile for iteration %s on rank %s" %(i,rank)]

print(rank)
print(data)

if rank == 0:
        output = data[0]
        for i in range(1, size):
                comm.recv(recv_buffer,ANY_SOURCE)
                output += recv_buffer[0]
else:
        # all other process send their result
        comm.send(data)


if rank == 0:
    print(output)

这一行在comm.recv(recv_buffer,ANY_SOURCE)行失败。 recv_buffer预先指定为recv_buffer = []。我怎样才能使这个可写?

1 个答案:

答案 0 :(得分:3)

使用小写界面时,无需提供缓冲区。您可以简单地省略buffer参数并获取返回值:

if rank == 0:
    output = data # Note, removed data[0], otherwise it is not a list
    for i in range(1, size):
        recv_buffer = comm.recv(source=ANY_SOURCE)
        output += recv_buffer

注意,在这里使用集体操作要好得多 - 而且更容易。

import itertools

# No separate send/recv, just this single line
output = comm.gather(data, root=0)
# on rank 0, output will be a list of lists
if rank == 0:
    # You can convert it to a flat list
    # If you iterate over the output, you can omit the list(), just use the chain.
    output = list(itertools.chain(*output))
    print(output)