2014-06-11 155 views
0

我已成功处理使用parLapply一个4芯计算机上的一些数据,使用如下面的码:无法打开连接错误与parLapply

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

+0

愚蠢的问题,但你的机器有8个核心? –

+0

好吧,我连接到一台远程计算机来做到这一点,并通过检查设备管理器显示8 ...我也被负责人告知,前一阵子有8个内核。不过,我可能想再次与他们核对。 – Neodyme

回答

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)) 

这似乎是值得的统治解决这些问题。

+0

第一个解决方案做到了 - 我的一些文件没有正确复制到新机器...谢谢! – Neodyme

0

您需要将当前目录作为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全部不被防火墙阻止。