Difference between revisions of "CSC352: Computing Pi in Parallel with Java"
(Created page with "--~~~~ ---- =Computing an Approximation of Pi: Serial Version: = <br /> <source lang="java"> package DT; // D. Thiebaut // CSC352 Examples // Computation of Pi. This versio...") |
|||
Line 54: | Line 54: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | |||
+ | =Computing Pi with 2 Threads= | ||
+ | This version uses two threads (extending Java Threads). The threads run independently in parallel while the main program waits | ||
+ | for them to be done. The main program then gather the two results and stops<br />. | ||
+ | <br /> | ||
+ | <source lang="java"> | ||
+ | package DT; | ||
+ | |||
+ | public class PiParallel1 { | ||
+ | |||
+ | public static void main( String[] args ) { | ||
+ | //--- syntax: java -jar PiSerial.jar N --- | ||
+ | if ( args.length == 0 ) { | ||
+ | System.out.println( "Syntax: PiParallel1 N\nwhere N is the number of iterations\n\n" ); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | int N = Integer.parseInt( args[0] ); | ||
+ | if ( N==0 ) N = 1000000; | ||
+ | |||
+ | //--- create two threads --- | ||
+ | PiThread t1 = new PiThread( N, 0, N/2 ); | ||
+ | PiThread t2 = new PiThread( N, N/2, N ); | ||
+ | |||
+ | //--- start two threads --- | ||
+ | t1.start(); | ||
+ | t2.start(); | ||
+ | |||
+ | //--- wait till they finish --- | ||
+ | try { | ||
+ | t1.join(); | ||
+ | t2.join(); | ||
+ | } catch (InterruptedException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | |||
+ | //--- get results and sum up --- | ||
+ | System.out.println( N + " iterations. Result = " + t1.getResult() + t2.getResult() + "\n\n" ); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * PiThread. A thread that computes a section of the area defining the | ||
+ | * approximation of Pi. | ||
+ | */ | ||
+ | class PiThread extends Thread { | ||
+ | private int N; // the total number of samples/iterations | ||
+ | private int start; // where this thread should start (included) | ||
+ | private int stop; // where this thread should stop iterating (excluded) | ||
+ | private double result; // the part of the approximation computed. | ||
+ | |||
+ | /** | ||
+ | * inspector. | ||
+ | * @return result computed. | ||
+ | */ | ||
+ | public double getResult() { | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Constructor. | ||
+ | * @param n: the total number of samples used by the main program. | ||
+ | * @param sta the starting point for the iterations | ||
+ | * @param sto the stopping point for the iterations | ||
+ | */ | ||
+ | public PiThread( int n, int sta, int sto ) { | ||
+ | N = n; | ||
+ | start = sta; | ||
+ | stop = stop; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * the function that computes the elementary term | ||
+ | * @param x | ||
+ | * @return | ||
+ | */ | ||
+ | double f( double x ) { | ||
+ | return 4 / ( 1 + x*x ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * the main RUN() method of the thread. It will be automatically called | ||
+ | * by start(). Does all the work, iterating from start to stop and putting | ||
+ | * the finished slice of the sum in result. | ||
+ | */ | ||
+ | @Override | ||
+ | public void run() { | ||
+ | result = 0; | ||
+ | double deltaX = 1.0/N; | ||
+ | for ( int i=start; i<stop; i++ ) | ||
+ | result += f( i*deltaX ); | ||
+ | result *= deltaX; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:Java]][[Category:CSC352]] |
Revision as of 12:58, 2 September 2013
--D. Thiebaut (talk) 13:29, 2 September 2013 (EDT)
Computing an Approximation of Pi: Serial Version:
package DT;
// D. Thiebaut
// CSC352 Examples
// Computation of Pi. This version is serial and takes a number of iterations to go
// through from the command line and sums up terms to yield an approximation of Pi.
// To compile and run
// javac PiSerial
// mkdir DT
// mv PiSerial.class DT
// java DT/PiSerial 100000
// 100000 iterations. Result = 3.1416026535731527
//
// To time the execution:
// time java DT/PiSerial 100000
// 100000 iterations. Result = 3.1416026535731527
// real 0m0.238s
// user 0m0.290s
// sys 0m0.051s
public class PiSerial {
private static double f( double x ) {
return 4.0 / ( 1 + x*x );
}
public static void main( String[] args ) {
//--- syntax: java -jar PiSerial.jar N ---
if ( args.length == 0 ) {
System.out.println( "Syntax: PiSerial N\nwhere N is the number of iterations\n\n" );
return;
}
int N = Integer.parseInt( args[0] );
if ( N==0 ) N = 1000;
double sum = 0;
double deltaX = 1.0/N;
//--- iterate ---
for ( int i=0; i<N; i++ )
sum += f( i * deltaX );
System.out.println( N + " iterations. Result = " + sum*deltaX + "\n\n" );
}
}
Computing Pi with 2 Threads
This version uses two threads (extending Java Threads). The threads run independently in parallel while the main program waits
for them to be done. The main program then gather the two results and stops
.
package DT;
public class PiParallel1 {
public static void main( String[] args ) {
//--- syntax: java -jar PiSerial.jar N ---
if ( args.length == 0 ) {
System.out.println( "Syntax: PiParallel1 N\nwhere N is the number of iterations\n\n" );
return;
}
int N = Integer.parseInt( args[0] );
if ( N==0 ) N = 1000000;
//--- create two threads ---
PiThread t1 = new PiThread( N, 0, N/2 );
PiThread t2 = new PiThread( N, N/2, N );
//--- start two threads ---
t1.start();
t2.start();
//--- wait till they finish ---
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
//--- get results and sum up ---
System.out.println( N + " iterations. Result = " + t1.getResult() + t2.getResult() + "\n\n" );
}
}
/**
* PiThread. A thread that computes a section of the area defining the
* approximation of Pi.
*/
class PiThread extends Thread {
private int N; // the total number of samples/iterations
private int start; // where this thread should start (included)
private int stop; // where this thread should stop iterating (excluded)
private double result; // the part of the approximation computed.
/**
* inspector.
* @return result computed.
*/
public double getResult() {
return result;
}
/**
* Constructor.
* @param n: the total number of samples used by the main program.
* @param sta the starting point for the iterations
* @param sto the stopping point for the iterations
*/
public PiThread( int n, int sta, int sto ) {
N = n;
start = sta;
stop = stop;
}
/**
* the function that computes the elementary term
* @param x
* @return
*/
double f( double x ) {
return 4 / ( 1 + x*x );
}
/**
* the main RUN() method of the thread. It will be automatically called
* by start(). Does all the work, iterating from start to stop and putting
* the finished slice of the sum in result.
*/
@Override
public void run() {
result = 0;
double deltaX = 1.0/N;
for ( int i=start; i<stop; i++ )
result += f( i*deltaX );
result *= deltaX;
}
}