# ライブラリの読み込み library(Rmpi) library(doSNOW) # 並列数の指定 n_slaves <- 31 # 乱数発生数の指定 N <- 10^6 # 並列化クラスタの作成 cl <- makeCluster(n_slaves, type = "MPI") registerDoSNOW(cl) # 計算の実行 result <- system.time(foreach(i = 1:1000,.combine = "cbind") %dopar% { sum(rnorm(N)) }) # クラスタの終了 stopCluster(cl) # 結果の表示 core.d <- c(n_slaves,result[3]) names(core.d) <- c("Slaves","ProcTime") print(core.d) # MPI終了処理 .Last <- function(){ if (is.loaded("mpi_initialize")){ if (mpi.comm.size(1) > 1){ print("Please use mpi.close.Rslaves() to close slaves") mpi.close.Rslaves(comm=1) } } print("Please use mpi.quit() to quit R") mpi.quit() }