C++使用多线程的无序列图填充

C++ population of unordered map using multithreading

本文关键字:填充 无序 多线程 C++      更新时间:2023-10-16

我想知道是否有一种方法可以加快名为(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));
            }
        }
    }  

您可以尝试以下模式是否为您提供了一些速度。 您基本上是在填充部分地图,然后将其合并到整个地图中。 加速很大程度上取决于元件的构建和插入需要多长时间。 如果您的元素构建和插入成本低,那么您的加速甚至可能是负的,因为对于每个部分地图,它必须遍历整个地图以寻找重复项。

#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());
}