Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
jActor - Pi Calculation
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.