R concurrent cluster error of class S4

I had a problem getting some code to work with a parallel package in R. I am using R 2.15.

Here is a simplified example ... I have a file "animal.R" that contains the following:

# animal.R
setClass("Animal", representation(species = "character", legs = "numeric"))

##Define some Animal methods
setGeneric("count",function(x) standardGeneric("count"))
setMethod("count", "Animal", function(x) { x@legs})

setGeneric("countAfterChopping",function(x) standardGeneric("countAfterChopping"))
setMethod("countAfterChopping", "Animal", function(x) { x@legs <- x@legs-1; x@legs})

Then in my R-terminal, I run:

library(parallel)
source('animal.R')

Launch a local cluster of two nodes:

cl <- makeCluster(rep('localhost', 2))

Tell the cluster nodes about the Animal class:

clusterEvalQ(cl, parse('animal.R'))

Then run some code in the cluster:

# This works
parSapply(cl, list(daisy, fred), count)

# This doesn't...
parSapply(cl, list(daisy, fred), countAfterChopping)

Stop cluster:

stopCluster(cl)

The first call to parSapply works as expected, but the second causes this error:

Error in checkForRemoteErrors(val) : 
  2 nodes produced errors; first error: "Animal" is not a defined class

Any idea what is going on? Why does the second call to the parSapply function not work?

+5
source share
1 answer

So here is what happens:

S4 "Animal" count legs. , , animal.R . parSapply.

countAfterChopping legs, . `@<-` `slot<-` check = TRUE. checkSlotAssignment, " , " ( ?checkSlotAssignment).

, S4 "Animal" . animal.R . , , .. "Animal" node.

, :

animal.R<-"
  setClass('Animal', representation(species = 'character', legs = 'numeric'))

  ##Define some Animal methods
  setGeneric('count',function(x) standardGeneric('count'))
  setMethod('count', signature(x='Animal'), function(x) { x@legs})

  setGeneric('countAfterChopping',function(x) standardGeneric('countAfterChopping'))
  setMethod('countAfterChopping', signature(x='Animal'),
    function(x) { x@legs <- x@legs-1; x@legs})
"
library(parallel)

source(textConnection(animal.R))

cl <- makeCluster(rep('localhost', 2))

daisy<-new("Animal",legs=2,species="H.sapiens")
fred<-new("Animal",legs=4,species="C.lupus")

parSapply(cl, list(daisy, fred), count)
# [1] 2 4

clusterExport(cl,"animal.R") # 
clusterEvalQ(cl,eval(parse(textConnection(animal.R),n=1)))

parSapply(cl, list(daisy, fred), countAfterChopping)
# [1] 1 3
+3

All Articles