본문 바로가기

병렬프로그래밍

병렬 프로그래밍(PPL,openMP,TBB)의 for문 속도 비교..!!


openMP와 TBB를 공부하던 도중 얼마나 빨라지는지 한번 비교를 해보았습니다..
비교하는김에 PPL까지 포함해서 3가지 다 비교해보았고.
비교의 대상은 가장 기초적이고 책의 첫부분에 나오는 for문...

일단 코드부터..

#include <tbb\tbb.h> 
#include <ppl.h>
#include <iostream> 
#include <string> 
#include <d3dx9math.h> 
#include <time.h>
#include <omp.h>

#pragma comment(lib,"d3dx9.lib")


using namespace std; 

int _tmain(int argc, _TCHAR* argv[])
{
  D3DXMATRIX mat; 

  clock_t  start, stop;

  start = clock();
  #pragma omp parallel for
  {
	for( int i = 0; i < 100000000; ++i ) 
		D3DXMatrixInverse(&mat, 0, &mat); 
  }	
  stop = clock();
  cout << "openMP Parallel Compute : " << double(stop - start) /double(CLOCKS_PER_SEC) << endl; 


  start = clock();
  tbb::parallel_for( tbb::blocked_range<size_t>(0, 100000000 ), [&](const tbb::blocked_range<size_t>&r)
  {
	for(int i = r.begin(); i!= r.end();i++)
		D3DXMatrixInverse(&mat, 0, &mat); 
  }); 
  stop = clock();
  cout << "TBB Parallel Compute : " << double(stop - start) /double(CLOCKS_PER_SEC) << endl; 


  start = clock();
  Concurrency::parallel_for(0, 100000000,[&](int i)
  {
	D3DXMatrixInverse(&mat, 0, &mat); 
  });
  stop = clock();
  cout << "PPL Parallel Compute : " << double(stop - start) /double(CLOCKS_PER_SEC) <<endl; 


  start = clock();
  for( int i = 0; i < 100000000; ++i ) 
  {
	D3DXMatrixInverse(&mat, 0, &mat); 
  }
  stop = clock();

  cout << "Serial Compute : " << double(stop - start) /double(CLOCKS_PER_SEC) <<endl; 
  return 0;
}

결과




위에 그림중 상단은 릴리즈 모드일때 하단은 디버그 모드.

PPL이 디버그모드 일때 의외의 모습을 보여주네요
그래도 릴리즈 모드일땐 속도도 많이 빨라지고.
또 vs2010에서는


이런식의 병렬 작업창까지 지원해줘서 태스크의 상태까지 볼수 있는 편리성을 제공해주고 있습니다..
tbb나 openMP도 intel parallel studio를 설치하면 볼수 있다고 합니다.

일단.. 결론은 TBB가 제일 좋네요...
중에 task까지 제대로 활용하면 순위가 뒤 바뀔지도...