【问题标题】:boost::iprobe does not return correct countboost::iprobe 不返回正确的计数
【发布时间】:2017-07-07 12:13:59
【问题描述】:

在下面的代码中,在探测时,没有获得正确的到达数。同样的功能也用标准的 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 是数组中的元素数。

【问题讨论】:

    标签: c++ boost mpi boost-mpi


    【解决方案1】:

    您的 Boost 版本实际上并没有发送 70 个int,而是一个单独的int [70]。对于 Boost,此类型不是 MPI 数据类型,因此正确的 (*stat).count&lt;decltype(a)&gt;(); 返回一个空的可选项。

    现在documentation 有点误导:

    类型 T 必须具有关联的数据类型,即 is_mpi_datatype&lt;T&gt; 必须派生 mpl::true_。如果类型T 与传输的类型不匹配,则此例程将返回一个空的optional&lt;int&gt;

    我似乎相反,在T 与传输类型不匹配的情况下,您将获得虚假结果或空optional&lt;int&gt;。如果它不是 mpi 数据类型,则会得到一个空的 optional&lt;int&gt;

    你得到 2 的原因是 Boost.MPI 为每个非 MPI 数据类型消息发送两条消息。一个包含序列化缓冲区的大小和实际消息。您的探针会处理大小消息,其中包含一个 size_t,其大小与 2 个 int 相同。

    不幸的是,Boost.MPI 充满了与实际传输消息的不同方式相关的此类微妙问题和错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      相关资源
      最近更新 更多