if ( !is.loaded("mpi_initialize") ){ library(Rmpi) } number_of_plots = 5 nodes = mpi.universe.size() mpi.spawn.Rslaves(nslaves=nodes) generate_plot <- function(filename){ print(mpi.comm.rank()) samples = rep(NA, 100000) for ( i in 1:100000 ){ samples[i] = mean(rexp(40, 0.2)) } jpeg(paste('plots/', filename, '.jpg', sep="")) hist(samples, main="", prob=T) lines(density(samples), col="darkblue", lwd=3) dev.off() } samples = 1:number_of_plots mpi.applyLB(samples, generate_plot) mpi.close.Rslaves() mpi.quit()