-
-
Notifications
You must be signed in to change notification settings - Fork 219
alternate openmp plugin changes #1414
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -586,10 +586,74 @@ compileAttributes <- function(pkgdir = ".", verbose = getOption("verbose")) { | |
| list(env = list(PKG_CXXFLAGS ="-std=c++2b")) | ||
| } | ||
|
|
||
| .openmpPluginDefault <- function() { | ||
| list(env = list(PKG_CXXFLAGS = "-fopenmp", PKG_LIBS = "-fopenmp")) | ||
| } | ||
|
|
||
| .openmpPluginDarwin <- function() { | ||
|
|
||
| # generate a test script for compilation | ||
| script <- tempfile("openmp-detect-", fileext = ".cpp") | ||
| writeLines("", con = script) | ||
| on.exit(unlink(script, force = TRUE), add = TRUE) | ||
|
|
||
| # get the C++ compiler from R | ||
| r <- file.path(R.home("bin"), "R") | ||
| output <- tryCatch( | ||
| system2(r, c("CMD", "SHLIB", "--dry-run", shQuote(script)), stdout = TRUE), | ||
| condition = identity | ||
| ) | ||
| if (inherits(output, "condition")) | ||
| return(.openmpPluginDefault()) | ||
|
|
||
| # extract the compiler invocation from the shlib output | ||
| # use some heuristics here... | ||
| index <- grep("make would use", output) | ||
| compile <- output[[index + 1L]] | ||
|
|
||
| # use everything up to the first include flag, which is normally | ||
| # the R headers from CPPFLAGS | ||
| idx <- regexpr(" -I", compile, fixed = TRUE) | ||
| cxx <- substring(compile, 1L, idx - 1L) | ||
|
|
||
| # check the compiler version | ||
| command <- paste(cxx, "--version") | ||
| version <- tryCatch( | ||
| system(command, intern = TRUE), | ||
| condition = identity | ||
| ) | ||
| if (inherits(version, "condition")) | ||
| return(.openmpPluginDefault()) | ||
|
|
||
| # if we're using Apple clang, use alternate flags | ||
| # assume libomp was installed following https://mac.r-project.org/openmp/ | ||
| if (any(grepl("Apple clang", version))) { | ||
| cxxflags <- "-Xclang -fopenmp" | ||
| libs <- "-lomp" | ||
| } | ||
|
|
||
| # if we're using Homebrew clang, add in libomp include paths | ||
| else if (any(grepl("Homebrew clang", version))) { | ||
| machine <- Sys.info()[["machine"]] | ||
| prefix <- if (machine == "arm64") "/opt/homebrew" else "/usr/local" | ||
| cxxflags <- sprintf("-I%s/opt/libomp/include -fopenmp", prefix) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I confirmed that the version of LLVM clang provided in Homebrew accepts |
||
| libs <- sprintf("-L%s/opt/libomp/lib -fopenmp", prefix) | ||
|
|
||
| # otherwise, use default -fopenmp flags for other compilers (LLVM clang; gcc) | ||
| } else { | ||
| cxxflags <- "-fopenmp" | ||
| libs <- "-fopenmp" | ||
| } | ||
|
|
||
| list(env = list(PKG_CXXFLAGS = cxxflags, PKG_LIBS = libs)) | ||
|
|
||
| } | ||
|
|
||
| ## built-in OpenMP plugin | ||
| .plugins[["openmp"]] <- function() { | ||
| list(env = list(PKG_CXXFLAGS="-fopenmp", | ||
| PKG_LIBS="-fopenmp")) | ||
| .plugins[["openmp"]] <- if (Sys.info()[["sysname"]] == "Darwin") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like how the detection simplifies the function definition on load. |
||
| .openmpPluginDarwin | ||
| } else { | ||
| .openmpPluginDefault | ||
| } | ||
|
|
||
| .plugins[["unwindProtect"]] <- function() { # nocov start | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, this would trigger:
R CMD SHLIB --dry-run $TMPDIR/openmp-detect-.cppGiving:
After the regex, we're left with:
This is the same as:
I'm not immediately seeing a huge benefit for a disk write and a regex to end up back at
CXXconfig. Is there something regarding a custom toolchain that this would protect against?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See earlier discussion between @kevinushey and myself. It is possible that, say,
CXX!=CXX17so that asking for the former does not give us the answer.I agree with the sentiment of this being more brittle but on balance that may be what we have to go with.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's exactly it. For example, if the
USE_CXX17environment variable is set, thenCXX17will be used during compilation. Some users might be doing things like this in their.Renvironto enforce the use of their configuredCXX17compiler for different packages.(And, as far as I can see, there isn't a way to ask R "what compiler do you want to use right now?" beyond something like this.)