我已经编写了一个运行良好的脚本,但它似乎并不是在执行并行处理。我试着把核心从3改变到16,但是生成数据的速度并没有改变。有谁能让我知道我做错了什么,我如何才能让它发挥作用?
setwd("E:/Infections")
if (!require("pacman")) install.packages("pacman")
pacman::p_load(lakemorpho,rgdal,maptools,sp,doParallel,foreach,
doParallel)
cl <- makeCluster(5, outfile="E:/Infections/debug.txt")
registerDoParallel(cl)
x<-readOGR("E:/Infections/ByHUC6","Kodiak")
x_lake_length<-vector("numeric",length = nrow(x))
for(i in 1:nrow(x)){
tmp<-lakeMorphoClass(x[i,],NULL,NULL,NULL)
x_lake_length[i]<-lakeMaxLength(tmp,200)
print(i)
Sys.sleep(0.1)
}
df_Kodiak <- data.frame(x_lake_length)
write.table(df_Kodiak,file="E:/Infections/ByHUC6/Kodiak.csv",row.names=TRUE,col.names=TRUE, sep=",")发布于 2016-06-09 20:20:26
好吧,我想我是通过调用foreach和%dopar%来得到它的
# Libraries ---------------------------------------------------------------
if (!require("pacman")) install.packages("pacman")
pacman::p_load(lakemorpho,rgdal,maptools,sp,doParallel,foreach,
doParallel)
# Data --------------------------------------------------------------------
ogrDrivers()
dsn <- system.file("vectors", package = "rgdal")[1]
ogrListLayers(dsn)
ogrInfo(dsn=dsn, layer="trin_inca_pl03")
owd <- getwd()
setwd(dsn)
ogrInfo(dsn="trin_inca_pl03.shp", layer="trin_inca_pl03")
setwd(owd)
x <- readOGR(dsn=dsn, layer="trin_inca_pl03")
summary(x)
# HPC ---------------------------------------------------------------------
cores_2_use <- detectCores() - 4
cl <- makeCluster(cores_2_use, useXDR = F)
clusterSetRNGStream(cl, 9956)
registerDoParallel(cl, cores_2_use)
# Analysis ----------------------------------------------------------------
myfun <- function(x,i){tmp<-lakeMorphoClass(x[i,],NULL,NULL,NULL)
x_lake_length<-vector("numeric",length = nrow(x))
x_lake_length[i]<-lakeMaxLength(tmp,200)
print(i)
Sys.sleep(0.1)}
foreach(i = 1:nrow(x),.combine=cbind,.packages=c("lakemorpho","rgdal")) %dopar% (
myfun(x,i)
)
df_Kodiak <- data.frame(x_lake_length)正如您在下面的屏幕截图中所看到的,这将使用24个CPU核心中的20个创建大量的Rscript.exe进程。当然,我使用的示例数据很小,所以它并不需要所有这些核心,但是它应该作为概念的证明。
我从来没有超过这一比率,因为如果您使用100%的所有CPU核心,有时会发生不好的事情,其他服务器用户可能不满意您。

https://stackoverflow.com/questions/37690547
复制相似问题