Skip to content

Commit 84e1f9b

Browse files
committed
Intial commit
0 parents  commit 84e1f9b

15 files changed

+515
-0
lines changed

.classpath

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
6+
<classpathentry combineaccessrules="false" kind="src" path="/Util"/>
7+
<classpathentry kind="output" path="bin"/>
8+
</classpath>

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
*.class

.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Pi</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

.settings/org.eclipse.jdt.core.prefs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Fri Sep 23 14:46:25 CDT 2011
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.6
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.source=1.6

README

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
PiApproximation
3+
---------------
4+
This application uses an approximation algorithm to compute pi.
5+
6+
Imagine a circle inscribed in a square with side length 1.
7+
The radius of the circle is 1/2.
8+
The area of a circle of radius r is: pi * r * r
9+
So, the area of our circle of radius 0.5 is: pi * 0.5 * 0.5 = pi/4
10+
Hence, pi is four times the area of the circle.
11+
12+
We can approximate the area of the circle by generating random points
13+
in the square of side length 1 and counting how many of them are
14+
within the circle, i.e. closer than 0.5 to the center.
15+
16+
So, the value of pi is the ratio between the number of inscribed points
17+
and the total number of points, times 4.
18+

src/pi/Driver.java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package pi;
2+
3+
import util.Timer;
4+
5+
public class Driver {
6+
private static long sequentialRuntime;
7+
private static final int ITERATIONS = 20000000;
8+
// update the live value each UPDATE_EACH cycles
9+
public static final int UPDATE_EACH = 10000;
10+
11+
public static void main(String[] args) throws Exception {
12+
13+
// Sequential
14+
PiApproximation sequential = new pi.sequential.PiSequential();
15+
test("Sequential version", sequential);
16+
System.out.println();
17+
18+
// Threads
19+
PiApproximation threads = new pi.threads.PiThreads();
20+
test("Threads version", threads);
21+
System.out.println();
22+
23+
// Live
24+
pi.live.PiLive live = new pi.live.PiLive();
25+
test("Live version", live);
26+
System.out.println(" Live Pi: "+live.liveValue()+"\n");
27+
28+
// Live with sync
29+
pi.live.sync.PiLiveWithSync liveWithSync = new pi.live.sync.PiLiveWithSync();
30+
test("Live version with sync", liveWithSync);
31+
System.out.println(" Live Pi: "+liveWithSync.liveValue()+"\n");
32+
33+
// Live with atomic
34+
pi.live.atomic.PiLiveWithAtomic liveWithAtomic = new pi.live.atomic.PiLiveWithAtomic();
35+
test("Live version with atomic", liveWithAtomic);
36+
System.out.println(" Live Pi: "+liveWithAtomic.liveValue()+"\n");
37+
}
38+
39+
40+
private static void test(String version, PiApproximation piApproximation) throws Exception {
41+
// warm-up
42+
warmup(piApproximation);
43+
if(sequentialRuntime == 0)
44+
warmup(piApproximation);
45+
46+
Timer.start();
47+
double pi = piApproximation.computePi(ITERATIONS);
48+
Timer.stop();
49+
System.out.println(version);
50+
System.out.println("-----------------------------");
51+
Timer.log("Time: ");
52+
if(sequentialRuntime == 0)
53+
sequentialRuntime = Timer.getRuntime();
54+
else
55+
System.out.printf("Speed-up: %.2f\n",sequentialRuntime/1.0/Timer.getRuntime());
56+
System.out.println(" Real Pi: "+3.141592653589793238462643383279D);
57+
System.out.println("Estimated Pi: "+pi);
58+
}
59+
60+
private static void warmup(PiApproximation piApproximation)
61+
throws Exception {
62+
piApproximation.computePi(ITERATIONS);
63+
piApproximation.computePi(ITERATIONS);
64+
}
65+
}

