並列プログラミング on Rmpi on R

ゴールデンウィークの「Rを用いた統計学向け並列プログラミング・シリーズ」の最終回。前回はsnowを用いた簡単並列プログラミングを紹介したが、snowは便利な反面、クラスター間の細かいやり取りには向かない側面がある。そういう場合にはRmpiを使ってMPIを直接操作する。

簡単なプログラム例:


masterSimple <- function(x=10)
{
library(Rmpi)
mpi.spawn.Rslaves()
mpi.bcast.Robj2slave(slaveSimple)
mpi.bcast.cmd(slaveSimple())

mpi.bcast(as.integer(x), type=1)

numCl <- mpi.universe.size()
rc <- array(NA, c(numCl))
# type=1: integer, type=2: double, type=3: string.
for(i in 1:numCl){mpi.send(as.double(i),type=2, dest=i, tag=1)}
for(i in 1:numCl){rc[i] <- mpi.recv.Robj(source=i, tag=2)}

mpi.close.Rslaves()

return(rc)
}

slaveSimple <- function()
{
# mpirank <- mpi.comm.rank()
x <- mpi.bcast(integer(1), type=1)
y <- mpi.recv(double(1), type=2, source=0, tag=1)

rc <- x + y
mpi.send.Robj(rc, dest=0, tag=2)
}

masterSimple()

以前のエントリー:
http://d.hatena.ne.jp/koiti_yano/20050503#p2
http://d.hatena.ne.jp/koiti_yano/20050505#p2