R语言并行运算示例 parallel 包

it2022-05-05  144

library(parallel)#example 1cl <- makeCluster(getOption("cl.cores", 2))clusterApply(cl, c(9,5), get("+"),1)   #加parSapply(cl, c(9,5), get("+"), 3)  stopCluster(cl)#example 2xx <- 1cl <- makeCluster(getOption("cl.cores", 2))clusterExport(cl, "xx")cy = function(y) xx + yclusterCall(cl, cy,2)stopCluster(cl)xx <- 1;y=2cl <- makeCluster(getOption("cl.cores", 2))clusterExport(cl, c("xx",'y'))cy = function() xx + yclusterCall(cl, cy)stopCluster(cl)#example 3no_cores = 3cl = makeCluster(no_cores)df = function(exponent) 2^exponentparLapply(cl, 2:4, df)stopCluster(cl)#example 4no_cores = detectCores() - 1cl<-makeCluster(no_cores)base <- 2clusterExport(cl, "base")ef = function(exponent) base^exponentparLapply(cl, 2:4,ef)stopCluster(cl)#example 5no_cores = detectCores() - 1cl<-makeCluster(no_cores)base <- 2clusterExport(cl, "base")parSapply(cl, 2:4, function(exponent)  base^exponent)stopCluster(cl)#example 6ncore = 3cl = makeCluster(ncore)myf = function(x) x^3mr = clusterApplyLB(cl, x=1:ncore, fun=myf) mrstopCluster(cl)

转载于:https://www.cnblogs.com/arcserver/p/9882522.html


最新回复(0)