The usual way is to put the function in a package and load the package.

Otherwise, you could do something along these lines

auto_function <- function( code, ... ){
    dots <- list(code, ...)
    function(...){
        do.call( cppFunction, dots )( ... )
    }
}

This way the function knows how to compile itself, for example:

> fun <- auto_function(' double inner_Cpp(double a){ return 1;  } ' )
# this takes a while the first time
> fun( 2 )
[1] 1
# this is instant thanks to caching of sourceCpp
> fun( 2 )
[1] 1

Romain

Le 26/09/13 18:57, Matteo Fasiolo a écrit :
Dear Rcpp developers,

  I'm trying to parallelize some of my algorithms but I have encountered
the following problem:

# I have a cppFunction
cppFunction(' double inner_Cpp(double a){ return 1;  } ')

# And an R wrapper around it
wrapper_R<- function(input)
{
   inner_Cpp(input)
}

# And I want to call the wrappen in parallel within algorithm
algo <- function(input)
{
   cl <- makeCluster(2)
   clusterExport(cl, "inner_Cpp")
   a <- clusterApply(cl, 1:2, wrapper_R)
   stopCluster(cl)
   a
}

algo(2)

Error in checkForRemoteErrors(val) :
   2 nodes produced errors; first error: NULL value passed as symbol address

It seems that I'm unable to find the address of inner_Cpp.
If the inner function is an R function there is no problem:

inner_R <- function(input) 1

wrapper_R<- function(input)
{
   inner_R(input)
}

algo <- function(input)
{
   cl <- makeCluster(2)
   clusterExport(cl, "inner_R")
   a <- clusterApply(cl, 1:2, wrapper_R)
   stopCluster(cl)
   a
}

algo(2)

[[1]]
[1] 1

[[2]]
[1] 1

Do you have any idea about why this is happening?
Given that I have just started parallelizing my algorithms in this
way, any suggestion/criticism about the overall approach is
more then welcome!

Matteo


--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to