Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
is now stored by value and re-thrown afresh instead of going through
`std::exception_ptr` (#3).

* The OpenMP thread-safety tests use at most 2 threads and respect
`OMP_THREAD_LIMIT`, so CRAN no longer sees "OMP: Warning #96: Cannot
form a team" (#3).

# RcppTrust 0.2.0

* The package license is now GPL-2, because the BOBYQA/NEWUOA ports and
Expand Down
3 changes: 2 additions & 1 deletion src/solver_test_objfuns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ List solver_openmp_stress_test(std::string solver, int n, int nStarts,

std::vector<int> threadIds(nStarts, 0);
#ifdef _OPENMP
int usedThreads = std::max(1, std::min(nThreadsRequested, omp_get_max_threads()));
int usedThreads = std::max(1, std::min({nThreadsRequested, omp_get_max_threads(),
omp_get_thread_limit()}));
#pragma omp parallel for num_threads(usedThreads) schedule(static)
#endif
for (int i = 0; i < nStarts; i++) {
Expand Down
5 changes: 4 additions & 1 deletion src/trust_test_objfuns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ List trust_openmp_stress_test(int nStarts, int nThreadsRequested) {
std::vector<int> threadIds(nStarts, -1);

#ifdef _OPENMP
int usedThreads = std::min(nThreadsRequested, omp_get_max_threads());
// Also respect OMP_THREAD_LIMIT (CRAN allows 2 threads), so the runtime
// never has to shrink the team and warn.
int usedThreads = std::min({nThreadsRequested, omp_get_max_threads(),
omp_get_thread_limit()});
if (usedThreads < 1) usedThreads = 1;
#pragma omp parallel for num_threads(usedThreads) schedule(static)
#endif
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-c-objfun-threadsafety.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
# (not thread-safe) and keeps no shared mutable state across calls.

test_that("trust_solve_c() gives identical results run sequentially vs. across OpenMP threads", {
res <- RcppTrust:::trust_openmp_stress_test(nStarts = 64L, nThreadsRequested = 8L)
# Two threads: the most CRAN allows, and enough to exercise concurrency.
res <- RcppTrust:::trust_openmp_stress_test(
nStarts = 64L,
nThreadsRequested = 2L
)

expect_equal(res$maxArgDiff, 0)
expect_equal(res$maxValDiff, 0)
Expand Down
Loading