r-lib / cpp11

cpp11 helps you to interact with R objects using C++ code.
https://cpp11.r-lib.org/
Other
199 stars 46 forks source link

Use cpp11 with OpenMP #243

Closed waynelapierre closed 1 month ago

waynelapierre commented 2 years ago

Can you add an example of using cpp11 with OpenMP in the documentation webpage?

jimhester commented 2 years ago

Apple does not ship libomp with its compiler, which means that it is not entirely trivial to compile packages using openMP on macOS.

For this reason I don't think it is generally a good idea to use openMP for R packages you intend to put on CRAN and for that reason I don't think it is a good idea to have an example of using openMP with cpp11.

fgp commented 1 year ago

Apple does not ship libomp with its compiler, which means that it is not entirely trivial to compile packages using openMP on macOS. For this reason I don't think it is generally a good idea to use openMP for R packages you intend to put on CRAN and for that reason I don't think it is a good idea to have an example of using openMP with cpp11.

Being an Apple use myself I don't see why Apple's stubbornness should be allowed to negatively affect other platforms. Especially when there's no obvious alternative for platform-agnostic parallelization in C++. Plus OpenMP makes it easy to write code that gracefully falls back to single-threaded behaviour if OpenMP is not present at compile-time. A well-known example of a package on CRAN that uses OpenMP but works perfectly fine without it is data.table, and I'm sure there are others.

I've previously tried to use Rcpp together with OpenMP and ran into a lot of issues. Mostly due to the fact that almost any operation you perform on an Rcpp vector may lead to calls into the R interpreter, which when done from an OpenMP thread messes up things badly.

Cpp11 might fare better, though, it seems to do fewer things behind the scenes. So having an example that clearly explain what may and may not be done from parallel code seems like a good idea to me.

waynelapierre commented 1 year ago

I have never run into any issues using OpenMP with Rcpp. Could you file an issue for Rcpp? Also, based on my experience using packages developed by RStudio, it seems they don't care so much about performance. So, I don't see cpp11 supporting OpenMP in the foreseeable future.

fgp commented 1 year ago

I have never run into any issues using OpenMP with Rcpp. Could you file an issue for Rcpp? Also, based on my experience using packages developed by RStudio, it seems they don't care so much about performance. So, I don't see cpp11 supporting OpenMP in the foreseeable future.

I don't think any of the issues I ran into where bugs in Rcpp, so filing issues isn't really the answer I think. Documented guidlines on what it and isn't safe to do from parallel code would be. My answer ended up being the most conservative possible -- I simply assumed nothing would be safe, and extracted raw pointers to the data before entering parallel code.

The same approach, btw, surely works for cpp11 as well. And it's really not that bad -- you obviously can't do allocations from parallel code anyway, so you have to make sure your input buffers are all ready and your output buffers are pre-allocated before entering parallel sections anway. At which point you might as well prepare raw pointers to these buffers.

DavisVaughan commented 1 month ago

We don't currently plan to write any guidelines like this