<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:13px">Thanks a lot for your replies, I will go for the package solution, given that</span><div style="font-family:arial,sans-serif;font-size:13px">I'm organizing all my code in a package in any case.</div>
<div class="" style="font-family:arial,sans-serif;font-size:13px"><div id=":ri" class="" tabindex="0"><img class="" src="https://mail.google.com/mail/ca/u/0/images/cleardot.gif" style=""></div><div id=":ri" class="" tabindex="0">
Matteo</div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Sep 26, 2013 at 6:15 PM, Romain Francois <span dir="ltr"><<a href="mailto:romain@r-enthusiasts.com" target="_blank">romain@r-enthusiasts.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The usual way is to put the function in a package and load the package.<br>
<br>
Otherwise, you could do something along these lines<br>
<br>
auto_function <- function( code, ... ){<br>
    dots <- list(code, ...)<br>
    function(...){<br>
        do.call( cppFunction, dots )( ... )<br>
    }<br>
}<br>
<br>
This way the function knows how to compile itself, for example:<br>
<br>
> fun <- auto_function(' double inner_Cpp(double a){ return 1;  } ' )<br>
# this takes a while the first time<br>
> fun( 2 )<br>
[1] 1<br>
# this is instant thanks to caching of sourceCpp<br>
> fun( 2 )<br>
[1] 1<br>
<br>
Romain<br>
<br>
Le 26/09/13 18:57, Matteo Fasiolo a écrit :<div class="HOEnZb"><div class="h5"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Dear Rcpp developers,<br>
<br>
  I'm trying to parallelize some of my algorithms but I have encountered<br>
the following problem:<br>
<br>
# I have a cppFunction<br>
cppFunction(' double inner_Cpp(double a){ return 1;  } ')<br>
<br>
# And an R wrapper around it<br>
wrapper_R<- function(input)<br>
{<br>
   inner_Cpp(input)<br>
}<br>
<br>
# And I want to call the wrappen in parallel within algorithm<br>
algo <- function(input)<br>
{<br>
   cl <- makeCluster(2)<br>
   clusterExport(cl, "inner_Cpp")<br>
   a <- clusterApply(cl, 1:2, wrapper_R)<br>
   stopCluster(cl)<br>
   a<br>
}<br>
<br>
algo(2)<br>
<br>
Error in checkForRemoteErrors(val) :<br>
   2 nodes produced errors; first error: NULL value passed as symbol address<br>
<br>
It seems that I'm unable to find the address of inner_Cpp.<br>
If the inner function is an R function there is no problem:<br>
<br>
inner_R <- function(input) 1<br>
<br>
wrapper_R<- function(input)<br>
{<br>
   inner_R(input)<br>
}<br>
<br>
algo <- function(input)<br>
{<br>
   cl <- makeCluster(2)<br>
   clusterExport(cl, "inner_R")<br>
   a <- clusterApply(cl, 1:2, wrapper_R)<br>
   stopCluster(cl)<br>
   a<br>
}<br>
<br>
algo(2)<br>
<br>
[[1]]<br>
[1] 1<br>
<br>
[[2]]<br>
[1] 1<br>
<br>
Do you have any idea about why this is happening?<br>
Given that I have just started parallelizing my algorithms in this<br>
way, any suggestion/criticism about the overall approach is<br>
more then welcome!<br>
<br>
Matteo<br>
</blockquote>
<br>
<br></div></div><span class="HOEnZb"><font color="#888888">
-- <br>
Romain Francois<br>
Professional R Enthusiast<br>
<a href="tel:%2B33%280%29%206%2028%2091%2030%2030" value="+33628913030" target="_blank">+33(0) 6 28 91 30 30</a><br>
<br>
______________________________<u></u>_________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-<u></u>project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-<u></u>project.org/cgi-bin/mailman/<u></u>listinfo/rcpp-devel</a><br>
</font></span></blockquote></div><br></div>