[Rcpp-devel] Exporting rcpp-based function into parLapply workers in an R package

Balamuta, James Joseph balamut2 at illinois.edu
Fri May 14 22:09:07 CEST 2021


Naeem,

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

From there, the C++ can easily be exported across parallel workers just by loading the package.

Consider looking at how this example package using doParallel is structured.

https://github.com/r-pkg-examples/rcpp-and-doparallel

Best,

JJB

From: Rcpp-devel <rcpp-devel-bounces at lists.r-forge.r-project.org> on behalf of Naeem Khoshnevis <khoshnevis.naeem at gmail.com>
Date: Friday, May 14, 2021 at 11:49 AM
To: "rcpp-devel at lists.r-forge.r-project.org" <rcpp-devel at lists.r-forge.r-project.org>
Subject: [Rcpp-devel] Exporting rcpp-based function into parLapply workers in an R package

Dear Rcpp developers:

Thanks for developing and maintaining the Rcpp package.
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.
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.

Original cpp function:
https://github.com/fasrc/CausalGPS/blob/master/src/compute_closest_wgps_helper.cpp<https://urldefense.com/v3/__https:/github.com/fasrc/CausalGPS/blob/master/src/compute_closest_wgps_helper.cpp__;!!DZ3fjg!oyv9eCC8FkfL4RzQ_LE613qZNCplLbfU22AlYI8Faem0SaWx-GcDeRWHef6zf-42AH4$>

Wrapper function that calls this function + temporal fix:
https://github.com/fasrc/CausalGPS/blob/master/R/compute_closest_wgps.R<https://urldefense.com/v3/__https:/github.com/fasrc/CausalGPS/blob/master/R/compute_closest_wgps.R__;!!DZ3fjg!oyv9eCC8FkfL4RzQ_LE613qZNCplLbfU22AlYI8Faem0SaWx-GcDeRWHef6z0KitDq4$>

The function that uses parLapply (please see line 63-89) to run the c++ code:
https://github.com/fasrc/CausalGPS/blob/master/R/compute_closest_wgps.R<https://urldefense.com/v3/__https:/github.com/fasrc/CausalGPS/blob/master/R/compute_closest_wgps.R__;!!DZ3fjg!oyv9eCC8FkfL4RzQ_LE613qZNCplLbfU22AlYI8Faem0SaWx-GcDeRWHef6z0KitDq4$>

Best regards,
Naeem
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20210514/9962d3e8/attachment-0001.html>


More information about the Rcpp-devel mailing list