使用 MPI 广播 STL 地图

Broadcast STL Map using MPI

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

map< string, vector<double> > a_data;

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

众所周知,我们只能使用原始数据类型。那么,我应该如何使用 MPI_Bcast() 来广播像地图这样的 STL 数据类型??

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

  • 先广播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 数据类型