Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | Related Pages

parallel_sort.h

00001 /*
00002     Copyright 2005-2008 Intel Corporation.  All Rights Reserved.
00003 
00004     The source code contained or described herein and all documents related
00005     to the source code ("Material") are owned by Intel Corporation or its
00006     suppliers or licensors.  Title to the Material remains with Intel
00007     Corporation or its suppliers and licensors.  The Material is protected
00008     by worldwide copyright laws and treaty provisions.  No part of the
00009     Material may be used, copied, reproduced, modified, published, uploaded,
00010     posted, transmitted, distributed, or disclosed in any way without
00011     Intel's prior express written permission.
00012 
00013     No license under any patent, copyright, trade secret or other
00014     intellectual property right is granted to or conferred upon you by
00015     disclosure or delivery of the Materials, either expressly, by
00016     implication, inducement, estoppel or otherwise.  Any license under such
00017     intellectual property rights must be express and approved by Intel in
00018     writing.
00019 */
00020 
00021 #ifndef __TBB_parallel_sort_H
00022 #define __TBB_parallel_sort_H
00023 
00024 #include "parallel_for.h"
00025 #include <algorithm>
00026 #include <iterator>
00027 #include <functional>
00028 
00029 namespace tbb {
00030 
00032 namespace internal {
00033 
00035 
00037 template<typename RandomAccessIterator, typename Compare>
00038 struct quick_sort_range {
00039     static const size_t grainsize = 500;
00040     const Compare &comp;
00041     RandomAccessIterator begin;
00042     size_t size;
00043 
00044     quick_sort_range( RandomAccessIterator begin_, size_t size_, const Compare &comp_ ) :
00045         comp(comp_), begin(begin_), size(size_) {}
00046 
00047     bool empty() const {return size==0;}
00048     bool is_divisible() const {return size>=grainsize;}
00049 
00050     quick_sort_range( quick_sort_range& range, split ) : comp(range.comp) {
00051         RandomAccessIterator array = range.begin;
00052         RandomAccessIterator key0 = range.begin; 
00053         size_t m = range.size/2u;
00054         std::swap ( array[0], array[m] );
00055 
00056         size_t i=0;
00057         size_t j=range.size;
00058         // Partition interval [i+1,j-1] with key *key0.
00059         for(;;) {
00060             __TBB_ASSERT( i<j, NULL );
00061             // Loop must terminate since array[l]==*key0.
00062             do {
00063                 --j;
00064                 __TBB_ASSERT( i<=j, "bad ordering relation?" );
00065             } while( comp( *key0, array[j] ));
00066             do {
00067                 __TBB_ASSERT( i<=j, NULL );
00068                 if( i==j ) goto partition;
00069                 ++i;
00070             } while( comp( array[i],*key0 ));
00071             if( i==j ) goto partition;
00072             std::swap( array[i], array[j] );
00073         }
00074 partition:
00075         // Put the partition key were it belongs
00076         std::swap( array[j], *key0 );
00077         // array[l..j) is less or equal to key.
00078         // array(j..r) is greater or equal to key.
00079         // array[j] is equal to key
00080         i=j+1;
00081         begin = array+i;
00082         size = range.size-i;
00083         range.size = j;
00084     }
00085 };
00086 
00088 
00089 template<typename RandomAccessIterator, typename Compare>
00090 struct quick_sort_body {
00091     void operator()( const quick_sort_range<RandomAccessIterator,Compare>& range ) const {
00092         //SerialQuickSort( range.begin, range.size, range.comp );
00093         std::sort( range.begin, range.begin + range.size, range.comp );
00094     }
00095 };
00096 
00098 
00099 template<typename RandomAccessIterator, typename Compare>
00100 void parallel_quick_sort( RandomAccessIterator begin, RandomAccessIterator end, const Compare& comp ) {
00101     parallel_for( quick_sort_range<RandomAccessIterator,Compare>(begin, end-begin, comp ), quick_sort_body<RandomAccessIterator,Compare>() );
00102 }
00103 
00104 } // namespace internal
00106 
00117 
00119 
00122 template<typename RandomAccessIterator, typename Compare>
00123 void parallel_sort( RandomAccessIterator begin, RandomAccessIterator end, const Compare& comp) { 
00124     const int min_parallel_size = 500; 
00125     if( end > begin ) {
00126         if (end - begin < min_parallel_size) { 
00127             std::sort(begin, end, comp);
00128         } else {
00129             internal::parallel_quick_sort(begin, end, comp);
00130         }
00131     }
00132 }
00133 
00135 
00136 template<typename RandomAccessIterator>
00137 inline void parallel_sort( RandomAccessIterator begin, RandomAccessIterator end ) { 
00138     parallel_sort( begin, end, std::less< typename std::iterator_traits<RandomAccessIterator>::value_type >() );
00139 }
00140 
00142 
00143 template<typename T>
00144 inline void parallel_sort( T * begin, T * end ) {
00145     parallel_sort( begin, end, std::less< T >() );
00146 }   
00148 
00149 
00150 } // namespace tbb
00151 
00152 #endif
00153 

Copyright © 2005-2008 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.