Case Studies Class 7 Experiencing Cluster Computing.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
CSCE 3110 Data Structures & Algorithm Analysis
HST 952 Computing for Biomedical Scientists Lecture 9.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Chapter 7: Sorting Algorithms
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
Sorting Algorithms and Average Case Time Complexity
CMPS1371 Introduction to Computing for Engineers SORTING.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Data Structures and Algorithms PLSD210 Sorting. Card players all know how to sort … First card is already sorted With all the rest, ¶Scan back from the.
Quick Sort. Quicksort Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare. The quick sort is an in-place, divide- and-conquer, massively.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
Sorting21 Recursive sorting algorithms Oh no, not again!
1 Lecture 11 Sorting Parallel Computing Fall 2008.
Merge sort, Insertion sort
Sorting Chapter 10.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Sorting Algorithms and Analysis Robert Duncan. Refresher on Big-O  O(2^N)Exponential  O(N^2)Quadratic  O(N log N)Linear/Log  O(N)Linear  O(log N)Log.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
CSE 373 Data Structures Lecture 19
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Divide-And-Conquer Sorting Small instance.  n
(c) , University of Washington
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Sorting HKOI Training Team (Advanced)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Computer Science Searching & Sorting.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
CSC 211 Data Structures Lecture 13
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Sorting CS 110: Data Structures and Algorithms First Semester,
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Advanced Sorting 7 2  9 4   2   4   7
Unit-2 Divide and Conquer
8/04/2009 Many thanks to David Sun for some of the included slides!
Analysis of Algorithms
Presentation transcript:

Case Studies Class 7 Experiencing Cluster Computing

Case 1: Number Guesser

Number Guesser 2 players game ~ T hinker & Guesser Thinker thinks of a number between 1 & 100 Guesser guesses Thinker tells the guesser whether guess is high, low or correct Guesser’s best strategy 1.Remember high and low guesses 2.Guess the number in between 3.If guess was high, reset remembered high guess to guess 4.If guess was low, reset remembered low guess to guess  2 processes Source

Number Guesser ThinkerGuesser Reply char Guess int Processor 0 Processor 1

Thinker #include thinker() { int number,guess; char reply = ‘x’; MPI_Status status; srand(clock()); number = rand()%100+1; printf("0: (I'm thinking of %d)\n",number); while(reply!='c') { MPI_Recv(&guess,1,MPI_INT,1,0,MPI_COMM_WORLD,&status); printf("0: 1 guessed %d\n",guess); if(guess==number)reply = 'c'; else if(guess>number)reply = 'h'; else reply = 'l'; MPI_Send(&reply,1,MPI_CHAR,1,0, MPI_COMM_WORLD); printf("0: I responded %c\n",reply); }

Thinker ( processor 0) clock() returns time in CLOCKS_PER_SEC since process started srand() seeds random number generator rand() returns next random number MPI_Recv receives in guess one int from processor 1 MPI_Send sends from reply one char to processor 1

Guesser guesser() { char reply; MPI_Status status; int guess,high,low; srand(clock()); low = 1; high = 100; guess = rand()%100+1; while(1) { MPI_Send(&guess,1,MPI_INT,0,0,MPI_COMM_WORLD); printf("1: I guessed %d\n",guess); MPI_Recv(&reply,1,MPI_CHAR,0,0,MPI_COMM_WORLD,&status); printf("1: 0 replied %c\n",reply); switch(reply) { case 'c': return; case 'h': high = guess; break; case 'l': low = guess; break; } guess = (high+low)/2; }

Guesser ( processor 1) MPI_Send sends from guess one int to processor 0 MPI_Recv receives in reply one char from processor 0

main main(argc,argv) int argc; char ** argv; { int id,p; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&id); if(id==0) thinker(); else guesser(); MPI_Finalize(); }

