Difference between revisions of "CSC352: Computing Pi in Parallel with Java"

From dftwiki3
Jump to: navigation, search
Line 6: Line 6:
 
<br />
 
<br />
 
<source lang="java">
 
<source lang="java">
package DT;
+
// PiSerial.java
 
// D. Thiebaut
 
// D. Thiebaut
 
// CSC352 Examples
 
// CSC352 Examples
Line 12: Line 12:
 
// through from the command line and sums up terms to yield an approximation of Pi.
 
// through from the command line and sums up terms to yield an approximation of Pi.
 
// To compile and run
 
// To compile and run
//    javac PiSerial
 
 
//    mkdir DT
 
//    mkdir DT
//    mv PiSerial.class DT
+
//    javac DT/PiSerial.java
 
//    java DT/PiSerial 100000
 
//    java DT/PiSerial 100000
 
//    100000 iterations.  Result = 3.1416026535731527
 
//    100000 iterations.  Result = 3.1416026535731527
Line 24: Line 23:
 
//    user 0m0.290s
 
//    user 0m0.290s
 
//    sys 0m0.051s
 
//    sys 0m0.051s
 +
package DT;
  
 
public class PiSerial {
 
public class PiSerial {
Line 60: Line 60:
 
<br />
 
<br />
 
<source lang="java">
 
<source lang="java">
 +
// 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
 +
//    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
 +
 
package DT;
 
package DT;
  

Revision as of 13:04, 2 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 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
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
//     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

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;
	}

}