cs240a-cilkapps

# cs240a-cilkapps - 1 CS 240A Examples with Cilk Thanks to...

This preview shows pages 1–11. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CS 240A : Examples with Cilk++ Thanks to Charles E. Leiserson for some of these slides • Divide & Conquer Paradigm • Solving recurrences • Sorting: Quicksort and Mergesort • Graph traversal: Breadth-First Search 2 TP = execution time on P processors T1 = work T∞ = span * * Also called critical-path length or computational depth . Speedup on p processors ∙ T1/Tp Parallelism ∙ T1/T ∞ Work and Span (Recap) 3 Sorting ∙ Sorting is possibly the most frequently executed operation in computing! ∙ Quicksort is the fastest sorting algorithm in practice with an average running time of O(N log N), (but O(N2) worst case performance) ∙ Mergesort has worst case performance of O(N log N) for sorting N elements ∙ Both based on the recursive divide-and- conquer paradigm 4 QUICK SORT ∙ Basic Quicksort sorting an array S works as follows: § If the number of elements in S is or 1 , then return. § Pick any element v in S . Call this pivot . § Partition the set S-{v} into two disjoint groups: ♦ S1 = {x e S-{v} | x £ v} ♦ S2 = {x e S-{v} | x ‡ v} § Return quicksort(S1) followed by v followed by quicksort(S2) 5 QUICK SORT 13 21 34 56 32 31 45 78 14 Select Pivot 13 21 34 56 32 31 45 78 14 6 QUICK SORT 13 21 34 56 32 31 45 78 14 Partition around Pivot 13 14 21 32 31 45 56 78 34 7 QUICK SORT 13 14 21 32 31 45 56 78 34 Quicksort recursively 13 14 21 32 31 34 45 56 78 13 14 21 32 31 34 45 56 78 8 Parallelizing Quicksort ∙ Serial Quicksort sorts an array S as follows: § If the number of elements in S is or 1 , then return. § Pick any element v in S . Call this pivot . § Partition the set S-{v} into two disjoint groups: ♦ S1 = {x e S-{v} | x £ v} ♦ S2 = {x e S-{v} | x ‡ v} § Return quicksort(S1) followed by v followed by quicksort(S2) Not necessarily so ! 9 template <typename T> void qsort(T begin, T end) { if (begin != end) { T middle = partition( begin, end, bind2nd( less<typename iterator_traits<T>::value_type>(), *begin ) ); cilk_spawn qsort(begin, middle); qsort(max(begin + 1, middle), end); cilk_sync ; } } Parallel Quicksort (Basic) • The second recursive call to qsort does not depend on the results of the first recursive call • We have an opportunity to speed up the call by making both calls in parallel. 10 Performance ∙ ./qsort 500000 -cilk_set_worker_count 1 >> 0.083 seconds ∙ ./qsort 500000 -cilk_set_worker_count 16....
View Full Document

## This note was uploaded on 12/27/2011 for the course CMPSC 240A taught by Professor Gilbert during the Fall '09 term at UCSB.

### Page1 / 49

cs240a-cilkapps - 1 CS 240A Examples with Cilk Thanks to...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online