<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style>
</head>
<body lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">Naeem,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The best path for including compiled code in a package is to place it within the `src/` directory instead of using `Rcpp::cppFunction()` to compile. The reasons for this are stated succiently here: https://stackoverflow.com/a/6074391/1345455<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">From there, the C++ can easily be exported across parallel workers just by loading the package.
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Consider looking at how this example package using doParallel is structured.
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><a href="https://github.com/r-pkg-examples/rcpp-and-doparallel">https://github.com/r-pkg-examples/rcpp-and-doparallel</a>
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Best,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">JJB<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:12.0pt;color:black">From: </span></b><span style="font-size:12.0pt;color:black">Rcpp-devel <rcpp-devel-bounces@lists.r-forge.r-project.org> on behalf of Naeem Khoshnevis <khoshnevis.naeem@gmail.com><br>
<b>Date: </b>Friday, May 14, 2021 at 11:49 AM<br>
<b>To: </b>"rcpp-devel@lists.r-forge.r-project.org" <rcpp-devel@lists.r-forge.r-project.org><br>
<b>Subject: </b>[Rcpp-devel] Exporting rcpp-based function into parLapply workers in an R package<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Dear Rcpp developers:<br>
<br>
Thanks for developing and maintaining the Rcpp package. <br>
I wrote a function in Rcpp. It is available throughout the package and works as expected; however, it is not available for praLapply workers. A temporary fix is just using Rcpp::cppFunction inside the function that parLapply workers call and copy the entire
 function over there. However, this does not seem right for bigger and more complicated functions.<br>
I would be grateful if you could let me know whether there is a better long-term solution. Here is the package and three functions that you might want to take a look at.<br>
<br>
Original cpp function:<br>
<a href="https://urldefense.com/v3/__https:/github.com/fasrc/CausalGPS/blob/master/src/compute_closest_wgps_helper.cpp__;!!DZ3fjg!oyv9eCC8FkfL4RzQ_LE613qZNCplLbfU22AlYI8Faem0SaWx-GcDeRWHef6zf-42AH4$">https://github.com/fasrc/CausalGPS/blob/master/src/compute_closest_wgps_helper.cpp</a><br>
<br>
Wrapper function that calls this function + temporal fix:<br>
<a href="https://urldefense.com/v3/__https:/github.com/fasrc/CausalGPS/blob/master/R/compute_closest_wgps.R__;!!DZ3fjg!oyv9eCC8FkfL4RzQ_LE613qZNCplLbfU22AlYI8Faem0SaWx-GcDeRWHef6z0KitDq4$">https://github.com/fasrc/CausalGPS/blob/master/R/compute_closest_wgps.R</a><br>
<br>
The function that uses parLapply (please see line 63-89) to run the c++ code:<br>
<a href="https://urldefense.com/v3/__https:/github.com/fasrc/CausalGPS/blob/master/R/compute_closest_wgps.R__;!!DZ3fjg!oyv9eCC8FkfL4RzQ_LE613qZNCplLbfU22AlYI8Faem0SaWx-GcDeRWHef6z0KitDq4$">https://github.com/fasrc/CausalGPS/blob/master/R/compute_closest_wgps.R</a><br>
<br>
Best regards,<br>
Naeem <o:p></o:p></p>
</div>
</div>
</body>
</html>