Parallel Sys

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Table of Contens

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(); } }

Screen Shot for the Sequential

PARALLEL / CONCURRENT PROGRAM


Class MySumAvg Syntax public class MySumAvg extends Thread { private int index; private int aList[]; private int sum = 0; private double count = 0.0; public MySumAvg(int i, int lis[]) { index = i; aList = lis; } public void run() { for(int i = 0; i < aList.length; i++) { if(aList[i] > 50) { if(aList[i]%2 != 0) { sum = sum + aList[i]; count = count + 1; System.out.println("Processing L" + 0 + " no." + (i+1) + " " + aList[i]); } } try { sleep(100); } catch(Exception e) { } } } public int getSum() { return sum; } 5

public double getCount() { return count; } }

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

Approach to the Sequential Processing


To perform the operation which processes SUM and AVERAGE of ODD numbers that are greater than 50 using a sequential program. The single instruction stream, single data stream (SISD) classification of machine according to Flynns taxonomy best describes the sequential process. In this approach a single processing unit receives the stream of instructions that would operate on the data stream Advantages of SISD Suitable for processing calculations that depend on the result of the previous calculations. It allows the data to be processed one step at a time (sequentially).

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

Approach to the Parallel Processing


This approach involves the implementation of the single program, multiple data, using the multiple instruction stream, multiple data stream (MIMD) of Flynns taxonomy. In the process the same program operation is run/ performed on the multiple processors used in the MIMD machine. On the MIMD machine multiple number of instruction (N-instructions) and multiple data (N-data) are computed using multiple processors (N-processors). In this approach the main array of data is first broken down into N number of multiple data N-data to allow simultaneous processing by the processors, this is referred to as Geometric or partitioned parallelism.

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

You might also like