自动类型的MPI广播变量

时间:2016-03-08 12:58:11

标签: c++ c++11 mpi

我正在研究生成C ++代码的编译器。例如:

MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == 0){
    auto i = function();
    // do something
    MPI_Bcast(&i, 1, MPI_INT, 0, MPI_COMM_WORLD);
}
else{
    auto i;
    MPI_Bcast(&i, 1, MPI_INT, 0, MPI_COMM_WORLD);
    cout << i;
}

变量的类型&#39; i&#39;在编译时确定,MPI是否具有允许广播此类变量的泛型类型?如果没有,我该如何广播这些变量?而且,我应该如何接受这些变量,因为只是声明auto i;是不允许的。

4 个答案:

答案 0 :(得分:3)

您可以使用模板返回所需信息:

#include <mpi.h>
#include <iostream>

int function() {
    int r;
    MPI_Comm_rank(MPI_COMM_WORLD, &r);
    return r;
}

struct typecount {
    MPI_Datatype mpitype;
    int count;
};

template<typename T> typecount gettypecount(T t) { typecount tc={ MPI_BYTE, sizeof(T) }; return tc; };
template<> typecount gettypecount(int t) { typecount tc={ MPI_INT, 1 }; return tc; };
template<> typecount gettypecount(double t) { typecount tc={ MPI_DOUBLE, 1 }; return tc; };
template<> typecount gettypecount(char t) { typecount tc={ MPI_CHAR, 1 }; return tc; };

int main(int argc, char **argv) {
    int rank;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    if (rank == 0){
        auto i = function();
        const typecount tmap = gettypecount(i);
        MPI_Bcast(&i, tmap.count, tmap.mpitype, 0, MPI_COMM_WORLD);
    }
    else{
        decltype(function()) i = 0;
        const typecount tmap = gettypecount(i);
        MPI_Bcast(&i, tmap.count, tmap.mpitype, 0, MPI_COMM_WORLD);
        std::cout << i << std::endl;
    }

    MPI_Finalize();
    return 0;
}

跑步给出:

$ mpicxx -o typetest typetest.cxx  --std=c++11 -Wc++11-extensions -Wall
$ mpirun -np 4 ./typetest
0
0
0

答案 1 :(得分:2)

您可以使用boost::mpi::get_mpi_datatype(),如下所示:

#include <boost/mpi/datatype.hpp>
...
decltype(function()) i; // or, maybe: typename std::decay<decltype(function())>::type i;
if (rank == 0)
   i = function();
auto mdt = boost::mpi::get_mpi_datatype(i);
MPI_Bcast((void*)(&i), 1, mdt, 0, MPI_COMM_WORLD);

对于自定义类型,可以通过以下模式轻松定义此类函数:

static MPI_Datatype mpi_custom_type; // initialized somewhere

template <typename T>
struct get_mpi_datatype_t;

// specialization for particular types:
template <>
struct get_mpi_datatype_t<unsigned char> {
   static const MPI_Datatype value = MPI_UNSIGNED_CHAR;
};
template <>
struct get_mpi_datatype_t<unsigned short> {
   static const MPI_Datatype value = MPI_UNSIGNED_SHORT;
};
template <>
struct get_mpi_datatype_t<custom_type> {
   static const MPI_Datatype& value = mpi_custom_type;
};
...
template <typename T>
MPI_Datatype get_mpi_datatype(const T& arg) {
   return get_mpi_datatype_t<T>::value;
}
template <typename T>
MPI_Datatype get_mpi_datatype() {
   return get_mpi_datatype(T());
}

或者,与Boost:

template <typename T>
struct get_mpi_datatype_t {
   static const MPI_Datatype value = boost::mpi::get_mpi_datatype<T>();
};
// specialization for custom types only:
template <>
struct get_mpi_datatype_t<custom_type> {
   static const MPI_Datatype& value = mpi_custom_type;
};
...

顺便说一句,Boost MPI是一个非常好用且有用的库,但是如果您使用许多不同的HPC系统并且使用不同的编译器和不同的MPI实现,有时可能难以使用/维护。上述解决方案的优点是不需要链接boost_mpi库,只需要包含boost/mpi/datatype.hpp头文件。

答案 2 :(得分:1)

我建议使用Boost.MPI。它提供了Jonathan Dursi在其broadcast(...)模板中为本机MPI类型解释的完全抽象。 MPI还有许多非常方便的C ++抽象。

答案 3 :(得分:0)

您可以尝试这样的事情:

 MPI_Bcast(&i, sizeof(i), MPI_BYTE, 0, MPI_COMM_WORLD);