12
Computer Science 320 A First Program in Parallel Java

Computer Science 320 A First Program in Parallel Java

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Computer Science 320 A First Program in Parallel Java

Computer Science 320

A First Program in Parallel Java

Page 2: 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

Page 3: Computer Science 320 A First Program in Parallel Java

A Test for Primalitypublic 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!

Page 4: Computer Science 320 A First Program in Parallel Java

Testing n Numbers Sequentiallystatic 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

Page 5: Computer Science 320 A First Program in Parallel Java

Add Some Timingstatic 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"); } }

Page 6: Computer Science 320 A First Program in Parallel Java

A Sample Run with 4 Values

Page 7: Computer Science 320 A First Program in Parallel Java

Testing n Numbers in Parallel, SMPimport 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.

Page 8: Computer Science 320 A First Program in Parallel Java

Specify Code to Run in Each Threadimport 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.

Page 9: Computer Science 320 A First Program in Parallel Java

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

Page 10: Computer Science 320 A First Program in Parallel Java

How It Works

Page 11: Computer Science 320 A First Program in Parallel Java

A Sample Run with 4 Values

Page 12: Computer Science 320 A First Program in Parallel Java

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