Difference between revisions of "R MPI Example"

From UFRC
Jump to navigation Jump to search
Line 1: Line 1:
 
[[Category:Software]]
 
[[Category:Software]]
 
[[R|back to the main R page]]
 
[[R|back to the main R page]]
 +
 
Example of using the parallel module to run MPI jobs under Rmpi.
 
Example of using the parallel module to run MPI jobs under Rmpi.
  

Revision as of 21:45, 20 July 2016

back to the main R page

Example of using the parallel module to run MPI jobs under Rmpi.

{{#fileAnchor: rmpi_test.R}} Download raw source of the [{{#fileLink: rmpi_test.R}} rmpi_test.R] file.

# Load the R MPI package if it is not already loaded.
if (!is.loaded("mpi_initialize")) {
    library("Rmpi")
    }
                                                                                
# Spawn as many slaves as possible
mpi.spawn.Rslaves()
                                                                                
# In case R exits unexpectedly, have it automatically clean up
# resources taken up by Rmpi (slaves, memory, etc...)
.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")
    }
}

# Tell all slaves to return a message identifying themselves
mpi.remote.exec(paste("I am",mpi.comm.rank(),"of",mpi.comm.size()))

# Tell all slaves to close down, and exit the program
mpi.close.Rslaves()
mpi.quit()