[R-sig-hpc] Error in UseMethod("stopCluster") : no applicable method for 'stopCluster' ...
Martin Morgan
mtmorgan at fhcrc.org
Fri Sep 21 23:20:23 CEST 2012
On 9/21/2012 12:16 PM, Marius Hofert wrote:
> Dear Martin,
>
> thanks for helping. Is there a solution without explicitly loading snow/parallel
> with library()/require()? I forgot to say, I was hoping for such a solution.
Not that I know of. Write a package and importFrom(parallel,
stopCluster)? Martin
>
> Cheers,
>
> Marius
>
> Martin Morgan <mtmorgan at fhcrc.org> writes:
>
>> On 09/21/2012 12:47 AM, Marius Hofert wrote:
>>> Dear expeRts,
>>>
>>> If I run the minimal example below, I obtain:
>>>
>>> Loading required package: Rmpi
>>> Loading required package: grDevices
>>> Loading required package: grDevices
>>> Loading required package: grDevices
>>> Loading required package: grDevices
>>> 4 slaves are spawned successfully. 0 failed.
>>> Error in UseMethod("stopCluster") :
>>> no applicable method for 'stopCluster' applied to an object of class "c('spawnedMPIcluster', 'MPIcluster', 'cluster')"
>>>
>>> However, if I run the body of the function (linewise; without calling the
>>> function), everything works. Why is that so?
>>>
>>> Cheers,
>>>
>>> Marius
>>>
>>>
>>> grid <- expand.grid(a=1:4, b=2:10)
>>> ngrid <- nrow(grid)
>>> f <- function(x) sum(x)
>>>
>>> ex <- function() {
>>> cl <- snow::makeCluster(parallel::detectCores(), type="MPI")
>>> on.exit(snow::stopCluster(cl))
>>> RNGkind("L'Ecuyer-CMRG")
>>> parallel::clusterExport(cl, varlist=c("f", "grid"))
>>> parallel::clusterApply(cl, seq_len(ngrid), function(i) f(grid[i,]))
>>> }
>>>
>>> res <- ex()
>>
>> An even more minimal example is
>>
>> ex <- function() {
>> cl <- snow::makeCluster(parallel::detectCores(), type="MPI")
>> snow::stopCluster(cl)
>> }
>> res <- ex()
>>
>> To be clear, there is no library(snow) in the script, and on.exit() is not
>> relevant. The reason is that snow::stopCluster is an S3 generic, and on the help
>> page ?NextMethod we have
>>
>>
>> Namespaces can register methods for generic functions. To support
>> this, 'UseMethod' and 'NextMethod' search for methods in two
>> places: first in the environment in which the generic function is
>> called, and then in the registration data base for the environment
>> in which the generic is defined (typically a namespace). So
>> methods for a generic function need to be available in the
>> environment of the call to the generic, or they must be
>> registered. (It does not matter whether they are visible in the
>> environment in which the generic is defined.)
>>
>> There are no stopCluster methods defined in the calling namespace (i.e., your
>> script) and since there has been no call to library(snow), there is no snow
>> namespace to search and hence no method to be found.
>>
>> The solution is to add
>>
>> library(snow)
>>
>> to the script (or better library(parallel) and use the 'parallel' library
>> throughout).
>>
>> Martin
>>
>>
>>>
>>> _______________________________________________
>>> R-sig-hpc mailing list
>>> R-sig-hpc at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-hpc
>>>
>
--
Dr. Martin Morgan, PhD
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109
More information about the R-sig-hpc
mailing list