Skip to content

Clarifying the status of OMP in CRAN binaries on Mac

9 messages · Gavin Simpson, @d@mhsp@rks m@iii@g oii tut@@com, webmail.gandi.net +2 more

#
Dear list,

I'm trying to clarify my understanding of what is and is not
implemented / expected in terms of openMP support in R and CRAN
package *binaries* for MacOS on apple silicon.

I have the CRAN's R binary for MacOS X (apple silicon) and also the
CRAN binaries for two packages, glmmTMB and mgcv, both of which seem
to be instrumented (following WRE) to use OpenMP if available. And
from discussions here I understand that thanks to Simon and  others,
OpenMP support for MacOS X is in R and so should I expect OpenMP to
work in these two packages?

If I am, neither of them is working on various of my Mac systems;
model fitting is single threaded and the test functions that glmmTMB
and mgcv use to detect OMP support both return FALSE:

R version 4.5.0 (2025-04-11) -- "How About a Twenty-Six"
Platform: aarch64-apple-darwin20 (64-bit)
r$> glmmTMB::omp_check()
[1] FALSE
r$> mgcv:::mgcv.omp()
[1] FALSE

Is this reproducible on other's systems? (Could someone run those two
bits of code on their systems if they use the MacOS X R binary from
CRAN and the CRAN Mac binaries for the packages - running mgcv's test
should be easier as this pkg is shipped with the R binary).

I am happy to provide further system details, but at this point I'm
mostly hoping to clarify what I should expect and whether what I'm
seeing is typical?

Thanks in advance

Gavin
#
Both of these return FALSE for me on my Mac mini M4.


best,
Adam



24 Apr 2025 at 17:58 by ucfagls at gmail.com:

  
  
#
It is possible to compile, say mgcv with OpenMP. There are several steps (done on a Mac M4 with Sequoia 15.3.2) :

- Installed R 4.5.0 patched (should not change from the initial 4.5.0 version, but to be correct)

- Installed the compilation tools with `xcode-select ?install`

- Installed gfortran-14.2-universal.pkg from here: https://mac.r-project.org/tools/ 

- Followed instructions here: https://mac.r-project.org/bin/, that is, `sudo R`, then `source("https://mac.R-project.org/bin/install.R ?)`, then 

```bash
install.libs("r-base-dev")
install.libs("gettext")
```

- Followed instructions on this page to get experimental OpenMP libraries: https://mac.r-project.org/openmp/ 

- Downloaded the latest .tar.gz source of mgcv and placed the file in the current directory

- Recompiled mgcv with: `PKG_CPPFLAGS='-Xclang -fopenmp' PKG_LIBS=-lomp R CMD INSTALL mgcv-1.9-3.tar.gz`

Now I have `mgcv:::mgcv.omp()` that returns TRUE.

Note that I used to recompile data.table also to get a multithreaded version, but apparently, it is not needed any more:

```R
install.packages("data.table")
library(data.table)
getDTthreads() # 8 for me by default (M4 max)
packageVersion("data.table") # 1.17.0
```

It would be nice to get multithreading, and especially OpenMP, enabled by default for the Mac. But I understand that it is risky since Apple does not support it officially.

Best,

Philippe

.........................................<?}))><........
 ) ) ) ) )
( ( ( ( (    Prof. Philippe Grosjean
 ) ) ) ) )
( ( ( ( (    Numerical Ecology
 ) ) ) ) )   Mons University, Belgium
