mpi4py 发送字符 [英] mpi4py Send characters

查看:59
本文介绍了mpi4py 发送字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用更快的发送(而不是更慢的发送)方法将一些字符从一个进程传输到另一个进程,但无法使用 mpi4py 获得有效的解决方案(仅限于使用这种语言,Python3)

I'd like to use the faster Send (not the slower send) method to transmit some characters from one process to another, but haven't been able to get a working solution with mpi4py (restricted to using this language, Python3)

这是我的玩具代码 - 如果有人能指出我做错了什么(以及如何解决),我将不胜感激.

Here's my toy code - if anyone can point out what I'm doing wrong (and how to fix it), I'd be very grateful.

from mpi4py import MPI
import numpy
from array import array
comm = MPI.COMM_WORLD
if comm.rank == 0:
    comm.Send([b'hello',MPI.CHAR],dest=1)
elif comm.rank == 1:
    buf = array('b')*255
    r = comm.Recv([buf,MPI.CHAR],source=0)
    print(r)

作为参考,我也在这里尝试了代码(没有用):https://groups.google.com/g/mpi4py/c/LDHbzApI55c/m/gENCO-_HAwUJ

For reference, I also tried the code here (didnt work): https://groups.google.com/g/mpi4py/c/LDHbzApI55c/m/gENCO-_HAwUJ

谢谢!

推荐答案

问题是在这种情况下,您的缓冲区 buf 在尝试接收消息时不足以容纳您的消息长度为 0.

The problem is that your buffer buf in this case is not big enough to fit your message when it tries to receive it as it is of length 0.

这里的解决方案是使用 bytearray 因为您可以将其初始化为固定大小,因此您的示例将是:

The solution here is to use a bytearray as you can initialise it on a fixed size, so your example will be:

from mpi4py import MPI

comm = MPI.COMM_WORLD
if comm.rank == 0:
    comm.Send([b'hello',MPI.CHAR],dest=1)
elif comm.rank == 1:
    buf = bytearray(256)
    comm.Recv([buf,MPI.CHAR],source=0)
    # Decode the string to remove the empty characters from bytearray
    print(buf.decode('utf-8'))

另请注意,Recv 不返回值,因此 r 将为空.结果会直接保存到buf

Also notice that the Recv does not return a value so r will be empty. The result will be saved directly to buf

这篇关于mpi4py 发送字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