如何使用 DoParallel 通过条件索引在 R 中并行化矩阵切片?

问题描述

我尝试在以下代码片段中并行化 for 循环:

t0 <- Sys.time()
x <- matrix(runif(200*1000,1,5),nrow =200,ncol = 1000)
print(Sys.time() - t0 )
t0 <- Sys.time()
u <- unique(as.vector(x))
print(Sys.time() - t0 )
print(length(u))
utransformed <- asinh(u)
uindex <- seq(from = 1,to = length(u),by = 1)
t0 <- Sys.time()
for (hh in uindex)
{
     x[x == u[hh] ] = utransformed[hh]
}
print(Sys.time() - t0 )

我尝试并行化 for 循环我进行如下修改:

library(doParallel)
#setup parallel backend to use many processors
cores=detectCores()
print(cores)
cl <- makeCluster(cores[1]-1) #not to overload your computer
registerDoParallel(cl)
t0 <- Sys.time()
x <- matrix(runif(200*1000,by = 1)
subsetter <- function(xh,uh,uth,h)
{
    xh[xh == uh[h]] = uth[h]
}
t0 <- Sys.time()
foreach(i = uindex,.combine=cbind)  %dopar% (
    subsetter(x,u,utransformed,i)
)
print(Sys.time() - t0 )

这显然不起作用,我不确定如何通过条件索引实现并行化我的 for 循环以更改矩阵 x 的某些值。非常感谢在这方面的任何帮助或指导。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...