0
在我的MPI程序中,我正在播放std::string
,如下所示。代码起作用,但我找不到比这更优雅的解决方案(在其他SO问题中都没有)。有没有更好的方法(不使用Boost.MPI)?特别是const_cast
让我感到紧张。在MPI中广播std :: string的最优雅方式是什么?
std::string line
// Variable line receives a value on mpiid == 0.
int line_size = line.size();
MPI_Bcast(&line_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (mpiid != 0)
line.resize(line_size);
MPI_Bcast(const_cast<char*>(line.data()), line_size, MPI_CHAR, 0, MPI_COMM_WORLD);
这不是性能关键代码,但是在初始化中,其中具有设置的输入文件在一个进程上被读取并被广播给其他进程。我宁愿不要固定字符串大小。 – Chiel
在这种情况下,原始代码对我来说看起来很好,从'MPI'的角度来看(我真的不是'C++'家伙) –