Created
June 1, 2013 08:50
-
-
Save chiral/5689735 to your computer and use it in GitHub Desktop.
demo for ZeroMQ on R
Pi caluculation
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
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)) |
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
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) | |
} |
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
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))) |
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
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