# # # R代码从装饰图案的xvalComputerClusters来源。Rnw“# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #代码块1号:xvalComputerClusters。Rnw: 34-35 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #选项(宽度= 60)# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #代码块2号:init # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #库(MLInterfaces)库(golubEsets)数据(Golub_Merge) smallG <——Golub_Merge(200:250,) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #代码块3号:xval # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # lk1 < - MLearn (ALL.AML ~。、smallG knnI (k = 1), xvalSpec(“厕所”))confuMat (lk1) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #代码块数量4:clusterinit (eval = FALSE) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #库(MLInterfaces) # #库(golubEsets) # # smallG <——Golub_Merge(200:250,) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #代码块5号:clusterstart (eval = FALSE) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #库(雪)# # cl <——makeCluster (8, MPI) # # clusterEvalQ (cl,库(MLInterfaces)) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #代码块6号:clustercalc (eval = FALSE) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # lk1 < xval (smallG,”所有。AML”, knnB xvalMethod = "厕所",# # = as.integer(0)集群= cl)表(lk1, smallG ALL.AML美元)# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #代码块7号:xvalComputerClusters。Rnw: 113 - 116 (eval = FALSE) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # setMethod (“xvalLoop”, # #签名(集群=“任何”),# #函数(集群,…)拉普兰人)# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #代码块8号:xvalComputerClusters。Rnw: 133 - 137 (eval = FALSE) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # setOldClass (“spawnedMPIcluster”) # # setMethod (“xvalLoop”, # #签名(集群=“spawnedMPIcluster”), # #函数(集群,…)parLapply) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #代码块9号:xvalComputerClusters。Rnw: 151 - 160 (eval = FALSE) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # setOldClass (“spawnedMPIcluster”) # # setMethod (“xvalLoop”, # #签名(集群=“spawnedMPIcluster”), # #函数(集群,…){# # relapply < -函数(X,有趣,…){# # parLapply(集群,X,有趣,…)# # relapply # # # #}}) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #代码块10号:xvalComputerClusters。Rnw:179-196 (eval = FALSE) ################################################### ## setMethod("xvalLoop", signature( cluster = "spawnedMPIcluster"), ## function( cluster, ... ) { ## clusterExportEnv <- function (cl, env = .GlobalEnv) ## { ## unpackEnv <- function(env) { ## for ( name in ls(env) ) assign(name, get(name, env), .GlobalEnv ) ## NULL ## } ## clusterCall(cl, unpackEnv, env) ## } ## relapply <- function(X, FUN, ...) { ## ## send all visible variables from the parent (i.e., xval) frame ## clusterExportEnv( cluster, parent.frame(1) ) ## parLapply( cluster, X, FUN, ... ) ## } ## relapply ## }) ################################################### ### code chunk number 11: xvalComputerClusters.Rnw:208-210 ################################################### res <- c(20.04,11.34, 9.23, 8.20, 7.87) plot(res,ylab="Time (seconds)", xlab="Nodes", ylim=c(0,max(res))) ################################################### ### code chunk number 12: xvalComputerClusters.Rnw:221-234 (eval = FALSE) ################################################### ## harness <- function( nodes, reps, data ) { ## if ( nodes > 1) { ## cl <- makeCluster(nodes, "MPI") ## clusterEvalQ(cl, library(MLInterfaces)) ## } else cl <- NULL ## func <- function(x) ## res <- xval(data, "ALL.AML", knnB, xvalMethod = "LOO", 0:0, cluster = cl ) ## func() # warm-up ## tm <- system.time( sapply( 1:reps, func ) )[3] ## if (nodes > 1) stopCluster(cl) ## tm ## } ## res <- sapply( 1:5, harness, 10, smallG ) ################################################### ### code chunk number 13: xvalComputerClusters.Rnw:279-286 ################################################### x <- 1:10 for (i in 1:10) x[i] <- i**2 x # x has been modified by the for loop x <- 1:10 res <- lapply(1:10, function(i) x[i] <- i**2) x # copy of x in lapply modified, not the global x