Professional Documents
Culture Documents
Parallel Sys
Parallel Sys
Parallel Sys
SEQUENTIAL PROGRAM codes Screen Shot for the Sequential CONCURRENT PROGRAM Screen Shot for the Concurrent Program Adv n Disadv of Sequential Adv n Disadv of Parallel Reference
Page
2-3 4 5-8 9 10 11 12
SEQUENTIAL PROGRAM
Class MySeqSumAvg Syntax import java.io.*; public class MySeqSumAvg { private int totNum = 0; private int theList[]; private int totSum = 0; private double avg = 0.0; public void getIntputData() { totNum = getNum("How many numbers? > "); theList = new int[totNum]; for(int k = 0; k < totNum; k++) { theList[k] = (int)(Math.random() * 100); } printList(0, theList); } public void processSumAvgData() { double count = 0.0; for(int i = 0; i < theList.length; i++) { if(theList[i] > 50) { if(theList[i]%2 != 0) { totSum = totSum + theList[i]; count = count + 1; System.out.println("Processing L" + 0 + " no." + (i+1) + " " + theList[i]); } try{ avg = (double)(totSum) / count; } catch(ArithmeticException e){ } } } }
public int getNum(String s) { int k = 0; BufferedReader br; br = new BufferedReader(new InputStreamReader(System.in)); System.out.print(s + " >> "); try { k = Integer.parseInt(br.readLine()); } catch (Exception e) { } return k; }
public void printResult() { System.out.println(); System.out.println("*********Total Sum of the Numbers*************"); System.out.println("Grand Total for all numbers is " + totSum); System.out.println(); System.out.println("*********Average of all Numbers*************"); System.out.println("The Average for all numbers is " + avg); } public void printList(int idx, int[] aList) { System.out.println(); System.out.print("List " + idx + " = "); for(int k = 0; k < aList.length; k++) System.out.print(aList[k] + " "); System.out.println(); }
public static void main(String args[]) { MySeqSumAvg mss = new MySeqSumAvg(); mss.getIntputData(); mss.processSumAvgData(); mss.printResult(); } }
Class MyConSumAvg Syntax import java.io.*; public class MyConSumAvg { private int totNum = 0; private int theList[]; private int totPro = 0; private MySumAvg subProcess[]; private int totSum = 0; private double theCount = 0.0; private double averageSum = 0.0; public void getInputData() { totNum = getNum("How many numbers? > "); theList = new int[totNum]; for(int k = 0; k < totNum; k++) { theList[k] = (int)(Math.random() * 100); } printList(0, theList); totPro = getNum("How many processor? >"); subProcess = new MySumAvg[totPro]; } public void processSumAvgData() { int range = (int)(theList.length/totPro); if(theList.length % totPro == 0) { for(int k = 0; k < totPro; k++) { int subList[] = new int[range]; for(int j=(range * k); j<(range * (k+1)); j++) subList[j - (range * k)] = theList[j]; 6
printList((k+1), subList); subProcess[k] = new MySumAvg((k+1), subList); } for(int k=0; k<totPro; k++) subProcess[k].start(); try { for(int k=0; k<totPro; k++) subProcess[k].join(); } catch(Exception e) { } } else { System.out.println(); System.out.println("*********Invalid entry!!********"); System.out.println("Array Number must be Divisible by number of processors"); System.out.println(); theList = new int[0]; subProcess = new MySumAvg[0]; getInputData(); processSumAvgData(); } } public void printResult() { for(int k=0; k<totPro; k++) { totSum = totSum + subProcess[k].getSum(); theCount = theCount + subProcess[k].getCount(); } try{ averageSum = (double)(totSum)/theCount; } catch(ArithmeticException e){ } System.out.println(); System.out.println("=========== Total for all Numbers============"); System.out.println("Grand Total for all numbers is " + totSum); System.out.println(); System.out.println("=========== Average for all Numbers============"); System.out.println("The Average for all numbers is " + averageSum); }
public int getNum(String s) { int k = 0; BufferedReader br; br = new BufferedReader(new InputStreamReader(System.in)); System.out.print(s + " >> "); try { k = Integer.parseInt(br.readLine()); } catch (Exception e) { } return k; } public void printList(int idx, int[] aList) { System.out.println(); System.out.print("Processor " + idx + " List " + " = "); for(int k = 0; k < aList.length; k++) System.out.print(aList[k] + " "); System.out.println(); } public static void main(String args[]) { MyConSumAvg mcsa = new MyConSumAvg(); mcsa.getInputData(); mcsa.processSumAvgData(); mcsa.printResult(); } }
Screen Shot for the Concurrent Program The following screen shot shows the error message the program provides the user when the user enters values that are not divisible
output of the total sum and average of the arrray list entered after it has been processed concurrently
Disadvantages of SISD Because sequential processing usually involves the use of a single processor it is quite slower than parallel processing. It is not suitable for processing larger amount of data
10
Advantages of this Approach Suitable for processing large amount of data. Allow simultaneous processing of several data via its multi-threading capabilities. Allows faster computation of data compared sequential of any other form of processing. They operate asynchronously i.e. each processor can perform different operation on the data they are processing at the same time since each processor runs its own program.
Disadvantages of this Approach Does not necessary permit shared memory since each processor can execute its own program.
11
References
Wikipedia the free Encyclopedia, Flynns http://en.wikipedia.org/wiki/Flynn's_taxonomy, accessed May 30, 2012. Taxonomy,
Wikipedia the free Encyclopedia, Multiple Instructions, Multiple Data (MIMD), http://en.wikipedia.org/wiki/MIMD, accessed May 30, 2012. Wikipedia the free Encyclopedia, Single Instructions, Single Data (SISD), http://en.wikipedia.org/wiki/SISD, accessed May 30, 2012.
12