-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_parallel3.R
55 lines (48 loc) · 1.38 KB
/
data_parallel3.R
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
library(pbdMPI, quiet=TRUE)
init()
rank <- comm.rank()
master <- comm.rank() == 0
comm.cat("Job start", as.character(date()), "\n", quiet=TRUE)
##
mu <- 1
sigma <- 2
N <- 1e5
nGrid <- 100
mulim <- c(-5, 5)
sigmalim <- c(0.001, 5)
DGP <- function(N, mean=mu, sd=sigma) {
x <- rnorm(N, mean, sd)
x
}
loglik <- function(par) {
mu <- par[1]
sigma <- par[2]
sum(dnorm(x, mu, sigma, log=TRUE))
}
## Now let just the master compute x and broadcast to all workers
x <- 0
# need to initialize x for all workers
if(master) {
x <- DGP(N)
# initialize "real" x
}
x <- bcast(x)
## Now lets calculate the grid
mus <- seq(from=mulim[1], to=mulim[2], length=nGrid)
sigmas <- seq(from=sigmalim[1], to=sigmalim[2], length=nGrid)
grid <- as.matrix(expand.grid(mu=mus, sigma=sigmas))
# note: every workers has calculated grid
res <- task.pull(seq(length=nrow(grid)), function(i) loglik(grid[i,]))
if(master) {
res <- matrix(unlist(res), nGrid, nGrid)
dimnames(res) <- list(mu=formatC(mus, format="f", digits=4, width=7),
sigma=formatC(sigmas, format="f", digits=4, width=7))
##
best <- which(res==max(res), arr.ind=TRUE)
i <- best[1]
j <- best[2]
bestMu <- mus[i]
bestSigma <- sigmas[j]
cat("Maximum", res[i,j], "at mu =", bestMu, "and sigma =", bestSigma, "\n")
}
finalize()