병렬 처리 쓰레드로 정렬을 한다. 

정렬할 데이터가 많고 CPU 코어 갯수가 높을 수록 일반 sort보다 많이 빨라지게 된다.

parallel_while이나 다른 알고리즘에 비해 아주 간단하게 쓸 수 있다.. -ㅅ-...

 

여기서는 람다를 사용을 하고 있지만...

아직 C++0x가 적용 안된 컴파일러 (Windows의 경우 visual studio 2008 이하) 는 람다 함수를 사용 할 수가 없다.

람다 함수를 사용하는 대신에 functor를 이용 해야 한다.

 

 

[-] Collapse
#include <stdio.h>
#include <tchar.h>
#include <time.h>
#include <tbb/tbb.h>

#ifdef _DEBUG
#pragma  comment(lib, "tbb_debug.lib")
#else
#pragma  comment(lib, "tbb.lib")
#endif // _DEBUG

using namespace tbb ;

class CNObject
{
public:
    bool operator<(const CNObject & t) const
    {
        return  (data < t.data) ;
    }
public:
    int data ;
    char dumy ;
};

int _tmain(int argc, _TCHAR* argv[])
{
    task_scheduler_init init ;
    srand((unsigned int)time(NULL)) ;
    printf("병렬 처리 정렬 알고리즘\n") ;

    // 정렬을 테스트를 위한 데이터 생성
    CNObject aValue[10] ;
    for (int n = 0 ; n < 10 ; ++n )
    {
        aValue[n].data = rand() % 10 ;
        printf("%d ", aValue[n].data) ;
    }
    printf("\n") ;

    // 연산자 오버로딩을 이용한 연산
    parallel_sort(aValue, aValue + 10 ) ;

    for (int n = 0  ; n < 10 ; ++n )
    {
        printf("%d ", aValue[n].data) ;
    }
    printf("\n") ;

    // 이번엔 람다를 이용해서 정렬
    parallel_sort(aValue, aValue + 10,
        [](const CNObject &t1, const CNObject &t2)->bool
    {
        return (t1.data > t2.data);
    }) ;

    for (int n = 0  ; n < 10 ; ++n )
    {
        printf("%d ", aValue[n].data) ;
    }
    printf("\n") ;

    getchar() ;
    return 0;
}


'C++ > Intell C++' 카테고리의 다른 글

간단한 TBB parallel_for 예제  (0) 2014.09.16
간단한 TBB parallel_reduce 예제  (0) 2014.09.16
간단한 TBB parallel_while 예제  (0) 2014.09.16
간단한 TBB parallel_invoke 예제  (1) 2014.09.16

+ Recent posts