Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 320 A First Program in Parallel Java.

Similar presentations


Presentation on theme: "Computer Science 320 A First Program in Parallel Java."— Presentation transcript:

1 Computer Science 320 A First Program in Parallel Java

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

3 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!

4 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

5 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"); }

6 A Sample Run with 4 Values

7 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.

8 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.

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

10 How It Works

11 A Sample Run with 4 Values

12 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


Download ppt "Computer Science 320 A First Program in Parallel Java."

Similar presentations


Ads by Google