I am having difficulties understanding how variables are scoped/passed to the functions when interacting with the parallel package
library(parallel)
test <- function(a = 1){
no_cores <- detectCores()-1
clust <- makeCluster(no_cores)
result <- parSapply(clust, 1:10, function(x){a + x})
stopCluster(clust)
return(result)
}
test()
[1] 4 5 6 7 8 9 10 11 12 13
x = 1
test(x)
Error in checkForRemoteErrors(val) :
3 nodes produced errors; first error: object 'x' not found
test() works but test(x) doesn't. When I modify the function as follows, it works.
test <- function(a = 1){
no_cores <- detectCores()-1
clust <- makeCluster(no_cores)
y = a
result <- parSapply(clust, 1:10, function(x){y + x})
stopCluster(clust)
return(result)
}
x = 1
test(x)
Can someone explain what is going on in memory?