# NOT RUN {
# Save code in a file "demo.r" and run in 4 processors by
# > mpiexec -np 4 Rscript demo.r
### Setup environment.
library(pmclust, quiet = TRUE)
### Load data
X <- as.matrix(iris[, -5])
### Distribute data
jid <- get.jid(nrow(X))
X.gbd <- X[jid,]
### Standardized
N <- allreduce(nrow(X.gbd))
p <- ncol(X.gbd)
mu <- allreduce(colSums(X.gbd / N))
X.std <- sweep(X.gbd, 2, mu, FUN = "-")
std <- sqrt(allreduce(colSums(X.std^2 / (N - 1))))
X.std <- sweep(X.std, 2, std, FUN = "/")
### Clustering
library(pmclust, quiet = TRUE)
comm.set.seed(123, diff = TRUE)
ret.mb1 <- pmclust(X.std, K = 3)
comm.print(ret.mb1)
ret.kms <- pkmeans(X.std, K = 3)
comm.print(ret.kms)
### Finish
finalize()
# }
Run the code above in your browser using DataLab