[R] help with random numbers and Rmpi

Faheem Mitha faheem at email.unc.edu
Mon Dec 1 21:23:18 CET 2003


Dear People,

This may not be the right place to ask a question about Rmpi, but I don't
know of a better one.

I am trying to get a simple program working using Rmpi with the model of 1
R master and n C slaves. What I am trying to do is have each of the C
slaves generate a random number from U[0,1], and then have the master
collect all n numbers as a vector and output it. However even doing this
is rather over my head. I'm trying to use rsprng and sprng, but I am sure
what I am currently doing is wrong.

I enclose a first attempt. Any suggestions would be appreciated. Thanks in
advance.

                                                                  Faheem.

*************************************************************************
rand.R
*************************************************************************
library(Rmpi)

rand <- function ()
{
  if (mpi.comm.size(1) > 1)
    stop ("It seems some slaves running on comm 1.")
  mpi.comm.spawn("./rand")
  mpi.intercomm.merge(2,0,1)

  mpi.init.sprng()
  free.sprng() #does this function exist here?

  rdata <- double(sum(mpi.comm.size(1)))
  out <- mpi.gather(0, 2, rdata) ##this isn't right
  mpi.comm.free()
  out
}

*************************************************************************
rand.c
*************************************************************************
#include <mpi.h>
#include <sprng.h>

int main(int argc, char **argv)
{
  double rand;
  double* randarray;

  MPI_Comm slavecomm, all_processes;

  /*Initialize MPI*/
  MPI_Init(&argc, &argv);

  MPI_Comm_get_parent(&slavecomm);
  MPI_Intercomm_merge(slavecomm, 1, &all_processes);

  /*How many processes are there?*/
  MPI_Comm_size(all_processes, &size);

  /*Which one am I?*/
  MPI_Comm_rank(all_processes, &rank);

  init_sprng()
  rand = sprng();
  free.sprng()

  randarray = (double *)malloc(sizeof(double)*size);

  /*Gather random numbers from all C slave processes*/
  /* Using randarray doesn't make sense since this should correspond
  to the rdata vector in the R master process */
  MPI_GAther(&rand, 1, 1, MPI_DOUBLE, randarray, 1, MPI_DOUBLE, 0, all_processes);

  /*All done*/
  MPI_Comm_free(&all_processes);
  MPI_Finalize();
  exit(0);
}




More information about the R-help mailing list