[R] help with random numbers and Rmpi

A.J. Rossini rossini at blindglobe.net
Mon Dec 1 21:45:48 CET 2003


use snow.

The general approach highlighted in 

http://www.analytics.washington.edu/~rossini/courses/cph-statcomp

in Lab 4 works with Rmpi as well.


Faheem Mitha <faheem at email.unc.edu> writes:

> 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);
> }
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>

-- 
rossini at u.washington.edu            http://www.analytics.washington.edu/ 
Biomedical and Health Informatics   University of Washington
Biostatistics, SCHARP/HVTN          Fred Hutchinson Cancer Research Center
UW (Tu/Th/F): 206-616-7630 FAX=206-543-3461 | Voicemail is unreliable
FHCRC  (M/W): 206-667-7025 FAX=206-667-4812 | use Email

CONFIDENTIALITY NOTICE: This e-mail message and any attachme...{{dropped}}




More information about the R-help mailing list