无法使用parLapply打开连接错误

时间:2014-06-11 17:49:40

标签: r parallel-processing core

我使用parLapply在4核计算机上成功处理了一些数据,使用如下代码:

require("parallel")
setwd("C:/...")

file_summary<-read.table("file_summary",header=T,sep=" ",stringsAsFactors=F)
new_filename<-sub(".asc",".dat",file_summary$filename)

file_list<-list.files()

myfunction <- function(k) {

x<-M$x[k]
y<-M$y[k]

for (i in 1:length(file_summary[,1])) {
    if ( # logical condition on x and y ) {
    new_file<-new_filename[i]
    new_data<-read.table(new_file,header=T,sep=" ")
    eval<-matrix(,nrow=length(new_data$x),ncol=1)

      for (j in 1:length(new_data$x)) {
      eval[j]<-(new_data$x[j]-x)^2+(new_data$y[j]-y)^2
    }
    index<-which(eval == max(eval))
    out<-c(new_data$x[index],new_data$y[index],new_data$mean[index],new_data$S[index])
}
rm(eval)
gc()
}
return(out)
}

n_tasks <- length(M$x) 
n_cores <- 8

Cl = makeCluster(n_cores, type = "PSOCK") 
clusterExport(Cl, "M")
clusterExport(Cl, "file_summary")
clusterExport(Cl, "new_filename")
clusterExport(Cl, "file_list")

Results <- parLapply(Cl, c(1:n_tasks), myfunction)

stopCluster(Cl)

现在使用完全相同的代码和相同的数据和目录结构(即路径),我试图在8核机器上运行分析,以进一步加快速度。但是,在我第一次尝试时,我收到以下错误:

8 nodes produced errors; first error: cannot open the connection

我尝试清除RAM(对于非R进程)以查看它是否有帮助,但它没有。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

我在&#34; myfunction&#34;中看到的唯一一个操作那可以生成一个&#34;无法打开连接&#34;错误是&#34; read.table&#34;。您可能希望在调用read.table之前添加以下内容:

if (! file.exists(new_file))
  stop(paste(new_file, "does not exist"))

检查工作人员是否有权读取文件可能也很有用:

if (file.access(new_file, mode=4) == -1)
  stop(paste("no read permission on", new_file))

似乎值得排除这些问题。

答案 1 :(得分:0)

您需要将当前目录作为parLapply()中的参数传递给您的函数。在您的函数myfunction内,您需要按setwd()重置工作目录:

myfunction = function(k, wd_)
{
      setwd(wd_)
        ...
}
...
wd_ = getwd()
Results <- parLapply(Cl, c(1:n_tasks), myfunction, wd_)

<强> N.B。确保防火墙不会阻止R / R Studio / R脚本。

相关问题