Difference between revisions of "CSC352: Computing Pi in Parallel with Java"
(→Computing Pi with 2 Threads) |
m (Thiebaut moved page Computing Pi to CSC352: Computing Pi) |
(No difference)
|
Revision as of 20:03, 4 September 2013
--D. Thiebaut (talk) 13:29, 2 September 2013 (EDT)
Computing an Approximation of Pi: Serial Version:
// PiSerial.java
// 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
// mkdir DT
// javac DT/PiSerial.java
// java DT/PiSerial 1000000
// 1000000 iterations. Result = 3.141593653589793
//
// To time the execution:
// time java DT/PiSerial 1000000
// 1000000 iterations. Result = 3.141593653589793
// real 0m0.253s
// user 0m0.299s
// sys 0m0.053s
package DT;
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
.
// PiParallel1.java
// D. Thiebaut
// CSC352 Examples
// Computation of Pi. This version is parallel and takes a number of iterations to go
// through from the command line and sums up terms to yield an approximation of Pi.
// It 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
//
// To compile and run
// javac DT/PiParallel1.java
// java DT/PiSerial 1000000
// 1000000 iterations. Result = 3.1415936535895885
// To time the execution:
// time java DT/PiSerial 1000000
// 1000000 iterations. Result = 3.1415936535895885
// real 0m0.248s
// user 0m0.331s
// sys 0m0.057s
package DT;
public class PiParallel1 {
public static void main( String[] args ) {
//--- syntax: java -jar PiSerial.jar N ---
if ( args.length == 0 ) {
Runtime runtime = Runtime.getRuntime();
System.out.println( "Syntax: PiParallel1 N\nwhere N is the number of iterations" );
System.out.println( "Note: there are " + runtime.availableProcessors()
+ " cores available\n\n" );
return;
}
int N = Integer.parseInt( args[0] );
if ( N==0 ) N = 1000000;
//System.out.println( "N = " + N );
//--- 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.
/**
* 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 start, int stop ) {
super( "Thread-"+start+"-"+stop ); // give a name to the thread
this.N = N;
this.start = start;
this.stop = stop;
}
/**
* inspector.
* @return result computed.
*/
public double getResult() {
return result;
}
/**
* the function that computes the elementary term
* @param x
* @return
*/
double f( double x ) {
return 4.0 / ( 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() {
//System.out.println( getName() + " starting..." );
result = 0;
double deltaX = 1.0/N;
for ( int i=start; i<stop; i++ )
result += f( i*deltaX );
result *= deltaX;
//System.out.println( getName() + " done: partial result = " + result );
}
}