Skip to content

Commit 6fc9f56

Browse files
committed
Inital commit
1 parent 3912480 commit 6fc9f56

35 files changed

+920
-0
lines changed

.classpath

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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.8"/>
5+
<classpathentry kind="lib" path="lib/apfloat.jar"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

.project

Lines changed: 17 additions & 0 deletions
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_Ramanujan2_swing</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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8

Pi_Calculation_Ramanujan.docx

205 KB
Binary file not shown.

bin/main/App$1.class

676 Bytes
Binary file not shown.

bin/main/App.class

3.3 KB
Binary file not shown.

bin/main/Calculator$1.class

1.03 KB
Binary file not shown.

bin/main/Calculator.class

3.83 KB
Binary file not shown.

bin/main/CustomMath.class

827 Bytes
Binary file not shown.

bin/main/GUI/DetailEvent.class

723 Bytes
Binary file not shown.

bin/main/GUI/DetailListener.class

206 Bytes
Binary file not shown.

bin/main/GUI/DetailsPanel$1.class

1.39 KB
Binary file not shown.

bin/main/GUI/DetailsPanel.class

3.75 KB
Binary file not shown.

bin/main/GUI/MainFrame$1.class

1.68 KB
Binary file not shown.
1.24 KB
Binary file not shown.
7.03 KB
Binary file not shown.
2.55 KB
Binary file not shown.

bin/main/GUI/MainFrame.class

2.67 KB
Binary file not shown.

bin/main/GUI/TextPanel.class

798 Bytes
Binary file not shown.

bin/main/Rumanujan2Pi.class

2.36 KB
Binary file not shown.

bin/main/Task.class

2.26 KB
Binary file not shown.

bin/main/TaskResult.class

917 Bytes
Binary file not shown.

lib/apfloat.jar

424 KB
Binary file not shown.

pi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405

src/main/App.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package main;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.FileOutputStream;
5+
import java.io.PrintWriter;
6+
import java.rmi.RemoteException;
7+
import java.util.concurrent.ExecutionException;
8+
9+
import main.Rumanujan2Pi;
10+
import main.Calculator;
11+
import main.CustomMath;
12+
import main.Task;
13+
import main.TaskResult;
14+
import main.Task;
15+
16+
import javax.swing.SwingUtilities;
17+
18+
import main.GUI.MainFrame;
19+
20+
import org.apfloat.Apfloat;
21+
22+
public class App {
23+
24+
public static boolean quiet = false;
25+
26+
public static void main(String[] args) throws FileNotFoundException,
27+
InterruptedException, ExecutionException, RemoteException {
28+
29+
int numThreads = 1, numTerms = 100;
30+
String outFile = "pi";
31+
32+
for (int i = 0; i < args.length; i++) {
33+
String arg = args[i];
34+
35+
if (arg.equals("-p") || arg.equals("--terms")) {
36+
numTerms = Integer.parseInt(args[i + 1]);
37+
i++;
38+
} else if (arg.equals("-t") || arg.equals("--threads")) {
39+
numThreads = Integer.parseInt(args[i + 1]);
40+
i++;
41+
} else if (arg.equals("-o") || arg.equals("--out")) {
42+
outFile = args[i + 1];
43+
i++;
44+
} else if (arg.equals("-q") || arg.equals("--quiet")) {
45+
quiet = true;
46+
} else {
47+
System.err.println("Unknown option " + arg);
48+
System.exit(1);
49+
}
50+
}
51+
52+
if (numTerms == 0) {
53+
System.err.println("Number of terms should be specified with -p");
54+
System.exit(1);
55+
}
56+
57+
if (numThreads == 0) {
58+
numThreads = Runtime.getRuntime().availableProcessors();
59+
System.out.println("Using " + numThreads + " threads");
60+
}
61+
62+
Rumanujan2Pi sum = new Rumanujan2Pi(numTerms, quiet);
63+
if (quiet) {
64+
CaltulatePi(sum, numTerms, numThreads, outFile);
65+
} else {
66+
final int numTermsF = numTerms;
67+
final int numThreadsF = numThreads;
68+
final String outFileF = outFile;
69+
70+
SwingUtilities.invokeLater(new Runnable() {
71+
public void run() {
72+
new MainFrame(numTermsF, numThreadsF, outFileF);
73+
}
74+
});
75+
}
76+
77+
}
78+
79+
private static void CaltulatePi(Rumanujan2Pi sum, int numTerms,
80+
int numThreads, String outFile) throws FileNotFoundException,
81+
InterruptedException, ExecutionException, RemoteException {
82+
83+
long startTime, timeToCompute;
84+
85+
Calculator calculator = new Calculator(numThreads);
86+
TaskResult result;
87+
Apfloat pi;
88+
89+
startTime = System.currentTimeMillis();
90+
result = calculator.calculate(sum, numTerms);
91+
pi = sum.roundPiSum(result);
92+
timeToCompute = System.currentTimeMillis() - startTime;
93+
calculator.shutdown();
94+
95+
System.out.println("Time to complete: " + timeToCompute + "ms");
96+
97+
PrintWriter file = new PrintWriter(new FileOutputStream(outFile, false));
98+
file.println(pi.toString());
99+
file.flush();
100+
file.close();
101+
102+
}
103+
}

