| 1 |
# This R profile can be used when a cluster does not allow spawning or a job |
| 2 |
# scheduler is required to launch any parallel jobs. Saving this file as |
| 3 |
# .Rprofile in the working directory or root directory. For unix platform, run |
| 4 |
# mpirun -n [cpu numbers] R --no-save -q |
| 5 |
|
| 6 |
# Another way is to modify R_home_dir/bin/R by adding the following line after |
| 7 |
# R_HOME_DIR |
| 8 |
# R_PROFILE=${R_HOME_DIR}/library/Rmpi/Rprofile; export R_PROFILE |
| 9 |
|
| 10 |
# For windows platform with mpich2, use mpiexec wrapper and specify a working |
| 11 |
# directory where .Rprofile is inside. |
| 12 |
|
| 13 |
# Cannot be used as Rprofile.site because it will not work |
| 14 |
|
| 15 |
# If no CPU consumptions of slaves while waiting are desirable, change |
| 16 |
# nonblocak=FALSE to nonblock=TRUE and change sleep time accordingly |
| 17 |
.nonblock=TRUE |
| 18 |
.sleep=0.1 |
| 19 |
|
| 20 |
# Following system libraries are not loaded automatically. So manual loads are |
| 21 |
# needed. |
| 22 |
library(utils) |
| 23 |
library(stats) |
| 24 |
library(datasets) |
| 25 |
library(grDevices) |
| 26 |
library(graphics) |
| 27 |
library(methods) |
| 28 |
|
| 29 |
#Change to TRUE if you don't want any slave host info |
| 30 |
quiet=FALSE |
| 31 |
|
| 32 |
if (!invisible(library(Rmpi,logical.return = TRUE))){ |
| 33 |
warning("Rmpi cannot be loaded") |
| 34 |
q(save = "no") |
| 35 |
} |
| 36 |
|
| 37 |
options(error=quote(assign(".mpi.err", FALSE, envir = .GlobalEnv))) |
| 38 |
|
| 39 |
if (mpi.comm.size(0) > 1) |
| 40 |
invisible(mpi.comm.dup(0,1)) |
| 41 |
|
| 42 |
if (mpi.comm.rank(0) >0){ |
| 43 |
#sys.load.image(".RData",TRUE) |
| 44 |
options(echo=FALSE) |
| 45 |
.comm <- 1 |
| 46 |
mpi.barrier(0) |
| 47 |
repeat { |
| 48 |
tmp.message=mpi.bcast.cmd(rank=0,comm=.comm, nonblock=.nonblock, sleep=.sleep) |
| 49 |
if (is.character(tmp.message) && tmp.message=="kaerb") |
| 50 |
break |
| 51 |
try(eval(tmp.message,envir=.GlobalEnv),TRUE) |
| 52 |
} |
| 53 |
#if (is.loaded("mpi_comm_disconnect")) |
| 54 |
# mpi.comm.disconnect(.comm) |
| 55 |
#else |
| 56 |
mpi.comm.free(.comm) |
| 57 |
mpi.quit() |
| 58 |
} |
| 59 |
|
| 60 |
if (mpi.comm.rank(0)==0) { |
| 61 |
#options(echo=TRUE) |
| 62 |
mpi.barrier(0) |
| 63 |
if(mpi.comm.size(0) > 1 && !quiet) |
| 64 |
slave.hostinfo(1) |
| 65 |
} |
| 66 |
|
| 67 |
.Last <- function(){ |
| 68 |
if (is.loaded("mpi_initialize")){ |
| 69 |
if (mpi.comm.size(1) > 1){ |
| 70 |
print("Please use mpi.close.Rslaves() to close slaves") |
| 71 |
mpi.close.Rslaves(comm=1) |
| 72 |
} |
| 73 |
} |
| 74 |
#print("Please use mpi.quit() to quit R") |
| 75 |
if (is.loaded("mpi_initialize")) |
| 76 |
.Call("mpi_finalize",PACKAGE = "Rmpi") |
| 77 |
} |