R 中 S4 对象的并行化错误

Error with parallelization in R for S4 objects

我正在尝试优化我将要对具有数百万个像元的多个栅格执行的函数,因此我想并行化此函数。

初始光栅

这是初始栅格:

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)

该函数的 objective 是获取一个数据框,其中包含栅格中存在的所有像元之间的距离,以及一列从、一列到和一列距离。

过渡层

为了做到这一点,我使用 gdistance 包创建了一个过渡层:

library(gdistance)
h16  <- transition(SPA, transitionFunction=function(x){1},16,symm=FALSE) 
h16   <- geoCorrection(h16, scl=FALSE)

每个单元格的原点:

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

距离函数

在一些 Whosebug 答案的帮助下,我制作了这个函数,它比 gdistance

中的 accCost 更快
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])
}

我要并行化的内容

然后使用 apply 我得到了我想要的 data.frame

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

这是我用 parApply 试过的

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

你的accCost版本确实比gdistance中的版本快。您的版本省略了检查您的点是否在过渡层范围内的检查。谨慎行事。

(您可以通过将单元格编号作为输入来使您的函数更快。另外,从每个节点发送回如此多的数据似乎效率不高。)