2

As my understanding, the slave processes cannot access variables in the global/parent environments in R package snowfall.

For example, the code below will give an error message as I expected:

parfun <- function(i, var1, var2) {
    return(var3)
}

var3 <- 'VAR3'
library(snowfall)
sfInit(parallel = TRUE, cpus = 2)
res <- sfLapply(seq(1, 2), parfun, 'VAR1', 'VAR2')
res

The error message:

Error in checkForRemoteErrors(val) : 
  2 nodes produced errors; first error: object 'var3' not found

However, there is no error message when parallel codes in another function.

fun1 <- function(var1, var2, var3) {
    parfun <- function(i, var1, var2) {
        return(var3)
    }
    library(snowfall)
    sfInit(parallel = TRUE, cpus = 2)
    res <- sfLapply(seq(1, 2), parfun, var1, var2)
    res
}

fun1('VAR1', 'VAR2', 'VAR3')

The results are as follows:

[[1]]
[1] "VAR3"

[[2]]
[1] "VAR3"

I don't understand this behavior as var3 doesn't exist in the function parfun, but in his parent environment fun1.

Could somebody explain to me? Thanks for any suggestions.

Bangyou
  • 9,462
  • 16
  • 62
  • 94

0 Answers0