我有dgCMatrix
的列表,我试图并行化函数。
parLapply
不一致 - 为某些索引生成正确的结果,而不为其他索引生成正确的结果。
lapply
是一致的。任何想法有什么不对?
一个虚拟的例子如下:
library(Matrix)
library(parallel)
li_coefs<-list("1"=list(Matrix(rep(1:5, each=2),ncol=1) , Matrix(rep(6:10, each=2),ncol=1) ),
"2"=list(Matrix(rep(11:15, each=2),ncol=1) , Matrix(rep(16:20, each=2),ncol=2), Matrix(rep(21:25, each=2),ncol=2),Matrix(rep(26:30, each=2),ncol=1) ),
"3"=list(Matrix(rep(101:105, each=2),ncol=2) , Matrix(rep(106:110, each=2),ncol=1), Matrix(rep(111:115, each=2),ncol=1) ),
"4"=list(Matrix(rep(131:135, each=2),ncol=1) , Matrix(rep(136:140, each=2),ncol=2), Matrix(rep(141:145, each=2),ncol=1) ),
"5"=list(Matrix(rep(991:995, each=2),ncol=2) , Matrix(rep(996:1000, each=2),ncol=1)))
doThings<-function(thecoefs,v){
tryCatch({
coefs<-thecoefs[[v]]
K <- length(coefs)
for(k in 1:K){
b<-coefs[[k]]
if(dim(b)[2]>1) b<-t(b)
##do some stuff
}
K}, error=function(e) return(paste0("Node ", v, " caused error: ",e)))
}
cl <- makeCluster(numcores)
clusterExport(cl,c("doThings", "li_coefs"))
result.par<- parLapply(cl, 1:5, function(v){
doThings(li_coefs,v)
})
stopCluster(cl)
result<-lapply(1:5, function(v){doThings(li_coefs,v)})
结果:
> result.par
[[1]]
[1] "Node 1 caused error: Error in if (dim(b)[2] > 1) b <- t(b): argument is of length zero\n"
[[2]]
[1] 4
[[3]]
[1] "Node 3 caused error: Error in if (dim(b)[2] > 1) b <- t(b): argument is of length zero\n"
[[4]]
[1] "Node 4 caused error: Error in if (dim(b)[2] > 1) b <- t(b): argument is of length zero\n"
[[5]]
[1] 2
> result
[[1]]
[1] 2
[[2]]
[1] 4
[[3]]
[1] 3
[[4]]
[1] 3
[[5]]
[1] 2