suppressMessages(library(pbdMPI, quietly = TRUE))
### Define method.
mean.simple <- function(x, ...){
ret <- allreduce(c(sum(x@a), length(x@a)), op = "sum")
ret[1] / ret[2]
}
.a <- setClass(Class = "simple",
representation = representation(a = "numeric"))
.a <- setGeneric(name = "mean",
useAsDefault = mean)
.a <- setMethod(f = "mean",
signature = signature(x = "simple"),
definition = mean.simple)
### Run.
x <- new("simple", a = comm.rank())
y <- mean(x)
comm.print(y, all.rank = TRUE)
finalize()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.