[R] Question on Rmpi looping

Kathy Gerber kathy at virginia.edu
Thu Jul 5 23:04:52 CEST 2007


Dear R list,

In the course of learning to work with Rmpi, we are confused about a few 
points.  The following simple program is based on some examples we retrieved 
from the web. Each slave is writing the same output line multiple times (a 
multiple equal to the number of slaves). In other words, the write 
statements are being executed a number of times equal to the number of 
slaves.

I am trying to print out the slave number to a file (once), but it is 
printing it 4 times (since the number of slaves is 4). The code is as 
follows:

# Initialize the Rmpi environment
library("Rmpi")

# We are spawning four slaves
mpi.spawn.Rslaves(nslaves=4)

if (mpi.comm.size() != 5)
     {
     print("Please initialize an MPI cluster of at least 5 processors.")
     print("Then, try again")
     mpi.quit()
     }

.Last <- function()
     {
     if (is.loaded("mpi_initialize"))
         {
         if (mpi.comm.size(1) > 0)
             {
             print("Please use mpi.close.Rslaves() to close slaves.")
             mpi.close.Rslaves()
             }
         print("Please use mpi.quit() to quit R")
         .Call("mpi_finalize")
         }
     }


# Define the base directory
basedir <- "/home/user/directory/"

fcnfishtest <- function()
   {
          wrout = paste(basedir, paste("processor",my_rank, sep=""), sep="")
          write(my_rank, wrout, append=TRUE)
   }
################## END OF SLAVES ##################


# We're in the parent.

#Have each slave get its rank
  mpi.bcast.cmd(my_rank <- mpi.comm.rank())
  mpi.bcast.Robj2slave(basedir)


# Send the function to the slaves
   
  mpi.bcast.Robj2slave(fcnfishtest)

# Call the function

x<- mpi.remote.exec(fcnfishtest())
x

# close slaves and exit
  mpi.close.Rslaves()
  mpi.quit(save = "no")

##### end code


The output is as follows:

file 1:
1
1
1
1
  
file 2:
2
2
2
2

file 3:
3
3
3
3

file 4:
4
4
4
4


We would like to call four slaves with output files like:

file 1:
1

file 2:
2

file 3:
3

file 4:
4


Any help would be greatly appreciated. Thank you!

Kathy Gerber
University of Virginia - ITC - Research Computing Support
kathy at virginia.edu                         434-982-4986



More information about the R-help mailing list