-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathRmpi.R
More file actions
39 lines (29 loc) · 949 Bytes
/
Rmpi.R
File metadata and controls
39 lines (29 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
## @knitr Rmpi
# example syntax of standard MPI functions
library(Rmpi)
## by default this should start one fewer workers than processes
## saving one for the master
## but on my system, this fails unless explicitly
## ask for one fewer slave than total number of slots across hosts
mpi.spawn.Rslaves(nslaves = mpi.universe.size()-1)
n = 5
mpi.bcast.Robj2slave(n)
mpi.bcast.cmd(id <- mpi.comm.rank())
mpi.bcast.cmd(x <- rnorm(id))
mpi.remote.exec(ls(.GlobalEnv))
mpi.bcast.cmd(y <- 2 * x)
mpi.remote.exec(print(y))
objs <- as.list(c('x', 'n'))
# next command sends value of objs on _master_ as argument to rm
mpi.remote.exec(do.call, rm, objs)
# verify that 'n' is gone:
mpi.remote.exec(print(n))
# collect results back via send/recv
mpi.remote.exec(mpi.send.Robj(y, dest = 0, tag = 1))
results = list()
for(i in 1:(mpi.comm.size()-1)){
results[[i]] = mpi.recv.Robj(source = i, tag = 1)
}
print(results)
mpi.close.Rslaves()
mpi.quit()