S4对象在R中的并行化错误

S4对象在R中的并行化错误,r,parallel-processing,r-raster,doparallel,R,Parallel Processing,R Raster,Doparallel,我正在尝试优化一个函数,我将用一个有数百万个单元的多个光栅来完成,所以我想并行化这个函数 初始光栅 这是初始光栅: library(raster) SPA <- raster(nrows=3, ncols=3, xmn = -10, xmx = -4, ymn = 4, ymx = 10) values(SPA) <- c(0.1, 0.4, 0.6, 0, 0.2, 0.4, 0, 0.1, 0.2) plot(SPA) 我对并行化相当陌生,我不确定我做错了什么您的代码中有几

我正在尝试优化一个函数,我将用一个有数百万个单元的多个光栅来完成,所以我想并行化这个函数

初始光栅 这是初始光栅:

library(raster)
SPA <- raster(nrows=3, ncols=3, xmn = -10, xmx = -4, ymn = 4, ymx = 10)

values(SPA) <- c(0.1, 0.4, 0.6, 0, 0.2, 0.4, 0, 0.1, 0.2)

plot(SPA)

我对并行化相当陌生,我不确定我做错了什么

您的代码中有几个语法问题

这个代码对我有用

library("parallel") 

accCost_wrap <- function(x){accCost2(h16,x)}
#Instead of including h16 in the parRapply function, 
#just get it in the node environment    

cl = makeCluster(3)  

clusterExport(cl, c("h16", "accCost2")) 
#B will be "sent" to the nodes through the parRapply function.

clusterEvalQ(cl, {library(gdistance)}) 
#raster is a dependency of gdistance, so no need to include raster here.    

pp <- parRapply(cl, x=B, FUN=accCost_wrap) 

stopCluster(cl)

connections <- data.frame(from = rep(1:nrow(B), each = nrow(B)),  
to = rep(1:nrow(B), nrow(B)),  
dist = as.vector(pp))
库(“并行”)

accCost\u wrap非常感谢@JacobVanEtten,您能否就如何加快accuCost2的速度多分享一些想法?我正在使用一个大光栅,并在其上复制了一千多个副本
B <- xyFromCell(SPA, cell = 1:ncell(SPA))
head(B)

      x y
[1,] -9 9
[2,] -7 9
[3,] -5 9
[4,] -9 7
[5,] -7 7
[6,] -5 7
accCost2 <- function(x, fromCoords) {

  fromCells <- cellFromXY(x, fromCoords)
  tr <- transitionMatrix(x)
  tr <- rBind(tr, rep(0, nrow(tr)))
  tr <- cBind(tr, rep(0, nrow(tr)))
  startNode <- nrow(tr)
  adjP <- cbind(rep(startNode, times = length(fromCells)), fromCells)
  tr[adjP] <- Inf
  adjacencyGraph <- graph.adjacency(tr, mode = "directed", weighted = TRUE)
  E(adjacencyGraph)$weight <- 1/E(adjacencyGraph)$weight
  return(shortest.paths(adjacencyGraph, v = startNode, mode = "out")[-startNode])
}
connections <- data.frame(from = rep(1:nrow(B), each = nrow(B)),to = rep(1:nrow(B), nrow(B)), dist =as.vector(apply(B,1, accCost2, x = h16)))

head(connections)

  from to     dist
1    1  1      0.0
2    1  2 219915.7
3    1  3 439831.3
4    1  4 221191.8
5    1  5 312305.7
6    1  6 493316.1
library("parallel")
cl = makeCluster(3)
clusterExport(cl, c("B", "h16", "accCost2"))
clusterEvalQ(cl, library(gdistance), library(raster))

connections <- data.frame(from = rep(1:nrow(B), each = nrow(B)),to = rep(1:nrow(B), nrow(B)), dist =as.vector(parRapply(cl, B,1, accCost2, x = h16)))

stopCluster(cl)
Error in x[i, , drop = FALSE] : object of type 'S4' is not subsettable
library("parallel") 

accCost_wrap <- function(x){accCost2(h16,x)}
#Instead of including h16 in the parRapply function, 
#just get it in the node environment    

cl = makeCluster(3)  

clusterExport(cl, c("h16", "accCost2")) 
#B will be "sent" to the nodes through the parRapply function.

clusterEvalQ(cl, {library(gdistance)}) 
#raster is a dependency of gdistance, so no need to include raster here.    

pp <- parRapply(cl, x=B, FUN=accCost_wrap) 

stopCluster(cl)

connections <- data.frame(from = rep(1:nrow(B), each = nrow(B)),  
to = rep(1:nrow(B), nrow(B)),  
dist = as.vector(pp))