Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cpp11 with OpenMP #243

Open
waynelapierre opened this issue Sep 26, 2021 · 4 comments
Open

Use cpp11 with OpenMP #243

waynelapierre opened this issue Sep 26, 2021 · 4 comments

Comments

@waynelapierre
Copy link

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

@jimhester
Copy link
Member

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
Copy link

fgp commented Oct 26, 2022

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
Copy link
Author

waynelapierre commented Oct 26, 2022

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
Copy link

fgp commented Oct 26, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants