[R] Parallel Programming
R. Michael Weylandt
michael.weylandt at gmail.com
Fri Sep 21 18:05:43 CEST 2012
On Fri, Sep 21, 2012 at 5:43 AM, Tjun Kiat Teo <teotjunk at gmail.com> wrote:
> I am trying to do parallel programming and I tried this
>
> library(doSNOW)
> library(foreach)
>
> testfunc<-function(x){
> x<-x+1
> x
> }
>
> noc<-2
>
> cl <- makeCluster(do.call(rbind,rep(list("localhost"),noc)), type = "SOCK")
> registerDoSNOW(cl)
> clusterExport(cl=cl,c("testfunc.r"))
>
>
> testl<-foreach(pp=1:2) %dopar% {
> testfunc(pp)
> }
>
>
> And this works but if I try to enclose my commands inside a text file
> to be sourced it doesn't work
>
> noc<-2
>
> testfunc<-function(x){
> x<-x+1
> x
> }
>
> cl <- makeCluster(do.call(rbind,rep(list("localhost"),noc)), type = "SOCK")
>
> registerDoSNOW(cl)
>
> clusterExport(cl=cl,c("a","testfunc.r"))
>
> testl<-foreach(pp=1:2)) %dopar% {
> source("test.r")
> }
I'm not sure this is a parallelization issue: when you source() a
file, it doesn't return the last value calculated in quite the way
that running a function directly does.
E.g.,
system("echo 2+2 > test.R")
x <- source("test.R")
identical(x,4) # FALSE
str(x) # Look at what actually returned
source("test.R")[[1]] # Actual result
Perhaps that's tripping you up?
Cheers,
Michael
More information about the R-help
mailing list