Skip to content

Commit

Permalink
configure: test a real OpenMP operation
Browse files Browse the repository at this point in the history
It turns out it's possible for omp_get_num_threads() to succeed while
failing to load the shared library that does more involved OpenMP
operations.
  • Loading branch information
aitap committed Dec 10, 2024
1 parent cae9be6 commit 4bd336f
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions tools/check-openmp-flags.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@ writeLines("
#ifdef _OPENMP
#include <omp.h>
#endif
void test_openmp(int * numprocs) {
*numprocs =
void test_openmp(int * result) {
int sum = 0;
#ifdef _OPENMP
omp_get_max_threads()
#else
0
// Have to test an actual OpenMP operation: simpler tests may succeed for a broken configuration, #6642
#pragma omp parallel for reduction(+:sum) num_threads(2)
for (int i = 1; i <= 2; ++i) sum += i;
#endif
;
*result = sum;
}
", "test.c")

# May fail to compile anyway
stopifnot(tools::Rcmd("SHLIB --preclean test.c") == 0)

dyn.load(paste0("test", .Platform$dynlib.ext))
success <- .C("test_openmp", ans = integer(1))$ans > 0

cat(sprintf("Result: %s\n", if (success) "yes" else "no"))
q("no", status = !success)
ans <- .C("test_openmp", ans = integer(1))$ans
desired <- 3L
cat(sprintf("Test result: %d (must be %d; should be 0 for disabled OpenMP)\n", ans, desired))
stopifnot(`Test failed` = identical(ans, desired))
cat("Success\n")

0 comments on commit 4bd336f

Please sign in to comment.