简体   繁体   English

在R中并行化嵌套的for循环

[英]parallelize a nested for loop in R

I want to parallelize the below code in R. It's a nested for loop. 我想并行化R中的以下代码。这是一个嵌套的for循环。

for (i in 1:nrow(my_dataset_preprocessed)){
    for (j in 1:ncol(my_dataset_preprocessed)){
      my_dataset_preprocessed[i,j] = min( my_dataset_preprocessed[i,j], 0.1 ) 
    }
}

I am trying the below code using doParallel 我正在尝试使用doParallel的以下代码

library(foreach)
library(doParallel)
registerDoParallel(detectCores())
clusterExport(cl, "my_dataset")

threshold_par <- function (X) { 
  co <- foreach(i=1:nrow(X)) %:%
                foreach (j=1:ncol(X)) %dopar% {   
                  co = min( X[i,j], 0.1 )
                }
  matrix(unlist(co), ncol=ncol(X))
}

system.time(threshold_par(my_dataset))

But I am getting the following error: 但是我收到以下错误:

Error in { : task 1 failed - "invalid 'type' (list) of argument" {中的错误:任务1失败-“参数的'类型'(列表)无效”

Is there any better way to parallelize this code (may be using parLapply)? 有没有更好的方法来并行化此代码(可能使用parLapply)? If not, how do I fix the above code? 如果没有,如何解决以上代码?

You didn't declare cl . 您没有声明cl The following worked if you remove clusterExport(cl, "my_dataset") 如果删除clusterExport(cl, "my_dataset")以下方法会起作用

library(foreach)
library(doParallel)    
registerDoParallel(detectCores())
getDoParWorkers()
# [1] 8

threshold_par <- function (X) { 
  co <- foreach(i=1:nrow(X)) %:%
                foreach (j=1:ncol(X)) %dopar% {   
                  co = min( X[i,j], 0.1 )
                }
  matrix(unlist(co), ncol=ncol(X))
}

test <- matrix(1:4, ncol=2)
system.time(threshold_par(test))
#      user  system elapsed 
#      0.01    0.00    0.02

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM