Last active
August 29, 2015 13:59
-
-
Save timyates/10474027 to your computer and use it in GitHub Desktop.
Pi Approximation in GPars with Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.* | |
import groovyx.gpars.actor.* | |
import groovyx.gpars.group.* | |
@Immutable class Calculate {} | |
@Immutable class Work { int start, nrOfElements } | |
@Immutable class Result { double value } | |
@Immutable class PiApproximation { double pi ; long duration } | |
double calculatePiFor( int start, int nrOfElements ) { | |
((start * nrOfElements)..((start + 1) * nrOfElements - 1)).inject( 0.0 ) { acc, i -> | |
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1) | |
} | |
} | |
def group = new DefaultPGroup() | |
def listener = group.actor { | |
loop { | |
react { | |
println "Pi approx $it.pi in $it.duration" | |
terminate() | |
} | |
} | |
} | |
int nrOfWorkers = 4 | |
int nrOfMessages = 1000 | |
int nrOfElements = 1000 | |
def workers = (1..nrOfWorkers).collect { | |
group.actor { | |
loop { | |
react { | |
reply new Result( calculatePiFor( it.start, it.nrOfElements ) ) | |
} | |
} | |
} | |
} | |
double pi = 0.0 | |
int nrOfResults = 0 | |
long start = System.currentTimeMillis() | |
def master = group.actor { | |
loop { | |
react { | |
switch( it ) { | |
case Calculate: | |
nrOfMessages.times { i -> workers[ i % nrOfWorkers ].send( new Work( i, nrOfElements ) ) } | |
break | |
case Result: | |
pi += it.value | |
if( ++nrOfResults >= nrOfMessages ) { | |
listener.send( new PiApproximation( pi, System.currentTimeMillis() - start ) ) | |
workers*.terminate() | |
terminate() | |
} | |
break | |
} | |
} | |
} | |
} | |
master << new Calculate() | |
master.join() |
Nice! I took the liberty to modify the sample and use static and dynamic dispatch actors instead. Might be a bit more performant -. https://gist.github.com/vaclav/10485027
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See here for the Akka version https://gist.github.com/timyates/10470012