Ves*_*ian 16 parallel-processing r distance spatial matrix
目前我正在使用build in function dist来计算我在R中的距离矩阵.
dist(featureVector,method="manhattan")
Run Code Online (Sandbox Code Playgroud)
这是目前应用程序的瓶颈,因此我们的想法是平衡这项任务(概念上这应该是可能的)
搜索谷歌和这个论坛没有成功.
有人有想法吗?
Zhi*_*Jia 19
R包amap为聚类和主成分分析提供了强大的并行化功能.在这些函数中,Dist方法提供了您正在寻找的内容:以并行方式计算和返回距离矩阵.
Dist(x, method = "euclidean", nbproc = 8)
Run Code Online (Sandbox Code Playgroud)
上面的代码用8个线程计算欧几里德距离.
这是您可以选择的一条路线的结构。它并不比仅仅使用该dist()
函数更快,而是花费了数倍的时间。它确实是并行处理的,但即使计算时间减少到零,启动函数并将变量导出到集群的时间可能会比仅仅使用更长dist()
library(parallel)
vec.array <- matrix(rnorm(2000 * 100), nrow = 2000, ncol = 100)
TaxiDistFun <- function(one.vec, whole.matrix) {
diff.matrix <- t(t(whole.matrix) - one.vec)
this.row <- apply(diff.matrix, 1, function(x) sum(abs(x)))
return(this.row)
}
cl <- makeCluster(detectCores())
clusterExport(cl, list("vec.array", "TaxiDistFun"))
system.time(dist.array <- parRapply(cl, vec.array,
function(x) TaxiDistFun(x, vec.array)))
stopCluster(cl)
dim(dist.array) <- c(2000, 2000)
Run Code Online (Sandbox Code Playgroud)