在下面的代码中,在探测时,未获得正确的到达数。使用标准 MPI 函数也测试了相同的功能,并获得了正确答案。为什么 Boost 版本不能产生正确的结果?
增强版:
#include <iostream>
#include <boost/mpi.hpp>
using namespace boost;
using namespace boost::mpi;
int main()
{
environment env;
communicator world;
if (world.rank() == 0)
{
int a[70];
auto req = world.isend(0, 0, a);
//req.wait(); // does not make any difference on the result.
optional<status> stat;
while (!stat)
{
stat = world.iprobe(0, 0);
if (stat)
{
optional<int> count = (*stat).count<int>();
if (count)
{
std::cout << *count << std::endl; // output: 2, expected: 70.
}
}
}
}
return 0;
}
标准版本:
#include <iostream>
#include <mpi.h>
int main()
{
MPI_Init(NULL, NULL);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0)
{
int a[70];
MPI_Request req;
MPI_Status stat;
MPI_Isend(a, 70, MPI_INT, 0, 0, MPI_COMM_WORLD, &req);
//MPI_Wait(&req, &stat);
int flag;
MPI_Iprobe(0, 0, MPI_COMM_WORLD, &flag, &stat);
if (flag)
{
int count;
MPI_Get_count(&stat, MPI_INT, &count);
std::cout << count << std::endl; // output: 70.
}
}
MPI_Finalize();
return 0;
}
编辑:使用isend(dest, tag, values, n)
而不是isend(dest, tag, values)
给出正确答案n
数组中元素的数量在哪里。