使用多线程的无序映射的C ++种群

时间:2017-04-02 02:42:06

标签: c++ multithreading hashtable unordered

我想知道是否有办法加速一个名为(path_from_startend)的无序地图的填充。无序地图始终具有唯一的密钥。

#pragma omp parallel for
    for (int i=start_index;i<end_index;++i){    
        for (int j=start_index;j<end_index;++j){
            string key= to_string(unique_locations[i])+to_string(unique_locations[j]); 
            //dont compute path to itself
            if (i==j){
            }
            else {              
                    vector <unsigned>  path = pathbot.FindPath(unique_locations[i],unique_locations[j],turn_penalty);
                    path_from_startend.insert(make_pair(key,path));

            }
        }

    }  

1 个答案:

答案 0 :(得分:1)

您可以尝试以下模式是否能获得一定的速度。您基本上填充了部分地图,然后将其合并到整个地图中。加速很大程度上取决于元件的构造和插入需要多长时间。如果你的元素构造和插入都很便宜,那么你的加速甚至可能是负的,因为对于每个部分地图,它必须遍历整个地图搜索重复。

#pragma omp parallel
{
  std::unordered_map < std::string, std::vector < unsigned > > partial;

  #pragma omp for
  for (int i = start_index; i < end_index; ++i)
  {    
    for (int j = start_index; j < end_index; ++j)
    {
      std::string key = std::to_string(unique_locations[i])
                      + std::to_string(unique_locations[j]); 

      //dont compute path to itself
      if (i != j)
      {              
        std::vector<unsigned> path = pathbot.FindPath(unique_locations[i],
                                                      unique_locations[j],
                                                      turn_penalty);
        partial.insert(std::make_pair(key,path));
      }
    }
  }

  #pragma omp critical
  path_from_startend.insert(partial.begin(),partial.end());
}