我正在尝试使用MPI发送和接收字符串,但结果是无望的。
发送函数:
接收函数:
“result”是一个字符串。
我没有收到任何错误信息,但程序不想结束。
发送函数:
MPI_Send(&result, result.size(), MPI_CHAR, 0, 0, MPI_COMM_WORLD);
接收函数:
MPI_Recv(&result, /* message buffer */
128, /* one data item */
MPI_CHAR, /* of type char real */
MPI_ANY_SOURCE, /* receive from any sender */
MPI_ANY_TAG, /* any type of message */
MPI_COMM_WORLD, /* default communicator */
&status); /* info about the received message */
“result”是一个字符串。
我没有收到任何错误信息,但程序不想结束。
result
有足够大的缓冲区(之前已创建),他可以使用&result[0]
来获取指向内部缓冲区的非 const 指针。 - Eranresult
是一个std::string
,那么&result[0]
应该与result.c_str()
相同,除了它是非const的。我个人会使用前者,并使用const_cast
。我明白你的意思,在MPI_Recv
中传递预分配的std::string
可以工作,但如果字符串包含0
,则会失败,因此需要小心处理... - piokucresult
声明为const
。然而,根据 Send/Recv 对的定义,你需要能够在接收端更改result
。因此它不能是一个const std::string
,而应该是一个非 const 的std::string
。 - NoseKnowsAll