【问题标题】:Broadcast STL Map using MPI使用 MPI 广播 STL 映射
【发布时间】:2021-01-29 10:58:27
【问题描述】:

我有一个看起来像这样的变量

map< string, vector<double> > a_data;

长话短说,a_data 只能由节点 0 填充。因此,使用 MPI_Bcast() 广播它是必要的。

众所周知,我们只能使用原始数据类型。那么,我应该如何使用 MPI_Bcast() 广播 STL 数据类型(如 map)?

【问题讨论】:

    标签: c++ performance parallel-processing stl mpi


    【解决方案1】:

    您可以采取的一种方法是:

    • 首先向每个进程广播key的数量;这样每个进程都知道需要计算的密钥数量;
    • 广播已编码每个键大小的数组;
    • 广播另一个数组,该数组已编码每个值数组的大小;
    • 创建一个循环来遍历键;
    • 首先广播密钥字符串(作为字符数组);
    • 接下来将值广播为双精度数组。

    所以在伪代码中应该是这样的:

    // number_of_keys <- get number of keys from a_data;
    // MPI_Bcast() number_of_keys;
    // int key_sizes[number_of_keys];
    // int value_sizes[number_of_keys];
    //
    // if(node == 0){ // the root process 
    //    for every key in a_data do
    //        key_sizes[i] = the size of the key;
    //        value_sizes[i] = size of the vector of values associated to key
    // }
    // 
    //  MPI_Bcast() the array key_sizes
    //  MPI_Bcast() the array value_sizes
    // 
    // for(int i = 0; i < number_of_keys; i++){
    //     key <- get key in position 0 from a_data
    //     values <- get the values associated with the key
    //
    //     MPI_Bcast() the key and use the size stored on key_sizes[i]
    //     MPI_Bcast() the values and use the size stored on value_sizes[i]
    //
    //     // Non root processes
    //     if(node != 0){ 
    //       add key to the a_data of the process
    //       add the values to the corresponded key
    //     }
    // }
    

    您只需将代码调整为 C++(我不是专家),因此您可能需要稍作调整,但大局已定。在使该方法起作用后,您可以通过减少所需的广播数量来进一步优化。这可以通过 per 广播打包更多信息来完成。例如,您可以首先广播项目的数量、键和值的大小,最后将键和值一起广播。对于后者,您需要创建类似于here 展示的示例的自定义 MPI 数据类型。

    【讨论】:

    • 或者干脆使用boost::mpi并在一行代码中广播几乎任意复杂的C++类型。
    • Boost 之于 C++ 就像 LAPACK 之于 Fortran。在 Boost 中包含 MPI 原语是促使从标准中删除 C++ 绑定的原因。
    • 遗憾的是,我不想在我的代码中放入这么大的库。而且,我的集群只支持C++-03。所以使用 Boost 不是一种选择。
    • 很久以前就有增强版本。 boost::mpi 来自 2005 年
    猜你喜欢
    • 2011-12-31
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2011-05-29
    • 2019-08-10
    • 1970-01-01
    相关资源
    最近更新 更多