Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort....

Click here to load reader

  • date post

    18-Dec-2015
  • Category

    Documents

  • view

    255
  • download

    3

Embed Size (px)

Transcript of Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort....

  • Slide 1
  • Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort
  • Slide 2
  • Rank Sort for(i=0 ; i
  • Rank Sort Using n processors forall(i=0 ; i
  • Compare-and-Exchange if(A > B) { temp = A; A = B; B = temp; }
  • Slide 7
  • Compare-and-Exchange Process P1 send(&A, P2); recv(&A, P2); Process P2 recv(&A, P1); if(A > B){ send(&B, P1); B = A; }else send(&A, P1);
  • Slide 8
  • Compare-and-Exchange Process P1 send(&A, P2); recv(&B, P2); if(A > B) A = B; Process P2 recv(&A, P1); send(&B, P1); if(A > B) B = A;
  • Slide 9
  • Compare-and-Exchange Data partitioning
  • Slide 10
  • Compare-and-Exchange Data partitioning
  • Slide 11
  • Bubble Sort and Odd-even Transposition Sort
  • Slide 12
  • for(i=n-1 ; i > 0 ; i++) for(j=0 ; j < i ; j++){ k = j+1; if( a[j] < a[k]) { temp = a[j]; a[i] = a[k]; a[k] = temp; } O(n 2 )
  • Slide 13
  • Bubble Sort and Odd-even Transposition Sort Parallel Code
  • Slide 14
  • Bubble Sort and Odd-even Transposition Sort
  • Slide 15
  • Pi,i=0,2,4,6,...,n-2(even) recv(&A, Pi+1); send(&B, Pi+1); if(A > B) B = A; Pi,i=1,3,5,..,n-1(odd) send(&A,Pi-1); recv(&B, Pi-1); if(A > B) A = B;
  • Slide 16
  • Bubble Sort and Odd-even Transposition Sort Pi,i=1,3,5,...,n-3(odd) send(&A, Pi+1); recv(&B, Pi+1); if(A > B) A = B; Pi,i=2,4,6,...,n-2(even) recv(&A, Pi-1); send(&B, Pi-1); if(A > B) B = A;
  • Slide 17
  • Bubble Sort and Odd-even Transposition Sort Pi,i=0,2,4,...,n-1(odd) send(&A, Pi-1); recv(&B, Pi-1); if(A > B) A=B; if(i B) B = A; if(i >= 2){ recv(&A, Pi-1); send(&B, Pi-1); if(A > B) B=A; }
  • Slide 18
  • Bubble Sort and Odd-even Transposition Sort Two-Dimension Sorting
  • Slide 19
  • Bubble Sort and Odd-even Transposition Sort Odd Phase, the following actios are done: Each row of numbers is sorted independently, in alternative directions: Even rows: The smallest number of each column is placed at rightmost end and largest number at the leftmost end Odd rows: The smallest number of each column is placed at the leftmost end and the largest number at the rightmost end. In even phase, the following actions are done: Each column of numbers is sorted independently, placing the smallest number of each column at the leftmost end and the largest number at the rightmost end.
  • Slide 20
  • Bubble Sort and Odd-even Transposition Sort
  • Slide 21
  • Slide 22
  • Merge Sort
  • Slide 23
  • Communication (division phase) Communication at each step Processor communication P0->P4 P0->P2;P4->P6 P0->P1;P2->P3;P4->P5;P6->P7
  • Slide 24
  • Merge Sort Communication (merge phase) Communication at each step Processor communication P0->P4 P0->P2;P4->P6 P0->P1;P2->P3;P4->P5;P6->P7
  • Slide 25
  • Merge Sort Communication
  • Slide 26
  • Merge Sort Computation P0 P0;P4 P0;P2;P4;P6 The parallel computational time complexity is O(p) using p processors and one number in each processor
  • Slide 27
  • Quick Sort quicksort(list, start, end) { if(start < end) partition(list, start, end pivot); quicksort(list, start, pivot-1); quicksort(list, pivot-1, end); }
  • Slide 28
  • Quick Sort
  • Slide 29
  • Slide 30
  • Computation Communication
  • Slide 31
  • Quick Sort Implementation
  • Slide 32
  • Quick Sort on Hypercube Complete list in one processor 1st step: 000 -> 100 (numbers greater than a pivot, say p1) 2nd step: 000 -> 010 (numbers greater than a pivot, say p2) 100 -> 110 (numbers greater than a pivot, say p3) 3rd step: 000 -> 001 (numbers greater than a pivot, say p4) 010 -> 011 (numbers greater than a pivot, say p5) 100 -> 101 (numbers greater than a pviot, say p6) 110 -> 111 (numbers greater than a pivot, say p7)
  • Slide 33
  • Quick Sort on Hypercube
  • Slide 34
  • Number initially distributed across all processors 1. one processor(say P0) selects (or computers) a suitable pivot and broadcast this to all others in the cube 2. The processors in the lower subcube send their numbers, which are greater than the pivot, to their partner processor in the upper subcube. The processors in the upper subcube send their numbers, which are equal to or less than the pivot, to their partner processor in the lower cube. 3. Each processor concatenates the list received with what remains of its own list.
  • Slide 35
  • Quick Sort on Hypercube
  • Slide 36
  • Slide 37
  • 1. Each processor sorts its list sequentially. 2. one processor(say P0) selects (or computers) a suitable pivot and broadcast this to all others in the cube 3. The processors in the lower subcube send their numbers, which are greater than the pivot, to their partner processor in the upper subcube. The processors in the upper subcube sned their numbers, which are equal to or less than the pivot, to their partner processor in the lower cube. 4. Each processor merger the list received with its own to obtain a sorted list.
  • Slide 38
  • Quick Sort on Hypercube
  • Slide 39
  • Computation-Pivot Selection O(1) : the (n/2p)th number Communication-Pivot broadcast Computation-Data split if the numbers are sorted and there are x numbers, the split operation can be done in logx steps. (same as binary search) Communication-Data from split Computation-Data Merge to merge two sorted lists into one sorted list requires x steps if the biggest list has x numbers
  • Slide 40
  • Odd-even Merge Sort 1.The elements with odd indices of each sequence-that is, A1, A3,A5, ,An-1, and B1, B3,B5, ,Bn-1---are merged into one sorted list, C1, C2, C3, ,Cn 2.The elements with even indices of each sequence---that is A2,A4,A6, ,An, and B2,B4,B6, ,Bn-2---are merged into one sorted list, D1, D2, D3, ,Dn. 3.The final sorted list, E1, E2, ,E2n, is obtained by the following: E2i=min{Ci+1, Di} E2i+1=max{Ci+1,Di} for 1
  • Bitonic Merge Sort Bitonic sequence A0 Ai+1>Ai+2> ..>An 3,5,8,9,7,4,2,1
  • Slide 44
  • Bitonic Merge Sort
  • Slide 45
  • Slide 46
  • Slide 47
  • Phase 1(step1) Covert pairs of numbers into increasing/decreasing sequences and hence into 4-bit bitonic sequences Phase 2(step2/3) Split each 4-bit bitonic sequence into two 2-bit bitonic sequences, higher sequence at center. Sort each 4-bit bitonic sequence increasing/decreasing sequences and merge into 8-bit bitonic sequence. Phase 3(step4/5/6) Sort 8-bit bitonic sequence
  • Slide 48
  • Bitonic Merge Sort
  • Slide 49