[R] SNOW, lexical scoping, and "programming style"

Ramon Diaz-Uriarte rdiaz at cnio.es
Fri Sep 3 14:21:16 CEST 2004


Dear All,

A programming style and scoping question when using SNOW and clusterApplyLB. 


### The following will not work:
uu <- matrix(rnorm(200 * 1000), ncol = 1000)

fm1 <- function(xmaster, ...) {
    fs1 <- function(dummy, ...) {
           dim(xmaster)
        }
        clusterApplyLB(TheCluster, 1:3, fs1)

}

fm1(uu) ## uu not found


### I can think of three alternatives:

### Use "force"
fm2 <- function(xmaster, ...) {
    force(xmaster)
    fs2 <- function(dummy, ...) {
        dim(xmaster)
    }
    clusterApplyLB(TheCluster, 1:50, fs2)
}

## Export the variable explicitly from inside the function
fs3 <- function(dummy, ...) {
    dim(xmaster.copy)
}
fm3 <- function(xmaster, ...) {
    xmaster.copy <<- xmaster
    clusterExport(TheCluster, "xmaster.copy")
    clusterApplyLB(TheCluster, 1:50, fs3)

}

## Pass the variable as an argument of clusterApplyLB
fs4 <- function(dummy, xpassedalong) {
    dim(xpassedalong)
}
fm4 <- function(xmaster, ...) {
    clusterApplyLB(TheCluster, 1:50, fs4, xmaster)
}




Which/why would be best? 
I dislike using ClusterExport after a "<<-", but this approach ought to be 
advantageous with clusterApplyLB if each slave node is visited repeatedly (if 
in ClusterApplyLB(cl, x, fun, ...), length(x) >> number of nodes) and the 
object is large. (Some limited testing shows that in these cases the fastest 
is option 3, followed by 2, followed by 4).

Thanks,

R.



-- 
Ramón Díaz-Uriarte
Bioinformatics Unit
Centro Nacional de Investigaciones Oncológicas (CNIO)
(Spanish National Cancer Center)
Melchor Fernández Almagro, 3
28029 Madrid (Spain)
Fax: +-34-91-224-6972
Phone: +-34-91-224-6900

http://ligarto.org/rdiaz
PGP KeyID: 0xE89B3462
(http://ligarto.org/rdiaz/0xE89B3462.asc)




More information about the R-help mailing list