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

From dftwiki3
Jump to: navigation, search
 
(6 intermediate revisions by the same user not shown)
Line 14: Line 14:
 
//    mkdir DT
 
//    mkdir DT
 
//    javac DT/PiSerial.java
 
//    javac DT/PiSerial.java
//    java DT/PiSerial 100000
+
//    java DT/PiSerial 1000000
//    100000 iterations.  Result = 3.1416026535731527
+
//    1000000 iterations.  Result = 3.141593653589793
 
//  
 
//  
 
// To time the execution:
 
// To time the execution:
//    time java DT/PiSerial 100000
+
//    time java DT/PiSerial 1000000
//    100000 iterations.  Result = 3.1416026535731527
+
//    1000000 iterations.  Result = 3.141593653589793
//     real 0m0.238s
+
//     real 0m0.253s
//     user 0m0.290s
+
//     user 0m0.299s
//     sys 0m0.051s
+
//     sys 0m0.053s
 +
 
 
package DT;
 
package DT;
  
Line 57: Line 58:
 
=Computing Pi with 2 Threads=
 
=Computing Pi with 2 Threads=
 
This version uses two threads (extending Java Threads).  The threads run independently in parallel while the main program waits
 
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 />.
+
for them to be done.  The main program then gathers the two results and stops<br />.
 
<br />
 
<br />
 
<source lang="java">
 
<source lang="java">
Line 72: Line 73:
 
//    javac DT/PiParallel1.java
 
//    javac DT/PiParallel1.java
 
//    java DT/PiSerial 1000000
 
//    java DT/PiSerial 1000000
//     100000 iterations.  Result = 3.1416026535731527
+
//   1000000 iterations.  Result = 3.1415936535895885
//
 
 
// To time the execution:
 
// To time the execution:
//    time java DT/PiSerial 100000
+
//    time java DT/PiSerial 1000000
//    100000 iterations.  Result = 3.1416026535731527
+
//    1000000 iterations.  Result = 3.1415936535895885
//    real 0m0.238s
+
//    real 0m0.248s
//    user 0m0.290s
+
//    user 0m0.331s
//    sys 0m0.051s
+
//    sys 0m0.057s
  
 
package DT;
 
package DT;
Line 88: Line 88:
 
//--- syntax: java -jar PiSerial.jar N ---
 
//--- syntax: java -jar PiSerial.jar N ---
 
    if ( args.length == 0 ) {
 
    if ( args.length == 0 ) {
            System.out.println( "Syntax: PiParallel1 N\nwhere N is the number of iterations\n\n" );
+
                    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;
 
            return;
 
    }
 
    }
Line 94: Line 97:
 
    int N = Integer.parseInt( args[0] );
 
    int N = Integer.parseInt( args[0] );
 
    if ( N==0 ) N = 1000000;
 
    if ( N==0 ) N = 1000000;
 +
    //System.out.println( "N = " + N );
 
     
 
     
 
    //--- create two threads ---
 
    //--- create two threads ---
Line 112: Line 116:
 
 
 
//--- get results and sum up ---
 
//--- get results and sum up ---
System.out.println( N + " iterations.  Result = " + t1.getResult() + t2.getResult() + "\n\n" );
+
System.out.println( N + " iterations.  Result = " + (t1.getResult() + t2.getResult()) + "\n\n" );
 
}
 
}
 
}
 
}
Line 126: Line 130:
 
private double result; // the part of the approximation computed.
 
private double result; // the part of the approximation computed.
 
 
/**
 
* inspector.
 
* @return result computed.
 
*/
 
public double getResult() {
 
return result;
 
}
 
 
 
 
/**
 
/**
Line 140: Line 137:
 
* @param sto the stopping point for the iterations
 
* @param sto the stopping point for the iterations
 
*/
 
*/
public PiThread( int n, int sta, int sto ) {
+
public PiThread( int N, int start, int stop ) {
N = n;
+
super( "Thread-"+start+"-"+stop ); // give a name to the thread
start = sta;
+
this.N = N;
stop = stop;
+
this.start = start;
 +
this.stop = stop;
 
}
 
}
 
 
 +
/**
 +
* inspector.
 +
* @return result computed.
 +
*/
 +
public double getResult() {
 +
return result;
 +
}
 +
 
/**
 
/**
 
* the function that computes the elementary term
 
* the function that computes the elementary term
Line 152: Line 158:
 
*/
 
*/
 
double f( double x ) {
 
double f( double x ) {
return 4 / ( 1 + x*x );
+
return 4.0 / ( 1 + x*x );
 
}
 
}
 
 
Line 162: Line 168:
 
@Override
 
@Override
 
public void run() {
 
public void run() {
 +
//System.out.println( getName() + " starting..." );
 
result = 0;
 
result = 0;
 
double deltaX = 1.0/N;
 
double deltaX = 1.0/N;
for ( int i=start; i<stop; i++ )
+
for ( int i=start; i<stop; i++ )  
 
result += f( i*deltaX );
 
result += f( i*deltaX );
 +
 
result *= deltaX;
 
result *= deltaX;
 +
//System.out.println( getName() + " done: partial result = " + result );
 
}
 
}
  

Latest revision as of 12:40, 31 January 2017

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

}