Number Guesser Process 0 is thinker & Process 1 is guesser % mpicc –O –o guess guess.c % mpirun –np 2 guess Output: 0: (I'm thinking of 59) 0: 1 guessed 46 0: I responded l 0: 1 guessed 73 0: I responded h 0: 1 guessed 59 0: I responded c 1: I guessed 46 1: 0 replied l 1: I guessed 73 1: 0 replied h 1: I guessed 59 1: 0 replied c

Case 2: Parallel Sort

Parallel Sort Sort a file of n integers on p processors Generate a sequence of random numbers Pad the numbers and make its length a multiple of p –n+p-n%p Scatter sequences of n/p+1 to the p processors Sort the scattered sequences in parallel on each processor Merge sorted sequences from neighbors in parallel –log 2 p steps are needed

Parallel Sort Proc 0 Proc 1Proc 2Proc p - 1… Scatter Merge Proc 0Proc 1Proc 2Proc 3Proc 4 Proc 0Proc 1Proc 2Proc 3Proc 4 Proc 0Proc 1Proc 2Proc 3Proc 4 1st 2nd 3rd

Parallel Sort e.g. Sort 125 integers with 8 processors Pad: %8 = = = 128 Merge (1 st step): 16 from P0 & 16 from P1  P0 == from P2 & 16 from P3  P2 == from P4 & 16 from P5  P4 == from P6 & 16 from P7  P6 == 32 Merge (2 nd step): 32 from P0 & 32 from P2  P0 == from P4 & 32 from P6  P4 == 64 Merge (3 rd step): 64 from P0 & 64 from P4  P0 == 128 Scatter: 16 integers on each proc 0 – proc 7 Sorting: each proc sorts its 16 integers.

Algorithm Root –Generate a sequence of random numbers –Pads data to make size a multiple of number of processors –Scatters data to all processors –Sorts one sequence of data Other processes –receive & sort one sequence of data Sequential Sorting Algorithm: Quick sort, bubble sort, merge sort, heap sort, selection sort, etc

Algorithm Each processor is either a merger or sender of data Keep track of distance (step) between merger and sender on each iteration –double step each time Merger rank must be a multiple of 2*step Sender rank must be merger rank + step If no sender of that rank then potential merger does nothing Otherwise must be a sender –send data to merger on left at sender rank - step –terminate Finished, root print out the result

Example Output $ mpirun -np 5 qsort 0 about to broadcast about to scatter 0 sorts sorts sorts sorts sorts step 1: 1 sends to 0 step 1: 0 gets from 1 step 1: 0 now has step 1: 3 sends to 2 step 1: 2 gets from 3 step 1: 2 now has step 2: 2 sends to 0 step 2: 0 gets from 2 step 2: 0 now has step 4: 4 sends to 0 step 4: 0 gets from 4 step 4: 0 now has

Quick Sort The quick sort is an in-place, divide-and-conquer, massively recursive sort. Divide and Conquer Algorithms –Algorithms that solve (conquer) problems by dividing them into smaller sub-problems until the problem is so small that it is trivially solved. In Place –In place sorting algorithms don't require additional temporary space to store elements as they sort; they use the space originally occupied by the elements. Reference Source qsort.c

Quick Sort The recursive algorithm consists of four steps (which closely resemble the merge sort): 1.If there are one or less elements in the array to be sorted, return immediately. 2.Pick an element in the array to serve as a "pivot" point. (Usually the left-most element in the array is used.) 3.Split the array into two parts - one with elements larger than the pivot and the other with elements smaller than the pivot. 4.Recursively repeat the algorithm for both halves of the original array.

Quick Sort The efficiency of the algorithm is majorly impacted by which element is chosen as the pivot point. The worst-case efficiency of the quick sort, O(n 2 ), occurs when the list is sorted and the left-most element is chosen. If the data to be sorted isn't random, randomly choosing a pivot point is recommended. As long as the pivot point is chosen randomly, the quick sort has an algorithmic complexity of O(n log n). Pros: Extremely fast. Cons: Very complex algorithm, massively recursive.

Quick Sort Performance ProcessesTime

Quick Sort Speedup ProcessesSpeedup

Discussion Quicksort takes time proportional to N*N for N data items –for 1,000,000 items, Nlog 2 N ~ 1,000,000*20 Constant communication cost – 2*N data items –for 1,000,000 must send/receive 2*1,000,000 from/to root In general, processing/communication proportional to N*log 2 N/2*N = log 2 N/2 –so for 1,000,000 items, only 20/2 =10 times as much processing as communication Suggests can only get speedup, with this parallelization, for very large N

Bubble Sort The bubble sort is the oldest and simplest sort in use. Unfortunately, it's also the slowest. The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items (in other words, all items are in the correct order). This causes larger values to "bubble" to the end of the list while smaller values "sink" towards the beginning of the list.

Bubble Sort The bubble sort is generally considered to be the most inefficient sorting algorithm in common usage. Under best-case conditions (the list is already sorted), the bubble sort can approach a constant O(n) level of complexity. General-case is O(n 2 ). Pros: Simplicity and ease of implementation. Cons: Horribly inefficient. Reference Source bubblesort.c

Bubble Sort Performance ProcessesTime

Bubble Sort Speedup ProcessesSpeedup

Discussion Bubble sort takes time proportional to N*N/2 for N data items This parallelization splits N data items into N/P so time on one of the P processors now proportional to (N/P*N/P)/2 –i.e. have reduced time by a factor of P*P! Bubble sort is much slower than quick sort! –better to run quick sort on single processor than bubble sort on many processors!

Merge Sort 1.The merge sort splits the list to be sorted into two equal halves, and places them in separate arrays. 2.Each array is recursively sorted, and then merged back together to form the final sorted list. Like most recursive sorts, the merge sort has an algorithmic complexity of O(n log n). Elementary implementations of the merge sort make use of three arrays - one for each half of the data set and one to store the sorted list in. The below algorithm merges the arrays in-place, so only two arrays are required. There are non- recursive versions of the merge sort, but they don't yield any significant performance enhancement over the recursive algorithm on most machines.

Merge Sort Pros: Marginally faster than the heap sort for larger sets. Cons: At least twice the memory requirements of the other sorts; recursive. Reference Source sorting/mergesort.c

Heap Sort The heap sort is the slowest of the O(n log n) sorting algorithms, but unlike the merge and quick sorts it doesn't require massive recursion or multiple arrays to work. This makes it the most attractive option for very large data sets of millions of items. The heap sort works as it name suggests 1.It begins by building a heap out of the data set, 2.Then removing the largest item and placing it at the end of the sorted array. 3.After removing the largest item, it reconstructs the heap and removes the largest remaining item and places it in the next open position from the end of the sorted array. 4.This is repeated until there are no items left in the heap and the sorted array is full. Elementary implementations require two arrays - one to hold the heap and the other to hold the sorted elements.

Heap Sort To do an in-place sort and save the space the second array would require, the algorithm below "cheats" by using the same array to store both the heap and the sorted array. Whenever an item is removed from the heap, it frees up a space at the end of the array that the removed item can be placed in. Pros: In-place and non-recursive, making it a good choice for extremely large data sets. Cons: Slower than the merge and quick sorts. Reference Source heapsort.c

End