Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
并行加载文件不使用foreach+;数据表_R_Foreach_Parallel Processing_Data.table - Fatal编程技术网

并行加载文件不使用foreach+;数据表

并行加载文件不使用foreach+;数据表,r,foreach,parallel-processing,data.table,R,Foreach,Parallel Processing,Data.table,我想将foreach与data.table(v.1.8.7)结合使用来加载文件并绑定它们foreach未并行化,并返回警告 write.table(matrix(rnorm(5e6),nrow=5e5),"myFile.csv",quote=F,sep=",",row.names=F,col.names=T) library(data.table); #I use fread from data.table 1.8.7 (dev) for performance and useability

我想将
foreach
data.table
(v.1.8.7)结合使用来加载文件并绑定它们<代码>foreach未并行化,并返回警告

write.table(matrix(rnorm(5e6),nrow=5e5),"myFile.csv",quote=F,sep=",",row.names=F,col.names=T) 
library(data.table); 
#I use fread from data.table 1.8.7 (dev) for performance and useability
DT = fread("myFile.csv") 
现在假设我有n个文件要加载和行绑定,我想对其进行parralellize。 (我在窗户上,所以没有叉子)


使用lappy

f1 <- function(allFiles){
    DT <- lapply(allFiles, FUN=fread) #will load sequentially myFile.csv 3 times with fread
    DT <- rbindlist(DT);
    return(DT);
}

为了得到答案:

警告消息告诉您,
foreach
没有注册并行后端。通过阅读了解如何做到这一点

小插曲中的简单示例:

library(doParallel) 
cl <- makeCluster(3) 
registerDoParallel(cl) 
foreach(i=1:3) %dopar% sqrt(i) 
库(双并行)

cl你读到警告了吗?当然,但我该怎么解决呢?小插曲没有提供任何进一步采取措施的信息。。。看起来它应该是从盒子里出来的好吧!!!我收到了同一个人的一篇论文,他没有提到那些东西,好吧,谢谢!
library(parallel)
f2 <- function(allFiles){
    mc <- detectCores(); #how many cores?
    cl <- makeCluster(mc); #build the cluster
    DT <- parLapply(cl,allFiles,fun=fread); #call fread on each core (well... using each core at least)
    stopCluster(cl);
    DT <- rbindlist(DT);
    return(DT);
}
library(foreach)
f3 <- function(allFiles){
    DT <- foreach(myFile=allFiles, .combine='rbind', .inorder=FALSE) %dopar% fread(myFile)
    return(DT);
}
system.time(DT <- f1(allFiles));
utilisateur     systÞme      ÚcoulÚ
      34.61        0.14       34.84
system.time(DT <- f2(allFiles));
utilisateur     systÞme      ÚcoulÚ
       1.03        0.40       24.30    
system.time(DT <- f3(allFiles));
executing %dopar% sequentially: no parallel backend registered
utilisateur     systÞme      ÚcoulÚ
      35.05        0.22       35.38
library(doParallel) 
cl <- makeCluster(3) 
registerDoParallel(cl) 
foreach(i=1:3) %dopar% sqrt(i)