Computer Science 320 A First Program in Parallel Java.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Computer Science 320 Clumping in Parallel Java. Sequential vs Parallel Program Initial setup Execute the computation Clean up Initial setup Create a parallel.
1 Repetition structures Overview while statement for statement do while statement.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Unit 171 Algorithms and Problem Solving - II Algorithm Efficiency Primality Testing Improved Primality Testing Sieve of Eratosthenes Primality Testing.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Java Unit 9: Arrays Declaring and Processing Arrays.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Computer Science 320 Load Balancing for Hybrid SMP/Clusters.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Debugging. 2 A Lot of Time is Spent Debugging Programs Debugging. Cyclic process of editing, compiling, and fixing errors. n Always a logical explanation.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Introduction to VSTS Introduction to Visual Studio 2008 Development Edition Understanding code complexity using Code Metrics.
SOFTWARE TECHNOLOGY - I JAVA/OOP Wickramanayake HMKSK Department of Electrical & Electronic Engineering Faculty of Engineering University.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Repetition Statements while and do while loops
Exercise 1 : ex1.java class C extends Thread { int i; C(int i) { this.i = i; } public void run() { System.out.println("Thread " + i + " says hi"); System.out.println("Thread.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Computer Science 320 Massive Parallelism. Example Problem: Breaking a Cipher Somehow obtain a sample plaintext and its ciphertext Then search for the.
SNU IDB Lab. Ch4. Performance Measurement © copyright 2006 SNU IDB Lab.
Computer Science 320 Introduction to Hybrid SMP/Clusters.
Computer Science 320 Reduction. Estimating π Throw N darts, and let C be the number of darts that land within the circle quadrant of a unit circle Then,
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Computer Science 320 Load Balancing with Clusters.
Java Review if Online Time For loop Quiz on Thursday.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Computer Science 320 Random Numbers for Parallel Programs.
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
Computer Science 320 Barrier Actions. 1-D Continuous Cellular Automata 1-D array of cells, each having a value between 0.0 and 1.0 Each cell has a neighborhood.
Computer Science 320 Reduction. Estimating π Throw N darts, and let C be the number of darts that land within the circle quadrant of a unit circle Then,
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Computer Science 320 Introduction to Cluster Computing.
Sophomore Scholars Java
Lab. 2 (April 15th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment.
Control Structures.
Primitive Data, Variables, Loops (Maybe)
Lab. 3 (May 6st) You may use either cygwin or visual studio for using OpenMP Compiling in cygwin “> gcc –fopenmp ex1.c” will generate a.exe Execute : “>
TK1114 Computer Programming
Java 24th sep /19/2018 CFILT.
Decision statements. - They can use logic to arrive at desired results
LRobot Game.
An Introduction to Java – Part I, language basics
Lab. 3 (May 11th) You may use either cygwin or visual studio for using OpenMP Compiling in cygwin “> gcc –fopenmp ex1.c” will generate a.exe Execute :
Unit 3 - The while Loop - Extending the Vic class - Examples
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
class PrintOnetoTen { public static void main(String args[]) {
Computer Science Core Concepts
Lab. 3 (May 1st) You may use either cygwin or visual studio for using OpenMP Compiling in cygwin “> gcc –fopenmp ex1.c” will generate a.exe Execute : “>
In this class, we will cover:
Exercise 1 : ex1.java Thread Creation and Execution
Introduction to Computer Science I.
Exercise 1 : ex1.java Thread Creation and Execution (sleep() and join()) class C extends Thread {   int i;   C(int i) { this.i = i; }   public void run()
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Loops CGS3416 Spring 2019 Lecture 7.
Presentation transcript:

Computer Science 320 A First Program in Parallel Java

A Simple Program Test several numbers for primes First develop a sequential version Then develop a parallel version Compare the running times

A Test for Primality public static boolean isPrime(long x){ if (x % 2 == 0) return false; long p = 3; long psqr = p * p; while (psqr <= x){ if (x % p == 0) return false; p += 2; psqr = p * p; } return true; } Divide x by 2 and by every odd number from 3 up to the square root of x. If remainder is 0, not prime!

Testing n Numbers Sequentially static int n; static long[] x; public static void main(String[] args) throws Exception{ n = args.length; x = new long[n]; for (int i = 0; i < n; ++i) x[i] = Long.parseLong(args[i]); for (int i = 0; i < n; ++i) isPrime(x[i]); } Processes the command line arguments as inputs

Add Some Timing static int n; static long[] x; static long t1, t2[], t3[]; public static void main(String[] args) throws Exception{ t1 = System.currentTimeMillis(); n = args.length; x = new long[n]; for (int i = 0; i < n; ++i) x[i] = Long.parseLong(args[i]); t2 = new long[n]; t3 = new long[n]; for (int i = 0; i < n; ++i){ t2[i] = System.currentTimeMillis(); isPrime(x[i]); t3[i] = System.currentTimeMillis(); } for (int i = 0; i < n; ++i){ System.out.println("i = " + i + " call start = " + (t2[i] - t1) + " msec"); System.out.println("i = " + i + " call finish = " + (t3[i] - t1) + " msec"); }

A Sample Run with 4 Values

Testing n Numbers in Parallel, SMP import edu.rit.pj.ParallelTeam; static int n; static long[] x; public static void main(String[] args) throws Exception{ n = args.length; x = new long[n]; for (int i = 0; i < n; ++i) x[i] = Long.parseLong(args[i]); new ParallelTeam(n)... } The parallel team will contain as many threads as there are inputs.

Specify Code to Run in Each Thread import edu.rit.pj.ParallelRegion; import edu.rit.pj.ParallelTeam; new ParallelTeam(n).execute(new ParallelRegion(){ public void run(){ int i = getThreadIndex(); isPrime(x[i]); } }); A parallel region contains the code that runs in each thread. Each thread executes the same run method, but with a different input value.

Add the Timing new ParallelTeam(n).execute(new ParallelRegion(){ public void run(){ int i = getThreadIndex(); t2[i] = System.currentTimeMillis(); isPrime(x[i]); t3[i] = System.currentTimeMillis(); } });

How It Works

A Sample Run with 4 Values

A New View of Repetition Logically, a loop repeats a sequence of instructions On a sequential machine, the sequence is repeated On a parallel computer, the sequence can be copied and distributed to multiple processors