2014-06-11 4 views
1

부스트 : 장벽에 성능 문제가 있습니다. 대기 메서드 호출의 시간을 측정합니다. 단일 스레드 상황에서는 대기 호출이 약 100,000 반복 될 때 약 0.5 초가 걸립니다. 불행히도이 스레드를 시나리오에 대한이 시간을 3 초로 확장하고 모든 스레드 (나는 8 코어 프로세서)가 더 악화되고있다.부스트 :: 배리어의 낮은 성능, 대기 동작

나는 동일한 기능을 제공하는 책임이있는 사용자 지정 방법을 구현했으며 훨씬 더 빠릅니다.

이 방법에서는 너무 느리게 작동합니다. 부스트에서 스레드를 동기화하는 더 빠른 방법이 있습니까 (모든 스레드가 모든 스레드에 의해 현재 작업이 완료 될 때까지 기다린 후 다음 작업으로 넘어 가고, 동기화 만하면 데이터 전송이 필요하지 않습니다).

현재 코드를 요청 받았습니다. 내가 달성하고자하는 것. 루프에서 함수를 실행하면이 함수를 많은 스레드로 나눌 수 있지만 모든 스레드는 다른 루프가 실행되기 전에 실행되는 현재 루프를 완료해야합니다.

내 현재 솔루션

volatile int barrierCounter1 =0; //it will store number of threads which completed current loop run 
volatile bool barrierThread1[NumberOfThreads]; //it will store go signal for all threads with id > 0. All values are set to false at the beginning 
boost::mutex mutexSetBarrierCounter; //mutex for barrierCounter1 modification 

void ProcessT(int threadId) 
{ 
    do 
    { 
     DoWork(); //function which should be executed by every thread 

     mutexSetBarrierCounter.lock(); 
     barrierCounter1++; //every thread notifies that it finish execution of function 
     mutexSetBarrierCounter.unlock(); 

     if(threadId == 0) 
     { 
     //main thread (0) awaits for completion of all threads 
     while(barrierCounter1!=NumberOfThreads) 
     { 
     //I assume that the number of threads is lower than the number of processor cores 
     //so this loop should not have an impact of overall performance 
     } 
     //if all threads completed, notify other thread that they can proceed to the consecutive loop 
     for(int i = 0; i<NumberOfThreads; i++) 
     { 
      barrierThread1[i] = true; 
     } 
     //clear counter, no lock is utilized because rest of threads await in else loop 
     barrierCounter1 = 0; 
     } 
     else 
     { 
     //rest of threads await for "go" signal 
     while(barrierThread1[i]==false) 
     { 

     } 
     //if thread is allowed to proceed then it should only clean up its barrier thread array 
     //no lock is utilized because '0' thread would not modify this value until all threads complete loop run 
     barrierThread1[i] = false; 
     } 
} 
while(!end) 
} 
+2

"동일한 기능을 제공하는 책임이있는 사용자 지정 메서드를 구현했으며 훨씬 더 빠릅니다"-이 정보를 보여 주면 왜 더 빠릅니까 (올바른지 여부도 설명 할 수 있음) – sehe

답변

1

잠금 동시성에 위배된다. 의 잠금은 항상 최악의 동작입니다.

IOW : 스레드 동기화 (자체적으로)가 조정되지 않습니다.

해결 방법 : 경합이 낮은 곳으로 만 또는이 작업에 대해 둘 이상의 스레드를 사용하려고하지 않습니다 (스레드가 "상대적으로 거의"[1]를 동기화해야하는) 상황에서 동기화 기본을 사용하여 공유 리소스와 경쟁합니다.

벤치 마크는 모든 스레드가 항상 대기하도록함으로써 최악의 경우 동작을 확대하는 것처럼 보입니다. 장벽 사이의 모든 근로자에게 상당한 업무량이있는 경우 오버 헤드가 줄어들고 쉽게 줄어들지 않을 수 있습니다.

  • 신뢰 당신은
  • 프로필에만 응용 프로그램 코드 (NO 바보 합성 벤치 마크)
  • 이 스레딩 비 스레딩 선호를 (기억! : 비동기 = 동시성) 프로파일

[1] 매우 상대적이고 주관적인 것