public
Created

jActor - Pi Calculation

  • Download Gist
gistfile1.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
import org.agilewiki.jactor.*;
import org.agilewiki.jactor.lpc.JLPCActor;
import org.agilewiki.jactor.lpc.Request;
 
import java.util.Random;
 
/**
* @author Daniel Seidler
* @since 2013/01/04
*/
public class PijActor implements PiCalculator{
public void calculate(final int nrOfWorkers, final int nrOfElements, final int nrOfMessages) throws Exception {
System.out.println("JACTOR - calc - ("+nrOfWorkers+","+nrOfElements+","+nrOfMessages+")");
MailboxFactory mailboxFactory = JAMailboxFactory.newMailboxFactory(nrOfWorkers);
Mailbox mailbox = mailboxFactory.createMailbox();
Master master = new Master();
master.initialize(mailbox);
JAFuture future = new JAFuture();
Start.init(nrOfElements,nrOfMessages).send(future,master);
mailboxFactory.close();
}
 
public static void main(String args[]) throws Exception {
PijActor pi = new PijActor();
pi.calculate(4, 20000, 20000);
}
}
 
class Start extends Request<Object, Master> {
private int nrOfElements;
private int nrOfMessages;
 
private Start(int nrOfElements, int nrOfMessages){
this.nrOfElements = nrOfElements;
this.nrOfMessages = nrOfMessages;
}
 
public static Start init(int nrOfElements, int nrOfMessages){
return new Start(nrOfElements, nrOfMessages);
}
 
public void processRequest(JLPCActor targetActor, final RP rp)
throws Exception {
Master a = (Master) targetActor;
RP prp = new RP() {
final long start = System.currentTimeMillis();
boolean pending = true;
double pi;
int nrOfResults;
 
public void processResponse(Object obj) throws Exception {
pi += ((Double) obj).doubleValue();
nrOfResults += 1;
if (nrOfResults == nrOfMessages) {
System.out.println(String.format("\tPi approximation: \t\t%s\n\tCalculation time: \t%s", pi, System.currentTimeMillis() - start));
rp.processResponse(null);
}
}
};
a.calculate(nrOfElements, nrOfMessages, prp);
}
 
public boolean isTargetType(Actor targetActor) {
return targetActor instanceof Master;
}
}
 
 
class Master extends JLPCActor {
public void calculate(final int nrOfMessages, int nrOfElements, final RP rp) throws Exception {
for (int start = 0; start < nrOfMessages; start++) {
Worker worker = new Worker();
worker.initialize(getMailboxFactory().createAsyncMailbox());
(new Work(start, nrOfElements)).send(this, worker, rp);
}
}
}
 
 
class Worker extends JLPCActor {
public double calculatePiFor(int start, int nrOfElements) {
double acc = 0.0;
for (int i = start * nrOfElements; i <= ((start + 1) * nrOfElements - 1); i++) {
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1);
}
return acc;
}
}
 
 
class Work extends Request<Double, Worker> {
private final int start;
private final int nrOfElements;
 
public Work(int start, int nrOfElements) {
this.start = start;
this.nrOfElements = nrOfElements;
}
 
@Override
public boolean isTargetType(Actor actor) {
return actor instanceof Worker;
 
}
 
@Override
public void processRequest(JLPCActor targetActor, RP rp) throws Exception {
Worker worker = (Worker) targetActor;
double result = ((Worker) targetActor).calculatePiFor(start, nrOfElements);
rp.processResponse(result);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.