src/pi/DriverForJPF.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package pi;
2+
3+
import util.Timer;
4+
5+
public class DriverForJPF {
6+
private static long sequentialRuntime;
7+
private static final int ITERATIONS = 20000000;
8+
// update the live value each UPDATE_EACH cicles
9+
public static final int UPDATE_EACH = 1000;
10+
11+
public static void main(String[] args) throws Exception {
12+
// Live
13+
pi.live.PiLive live = new pi.live.PiLive();
14+
test("Live version", live);
15+
System.out.println("Live value: "+live.liveValue()+"\n");
16+
}
17+
18+
19+
private static void test(String version, PiApproximation piApproximation) throws Exception {
20+
// warm-up
21+
warmup(piApproximation);
22+
if(sequentialRuntime == 0)
23+
warmup(piApproximation);
24+
25+
Timer.start();
26+
double pi = piApproximation.computePi(ITERATIONS);
27+
Timer.stop();
28+
System.out.println(version);
29+
System.out.println("-----------------------------");
30+
System.out.println("Pi: "+pi);
31+
Timer.log("Time: ");
32+
if(sequentialRuntime == 0)
33+
sequentialRuntime = Timer.getRuntime();
34+
else
35+
System.out.printf("Speed-up: %.2f\n",sequentialRuntime/1.0/Timer.getRuntime());
36+
}
37+
38+
private static void warmup(PiApproximation piApproximation)
39+
throws Exception {
40+
piApproximation.computePi(ITERATIONS);
41+
piApproximation.computePi(ITERATIONS);
42+
}
43+
}

src/pi/DriverForJPF.jpf

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#Target class
2+
target=pi.DriverForJPF

src/pi/PiApproximation.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package pi;
2+
3+
public interface PiApproximation {
4+
public double computePi(long iterations) throws Exception;
5+
}

src/pi/live/PiLive.java

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package pi.live;
2+
3+
import java.util.Random;
4+
5+
import pi.Driver;
6+
import pi.PiApproximation;
7+
8+
public class PiLive implements PiApproximation {
9+
10+
long globalInside, globalTotal;
11+
12+
public double computePi(long iterations) throws InterruptedException {
13+
int noProcessors = Runtime.getRuntime().availableProcessors();
14+
PiApproximationThread[] threads = new PiApproximationThread[noProcessors];
15+
16+
for (int i = 0; i < noProcessors; i++) {
17+
threads[i] = new PiApproximationThread(iterations / noProcessors);
18+
threads[i].start();
19+
}
20+
21+
double pi = 0;
22+
23+
for (int i = 0; i < noProcessors; i++) {
24+
threads[i].join();
25+
pi += threads[i].result() / noProcessors;
26+
}
27+
28+
return pi;
29+
}
30+
31+
public double liveValue() {
32+
return globalInside * 4.0 / globalTotal;
33+
}
34+
35+
class PiApproximationThread extends Thread {
36+
private double pi;
37+
private final long iterations;
38+
39+
public PiApproximationThread(long iterations) {
40+
this.iterations = iterations;
41+
}
42+
43+
@Override
44+
public void run() {
45+
Random rand = new java.util.Random();
46+
47+
long inside = 0;
48+
for (int i = 0; i < iterations / Driver.UPDATE_EACH; i++) {
49+
int insideJ = 0;
50+
for (int j = 0; j < Driver.UPDATE_EACH; j++) {
51+
double x = rand.nextDouble();
52+
double y = rand.nextDouble();
53+
double lenght = x * x + y * y;
54+
if (lenght < 1.0) {
55+
insideJ++;
56+
}
57+
}
58+
inside += insideJ;
59+
globalInside += insideJ;
60+
globalTotal += Driver.UPDATE_EACH;
61+
}
62+
pi = inside * 4.0 / iterations;
63+
}
64+
65+
public double result() {
66+
return pi;
67+
}
68+
}
69+
}
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package pi.live.atomic;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.atomic.AtomicLong;
5+
6+
import pi.Driver;
7+
import pi.PiApproximation;
8+
9+
public class PiLiveWithAtomic implements PiApproximation {
10+
11+
AtomicLong globalInside = new AtomicLong();
12+
AtomicLong globalTotal = new AtomicLong();
13+
14+
public double computePi(long iterations) throws InterruptedException {
15+
int noProcessors = Runtime.getRuntime().availableProcessors();
16+
PiApproximationThread[] threads = new PiApproximationThread[noProcessors];
17+
18+
for (int i = 0; i < noProcessors; i++) {
19+
threads[i] = new PiApproximationThread(iterations / noProcessors);
20+
threads[i].start();
21+
}
22+
23+
double pi = 0;
24+
25+
for (int i = 0; i < noProcessors; i++) {
26+
threads[i].join();
27+
pi += threads[i].result() / noProcessors;
28+
}
29+
30+
return pi;
31+
}
32+
33+
public double liveValue() {
34+
return globalInside.intValue() * 4.0 / globalTotal.intValue() ;
35+
}
36+
37+
class PiApproximationThread extends Thread {
38+
private double pi;
39+
private final long iterations;
40+
41+
public PiApproximationThread(long iterations) {
42+
this.iterations = iterations;
43+
}
44+
45+
public double result() {
46+
return pi;
47+
}
48+
49+
@Override
50+
public void run() {
51+
Random rand = new java.util.Random();
52+
53+
int inside = 0;
54+
for (int i = 0; i < iterations/Driver.UPDATE_EACH; i++) {
55+
int insideJ = 0;
56+
for (int j = 0; j < Driver.UPDATE_EACH; j++) {
57+
double x = rand.nextDouble();
58+
double y = rand.nextDouble();
59+
double lenght = x * x + y * y;
60+
if (lenght < 1.0) {
61+
insideJ++;
62+
}
63+
}
64+
inside += insideJ;
65+
globalInside.addAndGet(insideJ);
66+
globalTotal.addAndGet(Driver.UPDATE_EACH);
67+
}
68+
pi = inside * 4.0 / iterations;
69+
}
70+
}
71+
}

