0

我正在尝试将包含有关工作进程进度的信息的文本字符串传递回 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 = []。我怎样才能使它可写?

4

1 回答 1

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)
于 2016-03-18T16:56:18.153 回答