简体   繁体   中英

parallelize a nested for loop in R

I want to parallelize the below code in R. It's a nested for loop.

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

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"

Is there any better way to parallelize this code (may be using parLapply)? If not, how do I fix the above code?

You didn't declare cl . The following worked if you remove 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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