src/pi/live/sync/PiLiveWithSync.java

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package pi.live.sync;
2+
3+
import java.util.Random;
4+
5+
import pi.Driver;
6+
import pi.PiApproximation;
7+
8+
public class PiLiveWithSync implements PiApproximation {
9+
10+
long globalInside, globalTotal;
11+
12+
public double computePi(long iterations) throws InterruptedException {
13+
int noProcessors = Runtime.getRuntime().availableProcessors();
14+
PiApproximationThread[] threads = new PiApproximationThread[noProcessors];
15+
16+
for (int i = 0; i < noProcessors; i++) {
17+
threads[i] = new PiApproximationThread(iterations / noProcessors);
18+
threads[i].start();
19+
}
20+
21+
double pi = 0;
22+
23+
for (int i = 0; i < noProcessors; i++) {
24+
threads[i].join();
25+
pi += threads[i].result() / noProcessors;
26+
}
27+
28+
return pi;
29+
}
30+
31+
public double liveValue() {
32+
return globalInside * 4.0 / globalTotal ;
33+
}
34+
35+
class PiApproximationThread extends Thread {
36+
private double pi;
37+
private final long iterations;
38+
39+
public PiApproximationThread(long iterations) {
40+
this.iterations = iterations;
41+
}
42+
43+
@Override
44+
public void run() {
45+
Random rand = new java.util.Random();
46+
47+
long inside = 0;
48+
for (long i = 0; i < iterations / Driver.UPDATE_EACH; i++) {
49+
int insideJ = 0;
50+
for (long j = 0; j < Driver.UPDATE_EACH; j++) {
51+
double x = rand.nextDouble();
52+
double y = rand.nextDouble();
53+
double lenght = x * x + y * y;
54+
if (lenght < 1.0) {
55+
insideJ++;
56+
}
57+
}
58+
inside += insideJ;
59+
synchronized (PiLiveWithSync.class) {
60+
globalInside += insideJ;
61+
globalTotal += Driver.UPDATE_EACH;
62+
}
63+
}
64+
pi = inside * 4.0 / iterations;
65+
}
66+
67+
public double result() {
68+
return pi;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)