2017-04-02 4 views
2

(path_from_startend)라는 순서가 지정되지 않은 맵의 인구를 빠르게하는 방법이 있는지 궁금합니다. 순서가 지정되지 않은지도에는 항상 고유 한 키가 있습니다.멀티 스레딩을 사용하여 순서가 정렬되지 않은 C++ 인구

#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

'path_from_startend'는 스레드간에 공유되므로 삽입 작업은 중요한 섹션으로 이동해야합니다. 그러면 문제는'pathbot.FindPath'가 오랜 시간이 걸립니까? 그렇지 않으면 중요한 부분 때문에 맵을 순차적으로 채우고 있기 때문에 무의미합니다. –

+0

FindPath는 약간의 시간 (밀리 초)을 소요하는 A * 알고리즘입니다. –

+0

그러면 내 대답이 도움이 될 것입니다. 귀하의 코드와 일치하도록 업데이트했습니다. 동기화하지 않고 –

답변

0

다음 패턴이 어떤 속도를 얻는 지 확인할 수 있습니다. 기본적으로 부분 맵을 채우고 전체 맵에 병합합니다. 속도 향상은 요소의 구성 및 삽입이 얼마나 오래 걸리는지에 달려 있습니다. 요소를 구성하고 삽입하는 데 비용이 적게 든다면 각 부분지도에 대해 중복을 검색하는 전체지도를 통과해야하기 때문에 속도 향상이 음수 일 수도 있습니다.

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

포인터를 주셔서 감사합니다. 실제로 제 시간을 상당히 단축했습니다. @Jeremy가 말한대로 : 1) 문자열을 키로 사용하면 매우 비쌉니다. 2) 알고리즘을 O (n) 대신 O (n) 연산으로 변경할 수 있습니다.^2) –