src/main/Calculator.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package main;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.Callable;
6+
import java.util.concurrent.ExecutionException;
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.Future;
10+
11+
import org.apfloat.Apfloat;
12+
13+
/* Main thread operator */
14+
//Controls the threads, the desired calculator is split into
15+
16+
public class Calculator {
17+
private List<Task> tasks;
18+
private ExecutorService executor;
19+
private int numThreads;
20+
21+
public Calculator(int numThreads) {
22+
this.numThreads = numThreads;
23+
tasks = new ArrayList<Task>(numThreads);
24+
this.executor = Executors.newFixedThreadPool(numThreads);
25+
}
26+
27+
Calculator(int numThreads, int maxNumThreadsAtOnce) {
28+
tasks = new ArrayList<Task>(numThreads);
29+
this.executor = Executors.newFixedThreadPool(maxNumThreadsAtOnce);
30+
}
31+
32+
public TaskResult calculate(Rumanujan2Pi sum, int numTerms)
33+
throws InterruptedException, ExecutionException {
34+
35+
distributeTaskTerms(numTerms);
36+
final List<Future<TaskResult>> futureResults = new ArrayList<Future<TaskResult>>(
37+
tasks.size());
38+
39+
for (int i = 0; i < tasks.size(); i++) {
40+
Task taskToCall = tasks.get(i);
41+
42+
Callable<TaskResult> callableTask = new Callable<TaskResult>() {
43+
@Override
44+
public TaskResult call() throws Exception {
45+
return taskToCall.calculate(sum);
46+
}
47+
};
48+
49+
futureResults.add(executor.submit(callableTask));
50+
51+
}
52+
53+
List<TaskResult> finalResults = waitAndGetAllResults(futureResults);
54+
55+
return combineSums(finalResults);
56+
}
57+
58+
private void distributeTaskTerms(int numTerms) {
59+
int numTermsLeft = numTerms;
60+
int termsAssigned = numTerms / numThreads;
61+
int startIndex = 0;
62+
63+
for (int i = 0; i < numThreads - 1; i++) {
64+
tasks.add(new Task(i, startIndex, termsAssigned));
65+
startIndex += termsAssigned;
66+
numTermsLeft -= termsAssigned;
67+
}
68+
69+
tasks.add(new Task(numThreads - 1, startIndex, numTermsLeft));
70+
}
71+
72+
private List<TaskResult> waitAndGetAllResults(
73+
List<Future<TaskResult>> futureResults)
74+
throws InterruptedException, ExecutionException {
75+
76+
List<TaskResult> results = new ArrayList<TaskResult>(
77+
futureResults.size());
78+
79+
for (int i = 0; i < futureResults.size(); i++) {
80+
results.add(futureResults.get(i).get());
81+
}
82+
83+
return results;
84+
}
85+
86+
private TaskResult combineSums(List<TaskResult> results) {
87+
Apfloat sum = Apfloat.ZERO;
88+
Apfloat lastPartialTerm = Apfloat.ONE;
89+
int termCount = 0;
90+
91+
for (TaskResult partialResult : results) {
92+
sum = sum.add(lastPartialTerm.multiply(partialResult.getSum()));
93+
94+
lastPartialTerm = lastPartialTerm.multiply(partialResult
95+
.getLastPartialTerm());
96+
termCount += partialResult.getNumTerms();
97+
}
98+
99+
return new TaskResult(sum, lastPartialTerm, termCount, 1);
100+
}
101+
102+
public void shutdown() {
103+
executor.shutdown();
104+
}
105+
}

src/main/CustomMath.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main;
2+
import org.apfloat.Apfloat;
3+
4+
public class CustomMath {
5+
public static Apfloat fact(int num) {
6+
if (num == 0) {
7+
return Apfloat.ONE;
8+
}
9+
10+
return fact2(1, num);
11+
}
12+
13+
public static Apfloat fact2(int from, int to) {
14+
if (from == to) {
15+
return new Apfloat(from);
16+
}
17+
18+
int middle = (from + to) / 2;
19+
Apfloat a = fact2(from, middle);
20+
Apfloat b = fact2(middle + 1, to);
21+
22+
return a.multiply(b);
23+
}
24+
25+
}

src/main/GUI/DetailEvent.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main.GUI;
2+
import java.util.EventObject;
3+
4+
import javax.swing.JProgressBar;
5+
6+
7+
public class DetailEvent extends EventObject{
8+
private int[] details;
9+
JProgressBar[] progressBars;
10+
11+
public DetailEvent(Object source, int[] details,JProgressBar[] progressBars) {
12+
super(source);
13+
this.details = details;
14+
this.progressBars = progressBars;
15+
}
16+
17+
public int[] getDetails() {
18+
return details;
19+
}
20+
21+
public JProgressBar[] getProgressBars() {
22+
return progressBars;
23+
}
24+
}

src/main/GUI/DetailListener.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main.GUI;
2+
3+
import java.util.EventListener;
4+
5+
public interface DetailListener extends EventListener {
6+
public void detailEventOccured(DetailEvent event);
7+
8+
}

0 commit comments

Comments
 (0)