( ( ( ( (
..............................................................
#
Thanks Philippe,

I have been able to compile mgcv this way (although with the 4.5.0
update I hit a snag regarding libintl.h not being found, and even
then, there is a problem with XCode CLI tools 16.3 which bumps clang
to 1700 and that is not liking any of the libomp versions on Simon's
https://mac.r-project.org/openmp/ page although it's unclear whether
this is openmp-related for something that is in this version of clang
- I saw a number of references to a bug in this version that was
leading to the |symbol not found in flat namespace
&#x27;___kmpc_dispatch_deinit&#x27; | errors that have been reported
here today)

But the CRAN binary of R does now ship with libomp.dynlib even on
MacOS X and mgcv (and glmmTMB) has the relevant macros in its
./src/Makevars:

PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) $(SHLIB_OPENMP_CFLAGS)
PKG_CFLAGS = $(SHLIB_OPENMP_CFLAGS)

Hence my thinking perhaps now with 4.5.0 this might work out of the
box. Which would be good. But that's what I'm trying to ascertain with
this thread.

All the best

Gavin
On Thu, 24 Apr 2025 at 17:04, webmail.gandi.net <phgrosjean at sciviews.org> wrote:
--
Gavin Simpson, PhD [he/him/his]
[tw] @ucfagls [OrciD] 0000-0002-9084-8413
[web] fromthebottomoftheheap.net
? Assistant Professor
? Department of Animal & Veterinary Sciences, Aarhus University
? Adjunct Professor, Department of Biology, University of Regina.
#
Gavin,
No, OpenMP is not supported by Apple compilers, so R does not enable OpenMP support. You can see that in the logs, e.g:

https://mac.r-project.org/logs/log-R-4.5-branch.big-sur.arm64.html
[...]
checking for clang -arch arm64 -std=gnu2x option to support OpenMP... unsupported

That said, packages are free to run their own tests if they desire, so anything further is package-dependent. Most packages simply use R's settings and thus don't use OpenMP on macOS.

In cases where the package authors decided that OpenMP is critical for the package they can choose to add a check for '-Xclang -fopenmp' which will force Apple compilers to generate OpenMP code *if* a run-time is available (which is not the case for Apple tools) even if it is not supported by Apple. In that case the package's CRAN binary will have OpenMP support, because the run time is available on the CRAN build machines to support such packages. However, that only applies to the CRAN binaries, it is not supported for source installation due to the lack of support by Apple.

Cheers,
Simon
#
If it?s not compiled in, why is |libomp.dylib| distributed in R for Mac?

Bill
On 4/24/25 17:35, Simon Urbanek wrote:

            
&#8203;
#
See my explanation below - to guarantee a common run-time for CRAN packages that wish to use OpenMP even if neither R nor Apple support it. The CRAN R binary is designed specifically to support CRAN R package binaries so it "just works" for Mac users. If each package that wishes to go that route shipped their own (or linked statically) then chaos would ensue breaking all of them.

Cheers,
Simon
#
Thanks Simon, that's really helpful - I find the R manuals confusing
on this issue in part because things MacOS X related are bound up in
"Unix-alike" sections and there are often clauses and / or footnotes
relating to MacOS X but it's unclear what relates to CRAN builds /
configuration vs doing things on one's own system. If I were more
knowledgeable about this stuff I would suggest some changes, but
perhaps it's clear once one has gained sufficient knowledge :-)

I've had another close read of data.table's configure script and I
think I understand what is needed so that I can try to help Simon
(Wood) and Ben and Mollie with getting this configured in mgcv and
glmmTMB using the relevant bits from data.table.

And again, thank you for helping me through all this, and - because
I'm sure it doesn't get said enough - for everything you do to support
R on Mac!

FYI: there does seem to be a real problem related to OpenMP with clang
1700 provided in Xcode 16.3 (which got installed when or alongside the
system updates for Sequoia 15.4.1) - I'm seeing the same issue
reported here https://github.com/Rdatatable/data.table/issues/6622 and
down some deep rabbit hole I went digging into the error message I saw
during compilation of mcgv I saw reports that this was a bug in llvm
that was affecting this version of clang, but I can't find the source
for that now. I've downgraded to 16.2 now so I just have one problem
to solve (compile my own R or ditch homebrew).

The other day I said I had R installed from Homebrew and that it was
CRAN's binary. This is true, but I neglected the difference between
installing R as a formula or as a cask. The latter installs CRAN's
binary of R, while installing as a formula would lead to a local
compilation from sources using Homebrew. Sorry about that confusion.

All the best

G
On Thu, 24 Apr 2025 at 23:35, Simon Urbanek <simon.urbanek at r-project.org> wrote:

  
    
#
Gavin,

thanks, quick updates inline.
Basically, the main part is to detect OpenMP even if R doesn't enable it (possibly as an option?) and on macOS also try -Xclang in front of the regular -fopemp to see if it works.
Yes, I have confirmed in the meantime that Xcode 16.3 (using LLVM 19) is incompatible with earlier OpenMP run-times, and I have added the corresponding note on the OpenMP pages:

https://mac.r-project.org/openmp/

including a work-around if you want to use Xcode 16.3 and force the corresponding LLVM 19 run-time in the complied package instead of the shared one. Note that you still can't mix them in the same R session, so you have to to choose which run-time to use for *all* packages that require OpenMP (at least the ones you want to load together ;)).
No worries, it's all complicated :), but that makes sense.

Cheers,
Simon