Python MPI广播排

发布于 2025-01-25 12:20:09 字数 295 浏览 3 评论 0原文

因此,我正在尝试将一排Numpy阵列广播到每个节点,并在其自己的矩阵中将每个节点更新该特定行更新。

因此,代码如下:

comm.Bcast([A[i, i:], A[i, i:].size, MPI.DOUBLE], root = root)

其中a的类型为numpy.Array,其中i是迭代变量。 当我尝试这样做时,我以a成为结束。

做到这一点的正确方法是什么?

So I'm trying to broadcast a row of an numpy array to every node, and have every node update that specific row in their own matrix.

So the code is as follows:

comm.Bcast([A[i, i:], A[i, i:].size, MPI.DOUBLE], root = root)

Where A is of type numpy.array and where i is an iteration variable.
When I try doing this, I ended with A becoming None.

What would the correct way be to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦慕雪 2025-02-01 12:20:09

这需要一些数字骗局。您需要使用缓冲区的.data,然后将其变成带有偏移量的新的Numpy缓冲区。这应该接近您想要的内容:

datatype = np.intc
elementsize = datatype().itemsize
## or np.dtype('intc').itemsize
typechar = datatype().dtype.char
if procid==0:
    print("int size:",elementsize)
    print(typechar)

buffer = np.zeros( [nprocs,nprocs], dtype=datatype)
buffer[:,:] = -1
for proc in range(nprocs):
    if procid==proc:
        buffer[proc,:] = proc
    comm.Bcast\
        ( [ np.frombuffer\
            ( buffer.data,
              dtype=datatype,
              offset=(proc*nprocs+proc)*elementsize ),
            nprocs-proc, typechar ],
          root=proc )
    if procid==nprocs-1:
        print(buffer)

int size: 4
i
[[ 0  0  0  0]
 [-1 -1 -1 -1]
 [-1 -1 -1 -1]
 [-1 -1 -1 -1]]
[[ 0  0  0  0]
 [-1  1  1  1]
 [-1 -1 -1 -1]
 [-1 -1 -1 -1]]
[[ 0  0  0  0]
 [-1  1  1  1]
 [-1 -1  2  2]
 [-1 -1 -1 -1]]
[[ 0  0  0  0]
 [-1  1  1  1]
 [-1 -1  2  2]
 [ 3  3  3  3]]

请注意,这需要您选择的元素是连续的。对于发送一列,您需要派生类型。

This requires some numpy trickery. You need to take the .data of the buffer, and turn it into a new numpy buffer with offset and all that. This should be close to what you want:

datatype = np.intc
elementsize = datatype().itemsize
## or np.dtype('intc').itemsize
typechar = datatype().dtype.char
if procid==0:
    print("int size:",elementsize)
    print(typechar)

buffer = np.zeros( [nprocs,nprocs], dtype=datatype)
buffer[:,:] = -1
for proc in range(nprocs):
    if procid==proc:
        buffer[proc,:] = proc
    comm.Bcast\
        ( [ np.frombuffer\
            ( buffer.data,
              dtype=datatype,
              offset=(proc*nprocs+proc)*elementsize ),
            nprocs-proc, typechar ],
          root=proc )
    if procid==nprocs-1:
        print(buffer)

int size: 4
i
[[ 0  0  0  0]
 [-1 -1 -1 -1]
 [-1 -1 -1 -1]
 [-1 -1 -1 -1]]
[[ 0  0  0  0]
 [-1  1  1  1]
 [-1 -1 -1 -1]
 [-1 -1 -1 -1]]
[[ 0  0  0  0]
 [-1  1  1  1]
 [-1 -1  2  2]
 [-1 -1 -1 -1]]
[[ 0  0  0  0]
 [-1  1  1  1]
 [-1 -1  2  2]
 [ 3  3  3  3]]

Note that this requires that the elements you pick are contiguous. For sending a column you'd need derived types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文