Skip to content

Instantly share code, notes, and snippets.

@chiral
Created June 1, 2013 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chiral/5689735 to your computer and use it in GitHub Desktop.
Save chiral/5689735 to your computer and use it in GitHub Desktop.
demo for ZeroMQ on R Pi caluculation
library('rzmq')
context = init.context()
sock1 = init.socket(context,"ZMQ_PUSH")
bind.socket(sock1,"ipc:///test1")
sock2 = init.socket(context,"ZMQ_PULL")
bind.socket(sock2,"ipc:///test2")
nw <- 10000
n <- 100000000
pai_func <- function(n) {
x <- runif(n,0,1)
y <- runif(n,0,1)
return(4*length(which(x*x+y*y<1))/n)
}
n_unit <- n / nw
for (i in 1:nw) {
send.socket(sock1,list(pai_func,n_unit))
}
sum <- 0
for (i in 1:nw) {
res <- receive.socket(sock2)
sum <- sum+res
}
pai <- sum / nw
print(paste("pi =",pai))
library('rzmq')
context = init.context()
sock1 = init.socket(context,"ZMQ_PULL")
connect.socket(sock1,"ipc:///test1")
sock2 = init.socket(context,"ZMQ_PUSH")
connect.socket(sock2,"ipc:///test2")
while (1) {
u <- receive.socket(sock1)
func <- u[[1]]
n <- u[[2]]
res <- func(n)
send.socket(sock2,res)
}
n <- 100000000
pai <- function(n) {
x <- runif(n,0,1)
y <- runif(n,0,1)
return(4*length(which(x*x+y*y<1))/n)
}
print(paste("pi =",pai(n)))
Rscript slave.R &
Rscript slave.R &
Rscript slave.R &
Rscript slave.R &
Rscript slave.R &
Rscript slave.R &
Rscript slave.R &
Rscript slave.R &
Rscript slave.R &
Rscript slave.R &
echo 'stand alone test'
time Rscript standalone.R
echo 'many process test'
time Rscript master.R
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment