mpi.parallel.sim {Rmpi}R Documentation

Parallel Monto Carlo Simulation

Description

Carry out parallel Monto Carlo simulation on R slaves spawned by using slavedaemon.R script and all executed results are returned back to master.

Usage

mpi.parallel.sim(n=100, rand.gen=rnorm, rand.arg=NULL,statistic, 
nsim=100, run=1, slaveinfo=TRUE, comm=1, ...)  

Arguments

n sample size.
rand.gen the random data generating function. See the details section
rand.arg additional argument list to rand.gen.
statistic the statistic function to be simulated. See the details section
nsim the number of simulation carried on a slave which is counted as one slave job.
run the number of looping. See the details section.
slaveinfo if TRUE, the numbers of jobs finished by slaves will be displayed.
comm a communicator number
... optional arguments to statistic

Details

It is assumed that one simulation is carried out as statistic(rand.gen(n)), where rand.gen(n) can return any values as long as statistic can take them. Additional arguments can be passed to rand.gen by rand.arg as a list. Optional arguments can also be passed to statistic by the argument ....

To pass values from rand.arg and/or ... as default values, mpi.wrap.fun is used. If rand.gen (statistic) does not contain the argument ..., mpi.wrap.fun returns the same function with default values replaced. Otherwise it returns a wrap function so that additional arguments can be passed properly. It is recommended to use rand.gen and statistic without ... argument.

Each slave job consists of sapply(rep(n,nsim),function(n)statistic(rand.gen(n))), i.e., each job runs nsim number of simulation. The returned values are transported from slaves to master.

The total number of simulation (TNS) is calculated as follows. Let slave.num be the total number of slaves in a comm and it is mpi.comm.size(comm)-1. Then TNS=slave.num*nsim*run and the total number of slave jobs is slave.num*run, where run is the number of looping from master perspective. If run=1, each slave will run one slave job. If run=2, each slave will run two slaves jobs on average, and so on.

The purpose of using run has two folds. It allows a tuneup of slave job size and total number of slave jobs to deal with two different cluster environments. On a cluster of slaves with equal CPU power, run=1 is often enough. But if nsim is too big, one can set run=2 and the slave jog size to be nsim/2 so that TNS=slave.num*(nsim/2)*(2*run). This may improve R computation efficiency slightly. On a cluster of slaves with different CPU power, one can choose a big value of run and a small of value of nsim so that master can dispatch more jobs to slaves who run faster than others. This will keep all slaves busy so that load balancing is achieved.

See the warning section before you use mpi.parallel.sim.

Value

The returned values depend on values returned by sapply after applying statistic and the total number of simulation (TNS). If statistic returns a single value, then the result is a vector of length TNS. If statistic returns a vector (list) of length ncol, then the result is a matrix of dimension c(TNS,ncol).

Warning

It is assumed that the package rsprng must be installed and SPRNG must be properly initialized. mpi.spawn.Rslaves will initialize SPRNG as default. Though mpi.parallel.sim is still functional without SPRNG, the quality of simulation is not guarantied. See mpi.init.sprng how to initialize SPRNG.

mpi.parallel.sim will automatically transfer rand.gen and statistic to slaves. However, any functions that rand.gen and statistic reply on but are no in slaves must be transfered to slaves before using mpi.parallel.sim. You can use mpi.bcast.Robj2slave for that purpose. The same is applied to required packages or C/Fortran codes. You can use either mpi.bcast.cmd or put required(package) and/or dyn.load(so.lib) into rand.gen and statistic.

It is recommended that statistic should return a numeric vector or a list so master can use c to combine values together as a vector or a matrix. Master may have difficult to combine other values.

Author(s)

Hao Yu

See Also

mpi.spawn.Rslaves, mpi.bcast.cmd mpi.bcast.Robj2slave


[Package Contents]