【问题标题】:cannot open the connection error with parLapply无法使用 parLapply 打开连接错误
【发布时间】:2014-06-11 17:49:39
【问题描述】:

我使用 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 进程),看看它是否有帮助,但没有。有什么建议吗?

【问题讨论】:

  • 愚蠢的问题,但是你的机器有 8 个内核吗?
  • 嗯,我正在连接到远程计算机来执行此操作,通过检查设备管理器它显示 8... 负责人不久前还告诉我有 8 个内核。不过,我可能想再次与他们核实。

标签: r parallel-processing core


【解决方案1】:

我在“myfunction”中看到的唯一可以生成“无法打开连接”错误的操作是“read.table”。您可能希望在调用 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))

排除这些问题似乎是值得的。

【讨论】:

  • 第一个解决方案做到了——我的一些文件没有正确复制到新机器上......谢谢!
【解决方案2】:

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

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

注意确保 R/R Studio/R Script 都没有被防火墙阻止。

【讨论】:

    猜你喜欢
    • 2017-01-18
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 2013-01-05
    • 2016-06-06
    相关资源
    最近更新 更多