Skip to content

Commit 530a9d4

Browse files
committed
Fix OpenMP+BLAS linking on macOS
1 parent 4d15548 commit 530a9d4

3 files changed

Lines changed: 39 additions & 11 deletions

File tree

R/compiler.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@ quickr_fcompiler_env <- function(
9494
quickr_env_is_true("QUICKR_PREFER_FLANG"),
9595
write_lines = writeLines,
9696
sysname = Sys.info()[["sysname"]],
97-
use_openmp = FALSE
97+
use_openmp = FALSE,
98+
link_flags = character()
9899
) {
99100
stopifnot(is.character(build_dir), length(build_dir) == 1L, nzchar(build_dir))
100101

101102
use_openmp <- isTRUE(use_openmp)
103+
link_flags <- link_flags[nzchar(link_flags)]
102104

103105
flang <- ""
104106
flang_runtime <- character()
@@ -151,7 +153,10 @@ quickr_fcompiler_env <- function(
151153
}
152154
)
153155
},
154-
if (use_openmp) openmp_makevars_lines()
156+
if (use_openmp) openmp_makevars_lines(),
157+
if (length(link_flags)) {
158+
paste("PKG_LIBS +=", paste(link_flags, collapse = " "))
159+
}
155160
)
156161
write_lines(
157162
makevars_lines,

R/quick.R

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,21 @@ compile <- function(fsub, build_dir = tempfile(paste0(fsub@name, "-build-"))) {
234234
link_flags <- c(LAPACK_LIBS, BLAS_LIBS, FLIBS)
235235

236236
suppressWarnings({
237-
r_args <- c(
237+
use_openmp <- isTRUE(attr(fsub@scope, "uses_openmp", exact = TRUE))
238+
env <- quickr_fcompiler_env(
239+
build_dir = build_dir,
240+
use_openmp = use_openmp,
241+
link_flags = link_flags
242+
)
243+
r_args_base <- c(
238244
"CMD SHLIB --use-LTO",
239245
"-o",
240246
dll_path,
241247
fsub_path,
242-
c_wrapper_path,
243-
link_flags
244-
)
245-
use_openmp <- isTRUE(attr(fsub@scope, "uses_openmp", exact = TRUE))
246-
env <- quickr_fcompiler_env(
247-
build_dir = build_dir,
248-
use_openmp = use_openmp
248+
c_wrapper_path
249249
)
250+
r_args_libs <- c(r_args_base, link_flags)
251+
r_args <- if (length(env)) r_args_base else r_args_libs
250252
result <- system2(
251253
R.home("bin/R"),
252254
r_args,
@@ -257,7 +259,7 @@ compile <- function(fsub, build_dir = tempfile(paste0(fsub@name, "-build-"))) {
257259
if (!is.null(attr(result, "status")) && length(env)) {
258260
result2 <- system2(
259261
R.home("bin/R"),
260-
r_args,
262+
r_args_libs,
261263
stdout = TRUE,
262264
stderr = TRUE
263265
)

tests/testthat/test-parallel-declare.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,24 @@ test_that("parallel declarations do not cross control flow boundaries", {
161161
regexp = "parallel\\(\\)/omp\\(\\) must be followed by a for-loop or sapply\\(\\)"
162162
)
163163
})
164+
165+
test_that("openmp functions that use BLAS load and run", {
166+
openmp_supported_or_skip()
167+
168+
blas_parallel <- function(x, n) {
169+
declare(
170+
type(x = double(n, n)),
171+
type(n = integer(1)),
172+
type(out = double(n, n))
173+
)
174+
declare(parallel())
175+
for (i in seq_len(1L)) {
176+
out <- x %*% x
177+
}
178+
out
179+
}
180+
181+
set.seed(123)
182+
x <- matrix(runif(16), nrow = 4)
183+
expect_quick_equal(blas_parallel, list(x, 4L))
184+
})

0 commit comments

Comments
 (0)