如何解决如何使用 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 举报,一经查实,本站将立刻删除。